1 {
2  ***************************************************************************
3  *                                                                         *
4  *   This source is free software; you can redistribute it and/or modify   *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This code is distributed in the hope that it will be useful, but      *
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
12  *   General Public License for more details.                              *
13  *                                                                         *
14  *   A copy of the GNU General Public License is available on the World    *
15  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
16  *   obtain it by writing to the Free Software Foundation,                 *
17  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
18  *                                                                         *
19  ***************************************************************************
20 
21  Author: Mattias Gaertner
22 
23  Abstract:
24    Dialog used by the quick fix "Insert Assignment var := ..." for messages
25    Hint/Warning: (5036) Local variable "$1" does not seem to be initialized
26    Dialog shows a list of possible statements and a list of possible insert
27    positions.
28 }
29 unit QFInitLocalVarDlg;
30 
31 {$mode objfpc}{$H+}
32 
33 interface
34 
35 uses
36   Classes, SysUtils, Math, contnrs,
37   // LCL
38   Forms, Controls, Dialogs, ButtonPanel, ExtCtrls,
39   // LazUtils
40   LazLoggerBase,
41   // Codetools
42   CodeToolManager, CodeCache, StdCodeTools,
43   // IdeIntf
44   LazIDEIntf, IDEDialogs,
45   // IDE
46   LazarusIDEStrConsts;
47 
48 type
49 
50   { TQFInitLocalVarDialog }
51 
52   TQFInitLocalVarDialog = class(TForm)
53     ButtonPanel1: TButtonPanel;
54     Panel1: TPanel;
55     ValueRadioGroup: TRadioGroup;
56     WhereRadioGroup: TRadioGroup;
57     procedure ButtonPanel1OKButtonClick(Sender: TObject);
58     procedure FormCreate(Sender: TObject);
59   private
60   public
61     Statements: TStrings;
62     InsertPositions: TObjectList;
63     procedure Init(TheStatements: TStrings; TheInsertPositions: TObjectList);
64   end;
65 
QuickFixLocalVarNotInitializednull66 function QuickFixLocalVarNotInitialized(Code: TCodeBuffer; X, Y: integer): boolean;
67 
68 implementation
69 
QuickFixLocalVarNotInitializednull70 function QuickFixLocalVarNotInitialized(Code: TCodeBuffer; X, Y: integer): boolean;
71 var
72   Statements: TStrings;
73   InsertPositions: TObjectList;
74   Dlg: TQFInitLocalVarDialog;
75 begin
76   Result:=false;
77   Statements:=nil;
78   InsertPositions:=nil;
79   try
80     if not CodeToolBoss.GetPossibleInitsForVariable(Code,X,Y,Statements,
81       InsertPositions)
82     then begin
83       if CodeToolBoss.ErrorCode<>nil then
84         LazarusIDE.DoJumpToCodeToolBossError
85       else
86         ShowMessage('CodeToolBoss.GetPossibleInitsForVariable failed at '+Code.Filename+'('+IntToStr(Y)+','+IntToStr(X)+')');
87       exit;
88     end;
89 
90     Dlg:=TQFInitLocalVarDialog.Create(nil);
91     try
92       Dlg.Init(Statements,InsertPositions);
93       case Dlg.ShowModal of
94       mrOk: Result:=true;
95       mrAbort:
96         if CodeToolBoss.ErrorCode<>nil then
97           LazarusIDE.DoJumpToCodeToolBossError
98         else
99           IDEMessageDialog('Error','Unable to insert the code',mtError,[mbOk]);
100       else
101         // user cancel
102       end;
103     finally
104       Dlg.Free;
105     end;
106   finally
107     Statements.Free;
108     InsertPositions.Free;
109   end;
110 end;
111 
112 {$R *.lfm}
113 
114 { TQFInitLocalVarDialog }
115 
116 procedure TQFInitLocalVarDialog.FormCreate(Sender: TObject);
117 begin
118   Caption:=lisInitializeLocalVariable;
119   ButtonPanel1.OKButton.OnClick:=@ButtonPanel1OKButtonClick;
120 end;
121 
122 procedure TQFInitLocalVarDialog.ButtonPanel1OKButtonClick(Sender: TObject);
123 var
124   OldChange: Boolean;
125 begin
126   OldChange:=LazarusIDE.OpenEditorsOnCodeToolChange;
127   LazarusIDE.OpenEditorsOnCodeToolChange:=true;
128   try
129     if CodeToolBoss.InsertStatements(
130       TInsertStatementPosDescription(InsertPositions[Max(0,WhereRadioGroup.ItemIndex)]),
131       Statements[Max(0,ValueRadioGroup.ItemIndex)])
132     then begin
133       ModalResult:=mrOk;
134     end else begin
135       ModalResult:=mrAbort;
136     end;
137   finally
138     LazarusIDE.OpenEditorsOnCodeToolChange:=OldChange;
139   end;
140 end;
141 
142 procedure TQFInitLocalVarDialog.Init(TheStatements: TStrings;
143   TheInsertPositions: TObjectList);
144 var
145   i: Integer;
146   InsertPos: TInsertStatementPosDescription;
147   sl: TStringList;
148   s: String;
149   CodeXY: TCodeXYPosition;
150 begin
151   Statements:=TheStatements;
152   InsertPositions:=TheInsertPositions;
153   sl:=TStringList.Create;
154   try
155     // show possible insert positions
156     for i:=0 to InsertPositions.Count-1 do begin
157       InsertPos:=TInsertStatementPosDescription(InsertPositions[i]);
158       CodeXY:=InsertPos.CodeXYPos;
159       s:=ExtractFileName(CodeXY.Code.Filename)
160          +'('+IntToStr(CodeXY.Y)+','+IntToStr(CodeXY.X)+') '
161          +InsertPos.Description;
162       sl.Add(s);
163     end;
164     WhereRadioGroup.Items.Assign(sl);
165     WhereRadioGroup.ItemIndex:=0;
166     WhereRadioGroup.AutoSize:=true;
167     WhereRadioGroup.Caption:='Insert where:';
168 
169     // show possible statements
170     sl.Clear;
171     for s in Statements do
172       sl.Add(DbgStr(s));
173     ValueRadioGroup.Items.Assign(sl);
174     ValueRadioGroup.ItemIndex:=0;
175     ValueRadioGroup.Caption:='Insert what:';
176   finally
177     sl.Free;
178   end;
179 end;
180 
181 end.
182 
183