File: JcfIdeMain.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (382 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download | duplicates (8)
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
unit JcfIdeMain;

{ AFS 7 Jan 2K
  JEDI Code Format IDE plugin main class

  global object that implements the callbacks from the menu items }


{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is JcfIdeMain, released May 2003.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

uses
  { delphi }Windows, SysUtils, Classes,
  { delphi design time }
  ToolsAPI,
  { local}
  EditorConverter, FileConverter, ConvertTypes;

type
  TJcfIdeMain = class(TObject)
  private
    fcEditorConverter: TEditorConverter;
    fcFileConverter: TFileConverter;

    procedure MakeEditorConverter;

    procedure LogIDEMessage(const psFile, psMessage: string;
      const peMessageType: TStatusMessageType;
      const piY, piX: integer);
    procedure FormatFile(const psFileName: string);

    procedure ClearToolMessages;
    procedure ConvertEditor(const pciEditor: IOTASourceEditor);


  protected
  public
    constructor Create;
    destructor Destroy; override;

    procedure ShortcutKeyCallback(const Context: IOTAKeyContext;
      KeyCode: TShortcut; var BindingResult: TKeyBindingResult);

    procedure DoFormatCurrentIDEWindow(Sender: TObject);
    procedure DoFormatProject(Sender: TObject);
    procedure DoFormatOpen(Sender: TObject);
    procedure DoRegistrySettings(Sender: TObject);
    procedure DoFormatSettings(Sender: TObject);
    procedure DoAbout(Sender: TObject);
  end;


implementation

uses
  { delphi }
  Menus, Dialogs, Controls,
  { local }
  JcfStringUtils,
  fAllSettings, fAbout, JcfRegistrySettings, fRegistrySettings;


function FileIsAllowedType(const psFileName: string): boolean;
const
  ALLOWED_FILE_TYPES: array[1..3] of string = ('.pas', '.dpr', '.dpk');
begin
  Result := StrIsOneOf(StrRight(psFileName, 4), ALLOWED_FILE_TYPES);
end;


{ the function GetCurrentProject is from Erik's OpenTools API FAQ and Resources
  http://www.gexperts.org/opentools/
}
// Modified from code posted by Ray Lischner (www.tempest-sw.com)
function GetCurrentProject: IOTAProject;
var
  Services: IOTAModuleServices;
  Module: IOTAModule;
  Project: IOTAProject;
  ProjectGroup: IOTAProjectGroup;
  MultipleProjects: boolean;
  I: integer;
begin
  Result   := nil;
  MultipleProjects := False;
  Services := BorlandIDEServices as IOTAModuleServices;
  for I := 0 to Services.ModuleCount - 1 do
  begin
    Module := Services.Modules[I];
    if Module.QueryInterface(IOTAProjectGroup, ProjectGroup) = S_OK then
    begin
      Result := ProjectGroup.ActiveProject;
      Exit;
    end
    else if Module.QueryInterface(IOTAProject, Project) = S_OK then
    begin
      if Result = nil then
        // Found the first project, so save it
        Result := Project
      else
        MultipleProjects := True;
      // It doesn't look good, but keep searching for a project group
    end;
  end;
  if MultipleProjects then
    Result := nil;
end;


constructor TJcfIdeMain.Create;
begin
  inherited;
  { both of these are created on demand }
  fcEditorConverter := nil;
  fcFileConverter   := nil;
end;

destructor TJcfIdeMain.Destroy;
begin
  FreeAndNil(fcEditorConverter);
  FreeAndNil(fcFileConverter);
  inherited;
end;

procedure TJcfIdeMain.DoFormatCurrentIDEWindow(Sender: TObject);
var
  hRes:      HResult;
  lciEditManager: IOTAEditorServices;
  lciEditor: IOTASourceEditor;
begin
  // get the current editor window
  hRes := BorlandIDEServices.QueryInterface(IOTAEditorServices, lciEditManager);
  if hRes <> S_OK then
    exit;
  if lciEditManager = nil then
    exit;

  lciEditor := lciEditManager.TopBuffer;
  if (lciEditor = nil) or (lciEditor.EditViewCount = 0) then
  begin
    LogIdeMessage('', 'No current window', mtInputError, -1, -1);
    exit;
  end;

  ConvertEditor(lciEditor);
end;

procedure TJcfIdeMain.ConvertEditor(const pciEditor: IOTASourceEditor);
begin
  MakeEditorConverter;

  ClearToolMessages;
  fcEditorConverter.Clear;
  fcEditorConverter.BeforeConvert;
  fcEditorConverter.Convert(pciEditor);
  fcEditorConverter.AfterConvert;
end;

procedure TJcfIdeMain.DoFormatProject(Sender: TObject);
var
  lciProject: IOTAProject;
  lciModule:  IOTAModuleInfo;
  {$IFDEF VER170}
  lciAction: IOTAActionServices;
  {$ENDIF}
  liLoop:     integer;
  lsMsg:      string;
begin
  {$IFDEF VER170}
  lciAction := BorlandIDEServices as IOTAActionServices;
  {$ENDIF}
  lciProject := GetCurrentProject;
  if lciProject = nil then
    exit;

  lsMsg := 'JEDI Code Format of ' + lciProject.FileName + NativeLineBreak +
    'Are you sure that you want to format all ' + IntToStr(lciProject.GetModuleCount) +
    ' files in the project.';

  if MessageDlg(lsMsg, mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
    exit;

  ClearToolMessages;

  { loop through all modules in the project }
  for liLoop := 0 to lciProject.GetModuleCount - 1 do
  begin
    lciModule := lciProject.GetModule(liLoop);
    FormatFile(lciModule.FileName);
    {$IFDEF VER170}
    lciAction.ReloadFile(lciModule.FileName);
    {$ENDIF}
  end;
end;

procedure TJcfIdeMain.DoFormatOpen(Sender: TObject);
var
  hRes:      HResult;
  lciEditManager: IOTAEditorServices;
  lciIterateBuffers: IOTAEditBufferIterator;
  lciEditor: IOTASourceEditor;
  liLoop:    integer;
begin
  hRes := BorlandIDEServices.QueryInterface(IOTAEditorServices, lciEditManager);
  if hRes <> S_OK then
    exit;
  if lciEditManager = nil then
    exit;

  MakeEditorConverter;

  lciIterateBuffers := nil;
  lciEditManager.GetEditBufferIterator(lciIterateBuffers);
  if lciIterateBuffers = nil then
    exit;

  ClearToolMessages;
  fcEditorConverter.BeforeConvert;

  for liLoop := 0 to lciIterateBuffers.Count - 1 do
  begin
    lciEditor := lciIterateBuffers.EditBuffers[liLoop];

    // check that it's open, and a .pas or .dpr
    if (lciEditor <> nil) and (lciEditor.EditViewCount > 0) and
      (FileIsAllowedType(lciEditor.FileName)) then
    begin
      fcEditorConverter.Convert(lciEditor);
    end;
  end;

  fcEditorConverter.AfterConvert;
end;


procedure TJcfIdeMain.FormatFile(const psFileName: string);
begin
  if not FileExists(psFileName) then
    exit;

  // check that it's a .pas or .dpr
  if not FileIsAllowedType(psFileName) then
    exit;

  if fcFileConverter = nil then
  begin
    fcFileConverter := TFileConverter.Create;
    fcFileConverter.OnStatusMessage := LogIDEMessage;
  end;

  fcFileConverter.ProcessFile(psFileName);
end;

procedure TJcfIdeMain.DoFormatSettings(Sender: TObject);
var
  lfAllSettings: TFormAllSettings;
begin
  if not GetRegSettings.HasRead then
    GetRegSettings.ReadAll;

  lfAllSettings := TFormAllSettings.Create(nil);
  try
    lfAllSettings.Execute;
  finally
    lfAllSettings.Release;
  end;
end;

procedure TJcfIdeMain.DoAbout(Sender: TObject);
var
  lcAbout: TfrmAboutBox;
begin
  lcAbout := TfrmAboutBox.Create(nil);
  try
    lcAbout.ShowModal;
  finally
    lcAbout.Free;
  end;
end;

procedure TJcfIdeMain.DoRegistrySettings(Sender: TObject);
var
  lcAbout: TfmRegistrySettings;
begin
  if not GetRegSettings.HasRead then
    GetRegSettings.ReadAll;

  lcAbout := TfmRegistrySettings.Create(nil);
  try
    lcAbout.Execute;
  finally
    lcAbout.Free;
  end;
end;

procedure TJcfIdeMain.ShortcutKeyCallback(const Context: IOTAKeyContext;
  KeyCode: TShortcut; var BindingResult: TKeyBindingResult);
var
  liShortcut: TShortCut;
begin
  liShortcut := Shortcut(Ord('K'), [ssCtrl]);

  if KeyCode = liShortcut then
    DoFormatCurrentIDEWindow(nil);
end;

procedure TJcfIdeMain.LogIDEMessage(const psFile, psMessage: string;
  const peMessageType: TStatusMessageType;
  const piY, piX: integer);
var
  lciMessages: IOTAMessageServices40;
  hRes: HResult;
begin
  { no empty lines in this log }
  if psMessage = '' then
    exit;

  hRes := BorlandIDEServices.QueryInterface(IOTAMessageServices40, lciMessages);
  if hRes <> S_OK then
    exit;
  if lciMessages = nil then
    exit;

  if (piY >= 0) and (piX >= 0) then
    lciMessages.AddToolMessage(psFile, psMessage, 'JCF', piY, piX)
  else
    lciMessages.AddTitleMessage('JCF: ' + psFile + ' ' + psMessage);

end;

procedure TJcfIdeMain.MakeEditorConverter;
begin
  if fcEditorConverter = nil then
  begin
    fcEditorConverter := TEditorConverter.Create;
    fcEditorConverter.OnStatusMessage := LogIDEMessage;
  end;

  Assert(fcEditorConverter <> nil);
end;

procedure TJcfIdeMain.ClearToolMessages;
var
  lciMessages: IOTAMessageServices40;
  hRes: HResult;
begin
  hRes := BorlandIDEServices.QueryInterface(IOTAMessageServices40, lciMessages);
  if hRes <> S_OK then
    exit;
  if lciMessages = nil then
    exit;

  lciMessages.ClearToolMessages;
end;

end.