SynEdit: PasHighLighter, improve "external name ..."

This commit is contained in:
Martin 2025-03-15 10:09:28 +01:00
parent f76ca60849
commit 1ec1f1e30c
2 changed files with 73 additions and 10 deletions

View File

@ -133,6 +133,7 @@ type
// var Foo; public name 'bar';
// after public, export or external: "name" may follow
// >>> KEPT until ONE AFTER the ";" => to prevent next token from being mistaken
tsAfterExternalName, // "external name name" (2nd name is a string constant)
// >>> Also SET BY "var"/"type"/"const" => to prevent next token from being mistaken
tsAfterCvar, // cvar;
// >>> KEPT until ONE AFTER the ";" => to prevent next token from being mistaken
@ -1690,6 +1691,8 @@ begin
then begin
Result := tkKey;
fRange := fRange - [rsAsm, rsInClassHeader, rsInTypeHelper, rsInObjcProtocol, rsAfterClassMembers];
if FTokenState in [tsAfterExternal, tsAfterExternalName] then
FTokenState := tsNone;
PasCodeFoldRange.BracketNestLevel := 0; // Reset in case of partial code
sl := fStringLen;
// there may be more than on block ending here
@ -1905,6 +1908,7 @@ begin
then
begin
Result := tkModifier;
FNextTokenState := tsAfterExternalName;
fRange := fRange + [rsInObjcProtocol];
FOldRange := FOldRange - [rsInObjcProtocol];
end
@ -1916,7 +1920,7 @@ begin
then
begin
Result := tkModifier;
FNextTokenState := tsAfterExternal; // external 'foo' name 'bar'
FNextTokenState := tsAfterExternalName;
end
else
Result := tkIdentifier;
@ -2338,7 +2342,7 @@ begin
if (PasCodeFoldRange.BracketNestLevel = 0) and
( (FTokenState in [tsAfterTypedConst, tsAfterCvar])
or
( (not (FTokenState in [tsAfterExternal, tsAfterVarConstType])) and
( (not (FTokenState in [tsAfterExternal, tsAfterExternalName, tsAfterVarConstType])) and
( ( (fRange * [rsInProcHeader, rsProperty, rsAfterEqualOrColon, rsWasInProcHeader] = [rsWasInProcHeader]) and
(tfb in ProcModifierAllowed - [cfbtClass, cfbtClassSection, cfbtRecord, cfbtClassConstBlock, cfbtClassTypeBlock])
) or
@ -2978,7 +2982,7 @@ var
begin
tfb := TopPascalCodeFoldBlockType;
if (PasCodeFoldRange.BracketNestLevel in [0, 1]) and
(not(FTokenState in [tsAfterVarConstType, tsAfterExternal])) and
(not(FTokenState in [tsAfterVarConstType, tsAfterExternal, tsAfterExternalName])) and
( ( (fRange * [rsInProcHeader, rsProperty, rsAfterEqualOrColon, rsWasInProcHeader] = [rsWasInProcHeader]) and
(tfb in ProcModifierAllowed)
) or
@ -3010,7 +3014,7 @@ var
begin
tfb := TopPascalCodeFoldBlockType;
if (PasCodeFoldRange.BracketNestLevel = 0) and
(not(FTokenState in [tsAfterVarConstType, tsAfterExternal])) and
(not(FTokenState in [tsAfterVarConstType, tsAfterExternal, tsAfterExternalName])) and
( ( (fRange * [rsInProcHeader, rsProperty, rsAfterEqualOrColon, rsWasInProcHeader] = [rsWasInProcHeader]) and
(tfb in ProcModifierAllowed)
) or
@ -4772,7 +4776,7 @@ begin
if FTokenState = tsAfterCvar then
FNextTokenState := tsAfterCvar
else
if FTokenState = tsAfterExternal then
if FTokenState in [tsAfterExternal, tsAfterExternalName] then
FNextTokenState := tsAfterVarConstType
else
if (rsInTypedConst in fRange) and (PasCodeFoldRange.BracketNestLevel = 0) then
@ -4880,8 +4884,6 @@ end;
procedure TSynPasSyn.StringProc;
begin
fTokenID := tkString;
if FTokenState = tsAfterExternal then
FNextTokenState := tsAfterExternal; // external 'foo' name 'bar'
Inc(Run);
while (not (fLine[Run] in [#0, #10, #13])) do begin
if fLine[Run] = '''' then begin
@ -5208,8 +5210,14 @@ begin
FLastTokenTypeDeclExtraAttrib := FTokenTypeDeclExtraAttrib;
//end;
if not (FTokenID in [tkSpace, tkComment, tkIDEDirective, tkDirective, tkNull]) then
if not (FTokenID in [tkSpace, tkComment, tkIDEDirective, tkDirective, tkNull]) then begin
if (FNextTokenState = tsNone) and (FTokenState in [tsAfterExternal, tsAfterExternalName]) and
(FTokenID in [tkIdentifier, tkString, tkKey, tkSymbol])
then
FNextTokenState := FTokenState;
FTokenState := FNextTokenState;
end;
if (IsAtCaseLabel) and (rsAtCaseLabel in fRange) then begin
FTokenIsCaseLabel := True;

View File

@ -63,6 +63,7 @@ type
procedure TestContextForProcModifiers2;
procedure TestContextForProcModifiersName;
procedure TestContextForVarModifiers;
procedure TestContextForVarModifiers2;
procedure TestContextForProperties;
procedure TestContextForProcedure;
procedure TestContextForProcedureNameAttr;
@ -917,14 +918,17 @@ begin
'procedure name; external ''name'' name ''name'';',
'procedure name; public name ''name'';',
' begin end;',
'function name: name; external ''name'' name ''name'';',
'function name: name; external ''name'' name ''name'';', // 4
'function name: name; public name ''name'';',
' begin end;',
'',
'type TFoo = class ',
'type TFoo = class ', // 8
'procedure name; public name: name;', // just a public field
'function name: name; public name: name;', // just a public field
'end;',
'', // 12
'procedure name; external name name;', // external keyword_NAME const_NAME
'procedure name; external foo name name;', // external foo keyword_NAME const_NAME
''
]);
@ -960,6 +964,14 @@ begin
[tkKey, tkSpace, tkIdentifier+p, TK_Colon, tkSpace, tkIdentifier, TK_Semi, tkSpace,
tkKey, tkSpace, tkIdentifier, TK_Colon, tkSpace, tkIdentifier, TK_Semi]);
CheckTokensForLine('procedure name; external name name;', 13,
[tkKey, tkSpace, tkIdentifier+p, TK_Semi, tkSpace,
tkModifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
CheckTokensForLine('procedure name; external foo name name;', 14,
[tkKey, tkSpace, tkIdentifier+p, TK_Semi, tkSpace,
tkModifier, tkSpace, tkIdentifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
end;
end;
@ -1270,6 +1282,27 @@ begin
end;
end;
procedure TTestHighlighterPas.TestContextForVarModifiers2;
begin
ReCreateEdit;
EnableFolds([cfbtBeginEnd..cfbtNone]);
SetLines
([ 'Unit A; interface',
'var',
'name: name; external name name;', // external const_NAME keyword_NAME const_NAME
'name: name; external foo name name;', // external const_NAME keyword_NAME const_NAME
''
]);
CheckTokensForLine('name: name; external name name;', 2,
[tkIdentifier, TK_Colon, tkSpace, tkIdentifier, TK_Semi, tkSpace,
tkModifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
CheckTokensForLine('name: name; external foo name name;', 3,
[tkIdentifier, TK_Colon, tkSpace, tkIdentifier, TK_Semi, tkSpace,
tkModifier, tkSpace, tkIdentifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
end;
procedure TTestHighlighterPas.TestContextForProperties;
var
AFolds: TPascalCodeFoldBlockTypes;
@ -3351,6 +3384,10 @@ begin
'function Foo(d:word=2-x;e:boolean=(1=y*2);f:qword=default(qword); g:MySet=[a1..a2]): integer;',
'procedure Foo(a:byte;', //5
'b, b2:word);',
'', // 7
'procedure name; external &name name name;', // external const_NAME keyword_NAME const_NAME
'procedure name; external foo name name;', // external const_FOO keyword_NAME const_NAME
'procedure name; external name name;', // external keyword_NAME const_NAME
''
]);
@ -3402,6 +3439,10 @@ begin
tkIdentifier+ProcType, TK_Bracket, TK_Semi]);
CheckTokensForLine('procedure name; external name name name;', 8,
[tkKey, tkSpace, tkIdentifier+ProcName, TK_Semi, tkSpace,
tkModifier, tkSpace, tkIdentifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
//PropName.Clear;
//PasHighLighter.DeclaredTypeAttributeMode := tamIdentifierOnly;
//PasHighLighter.DeclaredValueAttributeMode := tamIdentifierOnly;
@ -3519,9 +3560,13 @@ begin
'b:byte;',
'c:array of word;',
'end;',
'', // 18
'name: name; external name name;', // external keyword_NAME const_NAME
'name: name; external foo name name;', // external foo keyword_NAME const_NAME
''
]);
DeclVarName := PasHighLighter.DeclarationVarConstNameAttr;
DeclTypeName := PasHighLighter.DeclarationTypeNameAttr;
DeclType := PasHighLighter.DeclarationTypeAttr;
@ -3587,6 +3632,16 @@ begin
//CheckTokensForLine('18: end', 17,
// [tkKey+DeclType, TK_Semi]);
CheckTokensForLine('name: name; external name name;', 19,
[tkIdentifier+DeclVarName, TK_Colon, tkSpace, tkIdentifier+DeclType, TK_Semi, tkSpace,
tkModifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
CheckTokensForLine('name: name; external foo name name;', 20,
[tkIdentifier+DeclVarName, TK_Colon, tkSpace, tkIdentifier+DeclType, TK_Semi, tkSpace,
tkModifier, tkSpace, tkIdentifier, tkSpace, tkModifier, tkSpace, tkIdentifier, TK_Semi]);
for i := 0 to 1 do begin
case i of
0: begin