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