PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/ideintf/srceditorintf.pas

http://github.com/graemeg/lazarus
Pascal | 422 lines | 326 code | 55 blank | 41 comment | 10 complexity | faa2e7064b0e43b3030edb90cd1a90a7 MD5 | raw file
  1{
  2 *****************************************************************************
  3 *                                                                           *
  4 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,    *
  5 *  for details about the copyright.                                         *
  6 *                                                                           *
  7 *  This program is distributed in the hope that it will be useful,          *
  8 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
  9 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
 10 *                                                                           *
 11 *****************************************************************************
 12
 13  Abstract:
 14    Defines interface to source editors.
 15}
 16unit SrcEditorIntf;
 17
 18{$mode objfpc}{$H+}
 19
 20interface
 21
 22uses
 23  Classes, SysUtils, LCLProc, FileUtil, Forms, Controls, ProjectIntf;
 24  
 25type
 26  TSrcEditSearchOption = (
 27    sesoMatchCase,
 28    sesoWholeWord,
 29    sesoBackwards,
 30    sesoEntireScope,
 31    sesoSelectedOnly,
 32    sesoReplace,
 33    sesoReplaceAll,
 34    sesoPrompt,
 35    sesoRegExpr,
 36    sesoMultiLine
 37    );
 38  TSrcEditSearchOptions = set of TSrcEditSearchOption;
 39
 40  TSrcEditReplaceAction = (seraCancel, seraSkip, seraReplace, seraReplaceAll);
 41
 42  { TSourceEditorInterface }
 43
 44  TSourceEditorInterface = class
 45  protected
 46    function GetBlockBegin: TPoint; virtual; abstract;
 47    function GetBlockEnd: TPoint; virtual; abstract;
 48    function GetCodeToolsBuffer: TObject; virtual; abstract;
 49    function GetCursorScreenXY: TPoint; virtual; abstract;
 50    function GetCursorTextXY: TPoint; virtual; abstract;
 51    function GetEditorControl: TWinControl; virtual; abstract;
 52    function GetFileName: string; virtual; abstract;
 53    function GetLines: TStrings; virtual; abstract;
 54    function GetLineText: string; virtual; abstract;
 55    function GetModified: Boolean; virtual; abstract;
 56    function GetReadOnly: Boolean; virtual; abstract;
 57    function GetSelection: string; virtual; abstract;
 58    function GetSelEnd: Integer; virtual; abstract;
 59    function GetSelStart: Integer; virtual; abstract;
 60    function GetSourceText: string; virtual; abstract;
 61    function GetTopLine: Integer; virtual; abstract;
 62    procedure SetBlockBegin(const AValue: TPoint); virtual; abstract;
 63    procedure SetBlockEnd(const AValue: TPoint); virtual; abstract;
 64    procedure SetCursorScreenXY(const AValue: TPoint); virtual; abstract;
 65    procedure SetCursorTextXY(const AValue: TPoint); virtual; abstract;
 66    procedure SetLines(const AValue: TStrings); virtual; abstract;
 67    procedure SetLineText(const AValue: string); virtual; abstract;
 68    procedure SetModified(const NewValue: Boolean); virtual; abstract;
 69    procedure SetReadOnly(const AValue: Boolean); virtual; abstract;
 70    procedure SetSelection(const AValue: string); virtual; abstract;
 71    procedure SetSelEnd(const AValue: Integer); virtual; abstract;
 72    procedure SetSelStart(const AValue: Integer); virtual; abstract;
 73    procedure SetSourceText(const AValue: string); virtual; abstract;
 74    procedure SetTopLine(const AValue: Integer); virtual; abstract;
 75  public
 76    // selections
 77    function SelectionAvailable: boolean; virtual; abstract;
 78    function GetText(OnlySelection: boolean): string; virtual; abstract;
 79    procedure SelectText(LineNum, CharStart, LineNum2, CharEnd: Integer);
 80    procedure SelectText(const StartPos, EndPos: TPoint); virtual; abstract;
 81    procedure ReplaceLines(StartLine, EndLine: integer; const NewText: string); virtual; abstract;
 82    procedure ReplaceText(const StartPos, EndPos: TPoint; const NewText: string);
 83    procedure AskReplace(Sender: TObject; const ASearch, AReplace: string;
 84                        Line, Column: integer;
 85                        var Action: TSrcEditReplaceAction); virtual; abstract;
 86    procedure CopyToClipboard; virtual; abstract;
 87    procedure CutToClipboard; virtual; abstract;
 88
 89    // screen and text position mapping
 90    function LineCount: Integer; virtual; abstract;
 91    function TextToScreenPosition(const Position: TPoint): TPoint; virtual; abstract;
 92    function ScreenToTextPosition(const Position: TPoint): TPoint; virtual; abstract;
 93
 94    // characters and pixels
 95    function WidthInChars: Integer; virtual; abstract;
 96    function HeightInLines: Integer; virtual; abstract;
 97    function CharWidth: integer; virtual; abstract;
 98    function CursorInPixel: TPoint; virtual; abstract;
 99    function ScreenToPixelPosition(const Position: TPoint): TPoint; virtual; abstract;// ScreenXY to pixel in EditorControl.
100      // To get the desktop pixel coords use:
101      //   with SourceEditorWindow.ActiveEditor do
102      //     DesktopXY:=EditorControl.ClientToScreen(ScreenToPixelPosition(ScreenXY));
103
104    // update
105    procedure BeginUndoBlock; virtual; abstract;
106    procedure EndUndoBlock; virtual; abstract;
107    procedure BeginUpdate; virtual; abstract; // block painting
108    procedure EndUpdate; virtual; abstract;
109    procedure IncreaseIgnoreCodeBufferLock; virtual; abstract;
110    procedure DecreaseIgnoreCodeBufferLock; virtual; abstract;
111    procedure UpdateCodeBuffer; virtual; abstract;// copy the source from EditorComponent to the codetools
112    function NeedsUpdateCodeBuffer: boolean; virtual; abstract;// needs UpdateCodeBuffer
113
114    // search and replace
115    function SearchReplace(const ASearch, AReplace: string;
116                           SearchOptions: TSrcEditSearchOptions): integer; virtual; abstract;
117
118    // context
119    function GetProjectFile: TLazProjectFile; virtual; abstract;
120    function GetDesigner(LoadForm: boolean): TIDesigner; virtual; abstract;
121  public
122    property BlockBegin: TPoint read GetBlockBegin write SetBlockBegin;
123    property BlockEnd: TPoint read GetBlockEnd write SetBlockEnd;
124    property CodeToolsBuffer: TObject read GetCodeToolsBuffer;
125    property CursorScreenXY: TPoint read GetCursorScreenXY write SetCursorScreenXY;
126    property CursorTextXY: TPoint read GetCursorTextXY write SetCursorTextXY;
127    property EditorControl: TWinControl read GetEditorControl;// normally TSynEdit
128    property FileName: string read GetFileName;
129    property Lines: TStrings read GetLines write SetLines;// the whole file
130    property CurrentLineText: string read GetLineText write SetLineText;// source of current line
131    property ReadOnly: Boolean read GetReadOnly write SetReadOnly;
132    property Selection: string read GetSelection write SetSelection;
133    property SelEnd: Integer read GetSelEnd write SetSelEnd;
134    property SelStart: Integer read GetSelStart write SetSelStart;
135    property SourceText: string read GetSourceText write SetSourceText;// the whole file
136    property TopLine: Integer read GetTopLine write SetTopLine;// first visible line
137    property Modified: Boolean read GetModified write SetModified;
138  end;
139
140  { TSourceEditorWindowInterface }
141
142  TSourceEditorWindowInterface = class(TForm)
143  protected
144    function GetItems(Index: integer): TSourceEditorInterface; virtual; abstract;
145  protected
146    function GetActiveEditor: TSourceEditorInterface; virtual; abstract;
147    procedure SetActiveEditor(const AValue: TSourceEditorInterface); virtual; abstract;
148    function GetCompletionBoxPosition: integer; virtual; abstract;
149  public
150    function SourceEditorIntfWithFilename(
151                                const Filename: string): TSourceEditorInterface;
152    property ActiveEditor: TSourceEditorInterface read GetActiveEditor
153                                                  write SetActiveEditor;
154    function Count: integer; virtual; abstract;
155    property Items[Index: integer]: TSourceEditorInterface read GetItems; default;
156    
157    function GetEditorControlSettings(EditControl: TControl): boolean; virtual; abstract;
158    function GetHighlighterSettings(Highlighter: TObject): boolean; virtual; abstract;
159    procedure ClearErrorLines; virtual; abstract;
160
161    property CompletionBoxPosition: integer read GetCompletionBoxPosition;
162  end;
163  
164  
165var
166  SourceEditorWindow: TSourceEditorWindowInterface = nil;// set by the IDE
167
168
169type
170
171  { TIDEInteractiveStringValue }
172
173  TIDEInteractiveStringValue = class(TPersistent)
174  private
175    FValue: string;
176  published
177    property Value: string read FValue write FValue;
178  end;
179
180  { TIDETemplateParser }
181
182  TIDETemplateParser = class
183  protected
184    function GetSrcPosition: Integer; virtual; abstract;
185    function GetDestPosition: Integer; virtual; abstract;
186    function GetDestPosX: Integer; virtual; abstract;
187    function GetDestPosY: Integer; virtual; abstract;
188    function GetSrcTemplate: String; virtual; abstract;
189    function GetDestTemplate: String; virtual; abstract;
190  public
191    property SrcTemplate: String read GetSrcTemplate;
192    property DestTemplate: String read GetDestTemplate;
193    property SrcPosition: Integer read GetSrcPosition;
194    property DestPosition: Integer read GetDestPosition;
195    property DestPosX: Integer read GetDestPosX;
196    property DestPosY: Integer read GetDestPosY;
197  end;
198
199  TIDECodeMacroGetValueProc = function(const Parameter: string;
200                      InteractiveValue: TPersistent;
201                      SrcEdit: TSourceEditorInterface;
202                      var Value, ErrorMsg: string): boolean;
203  TIDECodeMacroGetValueMethod = function(const Parameter: string;
204                      InteractiveValue: TPersistent;
205                      SrcEdit: TSourceEditorInterface;
206                      var Value, ErrorMsg: string): boolean of object;
207
208  TIDECodeMacroGetValueExProc = function(const Parameter: string;
209                      InteractiveValue: TPersistent;
210                      SrcEdit: TSourceEditorInterface;
211                      var Value, ErrorMsg: string;
212                      TemplateParser: TIDETemplateParser): boolean;
213  TIDECodeMacroGetValueExMethod = function(const Parameter: string;
214                      InteractiveValue: TPersistent;
215                      SrcEdit: TSourceEditorInterface;
216                      var Value, ErrorMsg: string;
217                      TemplateParser: TIDETemplateParser): boolean of object;
218
219  { TIDECodeMacro }
220
221  TIDECodeMacro = class
222  private
223    FInteractive: boolean;
224    FInteractiveValueClass: TPersistentClass;
225    FLongDescription: string;
226    FName: string;
227    FOnGetValueMethod: TIDECodeMacroGetValueMethod;
228    FOnGetValueProc: TIDECodeMacroGetValueProc;
229    FOnGetValueExMethod: TIDECodeMacroGetValueExMethod;
230    FOnGetValueExProc: TIDECodeMacroGetValueExProc;
231    FShortDescription: string;
232  protected
233    procedure Init; virtual;
234  public
235    constructor Create(const TheName: string);
236    property Name: string read FName;
237    property ShortDescription: string read FShortDescription write FShortDescription;
238    property LongDescription: string read FLongDescription write FLongDescription;
239    property OnGetValueProc: TIDECodeMacroGetValueProc read FOnGetValueProc
240                                                       write FOnGetValueProc;
241    property OnGetValueMethod: TIDECodeMacroGetValueMethod read FOnGetValueMethod
242                                                       write FOnGetValueMethod;
243    property OnGetValueExProc: TIDECodeMacroGetValueExProc read FOnGetValueExProc
244                                                       write FOnGetValueExProc;
245    property OnGetValueExMethod: TIDECodeMacroGetValueExMethod read FOnGetValueExMethod
246                                                       write FOnGetValueExMethod;
247    function GetValue(const Parameter: string; InteractiveValue: TPersistent;
248                      SrcEdit: TSourceEditorInterface; out Value, ErrorMsg: string;
249                      TemplateParser: TIDETemplateParser = nil): boolean; virtual;
250    property Interactive: boolean read FInteractive write FInteractive;
251    property InteractiveValueClass: TPersistentClass read FInteractiveValueClass
252                                                   write FInteractiveValueClass;
253  end;
254
255
256  { TIDECodeMacros }
257
258  TIDECodeMacros = class
259  protected
260    function GetItems(Index: integer): TIDECodeMacro; virtual; abstract;
261  public
262    property Items[Index: integer]: TIDECodeMacro read GetItems; default;
263    function Count: integer; virtual; abstract;
264    function Add(Macro: TIDECodeMacro): integer; virtual; abstract;
265    function FindByName(const AName: string): TIDECodeMacro; virtual; abstract;
266    function CreateUniqueName(const AName: string): string; virtual; abstract;
267  end;
268
269var
270  IDECodeMacros: TIDECodeMacros = nil; // set by the IDE
271
272function RegisterCodeMacro(const Name: string;
273  const ShortDescription, LongDescription: string;
274  OnGetValueProc: TIDECodeMacroGetValueProc;
275  OnGetValueMethod: TIDECodeMacroGetValueMethod): TIDECodeMacro;
276
277function RegisterCodeMacroEx(const Name: string;
278  const ShortDescription, LongDescription: string;
279  OnGetValueProc: TIDECodeMacroGetValueExProc;
280  OnGetValueMethod: TIDECodeMacroGetValueExMethod): TIDECodeMacro;
281
282{ SearchInFile to search in a file.
283  This can be interactively or without user interaction.
284  If the file is open in the source editor, changes will be added to the undo
285  history.
286}
287type
288  TIDESearchInTextAddMatch = procedure(const Filename: string;
289                                  const StartPos, EndPos: TPoint;
290                                  const Lines: string) of object;
291
292  { TIDESearchInTextProgress }
293
294  TIDESearchInTextProgress = class
295  private
296    FAbort: boolean;
297    FOnAddMatch: TIDESearchInTextAddMatch;
298  protected
299    procedure SetAbort(const AValue: boolean); virtual;
300  public
301    property Abort: boolean read FAbort write SetAbort;
302    property OnAddMatch: TIDESearchInTextAddMatch read FOnAddMatch write FOnAddMatch;
303  end;
304
305  TIDESearchInTextFunction = function(const TheFileName: string;
306    var TheText: string;// if TheFileName='' then use TheText
307    SearchFor, ReplaceText: string; Flags: TSrcEditSearchOptions;
308    var Prompt: boolean; Progress: TIDESearchInTextProgress = nil): TModalResult;
309
310var
311  IDESearchInText: TIDESearchInTextFunction = nil;// set by the IDE
312
313implementation
314
315function RegisterCodeMacro(const Name: string; const ShortDescription,
316  LongDescription: string; OnGetValueProc: TIDECodeMacroGetValueProc;
317  OnGetValueMethod: TIDECodeMacroGetValueMethod): TIDECodeMacro;
318var
319  NewName: String;
320begin
321  NewName:=IDECodeMacros.CreateUniqueName(Name);
322  Result:=TIDECodeMacro.Create(NewName);
323  Result.ShortDescription:=ConvertLineEndings(ShortDescription);
324  Result.LongDescription:=ConvertLineEndings(LongDescription);
325  Result.OnGetValueProc:=OnGetValueProc;
326  Result.OnGetValueMethod:=OnGetValueMethod;
327  IDECodeMacros.Add(Result);
328end;
329
330function RegisterCodeMacroEx(const Name: string; const ShortDescription,
331  LongDescription: string; OnGetValueProc: TIDECodeMacroGetValueExProc;
332  OnGetValueMethod: TIDECodeMacroGetValueExMethod): TIDECodeMacro;
333var
334  NewName: String;
335begin
336  NewName:=IDECodeMacros.CreateUniqueName(Name);
337  Result:=TIDECodeMacro.Create(NewName);
338  Result.ShortDescription:=ConvertLineEndings(ShortDescription);
339  Result.LongDescription:=ConvertLineEndings(LongDescription);
340  Result.OnGetValueExProc:=OnGetValueProc;
341  Result.OnGetValueExMethod:=OnGetValueMethod;
342  IDECodeMacros.Add(Result);
343end;
344
345{ TSourceEditorInterface }
346
347procedure TSourceEditorInterface.SelectText(LineNum, CharStart, LineNum2,
348  CharEnd: Integer);
349begin
350  SelectText(Point(CharStart,LineNum),Point(CharEnd,LineNum2));
351end;
352
353procedure TSourceEditorInterface.ReplaceText(const StartPos, EndPos: TPoint;
354  const NewText: string);
355begin
356  BeginUpdate;
357  BeginUndoBlock;
358  SelectText(StartPos,EndPos);
359  CursorTextXY:=StartPos;
360  Selection:=NewText;
361  EndUndoBlock;
362  EndUpdate;
363end;
364
365{ TIDECodeMacro }
366
367procedure TIDECodeMacro.Init;
368begin
369  FInteractiveValueClass:=TIDEInteractiveStringValue;
370end;
371
372constructor TIDECodeMacro.Create(const TheName: string);
373begin
374  FName:=TheName;
375  FShortDescription:=FName;
376  FLongDescription:=FName;
377end;
378
379function TIDECodeMacro.GetValue(const Parameter: string;
380  InteractiveValue: TPersistent; SrcEdit: TSourceEditorInterface;
381  out Value, ErrorMsg: string; TemplateParser: TIDETemplateParser = nil): boolean;
382begin
383  Value:=Parameter;
384  ErrorMsg:='';
385  if Assigned(OnGetValueProc) then
386    Result:=OnGetValueProc(Parameter,InteractiveValue,SrcEdit,Value,ErrorMsg)
387  else if Assigned(OnGetValueMethod) then
388    Result:=OnGetValueMethod(Parameter,InteractiveValue,SrcEdit,Value,ErrorMsg)
389  else if Assigned(OnGetValueExProc) then
390    Result:=OnGetValueExProc(Parameter,InteractiveValue,SrcEdit,Value,ErrorMsg,
391                             TemplateParser)
392  else if Assigned(OnGetValueExMethod) then
393    Result:=OnGetValueExMethod(Parameter,InteractiveValue,SrcEdit,Value,ErrorMsg,
394                               TemplateParser)
395  else
396    Result:=true;
397end;
398
399{ TSourceEditorWindowInterface }
400
401function TSourceEditorWindowInterface.SourceEditorIntfWithFilename(
402  const Filename: string): TSourceEditorInterface;
403var
404  i: Integer;
405begin
406  for i:=Count-1 downto 0 do begin
407    Result:=Items[i];
408    if CompareFilenames(Result.Filename,Filename)=0 then exit;
409  end;
410  Result:=nil;
411end;
412
413{ TIDESearchInTextProgress }
414
415procedure TIDESearchInTextProgress.SetAbort(const AValue: boolean);
416begin
417  if FAbort=AValue then exit;
418  fAbort:=AValue;
419end;
420
421end.
422