mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-03 01:00:34 +02:00
IDE: implemented code macro ofall
git-svn-id: trunk@19654 -
This commit is contained in:
parent
2e228a8d7f
commit
46cf4464ea
@ -401,6 +401,8 @@ type
|
||||
function FindAbstractMethods(Code: TCodeBuffer; X,Y: integer;
|
||||
out ListOfPCodeXYPosition: TFPList;
|
||||
SkipAbstractsInStartClass: boolean = false): boolean;
|
||||
function GetValuesOfCaseVariable(Code: TCodeBuffer; X,Y: integer;
|
||||
List: TStrings): boolean;
|
||||
|
||||
// rename identifier
|
||||
function FindReferences(IdentifierCode: TCodeBuffer;
|
||||
@ -2089,6 +2091,26 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TCodeToolManager.GetValuesOfCaseVariable(Code: TCodeBuffer; X,
|
||||
Y: integer; List: TStrings): boolean;
|
||||
var
|
||||
CursorPos: TCodeXYPosition;
|
||||
begin
|
||||
{$IFDEF CTDEBUG}
|
||||
DebugLn('TCodeToolManager.GetValuesOfCaseVariable A ',Code.Filename);
|
||||
{$ENDIF}
|
||||
Result:=false;
|
||||
if not InitCurCodeTool(Code) then exit;
|
||||
CursorPos.X:=X;
|
||||
CursorPos.Y:=Y;
|
||||
CursorPos.Code:=Code;
|
||||
try
|
||||
Result:=FCurCodeTool.GetValuesOfCaseVariable(CursorPos,List);
|
||||
except
|
||||
on e: Exception do Result:=HandleException(e);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TCodeToolManager.FindReferences(IdentifierCode: TCodeBuffer;
|
||||
X, Y: integer; TargetCode: TCodeBuffer; SkipComments: boolean;
|
||||
var ListOfPCodeXYPosition: TFPList): boolean;
|
||||
|
@ -981,7 +981,7 @@ function ExprTypeToString(const ExprType: TExpressionType): string;
|
||||
begin
|
||||
Result:='Desc='+ExpressionTypeDescNames[ExprType.Desc]
|
||||
+' SubDesc='+ExpressionTypeDescNames[ExprType.SubDesc]
|
||||
+FindContextToString(ExprType.Context);
|
||||
+' '+FindContextToString(ExprType.Context);
|
||||
end;
|
||||
|
||||
function CreateExpressionType(const Desc, SubDesc: TExpressionTypeDesc;
|
||||
|
@ -332,6 +332,8 @@ type
|
||||
function FindAbstractMethods(const CursorPos: TCodeXYPosition;
|
||||
out ListOfPCodeXYPosition: TFPList;
|
||||
SkipAbstractsInStartClass: boolean = false): boolean;
|
||||
function GetValuesOfCaseVariable(const CursorPos: TCodeXYPosition;
|
||||
List: TStrings): boolean;
|
||||
end;
|
||||
|
||||
const
|
||||
@ -1936,6 +1938,89 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TIdentCompletionTool.GetValuesOfCaseVariable(
|
||||
const CursorPos: TCodeXYPosition; List: TStrings): boolean;
|
||||
var
|
||||
CleanCursorPos: integer;
|
||||
CursorNode: TCodeTreeNode;
|
||||
CaseAtom: TAtomPosition;
|
||||
Params: TFindDeclarationParams;
|
||||
EndPos: LongInt;
|
||||
ExprType: TExpressionType;
|
||||
Node: TCodeTreeNode;
|
||||
Tool: TFindDeclarationTool;
|
||||
begin
|
||||
Result:=false;
|
||||
ActivateGlobalWriteLock;
|
||||
Params:=nil;
|
||||
try
|
||||
BuildTreeAndGetCleanPos(trTillCursor,CursorPos,CleanCursorPos,
|
||||
[{$IFNDEF DisableIgnoreErrorAfter}btSetIgnoreErrorPos{$ENDIF}]);
|
||||
|
||||
// find node at position
|
||||
CursorNode:=BuildSubTreeAndFindDeepestNodeAtPos(CleanCursorPos,true);
|
||||
|
||||
// find keyword case
|
||||
MoveCursorToNodeStart(CursorNode);
|
||||
CaseAtom:=CleanAtomPosition;
|
||||
repeat
|
||||
ReadNextAtom;
|
||||
if UpAtomIs('CASE') then
|
||||
CaseAtom:=CurPos
|
||||
until (CurPos.EndPos>SrcLen) or (CurPos.EndPos>CleanCursorPos);
|
||||
if CaseAtom.StartPos<1 then exit;
|
||||
|
||||
// find case variable
|
||||
EndPos:=FindEndOfExpression(CaseAtom.EndPos);
|
||||
if EndPos>CleanCursorPos then
|
||||
EndPos:=CleanCursorPos;
|
||||
//DebugLn(['TIdentCompletionTool.GetValuesOfCaseVariable Expr=',dbgstr(copy(Src,CaseAtom.EndPos,EndPos-CaseAtom.EndPos))]);
|
||||
|
||||
Params:=TFindDeclarationParams.Create;
|
||||
Params.ContextNode:=CursorNode;
|
||||
Params.Flags:=fdfGlobals+fdfDefaultForExpressions;
|
||||
ExprType:=FindExpressionTypeOfVariable(CaseAtom.EndPos,EndPos,Params,true);
|
||||
//DebugLn(['TIdentCompletionTool.GetValuesOfCaseVariable Type=',ExprTypeToString(ExprType)]);
|
||||
|
||||
case ExprType.Desc of
|
||||
|
||||
xtBoolean,xtByteBool,xtLongBool:
|
||||
begin
|
||||
List.Add('True');
|
||||
List.Add('False');
|
||||
end;
|
||||
|
||||
xtContext:
|
||||
begin
|
||||
Node:=ExprType.Context.Node;
|
||||
Tool:=ExprType.Context.Tool;
|
||||
if Node=nil then exit;
|
||||
case Node.Desc of
|
||||
|
||||
ctnEnumerationType:
|
||||
begin
|
||||
Node:=Node.FirstChild;
|
||||
while Node<>nil do begin
|
||||
List.Add(GetIdentifier(@Tool.Src[Node.StartPos]));
|
||||
Node:=Node.NextBrother;
|
||||
end;
|
||||
end;
|
||||
|
||||
else
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
else
|
||||
exit;
|
||||
end;
|
||||
|
||||
Result:=true;
|
||||
finally
|
||||
Params.Free;
|
||||
DeactivateGlobalWriteLock;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TIdentifierListItem }
|
||||
|
||||
function TIdentifierListItem.GetParamList: string;
|
||||
|
@ -174,14 +174,16 @@ function CodeMacroDate(const Parameter: string; InteractiveValue: TPersistent;
|
||||
function CodeMacroTime(const Parameter: string; InteractiveValue: TPersistent;
|
||||
SrcEdit: TSourceEditorInterface;
|
||||
var Value, ErrorMsg: string): boolean;
|
||||
function CodeMacroDateTime(const Parameter: string;
|
||||
InteractiveValue: TPersistent;
|
||||
function CodeMacroDateTime(const Parameter: string; InteractiveValue: TPersistent;
|
||||
SrcEdit: TSourceEditorInterface;
|
||||
var Value, ErrorMsg: string): boolean;
|
||||
function CodeMacroAddMissingEnd(const Parameter: string;
|
||||
InteractiveValue: TPersistent;
|
||||
SrcEdit: TSourceEditorInterface;
|
||||
var Value, ErrorMsg: string): boolean;
|
||||
function CodeMacroOfAll(const Parameter: string; InteractiveValue: TPersistent;
|
||||
SrcEdit: TSourceEditorInterface;
|
||||
var Value, ErrorMsg: string): boolean;
|
||||
|
||||
|
||||
const
|
||||
@ -465,6 +467,55 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function CodeMacroOfAll(const Parameter: string; InteractiveValue: TPersistent;
|
||||
SrcEdit: TSourceEditorInterface; var Value, ErrorMsg: string): boolean;
|
||||
// completes
|
||||
// case SomeEnum of
|
||||
// <list of enums>
|
||||
// end;
|
||||
var
|
||||
List: TStrings;
|
||||
Code: TCodeBuffer;
|
||||
CaretXY: TPoint;
|
||||
p: integer;
|
||||
i: Integer;
|
||||
begin
|
||||
List:=TStringList.Create;
|
||||
try
|
||||
CaretXY:=SrcEdit.CursorTextXY;
|
||||
Code:=SrcEdit.CodeToolsBuffer as TCodeBuffer;
|
||||
Code.LineColToPosition(CaretXY.Y,CaretXY.X,p);
|
||||
if p<1 then begin
|
||||
ErrorMsg:='outside of code';
|
||||
exit(false);
|
||||
end;
|
||||
while (p>1) and (IsIdentChar[Code.Source[p-1]]) do
|
||||
begin
|
||||
dec(p);
|
||||
dec(CaretXY.X);
|
||||
end;
|
||||
if not CodeToolBoss.GetValuesOfCaseVariable(
|
||||
SrcEdit.CodeToolsBuffer as TCodeBuffer,
|
||||
CaretXY.X,CaretXY.Y,List) then
|
||||
begin
|
||||
Result:=false;
|
||||
ErrorMsg:=CodeToolBoss.ErrorMessage;
|
||||
if ErrorMsg='' then
|
||||
ErrorMsg:='missing case variable';
|
||||
LazarusIDE.DoJumpToCodeToolBossError;
|
||||
exit;
|
||||
end;
|
||||
|
||||
Value:='';
|
||||
for i:=0 to List.Count-1 do
|
||||
Value:=Value+List[i]+': ;'+LineEnding;
|
||||
finally
|
||||
List.Free;
|
||||
end;
|
||||
|
||||
Result:=true;
|
||||
end;
|
||||
|
||||
procedure RegisterStandardCodeTemplatesMenuItems;
|
||||
var
|
||||
Path: string;
|
||||
@ -533,6 +584,9 @@ begin
|
||||
'check if the next token in source'
|
||||
+' is an end and if not returns lineend + end; + lineend',
|
||||
@CodeMacroAddMissingEnd,nil);
|
||||
RegisterCodeMacro('OfAll','list of all case values',
|
||||
'returns list of all values of case variable in front of variable',
|
||||
@CodeMacroOfAll,nil);
|
||||
end;
|
||||
|
||||
{ TCodeTemplateEditForm }
|
||||
|
@ -16,18 +16,19 @@ LazarusResources.Add('lazarus_dci_file','DCI',[
|
||||
+'[ife | if then (no begin/end) else (no begin/end)]'#10'if | then'#10#10'els'
|
||||
+'e'#10'[ifeb | if then else]'#10'if | then begin'#10#10'end'#10'else begin'
|
||||
+#10#10'end;'#10'[procedure | procedure declaration]'#10'procedure |();'#10'b'
|
||||
+'egin'#10#10'end;'#10'[trye | try except]'#10'try'#10' | '#10'except'#10#10
|
||||
+'end;'#10'[tryf | try finally]'#10'try'#10' | '#10'finally'#10#10'end;'#10
|
||||
+'[trycf | try finally (with Create/Free)]'#10'|variable := typename.Create; '
|
||||
+#10'try'#10#10'finally'#10' variable.Free;'#10'end;'#10'[whileb | while sta'
|
||||
+'tement]'#10'while | do begin'#10#10'end;'#10'[whiles | while (no begin)]'#10
|
||||
+'while | do'#10'[withb | with statement]'#10'with | do begin'#10#10'end;'#10
|
||||
+'[b | begin end]'#10'begin'#10' |'#10'end;'#10'[withs | with (no begin)]'#10
|
||||
+'with | do'#10'[hexc | HexStr(Cardinal(),8)]'#10'HexStr(Cardinal(|),8)'#10'['
|
||||
+'be | begin end else begin end]'#10'begin'#10' |'#10'end else begin'#10#10
|
||||
+'end;'#10'[withc | with for components]'#10'with | do begin'#10' Name:='''''
|
||||
+';'#10' Parent:=;'#10' Left:=;'#10' Top:=;'#10' Width:=;'#10' Height:=;'
|
||||
+#10' Caption:='''';'#10'end;'#10'[d | debugln]'#10'$(AttributesStart)'#10'E'
|
||||
+'nableMakros=true'#10'$(AttributesEnd)'#10'debugln([''$ProcedureName() ''|])'
|
||||
+';'#10
|
||||
+'egin'#10#10'end;'#10'[ofall | case of all enums]'#10'$(AttributesStart)'#10
|
||||
+'EnableMakros=true'#10'$(AttributesEnd)'#10'of'#10'|$OfAll()end;'#10'[trye |'
|
||||
+' try except]'#10'try'#10' | '#10'except'#10#10'end;'#10'[tryf | try finall'
|
||||
+'y]'#10'try'#10' | '#10'finally'#10#10'end;'#10'[trycf | try finally (with '
|
||||
+'Create/Free)]'#10'|variable := typename.Create; '#10'try'#10#10'finally'#10
|
||||
+' variable.Free;'#10'end;'#10'[whileb | while statement]'#10'while | do beg'
|
||||
+'in'#10#10'end;'#10'[whiles | while (no begin)]'#10'while | do'#10'[withb | '
|
||||
+'with statement]'#10'with | do begin'#10#10'end;'#10'[b | begin end]'#10'beg'
|
||||
+'in'#10' |'#10'end;'#10'[withs | with (no begin)]'#10'with | do'#10'[hexc |'
|
||||
+' HexStr(Cardinal(),8)]'#10'HexStr(Cardinal(|),8)'#10'[be | begin end else b'
|
||||
+'egin end]'#10'begin'#10' |'#10'end else begin'#10#10'end;'#10'[withc | wit'
|
||||
+'h for components]'#10'with | do begin'#10' Name:='''';'#10' Parent:=;'#10
|
||||
+' Left:=;'#10' Top:=;'#10' Width:=;'#10' Height:=;'#10' Caption:='''';'
|
||||
+#10'end;'#10'[d | debugln]'#10'$(AttributesStart)'#10'EnableMakros=true'#10
|
||||
+'$(AttributesEnd)'#10'debugln([''$ProcedureName() ''|]);'#10
|
||||
]);
|
||||
|
@ -71,6 +71,12 @@ procedure |();
|
||||
begin
|
||||
|
||||
end;
|
||||
[ofall | case of all enums]
|
||||
$(AttributesStart)
|
||||
EnableMakros=true
|
||||
$(AttributesEnd)
|
||||
of
|
||||
|$OfAll()end;
|
||||
[trye | try except]
|
||||
try
|
||||
|
|
||||
|
Loading…
Reference in New Issue
Block a user