Merge branch 'CCList_Keywords'

Keywords in Code Completion list, when auto invoke

See merge request freepascal.org/lazarus/lazarus!51
This commit is contained in:
Martin 2022-05-03 21:55:10 +02:00
commit 8229111e5f
8 changed files with 256 additions and 11 deletions

View File

@ -140,6 +140,8 @@ type
FPascalTools: TAVLTree; // tree of TCustomCodeTool sorted TCustomCodeTool(Data).Scanner.MainCode
FTabWidth: integer;
FUseTabs: boolean;
FIdentComplAutoInvokeOnType: Boolean;
FIdentComplIncludeKeywords: Boolean;
FVisibleEditorLines: integer;
FWriteExceptions: boolean;
FWriteLockCount: integer;// Set/Unset counter
@ -324,6 +326,8 @@ type
read FVisibleEditorLines write SetVisibleEditorLines;
property TabWidth: integer read FTabWidth write SetTabWidth;
property UseTabs: boolean read FUseTabs write SetUseTabs;
property IdentComplAutoInvokeOnType: Boolean read FIdentComplAutoInvokeOnType write FIdentComplAutoInvokeOnType;
property IdentComplIncludeKeywords: Boolean read FIdentComplIncludeKeywords write FIdentComplIncludeKeywords;
property CompleteProperties: boolean
read FCompleteProperties write SetCompleteProperties;
property AddInheritedCodeToOverrideMethod: boolean
@ -2514,6 +2518,7 @@ begin
{$ENDIF}
try
FIdentifierListUpdating:=true;
IdentifierList.IdentComplIncludeKeywords := IdentComplIncludeKeywords;
try
Result:=FCurCodeTool.GatherIdentifiers(CursorPos,IdentifierList);
finally

View File

@ -230,6 +230,7 @@ type
TIdentifierList = class
private
FContext: TFindContext;
FIdentComplIncludeKeywords: Boolean;
FNewMemberVisibility: TCodeTreeNodeDesc;
FContextFlags: TIdentifierListContextFlags;
FOnGatherUserIdentifiersToFilteredList: TOnGatherUserIdentifiersToFilteredList;
@ -296,6 +297,7 @@ type
property StartContextPos: TCodeXYPosition
read FStartContextPos write FStartContextPos;
property ContainsFilter: Boolean read FContainsFilter write FContainsFilter;
property IdentComplIncludeKeywords: Boolean read FIdentComplIncludeKeywords write FIdentComplIncludeKeywords;
property OnGatherUserIdentifiersToFilteredList: TOnGatherUserIdentifiersToFilteredList
read FOnGatherUserIdentifiersToFilteredList write FOnGatherUserIdentifiersToFilteredList;
end;
@ -419,7 +421,7 @@ type
procedure GatherUnitnames(const NameSpacePath: string = '');
procedure GatherSourceNames(const Context: TFindContext);
procedure GatherContextKeywords(const Context: TFindContext;
CleanPos: integer; BeautifyCodeOptions: TBeautifyCodeOptions);
CleanPos: integer; BeautifyCodeOptions: TBeautifyCodeOptions; const GatherContext: TFindContext);
procedure GatherUserIdentifiers(const ContextFlags: TIdentifierListContextFlags);
procedure InitCollectIdentifiers(const CursorPos: TCodeXYPosition;
var IdentifierList: TIdentifierList);
@ -1781,7 +1783,7 @@ end;
procedure TIdentCompletionTool.GatherContextKeywords(
const Context: TFindContext; CleanPos: integer;
BeautifyCodeOptions: TBeautifyCodeOptions);
BeautifyCodeOptions: TBeautifyCodeOptions; const GatherContext: TFindContext);
type
TPropertySpecifier = (
psIndex,psRead,psWrite,psStored,psImplements,psDefault,psNoDefault
@ -1890,6 +1892,7 @@ var
Node, SubNode, NodeInFront: TCodeTreeNode;
p, AtomStartPos, AtomEndPos: Integer;
NodeBehind, LastChild: TCodeTreeNode;
NotStartOfOp: Boolean;
begin
try
AtomStartPos:=CleanPos;
@ -1994,6 +1997,7 @@ begin
Add('type');
Add('var');
Add('const');
Add('label');
Add('procedure');
Add('function');
Add('resourcestring');
@ -2024,8 +2028,14 @@ begin
Add('type');
Add('var');
Add('const');
Add('label');
Add('procedure');
Add('function');
if not (ilcfDontAllowProcedures in CurrentIdentifierList.ContextFlags)
and (ilcfStartOfOperand in CurrentIdentifierList.ContextFlags)
then begin
Add('asm');
end;
end;
ctnProcedureHead:
@ -2060,6 +2070,11 @@ begin
if (Node.Desc=ctnRecordType) or (Node.Parent.Desc=ctnRecordType) then begin
Add('case');
end;
end
else
if Node.Parent.Desc = ctnOnBlock then
begin
Add('do');
end;
ctnTypeSection,ctnVarSection,ctnConstSection,ctnLabelSection,ctnResStrSection,
@ -2069,6 +2084,7 @@ begin
Add('const');
Add('var');
Add('resourcestring');
Add('label');
Add('procedure');
Add('function');
Add('property');
@ -2079,6 +2095,81 @@ begin
end;
end;
ctnWithVariable, ctnOnBlock:
begin
Add('do');
end;
ctnBeginBlock,ctnWithStatement,ctnOnStatement:
//ctnInitialization,ctnFinalization: //AllPascalStatements
begin
if CurrentIdentifierList.IdentComplIncludeKeywords and
(GatherContext.Node <> nil) and
(GatherContext.Node.Desc in [ctnBeginBlock,ctnWithStatement,ctnOnStatement])
then
begin
if not (ilcfDontAllowProcedures in CurrentIdentifierList.ContextFlags)
and (ilcfStartOfStatement in CurrentIdentifierList.ContextFlags)
then begin
Add('asm');
Add('begin');
Add('case');
Add('except');
Add('finally');
Add('for');
Add('goto');
Add('if');
Add('raise');
Add('repeat');
Add('try');
Add('until');
Add('while');
Add('with');
end;
if (ilcfStartInStatement in CurrentIdentifierList.ContextFlags)
and not (ilcfStartOfOperand in CurrentIdentifierList.ContextFlags)
and (CurrentIdentifierList.StartBracketLvl = 0)
then begin
Add('else');
Add('end');
Add('then');
Add('do');
Add('downto');
Add('of');
Add('on');
Add('to');
end;
if (ilcfStartOfStatement in CurrentIdentifierList.ContextFlags)
or (CurrentIdentifierList.ContextFlags * [ilcfStartInStatement, ilcfStartOfOperand] = [ilcfStartInStatement, ilcfStartOfOperand])
then
begin
Add('inherited');
end;
if (CurrentIdentifierList.ContextFlags * [ilcfIsExpression, ilcfStartInStatement] <> []) then
begin
NotStartOfOp := not (ilcfStartOfOperand in CurrentIdentifierList.ContextFlags);
if not NotStartOfOp then begin
MoveCursorToAtomPos(CurrentIdentifierList.StartAtomInFront);
NotStartOfOp := AtomIsNumber or AtomIsRealNumber;
end;
if NotStartOfOp then
begin
Add('and');
Add('or');
Add('xor');
Add('div');
Add('in');
Add('as');
Add('is');
Add('mod');
Add('shl');
Add('shr');
end;
Add('not');
end;
end;
end;
ctnProperty:
CheckProperty(Node);
@ -2982,9 +3073,6 @@ begin
FindContextClassAndAncestorsAndExtendedClassOfHelper(IdentStartXY, FICTClassAndAncestorsAndExtClassOfHelper);
end;
CursorContext:=CreateFindContext(Self,CursorNode);
GatherContextKeywords(CursorContext,IdentStartPos,Beautifier);
// check for incomplete context
// context bracket level
@ -3114,6 +3202,9 @@ begin
CurrentIdentifierList.ContextFlags+[ilcfEndOfLine];
end;
CursorContext:=CreateFindContext(Self,CursorNode);
GatherContextKeywords(CursorContext, IdentStartPos, Beautifier, GatherContext); //note: coth:
// search and gather identifiers in context
if (GatherContext.Tool<>nil) and (GatherContext.Node<>nil) then begin
{$IFDEF CTDEBUG}

View File

@ -0,0 +1,110 @@
{%identcomplincludekeywords:on}
unit {completion:+5=!do,!label,!repeat,!else,!inherited,!and,!not}
tcompletion_keywords {completion:+21=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+2=!do,!label,!repeat,!else,!inherited,!and,!not}
{$mode objfpc}{$H+}
{$ModeSwitch typehelpers}
{$ModeSwitch advancedrecords}
interface {completion:+11=!do,!label,!repeat,!else,!inherited,!and,!not}
uses {completion:+5=!do,!label,!repeat,!else,!inherited,!and,!not}
system {completion:+7=!do,!label,!repeat,!else,!inherited,!and,!not}
;
const {completion:+6=!do,!label,!repeat,!else,!inherited,!and,!not}
MYCONST
= {completion:+2=!do,!label,!repeat,!else,!inherited,!and,!not}
1 {completion:+2=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+2=!do,!label,!repeat,!else,!inherited,!and,!not}
MYCONST_B = 'test' ; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
var {completion:+4=!do,!label,!repeat,!else,!inherited,!and,!not}
MyVar {completion:+6=!do,!label,!repeat,!else,!inherited,!and,!not}
: {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
Integer {completion:+8=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
MyVar2: TObject ; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not,!case}
// TODO: "case" in record
MyVar3: record {completion:+7=!do,!label,!repeat,!else,!inherited,!and,!not}
a: Integer ; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
a2 {completion:+3=!do,!label,!repeat,!else,!inherited,!and,!not}
: {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
Integer {completion:+8=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
case
boolean {completion:+8=!do,!label,!repeat,!else,!inherited,!and,!not} // TODO: "of"
of
true: (
b: TObject;
);
false: (
c: array [0..1] of integer;
);
end;
type
TFoo {completion:+5=!do,!label,!repeat,!else,!inherited,!and,!not}
= {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
class {completion:+6=!do,!label,!repeat,!else,!inherited,!and,!not}
;
TIntA = {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not} // TODO: "array"
array {completion:+6=!do,!label,!repeat,!else,!inherited,!and,!not} // TODO: "of"
of
integer;
TIntB = array
[ 0..9 ] {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not} // TODO: "of"
of
integer;
TFoo = class {completion:+6=!do,!label,!repeat,!else,!inherited,!and,!not}
( {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
TObject {completion:+8=!do,!label,!repeat,!else,!inherited,!and,!not}
) {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
a {completion:+2=!do,!label,!repeat,!else,!inherited,!and,!not}
: {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
Integer {completion:+8=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
private
procedure {completion:+10=!do,!label,!repeat,!else,!inherited,!and,!not}
Bar {completion:+5=!do,!label,!repeat,!else,!inherited,!and,!not}
; {completion:+1=!do,!label,!repeat,!else,!inherited,!and,!not}
end;
procedure SomeProc(val: Integer; var v2, v3: TIntA);
function f1: TObject;
implementation
var
bVal: Boolean;
iVal: integer;
ia: TIntA;
procedure SomeProc(val: Integer);
begin {completion:+6=!do,!label,repeat,!else,inherited,!and,!not}
; {completion:+1=!do,!label,repeat,!else,inherited,!and,!not}
end;
procedure SomeProc(val: Integer; var v2, v3: TIntA);
begin
end;
function f1: TObject;
begin
end;
procedure TFoo.Bar;
begin
end;
begin
if {completion:+3=!and,not,inherited} // +3 test in space after if
iVal {completion:!and,!in,not,inherited;+1=!and,!in,!not,inherited;+5=and,in,!not,!inherited} // + 5 in spaces after iVal
end.

View File

@ -24,6 +24,9 @@
(* Expectation in test-files
{%identcomplincludekeywords:on}
Enables CodeToolBoss.IdentComplIncludeKeywords
{SELECT:TESTS=TEST(|TEST)*}
Each "{" comment starting with a..z is a test instruction, or a list of test instructions separated by |
@ -278,6 +281,11 @@ begin
DoParseModule(MainCode,FMainTool);
CommentP:=1;
Src:=MainTool.Src;
CodeToolBoss.IdentComplIncludeKeywords := False;
if pos('{%identcomplincludekeywords:on}', LowerCase(Src)) > 0 then
CodeToolBoss.IdentComplIncludeKeywords := True;
while CommentP<length(Src) do begin
CommentP:=FindNextComment(Src,CommentP);
if CommentP>length(Src) then break;
@ -472,6 +480,7 @@ begin
until CommentP >= CommentEnd;
end;
CheckReferenceMarkers;
CodeToolBoss.IdentComplIncludeKeywords := False;
end;
function TCustomTestFindDeclaration.GetMarkers(Index: integer): TFDMarker;
@ -515,6 +524,7 @@ procedure TCustomTestFindDeclaration.SetUp;
begin
inherited SetUp;
FMarkers:=TObjectList.Create(true);
CodeToolBoss.IdentComplIncludeKeywords := False;
end;
procedure TCustomTestFindDeclaration.TearDown;

View File

@ -128,6 +128,7 @@ type
FIdentComplAutoStartAfterPoint: boolean;
FIdentComplAutoUseSingleIdent: boolean;
FIdentComplUseContainsFilter: Boolean;
FIdentComplIncludeKeywords: Boolean;
FIdentComplIncludeCodeTemplates: Boolean;
FIdentComplIncludeWords: TIdentComplIncludeWords;
FIdentComplShowIcons: Boolean;
@ -269,6 +270,8 @@ type
write FIdentComplAutoUseSingleIdent;
property IdentComplUseContainsFilter: boolean read FIdentComplUseContainsFilter
write FIdentComplUseContainsFilter;
property IdentComplIncludeKeywords: Boolean read FIdentComplIncludeKeywords
write FIdentComplIncludeKeywords;
property IdentComplIncludeCodeTemplates: boolean read FIdentComplIncludeCodeTemplates
write FIdentComplIncludeCodeTemplates;
property IdentComplIncludeWords: TIdentComplIncludeWords read FIdentComplIncludeWords
@ -597,6 +600,8 @@ begin
'CodeToolsOptions/IdentifierCompletion/AutoUseSingleIdent',true);
FIdentComplUseContainsFilter:=XMLConfig.GetValue(
'CodeToolsOptions/IdentifierCompletion/UseContainsFilter',true);
FIdentComplIncludeKeywords:=XMLConfig.GetValue(
'CodeToolsOptions/IdentifierCompletion/IncludeKeywords',false);
FIdentComplIncludeCodeTemplates:=XMLConfig.GetValue(
'CodeToolsOptions/IdentifierCompletion/IncludeCodeTemplates',true);
FIdentComplIncludeWords:=IdentComplIncludeWordsNamesToEnum(XMLConfig.GetValue(
@ -782,6 +787,8 @@ begin
FIdentComplAutoUseSingleIdent,true);
XMLConfig.SetDeleteValue('CodeToolsOptions/IdentifierCompletion/UseContainsFilter',
FIdentComplUseContainsFilter,true);
XMLConfig.SetDeleteValue('CodeToolsOptions/IdentifierCompletion/IncludeKeywords',
FIdentComplIncludeKeywords,false);
XMLConfig.SetDeleteValue('CodeToolsOptions/IdentifierCompletion/IncludeCodeTemplates',
FIdentComplIncludeCodeTemplates,true);
XMLConfig.SetDeleteValue('CodeToolsOptions/IdentifierCompletion/IncludeWords',
@ -942,6 +949,7 @@ begin
FIdentComplAutoStartAfterPoint:=CodeToolsOpts.FIdentComplAutoStartAfterPoint;
FIdentComplAutoUseSingleIdent:=CodeToolsOpts.FIdentComplAutoUseSingleIdent;
FIdentComplUseContainsFilter:=CodeToolsOpts.FIdentComplUseContainsFilter;
FIdentComplIncludeKeywords := CodeToolsOpts.FIdentComplIncludeKeywords;
FIdentComplIncludeCodeTemplates:=CodeToolsOpts.FIdentComplIncludeCodeTemplates;
FIdentComplShowIcons:=CodeToolsOpts.FIdentComplShowIcons;
FIdentComplAddParameterBrackets:=CodeToolsOpts.FIdentComplAddParameterBrackets;
@ -1014,6 +1022,7 @@ begin
FIdentComplAutoStartAfterPoint:=true;
FIdentComplAutoUseSingleIdent:=true;
FIdentComplUseContainsFilter:=true;
FIdentComplIncludeKeywords := false;
FIdentComplIncludeCodeTemplates:=true;
FIdentComplShowIcons:=false;
FIdentComplAddParameterBrackets:=true;
@ -1105,6 +1114,7 @@ begin
and (FIdentComplAutoStartAfterPoint=CodeToolsOpts.FIdentComplAutoStartAfterPoint)
and (FIdentComplAutoUseSingleIdent=CodeToolsOpts.FIdentComplAutoUseSingleIdent)
and (FIdentComplUseContainsFilter=CodeToolsOpts.FIdentComplUseContainsFilter)
and (FIdentComplIncludeKeywords=CodeToolsOpts.FIdentComplIncludeKeywords)
and (FIdentComplIncludeCodeTemplates=CodeToolsOpts.FIdentComplIncludeCodeTemplates)
and (FIdentComplShowIcons=CodeToolsOpts.FIdentComplShowIcons)
and (FIdentComplAddParameterBrackets=CodeToolsOpts.FIdentComplAddParameterBrackets)
@ -1174,6 +1184,8 @@ begin
Boss.CursorBeyondEOL:=CursorBeyondEOL;
Boss.AddInheritedCodeToOverrideMethod:=AddInheritedCodeToOverrideMethod;
Boss.CompleteProperties:=CompleteProperties;
Boss.IdentComplAutoInvokeOnType:=IdentComplAutoInvokeOnType;
Boss.IdentComplIncludeKeywords:=IdentComplIncludeKeywords;
// CreateCode - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AssignTo(Boss.SourceChangeCache.BeautifyCodeOptions);

View File

@ -95,7 +95,7 @@ object CodetoolsIndentifierCompletionOptionsFrame: TCodetoolsIndentifierCompleti
Caption = 'ICReplaceCheckBox'
ParentShowHint = False
ShowHint = True
TabOrder = 16
TabOrder = 17
end
object ICAddDoCheckBox: TCheckBox
AnchorSideLeft.Control = Owner
@ -206,7 +206,7 @@ object CodetoolsIndentifierCompletionOptionsFrame: TCodetoolsIndentifierCompleti
Caption = 'ICJumpToErrorCheckBox'
ParentShowHint = False
ShowHint = True
TabOrder = 17
TabOrder = 18
end
object ICAutoUseSingleIdent: TCheckBox
AnchorSideLeft.Control = Owner
@ -232,7 +232,7 @@ object CodetoolsIndentifierCompletionOptionsFrame: TCodetoolsIndentifierCompleti
Top = 327
Width = 157
Caption = 'ICContainsFilterCheckBox'
TabOrder = 18
TabOrder = 19
end
object ICAppearanceDividerBevel: TDividerBevel
AnchorSideLeft.Control = Owner
@ -259,7 +259,7 @@ object CodetoolsIndentifierCompletionOptionsFrame: TCodetoolsIndentifierCompleti
Top = 409
Width = 224
Caption = 'ICUseIconsInCompletionBoxCheckBox'
TabOrder = 15
TabOrder = 16
end
object ICContentDividerBevel: TDividerBevel
AnchorSideLeft.Control = Owner
@ -302,17 +302,29 @@ object CodetoolsIndentifierCompletionOptionsFrame: TCodetoolsIndentifierCompleti
Style = csDropDownList
TabOrder = 13
end
object ICIncludeCodeTemplatesCheckBox: TCheckBox
object ICIncludeKeywordsCheckBox: TCheckBox
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = ICAddWordsComboBox
AnchorSideTop.Side = asrBottom
Left = 0
Height = 19
Top = 382
Width = 167
BorderSpacing.Top = 2
Caption = 'ICIncludeKeywordsCheckBox'
TabOrder = 14
end
object ICIncludeCodeTemplatesCheckBox: TCheckBox
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = ICIncludeKeywordsCheckBox
AnchorSideTop.Side = asrBottom
Left = 0
Height = 19
Top = 373
Width = 205
BorderSpacing.Top = 2
Caption = 'ICIncludeCodeTemplatesCheckBox'
TabOrder = 14
TabOrder = 15
end
object ICAutoOnTypeUseTimer: TCheckBox
AnchorSideLeft.Control = Owner

View File

@ -46,6 +46,7 @@ type
ICContainsFilterCheckBox: TCheckBox;
ICAddDoCheckBox: TCheckBox;
ICAutoAddParameterBracketsCheckBox: TCheckBox;
ICIncludeKeywordsCheckBox: TCheckBox;
ICIncludeCodeTemplatesCheckBox: TCheckBox;
ICMiscDividerBevel: TDividerBevel;
ICOpenDividerBevel: TDividerBevel;
@ -120,6 +121,7 @@ begin
dlgIncludeWordsToIdentCompl_IncludeFromAllUnits+LineEnding+
dlgIncludeWordsToIdentCompl_IncludeFromCurrentUnit+LineEnding+
dlgIncludeWordsToIdentCompl_DontInclude;
ICIncludeKeywordsCheckBox.Caption := dlgIncludeKeywordsToIdentCompl;
ICIncludeCodeTemplatesCheckBox.Caption := dlgIncludeCodeTemplatesToIdentCompl;
ICAppearanceDividerBevel.Caption:=lisAppearance;
@ -153,6 +155,7 @@ begin
ICSortForHistoryCheckBox.Checked:=IdentComplSortForHistory;
ICSortForScopeCheckBox.Checked:=IdentComplSortForScope;
ICContainsFilterCheckBox.Checked:=IdentComplUseContainsFilter;
ICIncludeKeywordsCheckBox.Checked := IdentComplIncludeKeywords;
ICIncludeCodeTemplatesCheckBox.Checked:=IdentComplIncludeCodeTemplates;
ICUseIconsInCompletionBoxCheckBox.Checked:=IdentComplShowIcons;
case IdentComplIncludeWords of
@ -185,6 +188,7 @@ begin
IdentComplSortForHistory:=ICSortForHistoryCheckBox.Checked;
IdentComplSortForScope:=ICSortForScopeCheckBox.Checked;
IdentComplUseContainsFilter:=ICContainsFilterCheckBox.Checked;
IdentComplIncludeKeywords := ICIncludeKeywordsCheckBox.Checked;
IdentComplIncludeCodeTemplates:=ICIncludeCodeTemplatesCheckBox.Checked;
IdentComplShowIcons:=ICUseIconsInCompletionBoxCheckBox.Checked;
case ICAddWordsComboBox.ItemIndex of

View File

@ -2066,6 +2066,7 @@ resourcestring
dlgIncludeWordsToIdentCompl_IncludeFromAllUnits = 'from all units';
dlgIncludeWordsToIdentCompl_IncludeFromCurrentUnit = 'from current unit';
dlgIncludeWordsToIdentCompl_DontInclude = 'don''t include';
dlgIncludeKeywordsToIdentCompl = 'Include all keywords and operators';
dlgIncludeCodeTemplatesToIdentCompl = 'Include code templates';
dlgMarkupUserDefined = 'User defined markup';