-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcTools.pas
518 lines (472 loc) · 17.5 KB
/
cTools.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
{-----------------------------------------------------------------------------
Unit Name: cTools
Author: Kiriakos Vlahos
Date: 02-Jun-2005
Purpose: Class definitions for Command Line Tools
History:
-----------------------------------------------------------------------------}
unit cTools;
interface
uses
SysUtils, Classes, ActnList;
type
TProcessStdInputOption = (piNone, piWordAtCursor, piCurrentLine, piSelection,
piActiveFile);
TProcessStdOutputOption = (poNone, poWordAtCursor, poCurrentLine, poSelection,
poActiveFile, poNewFile);
TToolContext = (tcAlwaysEnabled, tcActiveFile, tcActivePythonFile, tcSelectionAvailable);
TSaveFiles = (sfNone, sfActive, sfAll);
{
Describes the properties of a command line tool
}
TExternalTool = class(TPersistent)
private
fApplicationName: string;
fParameters: string;
fProcessInput: TProcessStdInputOption;
fProcessOutput: TProcessStdOutputOption;
fCaptureOutput: Boolean;
fTimeOut: Integer;
fWaitForTerminate: Boolean;
fConsoleHidden: Boolean;
fWorkingDirectory: string;
fDescription: string;
fCaption: string;
fShortCut: TShortCut;
fParseMessages : boolean;
fParseTraceback : boolean;
fMessagesFormat : string;
fContext: TToolContext;
fSaveFiles : TSaveFiles;
fEnvironment : TStrings;
fUseCustomEnvironment : boolean;
procedure SetEnvironment(const Value: TStrings);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
// The caption of the Menu Item corresponding to this tool
property Caption : string read fCaption write fCaption;
// The hint of the Menu Item corresponding to this tool
property Description : string read fDescription write fDescription;
property ApplicationName: string read fApplicationName write fApplicationName;
property Parameters: string read fParameters write fParameters;
property WorkingDirectory : string read fWorkingDirectory write fWorkingDirectory;
property ShortCut : TShortCut read fShortCut write fShortCut;
// External Tool action context (tcAlwaysEnabled, tcActiveFile, tcSelectionAvailable)
property Context : TToolContext read fContext write fContext
default tcAlwaysEnabled;
// Save file option (sfNone, sfActive, sfAll)
property SaveFiles : TSaveFiles read fSaveFiles write fSaveFiles default sfNone;
// Standard Input option
// piNone: No standard input
// piWordAtCursor: Word at cursor
// piCurrentLine: Current line
// piSelection: Selection
// piActiveFile: active file
property ProcessInput : TProcessStdInputOption read fProcessInput
write fProcessInput default piNone;
// Standard output option
// poNone: Do nothing
// poWordAtCursor: Replace word at cursor
// poCurrentLine: Replace current line
// poSelection: Replace selection
// poActiveFile: Replace active file
// poNewFile: Place in new file
property ProcessOutput : TProcessStdOutputOption read fProcessOutput
write fProcessOutput default poNone;
// Parse File Line LinePos info from output and put it in the Messages Window
property ParseMessages : boolean read fParseMessages write fParseMessages
default False;
// Parse TraceBack and Syntax Errors from Python output and put it in the Messages Window
property ParseTraceback : boolean read fParseTraceback write fParseTraceback
default False;
// Grep Expression for the parsing output lines for file/Line/Pos information
// Can use the parameters $[Filename], $[LineNumber], $[Line], $[Column]
property MessagesFormat : string read fMessagesFormat write fMessagesFormat;
// Capture command line output and place it in the Output Window
property CaptureOutput : Boolean read fCaptureOutput write fCaptureOutput
default True;
// Hide Console or External Tool window
property ConsoleHidden : Boolean read fConsoleHidden write fConsoleHidden
default True;
// Non-blocking wait for termination of the External tool
// Required for ParseMessage, ParseTraceback and other options
property WaitForTerminate: Boolean read fWaitForTerminate write
fWaitForTerminate default True;
// Give the user the oportunity to terminate the External tool after Timeout ms
// A value of zero disables this feature
property TimeOut : Integer read fTimeOut write fTimeOut default 0;
// Use the Custom Enviroment specified by the Environment property
property UseCustomEnvironment : boolean read fUseCustomEnvironment
write fUseCustomEnvironment default False;
// Custom Enviroment
property Environment : TStrings read fEnvironment write SetEnvironment
stored fUseCustomEnvironment;
end;
// Differs only in persistence
TExternalRun = class(TExternalTool)
published
property SaveFiles default sfAll;
property Context default tcActiveFile;
property ParseTraceback default True;
end;
TToolItem = class(TCollectionItem)
private
fExternalTool : TExternalTool;
protected
function GetDisplayName: string; override;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property ExternalTool : TExternalTool read fExternalTool
write fExternalTool;
end;
TExternalToolAction = class(TAction)
private
fExternalTool : TExternalTool;
protected
procedure Change; override;
public
function Execute: Boolean; override;
function Update: Boolean; override;
constructor CreateExtToolAction(AOwner: TComponent; ExternalTool : TExternalTool);
end;
{ Expands environment variables }
function ExpandEnv(const S: string): string;
{ Surrounds string with quotes when it contains spaces and is not quoted }
function AddQuotesUnless(const S: string): string;
{ Prepares an application name or the command line parameters for execution }
function PrepareCommandLine(S: string): string;
Const
GrepFileNameParam = '$[FileName]';
GrepLineNumberParam = '$[LineNumber]';
GrepColumnNumberParam = '$[ColumnNumber]';
Var
{ A persisted collection of user-defined tools }
ToolsCollection : TCollection;
{ External Python Tool }
ExternalPython : TExternalTool;
implementation
uses Windows, cParameters, Menus, frmCommandOutput,
dmCommands, uCommonFunctions, uEditAppIntfs, WideStrUtils;
function ExpandEnv(const S: string): string;
var
r: array[0..MAX_PATH] of WideChar;
begin
ExpandEnvironmentStringsW(PWideChar(S), @r, MAX_PATH);
Result := r;
end;
function AddQuotesUnless(const S: string): string;
begin
Result := Trim(S);
if (Pos(' ', Result) <> 0) and
((Result[1] <> WideChar('"')) or (WideLastChar(Result)^ <> WideChar('"'))) then
Result := '"' + Result + '"';
end;
function PrepareCommandLine(S: string): string;
begin
S := ExpandEnv(S);
S:= Parameters.ReplaceInText(S);
Result := Trim(S);
end;
{ TTool }
procedure TToolItem.Assign(Source: TPersistent);
begin
if Source is TToolItem then with TToolItem(Source) do begin
Self.fExternalTool.Assign(ExternalTool);
end else
inherited;
end;
constructor TToolItem.Create(Collection: TCollection);
begin
inherited;
fExternalTool := TExternalTool.Create;
end;
destructor TToolItem.Destroy;
begin
fExternalTool.Free;
inherited;
end;
function TToolItem.GetDisplayName: string;
begin
Result := fExternalTool.Caption;
end;
{ TExternalTool }
procedure TExternalTool.Assign(Source: TPersistent);
begin
if Source is TExternalTool then with TExternalTool(Source) do begin
Self.fCaption := Caption;
Self.fDescription := Description;
Self.fShortCut := ShortCut;
Self.fContext := Context;
Self.fSaveFiles := SaveFiles;
Self.fApplicationName := ApplicationName;
Self.fParameters := Parameters;
Self.fWorkingDirectory := WorkingDirectory;
Self.fProcessInput := ProcessInput;
Self.fProcessOutput := ProcessOutput;
Self.fParseMessages := ParseMessages;
Self.fParseTraceback := ParseTraceback;
Self.fMessagesFormat := MessagesFormat;
Self.fCaptureOutput := CaptureOutput;
Self.fConsoleHidden := ConsoleHidden;
Self.fTimeOut := TimeOut;
Self.fWaitForTerminate := WaitForTerminate;
Self.fUseCustomEnvironment := UseCustomEnvironment;
Self.fEnvironment.Assign(Environment);
end else
inherited;
end;
constructor TExternalTool.Create;
begin
inherited;
fProcessInput := piNone;
fProcessOutput := poNone;
fCaptureOutput := True;
fConsoleHidden := True;
fTimeOut := 0;
fWaitForTerminate := True;
fParseMessages := False;
fContext := tcAlwaysEnabled;
fSaveFiles := sfNone;
fMessagesFormat := GrepFileNameParam + ' '+GrepLineNumberParam;
fEnvironment := TStringList.Create;
end;
destructor TExternalTool.Destroy;
begin
fEnvironment.Free;
inherited;
end;
procedure TExternalTool.SetEnvironment(const Value: TStrings);
begin
fEnvironment.Assign(Value);
end;
{ TExternalToolAction }
procedure TExternalToolAction.Change;
begin
inherited;
if Assigned(fExternalTool) then
fExternalTool.ShortCut := ShortCut;
end;
constructor TExternalToolAction.CreateExtToolAction(AOwner: TComponent;
ExternalTool: TExternalTool);
var
S: string;
AppFile: string;
Index: Integer;
begin
inherited Create(AOwner);
fExternalTool := ExternalTool;
if Assigned(fExternalTool) then begin
ShortCut := fExternalTool.ShortCut;
Caption := fExternalTool.Caption;
S := StringReplace(Caption, ' ', '', [rfReplaceAll]);
S := StringReplace(S, '&', '', [rfReplaceAll]);
if IsValidIdent(S) then
Name := 'actTools' + S;
Category := 'External Tools';
Hint := fExternalTool.Description;
if (fExternalTool.ApplicationName <> '') then begin
AppFile := PrepareCommandLine(fExternalTool.ApplicationName);
if FileExists(AppFile) then begin
Index := GetIconIndexFromFile(AppFile, True);
ImageIndex :=
CommandsDataModule.Images.AddImage(CommandsDataModule.imlShellIcon, Index);
end;
end;
end;
end;
function TExternalToolAction.Execute: Boolean;
begin
if Assigned(fExternalTool) then begin
OutputWindow.ExecuteTool(fExternalTool);
end;
Result := True;
end;
function TExternalToolAction.Update: Boolean;
begin
Result := True;
if Assigned(fExternalTool) then begin
case fExternalTool.Context of
tcAlwaysEnabled : Enabled := True;
tcActiveFile : Enabled := Assigned(GI_ActiveEditor);
tcActivePythonFile :
Enabled := Assigned(GI_ActiveEditor) and GI_ActiveEditor.HasPythonFile;
tcSelectionAvailable : Enabled :=
Assigned(GI_ActiveEditor) and GI_ActiveEditor.SynEdit.SelAvail;
end;
end else
Enabled := False;
end;
initialization
ToolsCollection := TCollection.Create(TToolItem);
// Add a few standard tools to the collection
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Python &Interpreter';
Description := 'External Python Interpreter';
ApplicationName := '$[PythonExe-Short]';
WorkingDirectory := '$[ActiveDoc-Dir]';
SaveFiles := sfAll;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := False;
WaitForTerminate := False;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Python&Win help';
Description := 'Show Python Win Help';
ApplicationName := '$[PythonExe-Path-Short]Lib\site-packages\PyWin32.chm';
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := False;
WaitForTerminate := False;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Check &Indentation';
Description := 'Check the Indentation of the Python program';
ApplicationName := '$[PythonExe-Short]';
Parameters := '$[PythonDir-Short]Lib\tabnanny.py $[ActiveDoc-Short]';
WorkingDirectory := '$[ActiveDoc-Dir]';
ShortCut := Menus.Shortcut(Ord('T'), [ssShift, ssCtrl]);
Context := tcActivePythonFile;
SaveFiles := sfActive;
ParseTraceback := True;
ParseMessages := True;
CaptureOutput := True;
ConsoleHidden := True;
WaitForTerminate := True;
MessagesFormat := GrepFileNameParam + ' '+GrepLineNumberParam;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Command Prompt';
Description := 'Start a console at the directory of the active file';
ApplicationName := '%COMSPEC%';
WorkingDirectory := '$[ActiveDoc-Dir]';
SaveFiles := sfAll;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := False;
WaitForTerminate := False;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Profile';
Description := 'Profile active file';
ApplicationName := '$[PythonExe-Short]';
Parameters := '$[PythonDir-Short]Lib\profile.py $[ActiveDoc-Short] $[CmdLineArgs]';
WorkingDirectory := '$[ActiveDoc-Dir]';
Context := tcActivePythonFile;
SaveFiles := sfActive;
ParseTraceback := True;
CaptureOutput := True;
ConsoleHidden := True;
WaitForTerminate := True;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Py&lint';
Description := 'PyLint tool (www.logilab.org/projects/pylint)';
ApplicationName := '$[PythonExe-Short]';
Parameters := '$[PythonDir-Short]Lib\site-packages\pylint\lint.py $[ActiveDoc-Short] -f parseable';
ShortCut := Menus.Shortcut(Ord('L'), [ssCtrl]);
Context := tcActivePythonFile;
SaveFiles := sfAll;
ParseTraceback := True;
ParseMessages := True;
CaptureOutput := True;
ConsoleHidden := True;
WaitForTerminate := True;
MessagesFormat := Format('%s:%s: ', [GrepFileNameParam, GrepLineNumberParam]);
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Advanced Replace';
Description := 'Advanced Search and replace';
ApplicationName := '$[PythonExe-Short]';
Parameters := '-c "import sys, re;l=sys.stdin.read();sys.stdout.write(re.sub(''$[st?Search Text:]'', ''$[rt?Replace Text:]'', l))"';
Context := tcSelectionAvailable;
SaveFiles := sfNone;
ProcessInput := piSelection;
ProcessOutput := poSelection;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := True;
WaitForTerminate := True;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := 'Sort Selection';
Description := 'Sort the selected editor block ("one-liner" demo)';
ApplicationName := '$[PythonExe-Short]';
Parameters := '-c "import sys;l=sys.stdin.readlines();l.sort();sys.stdout.writelines(l)"';
ShortCut := Menus.Shortcut(Ord('S'), [ssShift, ssCtrl]);
Context := tcSelectionAvailable;
SaveFiles := sfNone;
ProcessInput := piSelection;
ProcessOutput := poSelection;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := True;
WaitForTerminate := True;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := '&Reindent';
Description := 'Reindent the active file';
ApplicationName := '$[PythonExe-Short]';
Parameters := '$[PythonDir-Short]Tools\Scripts\reindent.py';
Context := tcActivePythonFile;
SaveFiles := sfNone;
ProcessInput := piActiveFile;
ProcessOutput := poActiveFile;
ParseMessages := False;
CaptureOutput := True;
ConsoleHidden := True;
WaitForTerminate := True;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := '&Upper Case';
Description := 'Change selection to upper case';
ApplicationName := '$[PythonExe-Short]';
Parameters := '-c "import sys;sys.stdout.writelines([s.upper() for s in sys.stdin.readlines()])"';
Context := tcSelectionAvailable;
SaveFiles := sfNone;
ProcessInput := piSelection;
ProcessOutput := poSelection;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := True;
WaitForTerminate := True;
end;
with (ToolsCollection.Add as TToolItem).ExternalTool do begin
Caption := '&Lower Case';
Description := 'Change selection to lower case';
ApplicationName := '$[PythonExe-Short]';
Parameters := '-c "import sys;sys.stdout.writelines([s.lower() for s in sys.stdin.readlines()])"';
Context := tcSelectionAvailable;
SaveFiles := sfNone;
ProcessInput := piSelection;
ProcessOutput := poSelection;
ParseMessages := False;
CaptureOutput := False;
ConsoleHidden := True;
WaitForTerminate := True;
end;
// Create a Python External Run tool which is used in the run menu
ExternalPython := TExternalRun.Create;
with ExternalPython do begin
Caption := 'Python Interpreter';
Description := 'External Python Interpreter';
ApplicationName := '$[PythonExe-Short]';
Parameters := '$[ActiveDoc-Short] $[CmdLineArgs]';
WorkingDirectory := '$[ActiveDoc-Dir]';
SaveFiles := sfAll;
Context := tcActiveFile;
ParseTraceback := True;
CaptureOutput := True;
ConsoleHidden := True;
WaitForTerminate := True;
end;
finalization
ToolsCollection.Free;
ExternalPython.Free;
end.