Skip to content

Commit 9898b81

Browse files
committed
highlight new keyword and add support for changing any token
1 parent 26a6228 commit 9898b81

8 files changed

+648
-35
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
unit simba.editor_customtokenattri;
2+
3+
{$i simba.inc}
4+
5+
interface
6+
7+
uses
8+
Classes,
9+
SysUtils,
10+
SynEditHighlighter,
11+
SynHighlighterPas,
12+
simba.base,
13+
simba.settings;
14+
15+
type
16+
TSimbaEditorHighlighter = class(TSynFreePascalSyn)
17+
private
18+
FOverrides: TStringList;
19+
20+
procedure addTokenOverride(Text: String; Kind: TtkTokenKind);
21+
22+
procedure DoSettingChanged(Setting: TSimbaSetting);
23+
public
24+
constructor Create(AOwner: TComponent); override;
25+
destructor Destroy; override;
26+
27+
function AttributeNames: TStringArray;
28+
function GetTokenAttribute: TSynHighlighterAttributes; override;
29+
end;
30+
31+
implementation
32+
33+
uses
34+
simba.vartype_string;
35+
36+
procedure TSimbaEditorHighlighter.addTokenOverride(Text: String; Kind: TtkTokenKind);
37+
begin
38+
FOverrides.AddObject(Text, TObject(PtrUInt(Kind)));
39+
end;
40+
41+
procedure TSimbaEditorHighlighter.DoSettingChanged(Setting: TSimbaSetting);
42+
var
43+
Arg, Args, Key, Value: String;
44+
I: Integer;
45+
begin
46+
Args := Setting.Value;
47+
for Arg in Args.Split(',') do
48+
begin
49+
Key := Arg.Before('=');
50+
Value := Arg.After('=');
51+
for I := 0 to AttrCount - 1 do
52+
if (Attribute[I].Name = Value) then
53+
begin
54+
if (Attribute[I] = CommentAttri) then
55+
addTokenOverride(Key, tkComment)
56+
else if (Attribute[I] = IdentifierAttri) then
57+
addTokenOverride(Key, tkIdentifier)
58+
else if (Attribute[I] = KeyAttri) then
59+
addTokenOverride(Key, tkKey)
60+
else if (Attribute[I] = ModifierAttri) then
61+
addTokenOverride(Key, tkModifier)
62+
else if (Attribute[I] = NumberAttri) then
63+
addTokenOverride(Key, tkNumber)
64+
else if (Attribute[I] = StringAttri) then
65+
addTokenOverride(Key, tkString)
66+
else if (Attribute[I] = SymbolAttri) then
67+
addTokenOverride(Key, tkSymbol)
68+
else if (Attribute[I] = DirectiveAttri) then
69+
addTokenOverride(Key, tkDirective);
70+
end;
71+
end;
72+
end;
73+
74+
constructor TSimbaEditorHighlighter.Create(AOwner: TComponent);
75+
begin
76+
inherited Create(AOwner);
77+
78+
FOverrides := TStringList.Create();
79+
FOverrides.UseLocale := False;
80+
FOverrides.CaseSensitive := False;
81+
FOverrides.Sorted := True;
82+
83+
addTokenOverride('new', tkKey);
84+
85+
SimbaSettings.RegisterChangeHandler(Self, SimbaSettings.Editor.CustomTokenAttris, @DoSettingCHanged, True);
86+
end;
87+
88+
destructor TSimbaEditorHighlighter.Destroy;
89+
begin
90+
FreeAndNil(FOverrides);
91+
92+
inherited Destroy();
93+
end;
94+
95+
function TSimbaEditorHighlighter.AttributeNames: TStringArray;
96+
var
97+
I, C: Integer;
98+
begin
99+
C := 0;
100+
SetLength(Result, AttrCount);
101+
for I := 0 to AttrCount - 1 do
102+
begin
103+
// dont add things we have no use for
104+
if (Attribute[I].StoredName = 'PasDoc-Keyword') or
105+
(Attribute[I].StoredName = 'PasDoc-Symbol') or
106+
(Attribute[I].StoredName = 'PasDoc-Unknown') or
107+
(Attribute[I].StoredName = 'Assembler') then
108+
Continue;
109+
110+
Result[C] := Attribute[I].Name;
111+
Inc(C);
112+
end;
113+
SetLength(Result, C);
114+
end;
115+
116+
function TSimbaEditorHighlighter.GetTokenAttribute: TSynHighlighterAttributes;
117+
var
118+
idx: Integer;
119+
begin
120+
idx := FOverrides.IndexOf(GetToken());
121+
if (idx > -1) then
122+
begin
123+
case TtkTokenKind(PtrUInt(FOverrides.Objects[idx])) of
124+
tkComment: Result := CommentAttri;
125+
tkIdentifier: Result := IdentifierAttri;
126+
tkKey: Result := KeyAttri;
127+
tkModifier: Result := ModifierAttri;
128+
tkNumber: Result := NumberAttri;
129+
tkSpace: Result := SpaceAttri;
130+
tkString: Result := StringAttri;
131+
tkSymbol: Result := SymbolAttri;
132+
tkDirective: Result := DirectiveAttri;
133+
end;
134+
end else
135+
Result := inherited;
136+
end;
137+
138+
end.
139+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
Author: Raymond van Venetië and Merlijn Wajer
3+
Project: Simba (https://github.com/MerlijnWajer/Simba)
4+
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0)
5+
}
6+
unit simba.editor_highlighter;
7+
8+
{$i simba.inc}
9+
10+
interface
11+
12+
uses
13+
Classes,
14+
SysUtils,
15+
SynEditHighlighter,
16+
SynHighlighterPas,
17+
simba.base,
18+
simba.settings;
19+
20+
type
21+
TSimbaEditorHighlighter = class(TSynFreePascalSyn)
22+
private
23+
FOverrides: TStringList;
24+
25+
procedure Reset;
26+
procedure addTokenOverride(Text: String; Kind: TtkTokenKind);
27+
28+
procedure DoSettingChanged(Setting: TSimbaSetting);
29+
public
30+
constructor Create(AOwner: TComponent); override;
31+
destructor Destroy; override;
32+
33+
function AttributeNames: TStringArray;
34+
function GetTokenAttribute: TSynHighlighterAttributes; override;
35+
end;
36+
37+
implementation
38+
39+
uses
40+
simba.vartype_string;
41+
42+
procedure TSimbaEditorHighlighter.Reset;
43+
begin
44+
FOverrides.Clear();
45+
addTokenOverride('new', tkKey);
46+
end;
47+
48+
procedure TSimbaEditorHighlighter.addTokenOverride(Text: String; Kind: TtkTokenKind);
49+
begin
50+
FOverrides.AddObject(Text, TObject(PtrUInt(Kind)));
51+
end;
52+
53+
procedure TSimbaEditorHighlighter.DoSettingChanged(Setting: TSimbaSetting);
54+
var
55+
Arg, Args, Key, Value: String;
56+
I: Integer;
57+
begin
58+
Reset();
59+
60+
Args := Setting.Value;
61+
for Arg in Args.Split(',') do
62+
begin
63+
Key := Arg.Before('=');
64+
Value := Arg.After('=');
65+
for I := 0 to AttrCount - 1 do
66+
if (Attribute[I].Name = Value) then
67+
begin
68+
if (Attribute[I] = CommentAttri) then
69+
addTokenOverride(Key, tkComment)
70+
else if (Attribute[I] = IdentifierAttri) then
71+
addTokenOverride(Key, tkIdentifier)
72+
else if (Attribute[I] = KeyAttri) then
73+
addTokenOverride(Key, tkKey)
74+
else if (Attribute[I] = ModifierAttri) then
75+
addTokenOverride(Key, tkModifier)
76+
else if (Attribute[I] = NumberAttri) then
77+
addTokenOverride(Key, tkNumber)
78+
else if (Attribute[I] = StringAttri) then
79+
addTokenOverride(Key, tkString)
80+
else if (Attribute[I] = SymbolAttri) then
81+
addTokenOverride(Key, tkSymbol)
82+
else if (Attribute[I] = DirectiveAttri) then
83+
addTokenOverride(Key, tkDirective);
84+
end;
85+
end;
86+
end;
87+
88+
constructor TSimbaEditorHighlighter.Create(AOwner: TComponent);
89+
begin
90+
inherited Create(AOwner);
91+
92+
FOverrides := TStringList.Create();
93+
FOverrides.UseLocale := False;
94+
FOverrides.CaseSensitive := False;
95+
FOverrides.Sorted := True;
96+
97+
Reset();
98+
99+
SimbaSettings.RegisterChangeHandler(Self, SimbaSettings.Editor.CustomTokenAttris, @DoSettingChanged, True);
100+
end;
101+
102+
destructor TSimbaEditorHighlighter.Destroy;
103+
begin
104+
FreeAndNil(FOverrides);
105+
106+
inherited Destroy();
107+
end;
108+
109+
function TSimbaEditorHighlighter.AttributeNames: TStringArray;
110+
var
111+
I, C: Integer;
112+
begin
113+
C := 0;
114+
SetLength(Result, AttrCount);
115+
for I := 0 to AttrCount - 1 do
116+
begin
117+
// dont add things we have no use for
118+
if (Attribute[I].StoredName = 'PasDoc-Keyword') or
119+
(Attribute[I].StoredName = 'PasDoc-Symbol') or
120+
(Attribute[I].StoredName = 'PasDoc-Unknown') or
121+
(Attribute[I].StoredName = 'Assembler') then
122+
Continue;
123+
124+
Result[C] := Attribute[I].Name;
125+
Inc(C);
126+
end;
127+
SetLength(Result, C);
128+
end;
129+
130+
function TSimbaEditorHighlighter.GetTokenAttribute: TSynHighlighterAttributes;
131+
var
132+
Index: Integer;
133+
begin
134+
Index := FOverrides.IndexOf(GetToken());
135+
if (Index > -1) then
136+
begin
137+
case TtkTokenKind(PtrUInt(FOverrides.Objects[Index])) of
138+
tkComment: Result := CommentAttri;
139+
tkIdentifier: Result := IdentifierAttri;
140+
tkKey: Result := KeyAttri;
141+
tkModifier: Result := ModifierAttri;
142+
tkNumber: Result := NumberAttri;
143+
tkSpace: Result := SpaceAttri;
144+
tkString: Result := StringAttri;
145+
tkSymbol: Result := SymbolAttri;
146+
tkDirective: Result := DirectiveAttri;
147+
end;
148+
end else
149+
Result := inherited;
150+
end;
151+
152+
end.
153+

Source/ide/simba.form_settings.lfm

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
object SimbaSettingsForm: TSimbaSettingsForm
22
Left = -1107
3-
Height = 625
3+
Height = 1000
44
Top = 465
5-
Width = 750
5+
Width = 1200
66
Caption = 'Settings'
7-
ClientHeight = 0
8-
ClientWidth = 0
9-
DesignTimePPI = 120
10-
OnShow = FormShow
7+
ClientHeight = 1000
8+
ClientWidth = 1200
9+
DesignTimePPI = 192
1110
Position = poMainFormCenter
1211
ShowInTaskBar = stAlways
13-
LCLVersion = '3.0.0.3'
12+
OnShow = FormShow
1413
object TreeView: TTreeView
1514
Left = 0
16-
Height = 566
15+
Height = 920
1716
Top = 0
18-
Width = 200
17+
Width = 320
1918
Align = alLeft
2019
AutoExpand = True
21-
BorderSpacing.Right = 1
20+
BorderSpacing.Right = 2
2221
ReadOnly = True
2322
ScrollBars = ssAutoBoth
2423
ShowRoot = False
2524
TabOrder = 0
26-
OnSelectionChanged = TreeViewSelectionChanged
2725
Options = [tvoAutoExpand, tvoAutoItemHeight, tvoHideSelection, tvoKeepCollapsedNodes, tvoReadOnly, tvoShowButtons, tvoShowLines, tvoToolTips, tvoThemedDraw]
26+
OnSelectionChanged = TreeViewSelectionChanged
2827
end
2928
object Notebook: TNotebook
30-
Left = 212
31-
Height = 556
32-
Top = 10
33-
Width = 533
29+
Left = 340
30+
Height = 904
31+
Top = 16
32+
Width = 852
3433
Align = alClient
35-
BorderSpacing.Left = 5
36-
BorderSpacing.Top = 10
37-
BorderSpacing.Right = 5
34+
BorderSpacing.Left = 8
35+
BorderSpacing.Top = 16
36+
BorderSpacing.Right = 8
3837
TabOrder = 1
3938
end
4039
object ButtonPanel: TButtonPanel
41-
Left = 8
42-
Height = 43
43-
Top = 574
44-
Width = 734
45-
BorderSpacing.Around = 8
40+
Left = 13
41+
Height = 54
42+
Top = 933
43+
Width = 1174
44+
BorderSpacing.Around = 13
4645
OKButton.Name = 'OKButton'
4746
OKButton.DefaultCaption = True
4847
OKButton.OnClick = OKButtonClick
@@ -56,9 +55,9 @@ object SimbaSettingsForm: TSimbaSettingsForm
5655
ShowButtons = [pbOK, pbCancel]
5756
end
5857
object Splitter: TSplitter
59-
Left = 201
60-
Height = 566
58+
Left = 322
59+
Height = 920
6160
Top = 0
62-
Width = 6
61+
Width = 10
6362
end
6463
end

0 commit comments

Comments
 (0)