CodeEditor.h

cbpporter cbpporter, 01/04/2017 04:42 PM

Download (15.8 KB)

 
1
#ifndef IDEEDITOR_H
2
#define IDEEDITOR_H
3

    
4
//#include <ide/Common/Common.h>
5
#include <CtrlLib/CtrlLib.h>
6
#include <plugin/pcre/Pcre.h>
7

    
8
namespace Upp {
9

    
10
#define  LAYOUTFILE <CodeEditor/CodeEditor.lay>
11
#include <CtrlCore/lay.h>
12

    
13
#define IMAGEVECTOR Vector
14
#define IMAGECLASS  CodeEditorImg
15
#define IMAGEFILE   <CodeEditor/CodeEditor.iml>
16
#include <Draw/iml_header.h>
17

    
18

    
19
void FindWildcardMenu(Callback1<const char *> cb, Point p, bool tablf, Ctrl *owner, bool regexp);
20

    
21
struct LineInfoRecord {
22
        int    lineno;
23
        String breakpoint;
24
        int    count;
25
        int    error;
26
        int    firstedited;
27
        int    edited;
28

    
29
        LineInfoRecord() { error = 0; edited = 0; }
30
};
31

    
32
typedef Array<LineInfoRecord> LineInfo;
33

    
34
void ClearErrors(LineInfo& li);
35

    
36
struct LineInfoRemRecord : Moveable<LineInfoRemRecord> {
37
        int    firstedited;
38
        int    edited;
39
};
40

    
41
typedef Vector<LineInfoRemRecord> LineInfoRem;
42

    
43
void Renumber(LineInfo& lf);
44
void ClearBreakpoints(LineInfo& lf);
45
void ValidateBreakpoints(LineInfo& lf);
46

    
47
class CodeEditor;
48

    
49
class EditorBar : public FrameLeft<Ctrl> {
50
public:
51
        virtual void Paint(Draw& w);
52
        virtual void MouseMove(Point p, dword flags);
53
        virtual void MouseLeave();
54
        virtual void LeftDown(Point p, dword flags);
55
        virtual void LeftDouble(Point p, dword flags);
56
        virtual void RightDown(Point p, dword flags);
57
        virtual void MouseWheel(Point p, int zdelta, dword keyflags);
58

    
59
private:
60
        struct LnInfo : Moveable<LnInfo> {
61
                int    lineno;
62
                String breakpoint;
63
                int    error;
64
                int    firstedited;
65
                int    edited;
66
                Image  icon;
67
                String annotation;
68

    
69
                LnInfo() { lineno = -1; error = 0; firstedited = 0; edited = 0; }
70
        };
71
        
72
        Vector<LnInfo>   li;
73
        LineInfoRem      li_removed;
74

    
75
        CodeEditor       *editor;
76
        int              ptrline[2];
77
        Image            ptrimg[2];
78
        bool             bingenabled;
79
        bool             hilite_if_endif;
80
        bool             line_numbers;
81
        int              annotations;
82
        bool             ignored_next_edit;
83
        int              next_age;
84
        int              active_annotation;
85
        bool             check_edited;
86

    
87
        String& PointBreak(int& y);
88
        void    sPaintImage(Draw& w, int x, int y, int fy, const Image& img);
89

    
90
public:
91
        Event<int> WhenBreakpoint;
92
        Event<>        WhenAnnotationMove;
93
        Event<>        WhenAnnotationClick;
94
        Event<>        WhenAnnotationRightClick;
95

    
96
        void InsertLines(int i, int count);
97
        void RemoveLines(int i, int count);
98
        void ClearLines();
99

    
100
        void Scroll()                          { Refresh(); }
101

    
102
        void SyncSize();
103

    
104
        void Renumber(int linecount);
105
        void ClearBreakpoints();
106
        void ValidateBreakpoints();
107

    
108
        String  GetBreakpoint(int ln);
109
        void    SetBreakpoint(int ln, const String& s);
110
        void    SetEdited(int ln, int count = 1);
111
        void    ClearEdited();
112
        void    SetError(int ln, int err);
113
        void    ClearErrors(int ln);
114

    
115
        void SetEditor(CodeEditor *e)           { editor = e; }
116

    
117
        LineInfo GetLineInfo() const;
118
        void     SetLineInfo(const LineInfo& li, int total);
119
        LineInfoRem & GetLineInfoRem()                   { return li_removed; }
120
        void     SetLineInfoRem(LineInfoRem pick_ li)    { li_removed = pick(li); }
121
        
122
        void     ClearAnnotations();
123
        void     SetAnnotation(int line, const Image& img, const String& ann);
124
        String   GetAnnotation(int line) const;
125

    
126
        int      GetLineNo(int lineno) const;
127
        int      GetNoLine(int line) const;
128

    
129
        void     SetPtr(int line, const Image& img, int i);
130
        void     HidePtr();
131

    
132
        void     EnableBreakpointing(bool b)     { bingenabled = b; SyncSize(); }
133
        void     HiliteIfEndif(bool b)           { hilite_if_endif = b; SyncSize(); Refresh(); }
134
        void     LineNumbers(bool b);
135
        void     Annotations(int width);
136
        void     CheckEdited(bool e = true)      { check_edited = e; SyncSize(); }
137
        
138
        bool     IsHiliteIfEndif() const         { return hilite_if_endif; }
139
        
140
        int      GetActiveAnnotationLine() const { return active_annotation; }
141

    
142
        EditorBar();
143
        virtual ~EditorBar();
144
};
145

    
146
struct IdentPos {
147
        int    begin;
148
        int    end;
149
        String ident;
150
};
151

    
152
Array<IdentPos> GetLineIdent(const char *line);
153
Vector<Point>   GetLineString(const wchar *wline, bool& is_begin, bool& is_end);
154

    
155
inline int  CharFilterCIdent(int i)  { return IsAlNum(i) || i == '_' ? i : 0; }
156
inline bool iscidl(int c)            { return iscid(c) || IsLetter(c); }
157
inline bool islbrkt(int c)           { return c == '{' || c == '[' || c == '('; }
158
inline bool isrbrkt(int c)           { return c == '}' || c == ']' || c == ')'; }
159
inline bool isbrkt(int c)            { return islbrkt(c) || isrbrkt(c); }
160

    
161
struct FindReplaceDlg : FrameBottom< WithIDEFindReplaceLayout<TopWindow> > {
162
        WString itext;
163
        bool    replacing;
164

    
165
        virtual bool Key(dword key, int count);
166
        void Setup(bool doreplace);
167
        void Sync();
168
        bool IsIncremental() const              { return incremental.IsEnabled() && incremental; }
169
        
170
        typedef FindReplaceDlg CLASSNAME;
171

    
172
        FindReplaceDlg();
173
};
174

    
175
#include "Syntax.h"
176
#include "CSyntax.h"
177
#include "DiffSyntax.h"
178
#include "TagSyntax.h"
179
#include "LogSyntax.h"
180
#include "PythonSyntax.h"
181

    
182
class CodeEditor : public LineEdit,
183
public HighlightSetup
184
{
185
        friend class EditorBar;
186

    
187
public:
188
        virtual bool  Key(dword code, int count);
189
        virtual void  LeftDown(Point p, dword keyflags);
190
        virtual void  LeftDouble(Point p, dword keyflags);
191
        virtual void  LeftTriple(Point p, dword keyflags);
192
        virtual void  LeftRepeat(Point p, dword keyflags);
193
        virtual void  MouseMove(Point p, dword keyflags);
194
        virtual Image CursorImage(Point p, dword keyflags);
195
        virtual void  Serialize(Stream& s);
196
        virtual void  MouseLeave();
197
        virtual void  MouseWheel(Point p, int zdelta, dword keyFlags);
198

    
199
protected:
200
        virtual void HighlightLine(int line, Vector<LineEdit::Highlight>& h, int pos);
201
        virtual void PreInsert(int pos, const WString& s);
202
        virtual void PostInsert(int pos, const WString& s);
203
        virtual void PreRemove(int pos, int size);
204
        virtual void PostRemove(int pos, int size);
205
        virtual void DirtyFrom(int line);
206
        virtual void SelectionChanged();
207

    
208
        virtual void ClearLines();
209
        virtual void InsertLines(int line, int count);
210
        virtual void RemoveLines(int line, int count);
211

    
212
        virtual void NewScrollPos();
213

    
214
        virtual String  GetPasteText();
215

    
216
        EditorBar   bar;
217
        Vector<int> line2;
218

    
219
        struct SyntaxPos {
220
                int    line;
221
                String data;
222
                
223
                void Clear() { line = 0; data.Clear(); }
224
        };
225
        
226
        SyntaxPos   syntax_cache[6];
227

    
228
//        EditorSyntax rm_ins;
229

    
230
        char        rmb;
231
        int         highlight_bracket_pos0;
232
        int         highlight_bracket_pos;
233
        bool        bracket_flash;
234
        int         bracket_start;
235

    
236
        bool    barline : 1;
237
        double  stat_edit_time;
238
        Time    last_key_time;
239

    
240
        bool    auto_enclose;
241
        bool    mark_lines;
242
        bool    check_edited;
243
        bool    persistent_find_replace;
244
        bool    do_ff_restore_pos;
245
        bool    withfindreplace;
246

    
247
        int     ff_start_pos;
248

    
249
        FindReplaceDlg findreplace;
250
        
251
        enum {
252
                WILDANY = 16,
253
                WILDONE,
254
                WILDSPACE,
255
                WILDNUMBER,
256
                WILDID,
257
        };
258

    
259
        struct Found {
260
                int     type;
261
                WString text;
262
        };
263

    
264
        Array<Found> foundwild;
265
        WString      foundtext;
266
        bool   foundsel;
267
        bool   found, notfoundfw, notfoundbk;
268
        int    foundpos, foundsize;
269
        
270
        enum { SEL_CHARS, SEL_WORDS, SEL_LINES };
271
        int    selkind;
272

    
273
        WString selword;
274

    
275
        String  iwc;
276

    
277
        String highlight;
278
        
279
        int    spellcheck_comments = 0;
280
        bool   wordwrap_comments = true;
281
        
282
        struct Tip : Ctrl {
283
                Value v;
284
                const Display *d;
285
                
286
                virtual void Paint(Draw& w);
287
                
288
                Tip();
289
        };
290
        
291
        Tip   tip;
292
        int   tippos;
293
        
294
        int   replacei;
295

    
296
        struct HlSt;
297
        
298
        bool   MouseSelSpecial(Point p, dword flags);
299
        void   InitFindReplace();
300
        void   CancelBracketHighlight(int& pos);
301
        void   FindPrevNext(bool prev);
302
        void   CheckBrackets();
303
        void   OpenNormalFindReplace0(bool replace);
304
        void   OpenNormalFindReplace(bool replace);
305
        void   FindReplaceAddHistory();
306
        void   FindWildcard();
307
        void   ReplaceWildcard();
308
        void   InsertWildcard(const char *s);
309
        void   IncrementalFind();
310
        void   NotFound();
311
        void   NoFindError();
312
        void   CheckSyntaxRefresh(int pos, const WString& text);
313

    
314
        void   SetFound(int fi, int type, const WString& text);
315

    
316
        int     Match(const wchar *f, const wchar *s, int line, bool we, bool icase, int fi = 0);
317
        WString GetWild(int type, int& i);
318
        WString GetReplaceText();
319

    
320
        bool   InsertRS(int chr, int count = 1);
321

    
322
        void   IndentInsert(int chr, int count);
323

    
324
        void   ForwardWhenBreakpoint(int i);
325

    
326
        bool   ToggleSimpleComment(int &start_line, int &end_line, bool usestars = true);
327
        void   ToggleLineComments(bool usestars = false);
328
        void   ToggleStarComments();
329
        void   Enclose(const char *c1, const char *c2, int l = -1, int h = -1);
330
        void   Make(Event<String&> op);
331
        void   TabsOrSpaces(String& out, bool maketabs);
332
        void   LineEnds(String& out);
333

    
334
        enum {
335
                TIMEID_PERIODIC = Ctrl::TIMEID_COUNT,
336
                TIMEID_COUNT,
337
        };
338

    
339
        void   Periodic();
340

    
341
public:
342
        struct MouseTip {
343
                int            pos;
344
                Value          value;
345
                const Display *display;
346
                Size           sz;
347
        };
348

    
349
        Event<>            WhenSelection;
350
        Gate1<MouseTip&>   WhenTip;
351
        Event<>            WhenLeftDown;
352
        Event<int>     WhenCtrlClick;
353
        Event<>            WhenAnnotationMove;
354
        Event<>            WhenAnnotationClick;
355
        Event<>            WhenAnnotationRightClick;
356
        Event<>            WhenOpenFindReplace;
357
        Event<String&> WhenPaste;
358
        Event<>            WhenUpdate;
359

    
360
        FrameTop<Button>    topsbbutton;
361
        FrameTop<Button>    topsbbutton1;
362

    
363
        static dword find_next_key;
364
        static dword find_prev_key;
365
        static dword replace_key;
366

    
367
        void   Clear()                    { LineEdit::Clear(); found = notfoundfw = notfoundbk = false; }
368

    
369
        void   Highlight(const String& h);
370
        String GetHighlight() const       { return highlight; }
371

    
372
        void   EscapeFindReplace();
373
        void   CloseFindReplace();
374
        void   FindReplace(bool pick_selection, bool pick_text, bool replace);
375
        bool   FindFrom(int pos, bool back, bool block);
376
        bool   RegExpFind(int pos, bool block);
377
        bool   Find(bool back, bool block);
378
        bool   Find(bool back, bool blockreplace, bool replace);
379
        void   FindNext();
380
        void   FindPrev();
381
        bool   GetStringRange(int cursor, int& b, int &e) const;
382
        bool   GetStringRange(int& b, int &e) const { return GetStringRange(GetCursor(), b, e); }
383
        bool   FindString(bool back);
384
        bool   FindLangString(bool back);
385
        void   Replace();
386
        void   ReplaceAll(bool rest);
387
        int    BlockReplace();
388

    
389
        void   MakeTabsOrSpaces(bool tabs);
390
        void   MakeLineEnds();
391

    
392
        void   CopyWord();
393
        void   SwapChars();
394
        void   DuplicateLine();
395
        void   Put(int chr);
396
        void   FinishPut();
397

    
398
        void   SerializeFind(Stream& s);
399
        bool   IsFindOpen() const                       { return findreplace.IsOpen(); }
400
        void   FindClose()                              { CloseFindReplace(); }
401

    
402
        void   Goto();
403

    
404
        void   DoFind();
405
        void   DoFindBack();
406

    
407
//        void    FindWord(bool back);
408
        WString GetI();
409
        void    SetI(Ctrl *edit);
410
        void    PutI(WithDropChoice<EditString>& edit);
411

    
412
        void   MoveNextWord(bool sel);
413
        void   MovePrevWord(bool sel);
414
        void   MoveNextBrk(bool sel);
415
        void   MovePrevBrk(bool sel);
416

    
417
        String GetWord(int pos);
418
        String GetWord();
419

    
420
        bool   GetWordPos(int pos, int& l, int& h);
421

    
422
        void   DeleteWord();
423
        void   DeleteWordBack();
424
        void   SetLineSelection(int l, int h);
425
        bool   GetLineSelection(int& l, int& h);
426
        void   TabRight();
427
        void   TabLeft();
428

    
429
        Event<int> WhenBreakpoint;
430

    
431

    
432
        void    CheckEdited(bool e = true)                { bar.CheckEdited(e); check_edited = e; }
433
        bool    GetCheckEdited()                          { return check_edited; }
434

    
435
        void    EditorBarLayout()                         { bar.SyncSize(); }
436

    
437
        LineInfo GetLineInfo() const                      { return bar.GetLineInfo(); }
438
        void     SetLineInfo(const LineInfo& lf);
439
        LineInfoRem GetLineInfoRem()                      { return LineInfoRem(bar.GetLineInfoRem(), 0); }
440
        void     SetLineInfoRem(LineInfoRem pick_  lf)    { bar.SetLineInfoRem(LineInfoRem(lf, 0)); }
441
        double   GetStatEditTime() const                  { return stat_edit_time; }
442
        void     Renumber()                               { bar.Renumber(GetLineCount()); }
443
        void     ClearBreakpoints()                       { bar.ClearBreakpoints(); }
444
        void     ValidateBreakpoints()                    { bar.ValidateBreakpoints(); }
445
        int      GetLineNo(int line) const                { return bar.GetLineNo(line); }
446
        int      GetNoLine(int line) const                { return bar.GetNoLine(line); }
447
        void     SetPtr(int line, const Image& img, int i){ bar.SetPtr(line, img, i); }
448
        void     HidePtr()                                { bar.HidePtr(); }
449
        String   GetBreakpoint(int line)                  { return bar.GetBreakpoint(line); }
450
        void     SetBreakpoint(int line, const String& b) { bar.SetBreakpoint(line, b); }
451
        void     SetError(int line, int err)              { bar.SetError(line, err); }
452
        void     ClearErrors(int line = -1)               { bar.ClearErrors(line); }
453
        void     ClearEdited()                            { bar.ClearEdited(); }
454
        int                 GetUndoCount()                           { return undo.GetCount(); }
455
        void     GotoLine(int line);
456
        void     EnableBreakpointing()                    { bar.EnableBreakpointing(true); }
457
        void     DisableBreakpointing()                   { bar.EnableBreakpointing(false); }
458
        void     Renumber2();
459
        int      GetLine2(int i) const;
460
        void     ReformatComment();
461

    
462
// TODO: Syntax: Remove
463
        void     HiliteScope(byte b)                      { EditorSyntax::hilite_scope = b; Refresh(); }
464
        void     HiliteBracket(byte b)                    { EditorSyntax::hilite_bracket = b; Refresh(); }
465
        void     HiliteIfDef(byte b)                      { EditorSyntax::hilite_ifdef = b; Refresh(); }
466
        void     HiliteIfEndif(bool b)                    { bar.HiliteIfEndif(b); }
467

    
468
        void     ThousandsSeparator(bool b)               { thousands_separator = b; Refresh(); }
469
        void     IndentSpaces(bool is)                    { indent_spaces = is; }
470
        void     IndentAmount(int ia)                     { indent_amount = ia; }
471
        void     NoParenthesisIndent(bool b)              { no_parenthesis_indent = b; }
472
        
473
        void     SpellcheckComments(int lang)             { spellcheck_comments = lang; Refresh(); }
474
        int      GetSpellcheckComments() const            { return spellcheck_comments; }
475
        void     WordwrapComments(bool b)                 { wordwrap_comments = b; }
476
        bool     IsWordwrapComments() const               { return wordwrap_comments; }
477

    
478
        void     NoFindReplace()                          { withfindreplace = false; }
479

    
480
        void     LineNumbers(bool b)                      { bar.LineNumbers(b); }
481
        void     MarkLines(bool b)                        { mark_lines = b; }
482
        bool     GetMarkLines()                           { return mark_lines; }
483
        void     AutoEnclose(bool b)                      { auto_enclose = b; }
484
        void     BarLine(bool b)                          { barline = b; }
485
        
486
        void     PersistentFindReplace(bool b = true)     { persistent_find_replace = b; }
487
        bool     IsPersistentFindReplace() const          { return persistent_find_replace; }
488

    
489
        void     FindReplaceRestorePos(bool b = true)     { do_ff_restore_pos = b; }
490
        bool     IsFindReplaceRestorePos() const          { return do_ff_restore_pos; }
491
        
492
        void     Annotations(int width)                   { bar.Annotations(width); }
493
        void     ClearAnnotations()                       { bar.ClearAnnotations(); }
494
        void     SetAnnotation(int i, const Image& icon, const String& a) { bar.SetAnnotation(i, icon, a); }
495
        String   GetAnnotation(int i) const               { return bar.GetAnnotation(i); }
496
        int      GetActiveAnnotationLine() const          { return bar.GetActiveAnnotationLine(); }
497

    
498
        void     HideBar()                                { bar.Hide(); }
499

    
500
        void     SyncTip();
501
        void     CloseTip()                               { if(tip.IsOpen()) tip.Close(); tip.d = NULL;  }
502

    
503
        One<EditorSyntax> GetSyntax(int line);
504
        bool IsCursorBracket(int pos) const;
505
        bool IsMatchingBracket(int pos) const;
506

    
507
// TODO: Do we really need this ?
508
        Vector<IfState> GetIfStack(int line)              { return GetSyntax(line)->PickIfStack(); }
509

    
510
        struct FindReplaceData {
511
                String find, replace;
512
                String find_list, replace_list;
513
                bool   wholeword, wildcards, ignorecase, samecase, regexp;
514
        };
515
        
516
        FindReplaceData GetFindReplaceData();
517
        void            SetFindReplaceData(const FindReplaceData& d);
518

    
519
        typedef CodeEditor CLASSNAME;
520

    
521
        CodeEditor();
522
        virtual ~CodeEditor();
523

    
524
        static void InitKeywords();
525
};
526

    
527
String ReadList(WithDropChoice<EditString>& e);
528
void   WriteList(WithDropChoice<EditString>& e, const String& data);
529

    
530
}
531

    
532
#endif