IDE: Disable UseProjUnit menu item when there are no available units

git-svn-id: trunk@30366 -
This commit is contained in:
juha 2011-04-18 08:45:18 +00:00
parent 314d8c539a
commit b8367c2630
2 changed files with 78 additions and 52 deletions

View File

@ -3896,6 +3896,8 @@ var
ASrcEdit: TSourceEditor;
AnUnitInfo: TUnitInfo;
Editable, SelAvail, IdentFound, StringFound: Boolean;
CurrentUnitName: String;
AvailUnits: TStringList;
StartCode, EndCode: TCodeBuffer;
StartPos, EndPos: TPoint;
NewX, NewY, NewTopLine: integer;
@ -3905,32 +3907,41 @@ begin
SelAvail:=False;
IdentFound:=False;
StringFound:=False;
if BeginCodeTool(ASrcEdit,AnUnitInfo,[]) then begin
Editable:=not ASrcEdit.ReadOnly;
SelAvail:=ASrcEdit.SelectionAvailable;
AvailUnits:=nil;
try
if BeginCodeTool(ASrcEdit,AnUnitInfo,[]) then begin
Editable:=not ASrcEdit.ReadOnly;
SelAvail:=ASrcEdit.SelectionAvailable;
// Try to find main identifier declaration to enable rename feature.
CursorXY:=ASrcEdit.EditorComponent.LogicalCaretXY;
IdentFound:=CodeToolBoss.FindMainDeclaration(AnUnitInfo.Source,
CursorXY.X,CursorXY.Y,StartCode,NewX,NewY,NewTopLine);
// Get Available Units count to enable UseProjUnit feature.
AvailUnits:=GetAvailableUnits(ASrcEdit, CurrentUnitName);
// Calculate start and end of string expr to enable ResourceString feature.
if ASrcEdit.EditorComponent.SelAvail then
CursorXY:=ASrcEdit.EditorComponent.BlockBegin;
if CodeToolBoss.GetStringConstBounds(AnUnitInfo.Source,CursorXY.X,CursorXY.Y,
StartCode,StartPos.X,StartPos.Y,
EndCode,EndPos.X,EndPos.Y,true) then
StringFound:=(StartCode<>EndCode) or (CompareCaret(StartPos,EndPos)<>0);
end;
with MainIDEBar do begin
//itmRefactorCodeTools
itmRefactorCompleteCode.Enabled:=Editable;
itmRefactorUseUnit.Enabled:=Editable;
itmRefactorRenameIdentifier.Enabled:=Editable and IdentFound;
itmRefactorExtractProc.Enabled:=Editable and SelAvail;
itmRefactorInvertAssignment.Enabled:=Editable and SelAvail;
//itmRefactorAdvanced
itmRefactorMakeResourceString.Enabled:=Editable and StringFound;
// Try to find main identifier declaration to enable rename feature.
CursorXY:=ASrcEdit.EditorComponent.LogicalCaretXY;
IdentFound:=CodeToolBoss.FindMainDeclaration(AnUnitInfo.Source,
CursorXY.X,CursorXY.Y,StartCode,NewX,NewY,NewTopLine);
// Calculate start and end of string expr to enable ResourceString feature.
if ASrcEdit.EditorComponent.SelAvail then
CursorXY:=ASrcEdit.EditorComponent.BlockBegin;
if CodeToolBoss.GetStringConstBounds(AnUnitInfo.Source,CursorXY.X,CursorXY.Y,
StartCode,StartPos.X,StartPos.Y,
EndCode,EndPos.X,EndPos.Y,true) then
StringFound:=(StartCode<>EndCode) or (CompareCaret(StartPos,EndPos)<>0);
end;
with MainIDEBar do begin
//itmRefactorCodeTools
itmRefactorCompleteCode.Enabled:=Editable;
itmRefactorUseUnit.Enabled:=Editable and
Assigned(AvailUnits) and (AvailUnits.Count>0);
itmRefactorRenameIdentifier.Enabled:=Editable and IdentFound;
itmRefactorExtractProc.Enabled:=Editable and SelAvail;
itmRefactorInvertAssignment.Enabled:=Editable and SelAvail;
//itmRefactorAdvanced
itmRefactorMakeResourceString.Enabled:=Editable and StringFound;
end;
finally
AvailUnits.Free;
end;
end;

View File

@ -31,7 +31,7 @@ interface
uses
Classes, SysUtils, Forms, Controls, ComCtrls, StdCtrls, ExtCtrls, Buttons,
ButtonPanel, Dialogs, LCLProc, FileProcs,
SrcEditorIntf, LazIDEIntf, IDEImagesIntf, LazarusIDEStrConsts,
SourceEditor, LazIDEIntf, IDEImagesIntf, LazarusIDEStrConsts,
ProjectIntf, Project, CodeCache, CodeToolManager;
type
@ -57,39 +57,34 @@ type
end;
function GetAvailableUnits(SrcEdit: TSourceEditor;
out CurrentUnitName: String): TStringList;
function ShowUseProjUnitDialog: TModalResult;
implementation
{$R *.lfm}
function ShowUseProjUnitDialog: TModalResult;
function GetAvailableUnits(SrcEdit: TSourceEditor;
out CurrentUnitName: String): TStringList;
var
UseProjUnitDlg: TUseProjUnitDialog;
SrcEdit: TSourceEditorInterface;
Code: TCodeBuffer;
ProjFile: TUnitInfo;
MainUsedUnits, ImplUsedUnits: TStrings;
AvailUnits: TStringList;
CurrentUnitName, s: String;
CTRes: Boolean;
ProjFile: TUnitInfo;
s: String;
begin
Result:=mrOk;
if not LazarusIDE.BeginCodeTools then exit;
// get cursor position
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
if SrcEdit=nil then exit;
Code:=TCodeBuffer(SrcEdit.CodeToolsBuffer);
if Code=nil then exit;
MainUsedUnits:=nil;
ImplUsedUnits:=nil;
AvailUnits:=TStringList.Create;
Result:=nil;
try
if not CodeToolBoss.FindUsedUnitNames(Code,MainUsedUnits,ImplUsedUnits) then begin
if SrcEdit=nil then exit;
Assert(Assigned(SrcEdit.CodeBuffer));
if not CodeToolBoss.FindUsedUnitNames(SrcEdit.CodeBuffer,
MainUsedUnits,ImplUsedUnits) then begin
DebugLn(['ShowUseProjUnitDialog CodeToolBoss.FindUsedUnitNames failed']);
LazarusIDE.DoJumpToCodeToolBossError;
exit(mrCancel);
exit;
end;
Result:=TStringList.Create; // Result TStringList must be freed by caller.
TStringList(MainUsedUnits).CaseSensitive:=False;
TStringList(ImplUsedUnits).CaseSensitive:=False;
// Debug message will be cleaned soon!!!
@ -97,8 +92,8 @@ begin
CurrentUnitName:=TUnitInfo(SrcEdit.GetProjectFile).Unit_Name
else
CurrentUnitName:='';
DebugLn('ShowUseProjUnitDialog: CurrentUnitName before loop = ' + CurrentUnitName);
// Add available unit names to AvailUnits.
DebugLn('ShowUseProjUnitDialog: CurrentUnitName before loop = '+CurrentUnitName);
// Add available unit names to Result.
ProjFile:=Project1.FirstPartOfProject;
while ProjFile<>nil do begin
s:=ProjFile.Unit_Name;
@ -112,11 +107,33 @@ begin
end;
if (ProjFile<>Project1.MainUnitInfo) and (s<>'') then
if (MainUsedUnits.IndexOf(s)<0) and (ImplUsedUnits.IndexOf(s)<0) then
AvailUnits.Add(s);
Result.Add(s);
ProjFile:=ProjFile.NextPartOfProject;
end;
// Show the dialog.
finally
ImplUsedUnits.Free;
MainUsedUnits.Free;
end;
end;
function ShowUseProjUnitDialog: TModalResult;
var
UseProjUnitDlg: TUseProjUnitDialog;
SrcEdit: TSourceEditor;
AvailUnits: TStringList;
CurrentUnitName, s: String;
CTRes: Boolean;
begin
Result:=mrOk;
if not LazarusIDE.BeginCodeTools then exit;
// get cursor position
SrcEdit:=SourceEditorManager.ActiveEditor;
try
AvailUnits:=GetAvailableUnits(SrcEdit, CurrentUnitName);
if AvailUnits=nil then
exit(mrCancel);
if AvailUnits.Count>0 then begin
// Show the dialog.
AvailUnits.Sorted:=True;
UseProjUnitDlg:=TUseProjUnitDialog.Create(nil);
try
@ -129,9 +146,9 @@ begin
s:=UseProjUnitDlg.SelectedUnit;
if s<>'' then begin
if UseProjUnitDlg.InterfaceSelected then
CTRes:=CodeToolBoss.AddUnitToMainUsesSection(Code, s, '')
CTRes:=CodeToolBoss.AddUnitToMainUsesSection(SrcEdit.CodeBuffer, s, '')
else
CTRes:=CodeToolBoss.AddUnitToImplementationUsesSection(Code, s, '');
CTRes:=CodeToolBoss.AddUnitToImplementationUsesSection(SrcEdit.CodeBuffer, s, '');
if not CTRes then begin
LazarusIDE.DoJumpToCodeToolBossError;
exit(mrCancel);
@ -144,13 +161,11 @@ begin
end
else begin
if CurrentUnitName='' then
CurrentUnitName:=ExtractFileNameOnly(Code.Filename);
CurrentUnitName:=ExtractFileNameOnly(SrcEdit.CodeBuffer.Filename);
ShowMessage(Format(dlgAlreadyUsesAllOtherUnits,[CurrentUnitName]));
end;
finally
CodeToolBoss.SourceCache.ClearAllSourceLogEntries;
ImplUsedUnits.Free;
MainUsedUnits.Free;
AvailUnits.Free;
end;
end;