IDE: quick fix for local variable not initialized: add assignment

git-svn-id: trunk@47838 -
This commit is contained in:
mattias 2015-02-16 22:32:30 +00:00
parent f4c1725bcd
commit e6e06c00ca
4 changed files with 163 additions and 45 deletions

View File

@ -9,7 +9,7 @@ uses
implementation implementation
procedure TestInteger(var i: integer); procedure TestInteger(i: integer);
begin begin
writeln(i); writeln(i);
end; end;
@ -21,5 +21,19 @@ begin
TestInteger(i); TestInteger(i);
end; end;
procedure TestLots({%H-}s: string; {%H-}c: char; {%H-}p: pointer);
begin
end;
procedure DoLots;
var
s: string;
c: char;
p: Pointer;
begin
TestLots(s,c,p);
end;
end. end.

View File

@ -5904,6 +5904,7 @@ resourcestring
lisPageNameAlreadyExists = 'Page name "%s" already exists. Not added.'; lisPageNameAlreadyExists = 'Page name "%s" already exists. Not added.';
lisJumpToProcedure = 'Jump to procedure %s'; lisJumpToProcedure = 'Jump to procedure %s';
lisFindDeclarationOf = 'Find Declaration of %s'; lisFindDeclarationOf = 'Find Declaration of %s';
lisInitializeLocalVariable = 'Initialize Local Variable';
implementation implementation
end. end.

View File

@ -1,18 +1,18 @@
object QFInitLocalVarDialog: TQFInitLocalVarDialog object QFInitLocalVarDialog: TQFInitLocalVarDialog
Left = 314 Left = 314
Height = 240 Height = 225
Top = 251 Top = 251
Width = 320 Width = 564
Caption = 'QFInitLocalVarDialog' Caption = 'QFInitLocalVarDialog'
ClientHeight = 240 ClientHeight = 225
ClientWidth = 320 ClientWidth = 564
OnCreate = FormCreate OnCreate = FormCreate
LCLVersion = '1.5' LCLVersion = '1.5'
object ButtonPanel1: TButtonPanel object ButtonPanel1: TButtonPanel
Left = 6 Left = 6
Height = 36 Height = 36
Top = 198 Top = 183
Width = 308 Width = 552
OKButton.Name = 'OKButton' OKButton.Name = 'OKButton'
OKButton.DefaultCaption = True OKButton.DefaultCaption = True
HelpButton.Name = 'HelpButton' HelpButton.Name = 'HelpButton'
@ -24,32 +24,53 @@ object QFInitLocalVarDialog: TQFInitLocalVarDialog
TabOrder = 0 TabOrder = 0
ShowButtons = [pbOK, pbCancel] ShowButtons = [pbOK, pbCancel]
end end
object WhereRadioGroup: TRadioGroup object Panel1: TPanel
Left = 6 Left = 0
Height = 74 Height = 177
Top = 6 Top = 0
Width = 308 Width = 564
Align = alTop Align = alClient
AutoFill = True BevelOuter = bvNone
BorderSpacing.Around = 6 ChildSizing.EnlargeHorizontal = crsScaleChilds
Caption = 'WhereRadioGroup'
ChildSizing.LeftRightSpacing = 6
ChildSizing.EnlargeHorizontal = crsHomogenousChildResize
ChildSizing.EnlargeVertical = crsHomogenousChildResize ChildSizing.EnlargeVertical = crsHomogenousChildResize
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.Layout = cclLeftToRightThenTopToBottom ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 1 ChildSizing.ControlsPerLine = 1
ClientHeight = 177
ClientWidth = 564
TabOrder = 1 TabOrder = 1
end object WhereRadioGroup: TRadioGroup
object ValueGroupBox: TGroupBox Left = 6
Left = 6 Height = 78
Height = 106 Top = 6
Top = 86 Width = 548
Width = 308 AutoFill = True
Align = alClient BorderSpacing.Around = 6
BorderSpacing.Around = 6 Caption = 'WhereRadioGroup'
Caption = 'ValueGroupBox' ChildSizing.LeftRightSpacing = 6
TabOrder = 2 ChildSizing.EnlargeHorizontal = crsHomogenousChildResize
ChildSizing.EnlargeVertical = crsHomogenousChildResize
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 1
TabOrder = 0
end
object ValueRadioGroup: TRadioGroup
Left = 6
Height = 77
Top = 90
Width = 548
AutoFill = True
BorderSpacing.Around = 6
Caption = 'ValueRadioGroup'
ChildSizing.LeftRightSpacing = 6
ChildSizing.EnlargeHorizontal = crsHomogenousChildResize
ChildSizing.EnlargeVertical = crsHomogenousChildResize
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 1
TabOrder = 1
end
end end
end end

View File

@ -32,8 +32,11 @@ unit QFInitLocalVarDlg;
interface interface
uses uses
Classes, SysUtils, contnrs, FileUtil, CodeToolManager, CodeCache, LazIDEIntf, Classes, SysUtils, Math, contnrs, LazLogger,
Forms, Controls, Graphics, Dialogs, ButtonPanel, ExtCtrls, StdCtrls; Forms, Controls, Graphics, Dialogs, ButtonPanel, ExtCtrls, StdCtrls,
CodeToolManager, CodeCache, StdCodeTools,
LazIDEIntf, IDEDialogs,
LazarusIDEStrConsts;
type type
@ -41,11 +44,16 @@ type
TQFInitLocalVarDialog = class(TForm) TQFInitLocalVarDialog = class(TForm)
ButtonPanel1: TButtonPanel; ButtonPanel1: TButtonPanel;
ValueGroupBox: TGroupBox; Panel1: TPanel;
ValueRadioGroup: TRadioGroup;
WhereRadioGroup: TRadioGroup; WhereRadioGroup: TRadioGroup;
procedure ButtonPanel1OKButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
private private
public public
Statements: TStrings;
InsertPositions: TObjectList;
procedure Init(TheStatements: TStrings; TheInsertPositions: TObjectList);
end; end;
function QuickFixLocalVarNotInitialized(Code: TCodeBuffer; X, Y: integer; function QuickFixLocalVarNotInitialized(Code: TCodeBuffer; X, Y: integer;
@ -58,19 +66,42 @@ function QuickFixLocalVarNotInitialized(Code: TCodeBuffer; X, Y: integer;
var var
Statements: TStrings; Statements: TStrings;
InsertPositions: TObjectList; InsertPositions: TObjectList;
Dlg: TQFInitLocalVarDialog;
begin begin
Result:=false; Result:=false;
if not CodeToolBoss.GetPossibleInitsForVariable(Code,X,Y,Statements, Statements:=nil;
InsertPositions) InsertPositions:=nil;
then begin try
if CodeToolBoss.ErrorCode<>nil then if not CodeToolBoss.GetPossibleInitsForVariable(Code,X,Y,Statements,
LazarusIDE.DoJumpToCodeToolBossError InsertPositions)
else then begin
ShowMessage('CodeToolBoss.GetPossibleInitsForVariable failed at '+Code.Filename+'('+IntToStr(Y)+','+IntToStr(X)+')'); if CodeToolBoss.ErrorCode<>nil then
exit; LazarusIDE.DoJumpToCodeToolBossError
end; else
ShowMessage('CodeToolBoss.GetPossibleInitsForVariable failed at '+Code.Filename+'('+IntToStr(Y)+','+IntToStr(X)+')');
exit;
end;
ShowMessage('QuickFixLocalVarNotInitialized not yet implemented'); Dlg:=TQFInitLocalVarDialog.Create(nil);
try
Dlg.Init(Statements,InsertPositions);
case Dlg.ShowModal of
mrOk: Result:=true;
mrAbort:
if CodeToolBoss.ErrorCode<>nil then
LazarusIDE.DoJumpToCodeToolBossError
else
IDEMessageDialog('Error','Unable to insert the code',mtError,[mbOk]);
else
// user cancel
end;
finally
Dlg.Free;
end;
finally
Statements.Free;
InsertPositions.Free;
end;
end; end;
{$R *.lfm} {$R *.lfm}
@ -79,8 +110,59 @@ end;
procedure TQFInitLocalVarDialog.FormCreate(Sender: TObject); procedure TQFInitLocalVarDialog.FormCreate(Sender: TObject);
begin begin
Caption:='Initialize local variable'; Caption:=lisInitializeLocalVariable;
WhereRadioGroup.AutoSize:=true; ButtonPanel1.OKButton.OnClick:=@ButtonPanel1OKButtonClick;
end;
procedure TQFInitLocalVarDialog.ButtonPanel1OKButtonClick(Sender: TObject);
begin
if CodeToolBoss.InsertStatements(
TInsertStatementPosDescription(InsertPositions[Max(0,WhereRadioGroup.ItemIndex)]),
Statements[Max(0,ValueRadioGroup.ItemIndex)])
then begin
ModalResult:=mrOk;
end else begin
ModalResult:=mrAbort;
end;
end;
procedure TQFInitLocalVarDialog.Init(TheStatements: TStrings;
TheInsertPositions: TObjectList);
var
i: Integer;
InsertPos: TInsertStatementPosDescription;
sl: TStringList;
s: String;
CodeXY: TCodeXYPosition;
begin
Statements:=TheStatements;
InsertPositions:=TheInsertPositions;
sl:=TStringList.Create;
try
// show possible insert positions
for i:=0 to InsertPositions.Count-1 do begin
InsertPos:=TInsertStatementPosDescription(InsertPositions[i]);
CodeXY:=InsertPos.CodeXYPos;
s:=ExtractFileName(CodeXY.Code.Filename)
+'('+IntToStr(CodeXY.Y)+','+IntToStr(CodeXY.X)+') '
+InsertPos.Description;
sl.Add(s);
end;
WhereRadioGroup.Items.Assign(sl);
WhereRadioGroup.ItemIndex:=0;
WhereRadioGroup.AutoSize:=true;
WhereRadioGroup.Caption:='Insert where:';
// show possible statements
sl.Clear;
for s in Statements do
sl.Add(DbgStr(s));
ValueRadioGroup.Items.Assign(sl);
ValueRadioGroup.ItemIndex:=0;
ValueRadioGroup.Caption:='Insert what:';
finally
sl.Free;
end;
end; end;
end. end.