Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit eb9a29d

Browse files
committed
Fix defines highlighting
1 parent b1524bb commit eb9a29d

File tree

4 files changed

+84
-37
lines changed

4 files changed

+84
-37
lines changed

SourcepawnCondenser/SourcepawnCondenser/Tokenizer/Tokenizer.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
35

46
namespace SourcepawnCondenser.Tokenizer
57
{
@@ -174,7 +176,7 @@ public static List<Token> TokenizeString(string Source, bool IgnoreMultipleEOL)
174176
continue;
175177
}
176178
}
177-
179+
178180
// TODO: Create a real char token
179181
if (c == '\'' && i + 1 < sArrayLength)
180182
{
@@ -393,16 +395,37 @@ public static List<Token> TokenizeString(string Source, bool IgnoreMultipleEOL)
393395

394396
var directiveString = Source.Substring(startIndex, endIndex - startIndex);
395397
token.Add(new Token(directiveString, TokenKind.PrePocessorDirective, startIndex));
398+
if (directiveString == "#define" && sArray[endIndex] == ' ')
399+
{
400+
var name = new StringBuilder();
401+
for (var j = endIndex+1; j < sArrayLength; ++j)
402+
{
403+
if (sArray[j] == '\n' || sArray[j] == '\r')
404+
{
405+
i = j - 1;
406+
break;
407+
}
408+
409+
if (sArray[j] == ' ')
410+
{
411+
token.Add(
412+
new Token(name.ToString(), TokenKind.Identifier, endIndex+1));
413+
break;
414+
}
415+
name.Append(sArray[j]);
416+
}
417+
}
418+
396419
for (var j = i + 1; j < sArrayLength; ++j)
397420
if (sArray[j] == '\n' || sArray[j] == '\r')
398421
{
399-
i = j-1;
422+
i = j - 1;
400423
break;
401424
}
425+
402426
continue;
403427
}
404428
}
405-
406429
}
407430

408431
#endregion

UI/Components/EditorElement.xaml.cs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Text;
7+
using System.Text.RegularExpressions;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using System.Timers;
@@ -16,6 +17,7 @@
1617
using ICSharpCode.AvalonEdit.Document;
1718
using ICSharpCode.AvalonEdit.Editing;
1819
using ICSharpCode.AvalonEdit.Folding;
20+
using ICSharpCode.AvalonEdit.Highlighting;
1921
using ICSharpCode.AvalonEdit.Rendering;
2022
using ICSharpCode.AvalonEdit.Utils;
2123
using MahApps.Metro.Controls.Dialogs;
@@ -170,6 +172,36 @@ public EditorElement(string filePath)
170172
CompileBox.IsChecked = filePath.EndsWith(".sp");
171173
}
172174

175+
public string FullFilePath
176+
{
177+
get => _FullFilePath;
178+
set
179+
{
180+
var fInfo = new FileInfo(value);
181+
_FullFilePath = fInfo.FullName;
182+
Parent.Title = fInfo.Name;
183+
if (fileWatcher != null) fileWatcher.Path = fInfo.DirectoryName;
184+
}
185+
}
186+
187+
public bool NeedsSave
188+
{
189+
get => _NeedsSave;
190+
set
191+
{
192+
if (!(value ^ _NeedsSave)) //when not changed
193+
return;
194+
_NeedsSave = value;
195+
if (Parent != null)
196+
{
197+
if (_NeedsSave)
198+
Parent.Title = "*" + Parent.Title;
199+
else
200+
Parent.Title = Parent.Title.Trim('*');
201+
}
202+
}
203+
}
204+
173205
private async void TextArea_MouseDown(object sender, MouseButtonEventArgs e)
174206
{
175207
if (!Keyboard.IsKeyDown(Key.LeftCtrl)) return;
@@ -254,9 +286,9 @@ private string GetWordAtMousePosition(MouseEventArgs e)
254286
if (offset >= editor.TextArea.Document.TextLength)
255287
offset--;
256288

257-
int offsetStart = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
289+
var offsetStart = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
258290
LogicalDirection.Backward, CaretPositioningMode.WordBorder);
259-
int offsetEnd = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
291+
var offsetEnd = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
260292
LogicalDirection.Forward, CaretPositioningMode.WordBorder);
261293

262294
if (offsetEnd == -1 || offsetStart == -1)
@@ -270,36 +302,6 @@ private string GetWordAtMousePosition(MouseEventArgs e)
270302
return editor.TextArea.Document.GetText(offsetStart, offsetEnd - offsetStart);
271303
}
272304

273-
public string FullFilePath
274-
{
275-
get => _FullFilePath;
276-
set
277-
{
278-
var fInfo = new FileInfo(value);
279-
_FullFilePath = fInfo.FullName;
280-
Parent.Title = fInfo.Name;
281-
if (fileWatcher != null) fileWatcher.Path = fInfo.DirectoryName;
282-
}
283-
}
284-
285-
public bool NeedsSave
286-
{
287-
get => _NeedsSave;
288-
set
289-
{
290-
if (!(value ^ _NeedsSave)) //when not changed
291-
return;
292-
_NeedsSave = value;
293-
if (Parent != null)
294-
{
295-
if (_NeedsSave)
296-
Parent.Title = "*" + Parent.Title;
297-
else
298-
Parent.Title = Parent.Title.Trim('*');
299-
}
300-
}
301-
}
302-
303305
private void AutoSaveTimer_Elapsed(object sender, ElapsedEventArgs e)
304306
{
305307
if (NeedsSave)
@@ -657,7 +659,6 @@ private void Caret_PositionChanged(object sender, EventArgs e)
657659
, fInfo.Name).Condense();
658660

659661
if (fInfo.Extension.Trim('.').ToLowerInvariant() == "sp")
660-
{
661662
if (el.IsLoaded)
662663
{
663664
caret = el.editor.CaretOffset;
@@ -666,7 +667,6 @@ private void Caret_PositionChanged(object sender, EventArgs e)
666667
.Condense();
667668
currentFunctions = definitions[i].Functions;
668669
}
669-
}
670670
}
671671

672672
var smDef = Program.Configs[Program.SelectedConfig].GetSMDef()
@@ -675,6 +675,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
675675
var acNodes = smDef.ProduceACNodes();
676676
var isNodes = smDef.ProduceISNodes();
677677

678+
ce.editor.SyntaxHighlighting = new AeonEditorHighlighting(smDef);
678679
foreach (var el in ee)
679680
{
680681
if (el == ce)
@@ -683,6 +684,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
683684
if (ce.ISAC_Open) continue;
684685
}
685686

687+
686688
el.InterruptLoadAutoCompletes(smDef.FunctionStrings, smFunctions, acNodes,
687689
isNodes, smDef.Methodmaps.ToArray(), smDef.Variables.ToArray());
688690
}

UI/Components/EditorElementHighlighter.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
using System.Windows.Media;
1010
using ICSharpCode.AvalonEdit.Highlighting;
1111
using ICSharpCode.AvalonEdit.Rendering;
12+
using SourcepawnCondenser.SourcemodDefinition;
1213

1314
namespace Spcode.UI.Components
1415
{
1516
public class AeonEditorHighlighting : IHighlightingDefinition
1617
{
18+
19+
private SMDefinition smDef;
20+
public AeonEditorHighlighting() {}
21+
22+
public AeonEditorHighlighting(SMDefinition smDef)
23+
{
24+
this.smDef = smDef;
25+
}
26+
1727
public string Name => "SM";
1828

1929
public HighlightingRuleSet MainRuleSet
@@ -169,6 +179,17 @@ public HighlightingRuleSet MainRuleSet
169179
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture),
170180
Color = new HighlightingColor {Foreground = stringBrush}
171181
});
182+
183+
if (smDef != null)
184+
{
185+
if (smDef.Defines.Count > 0)
186+
rs.Rules.Add(new HighlightingRule
187+
{
188+
Regex = new Regex(string.Join("|", smDef.Defines.Select(e => "\\b" + e.Name + "\\b").ToArray())),
189+
Color = new HighlightingColor
190+
{Foreground = new SimpleHighlightingBrush(Program.OptionsObject.SH_Constants)}
191+
});
192+
}
172193
var def = Program.Configs[Program.SelectedConfig].GetSMDef();
173194
if (def.TypeStrings.Length > 0)
174195
rs.Rules.Add(new HighlightingRule //types

UI/MainWindowConfigHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Spcode.UI.Windows;
33
using System.Windows;
44
using System.Windows.Controls;
5+
using ICSharpCode.AvalonEdit.Highlighting;
56

67
namespace Spcode.UI
78
{

0 commit comments

Comments
 (0)