File: unusedunitsdlg.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 (310 lines) | stat: -rw-r--r-- 9,385 bytes parent folder | download | duplicates (2)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    A dialog showing the unused units of the current unit
    (at cursor in source editor).
    With the ability to remove them automatically.
}
unit UnusedUnitsDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LCL
  LCLProc, Forms, Controls, ComCtrls, StdCtrls, ExtCtrls, Buttons, Dialogs,
  // Codetools
  CodeCache, CodeToolManager,
  // IdeIntf
  SrcEditorIntf, LazIDEIntf, IDEImagesIntf, IDEDialogs,
  // IDE
  LazarusIDEStrConsts;

type

  { TUnusedUnitsDialog }

  TUnusedUnitsDialog = class(TForm)
    CancelBitBtn: TBitBtn;
    ShowInitializationCheckBox: TCheckBox;
    RemoveAllBitBtn: TBitBtn;
    RemoveSelectedBitBtn: TBitBtn;
    Panel1: TPanel;
    UnitsTreeView: TTreeView;
    procedure CancelBitBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RemoveAllBitBtnClick(Sender: TObject);
    procedure RemoveSelectedBitBtnClick(Sender: TObject);
    procedure ShowInitializationCheckBoxClick(Sender: TObject);
    procedure UnitsTreeViewSelectionChanged(Sender: TObject);
  private
    FCode: TCodeBuffer;
    FUnits: TStrings;
    ImgIDInterface: LongInt;
    ImgIDImplementation: LongInt;
    ImgIDInitialization: LongInt;
    ImgIDNone: LongInt;
    procedure SetCode(AValue: TCodeBuffer);
    procedure SetUnits(const AValue: TStrings);
    procedure RebuildUnitsTreeView;
    procedure UpdateButtons;
  public
    function GetSelectedUnits: TStrings;
    function GetAllUnits: TStrings;
    property Units: TStrings read FUnits write SetUnits;
    property Code: TCodeBuffer read FCode write SetCode;
  end;


function ShowUnusedUnitsDialog: TModalResult;
function ShowUnusedUnitsDialog(Code: TCodeBuffer): TModalResult;

implementation

{$R *.lfm}

function ShowUnusedUnitsDialog: TModalResult;
var
  SrcEdit: TSourceEditorInterface;
  Code: TCodeBuffer;
begin
  // get cursor position
  Result:=mrAbort;
  SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
  if SrcEdit=nil then exit;
  Code:=TCodeBuffer(SrcEdit.CodeToolsBuffer);
  if Code=nil then exit;
  Result:=ShowUnusedUnitsDialog(Code);
end;

function ShowUnusedUnitsDialog(Code: TCodeBuffer): TModalResult;
var
  UnusedUnitsDialog: TUnusedUnitsDialog;
  Units: TStringList;
  RemoveUnits: TStrings;
  i: Integer;
  DlgResult: TModalResult;
  SrcEdit: TSourceEditorInterface;
begin
  Result:=mrOk;
  if Code=nil then exit;
  if not LazarusIDE.BeginCodeTools then exit;

  UnusedUnitsDialog:=nil;
  RemoveUnits:=nil;
  Units:=TStringList.Create;
  try
    if not CodeToolBoss.FindUnusedUnits(Code,Units) then begin
      DebugLn(['ShowUnusedUnitsDialog CodeToolBoss.FindUnusedUnits failed']);
      LazarusIDE.DoJumpToCodeToolBossError;
      exit(mrCancel);
    end;
    Units.Sort;

    UnusedUnitsDialog:=TUnusedUnitsDialog.Create(nil);
    UnusedUnitsDialog.Units:=Units;
    UnusedUnitsDialog.Code:=Code;
    DlgResult:=UnusedUnitsDialog.ShowModal;
    if DlgResult=mrOk then
      RemoveUnits:=UnusedUnitsDialog.GetSelectedUnits
    else if DlgResult=mrAll then
      RemoveUnits:=UnusedUnitsDialog.GetAllUnits
    else
      RemoveUnits:=nil;
    if (RemoveUnits<>nil) and (RemoveUnits.Count>0) then begin
      LazarusIDE.DoOpenEditorFile(Code.Filename,-1,-1,[]);
      SrcEdit:=SourceEditorManagerIntf.SourceEditorIntfWithFilename(Code.Filename);
      if SrcEdit=nil then begin
        IDEMessageDialog(lisCCOErrorCaption,
          Format(lisUnableToOpen, [Code.Filename]),
          mtError,[mbCancel]);
        exit(mrCancel);
      end;

      SrcEdit.BeginUndoBlock{$IFDEF SynUndoDebugBeginEnd}('ShowUnusedUnitsDialog'){$ENDIF};
      try
        for i:=0 to RemoveUnits.Count-1 do begin
          if not CodeToolBoss.RemoveUnitFromAllUsesSections(Code,RemoveUnits[i])
          then begin
            LazarusIDE.DoJumpToCodeToolBossError;
            exit(mrCancel);
          end;
        end;
      finally
        SrcEdit.EndUndoBlock{$IFDEF SynUndoDebugBeginEnd}('ShowUnusedUnitsDialog'){$ENDIF};
      end;
    end;
  finally
    CodeToolBoss.SourceCache.ClearAllSourceLogEntries;
    RemoveUnits.Free;
    UnusedUnitsDialog.Free;
    Units.Free;
  end;
end;

{ TUnusedUnitsDialog }

procedure TUnusedUnitsDialog.FormCreate(Sender: TObject);
begin
  ShowInitializationCheckBox.Caption:=lisShowUnitsWithInitialization;
  ShowInitializationCheckBox.Hint:=lisShowUnitsWithInitializationHint;
  RemoveSelectedBitBtn.Caption:=lisRemoveSelectedUnits;
  RemoveAllBitBtn.Caption:=lisRemoveAllUnits;
  CancelBitBtn.Caption:=lisCancel;

  UnitsTreeView.StateImages := IDEImages.Images_16;
  ImgIDInterface := IDEImages.LoadImage('ce_interface');
  ImgIDImplementation := IDEImages.LoadImage('ce_implementation');
  ImgIDInitialization := IDEImages.LoadImage('ce_initialization');
  ImgIDNone := IDEImages.LoadImage('ce_default');
end;

procedure TUnusedUnitsDialog.RemoveAllBitBtnClick(Sender: TObject);
begin
  ModalResult:=mrAll;
end;

procedure TUnusedUnitsDialog.CancelBitBtnClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TUnusedUnitsDialog.RemoveSelectedBitBtnClick(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

procedure TUnusedUnitsDialog.ShowInitializationCheckBoxClick(Sender: TObject);
begin
  RebuildUnitsTreeView;
end;

procedure TUnusedUnitsDialog.UnitsTreeViewSelectionChanged(Sender: TObject);
begin
  UpdateButtons;
end;

procedure TUnusedUnitsDialog.SetUnits(const AValue: TStrings);
begin
  if FUnits=AValue then exit;
  FUnits:=AValue;
  RebuildUnitsTreeView;
end;

procedure TUnusedUnitsDialog.SetCode(AValue: TCodeBuffer);
begin
  if FCode=AValue then Exit;
  FCode:=AValue;
  if FCode<>nil then
    Caption:=Format(lisUnusedUnitsOf, [ExtractFilename(Code.Filename)]);
end;

procedure TUnusedUnitsDialog.RebuildUnitsTreeView;
var
  i: Integer;
  AUnitname: string;
  Flags: string;
  UseInterface: Boolean;
  InImplUsesSection: Boolean;
  UseCode, HideCode: Boolean;
  IntfTreeNode: TTreeNode;
  ImplTreeNode: TTreeNode;
  ParentNode: TTreeNode;
  TVNode: TTreeNode;
begin
  UnitsTreeView.BeginUpdate;
  UnitsTreeView.Items.Clear;
  IntfTreeNode:=UnitsTreeView.Items.Add(nil,'Interface');
  IntfTreeNode.StateIndex:=ImgIDInterface;
  ImplTreeNode:=UnitsTreeView.Items.Add(nil,'Implementation');
  ImplTreeNode.StateIndex:=ImgIDImplementation;
  if Units<>nil then
  begin
    for i:=0 to Units.Count-1 do
    begin
      AUnitname:=Units.Names[i];
      Flags:=Units.ValueFromIndex[i];
      InImplUsesSection:=System.Pos(',implementation',Flags)>0;
      UseInterface:=System.Pos(',used',Flags)>0;
      UseCode:=System.Pos(',code',Flags)>0;
      HideCode:=not ShowInitializationCheckBox.Checked;
      if not (UseInterface or (HideCode and UseCode)) then
      begin
        if InImplUsesSection then
          ParentNode:=ImplTreeNode
        else
          ParentNode:=IntfTreeNode;
        TVNode:=UnitsTreeView.Items.AddChild(ParentNode,AUnitname);
        if UseCode then
          TVNode.StateIndex:=ImgIDInitialization
        else
          TVNode.StateIndex:=ImgIDInterface;
      end;
    end;
  end;
  IntfTreeNode.Expanded:=true;
  ImplTreeNode.Expanded:=true;
  UnitsTreeView.EndUpdate;
  UpdateButtons;
end;

procedure TUnusedUnitsDialog.UpdateButtons;
var
  RemoveUnits: TStrings;
begin
  RemoveUnits:=GetSelectedUnits;
  RemoveSelectedBitBtn.Enabled:=RemoveUnits.Count>0;
  RemoveAllBitBtn.Enabled:=Units.Count>0;
  RemoveUnits.Free;
end;

function TUnusedUnitsDialog.GetSelectedUnits: TStrings;
var
  TVNode: TTreeNode;
begin
  Result:=TStringList.Create;
  TVNode:=UnitsTreeView.Items.GetFirstNode;
  while TVNode<>nil do begin
    if TVNode.MultiSelected and (TVNode.Level=1) then
      Result.Add(TVNode.Text);
    TVNode:=TVNode.GetNext;
  end;
end;

function TUnusedUnitsDialog.GetAllUnits: TStrings;
var
  TVNode: TTreeNode;
begin
  Result:=TStringList.Create;
  TVNode:=UnitsTreeView.Items.GetFirstNode;
  while TVNode<>nil do begin
    if (TVNode.Level=1) then
      Result.Add(TVNode.Text);
    TVNode:=TVNode.GetNext;
  end;
end;

end.