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

Commit 263b686

Browse files
committed
Fix highlighting?
1 parent a863ae6 commit 263b686

File tree

3 files changed

+165
-117
lines changed

3 files changed

+165
-117
lines changed

SourcepawnCondenser/SourcepawnCondenser/SourcemodDefinition/SMDefinition.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public string[]
4141
public List<SMTypedef> Typedefs = new List<SMTypedef>();
4242
public string[] TypeStrings = new string[0];
4343
public List<SMVariable> Variables = new List<SMVariable>();
44+
public SMFunction currentFunction;
4445

4546
public void Sort()
4647
{

UI/Components/EditorElement.xaml.cs

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,84 @@ private async void TextArea_MouseDown(object sender, MouseButtonEventArgs e)
212212
if (word.Trim().Length == 0) return;
213213

214214
e.Handled = true;
215-
var smDef = Program.Configs[Program.SelectedConfig].GetSMDef();
215+
// var smDef = currentSmDef ?? Program.Configs[Program.SelectedConfig].GetSMDef();
216+
217+
/**** First try to match variables in the current file ***/
218+
var sm = MatchDefinition(currentSmDef, word, e, true);
219+
if (sm != null)
220+
{
221+
editor.TextArea.Caret.Offset = sm.Index;
222+
editor.TextArea.Caret.BringCaretToView();
223+
await Task.Delay(100);
224+
editor.TextArea.Selection = Selection.Create(editor.TextArea, sm.Index, sm.Index + sm.Length);
225+
return;
226+
}
227+
228+
sm = MatchDefinition(Program.Configs[Program.SelectedConfig].GetSMDef(), word, e);
229+
if (sm != null)
230+
{
231+
var config = Program.Configs[Program.SelectedConfig].SMDirectories.First();
232+
var file = Path.GetFullPath(Path.Combine(config, "include", sm.File)) + ".inc";
233+
var result = Program.MainWindow.TryLoadSourceFile(file,
234+
true, false, true);
235+
if (!result)
236+
{
237+
Debug.Print("File {file} not found!");
238+
return;
239+
}
240+
241+
var newEditor = Program.MainWindow.GetCurrentEditorElement();
242+
Debug.Assert(newEditor != null);
243+
newEditor.editor.TextArea.Caret.Offset = sm.Index;
244+
newEditor.editor.TextArea.Caret.BringCaretToView();
245+
await Task.Delay(100);
246+
newEditor.editor.TextArea.Selection =
247+
Selection.Create(newEditor.editor.TextArea, sm.Index, sm.Index + sm.Length);
248+
}
249+
250+
251+
}
252+
253+
private SMBaseDefinition MatchDefinition(SMDefinition smDef, string word, MouseButtonEventArgs e, bool currentFile = false)
254+
{
255+
var mousePosition = editor.GetPositionFromPoint(e.GetPosition(this));
256+
257+
if (mousePosition == null)
258+
return null;
259+
260+
var line = mousePosition.Value.Line;
261+
var column = mousePosition.Value.Column;
262+
var offset = editor.TextArea.Document.GetOffset(line, column);
263+
216264
var sm = (SMBaseDefinition) smDef.Functions.FirstOrDefault(i => i.Name == word);
217265

266+
if (currentFile)
267+
{
268+
sm ??= smDef.Functions.FirstOrDefault(func => func.Index <= offset && offset <= func.EndPos)
269+
?.FuncVariables?.FirstOrDefault(i => i.Name.Equals(word));
270+
}
271+
272+
sm ??= smDef.Variables.FirstOrDefault(i =>
273+
i.Name.Equals(word));
274+
218275
sm ??= smDef.Constants.FirstOrDefault(i =>
219-
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
276+
i.Name.Equals(word));
220277

221-
sm ??= smDef.Defines.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
278+
sm ??= smDef.Defines.FirstOrDefault(i => i.Name.Equals(word));
222279

223-
sm ??= smDef.Enums.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
280+
sm ??= smDef.Enums.FirstOrDefault(i => i.Name.Equals(word));
224281

225-
foreach (var smEnum in smDef.Enums)
282+
if (sm == null)
226283
{
227-
var str = smEnum.Entries.FirstOrDefault(
228-
i => i.Equals(word, StringComparison.InvariantCultureIgnoreCase));
284+
foreach (var smEnum in smDef.Enums)
285+
{
286+
var str = smEnum.Entries.FirstOrDefault(
287+
i => i.Equals(word));
229288

230-
if (str == null) continue;
231-
sm = smEnum;
232-
break;
289+
if (str == null) continue;
290+
sm = smEnum;
291+
break;
292+
}
233293
}
234294

235295

@@ -244,34 +304,9 @@ private async void TextArea_MouseDown(object sender, MouseButtonEventArgs e)
244304

245305
sm ??= smDef.Typedefs.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
246306

247-
sm ??= smDef.Variables.FirstOrDefault(i =>
248-
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
249-
250-
Debug.Print($"Function {word} found with {sm}!");
251-
252-
if (sm == null)
253-
{
254-
Debug.Print("Definition not found!");
255-
return;
256-
}
257-
258-
var config = Program.Configs[Program.SelectedConfig].SMDirectories.First();
259-
var file = Path.GetFullPath(Path.Combine(config, "include", sm.File)) + ".inc";
260-
var result = Program.MainWindow.TryLoadSourceFile(file,
261-
true, false, true);
262-
if (!result)
263-
{
264-
Debug.Print("File {file} not found!");
265-
return;
266-
}
307+
// Debug.Print($"Function {word} found with {sm}!");
267308

268-
var newEditor = Program.MainWindow.GetCurrentEditorElement();
269-
Debug.Assert(newEditor != null);
270-
newEditor.editor.TextArea.Caret.Offset = sm.Index;
271-
newEditor.editor.TextArea.Caret.BringCaretToView();
272-
await Task.Delay(100);
273-
newEditor.editor.TextArea.Selection =
274-
Selection.Create(newEditor.editor.TextArea, sm.Index, sm.Index + sm.Length);
309+
return sm;
275310
}
276311

277312
private string GetWordAtMousePosition(MouseEventArgs e)
@@ -657,13 +692,19 @@ private void Caret_PositionChanged(object sender, EventArgs e)
657692
parseTimer.Elapsed += ParseIncludes;
658693
}
659694

695+
private SMDefinition currentSmDef;
696+
697+
public SMDefinition CurrentSmDef => currentSmDef;
698+
660699
private void ParseIncludes(object sender, EventArgs e)
661700
{
662701
Dispatcher.Invoke(() =>
663702
{
703+
if (Program.MainWindow == null) return;
704+
664705
var ee = Program.MainWindow.GetAllEditorElements();
665706
var ce = Program.MainWindow.GetCurrentEditorElement();
666-
707+
667708
var caret = -1;
668709

669710
if (ee == null) return;
@@ -688,6 +729,15 @@ private void ParseIncludes(object sender, EventArgs e)
688729
new Condenser(text, fInfo.Name)
689730
.Condense();
690731
currentFunctions = definitions[i].Functions;
732+
if (el == ce)
733+
{
734+
currentSmDef = definitions[i];
735+
var caret1 = caret;
736+
currentSmDef.currentFunction =
737+
currentFunctions.FirstOrDefault(
738+
func => func.Index <= caret1 && caret1 <= func.EndPos);
739+
740+
}
691741
}
692742
}
693743

UI/Components/IntelliSenseController.cs

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public partial class EditorElement
3939

4040
private int LastShowedLine = -1;
4141

42-
public ulong LastSMDefUpdateUID = 0;
43-
4442
// TODO Add EnumStructs
4543
private SMMethodmap[] methodMaps;
4644

@@ -89,6 +87,7 @@ public void InterruptLoadAutoCompletes(string[] FunctionStrings, SMFunction[] Fu
8987
});
9088
}
9189

90+
private Regex methodExp = new Regex(@"(?<=\.)[A-Za-z_]\w*", RegexOptions.RightToLeft);
9291
private void EvaluateIntelliSense()
9392
{
9493
if (editor.SelectionLength > 0)
@@ -172,107 +171,105 @@ private void EvaluateIntelliSense()
172171
else if (text[i] == '(')
173172
{
174173
scopeLevel--;
175-
if (scopeLevel < 0)
176-
{
177-
var FoundMatch = false;
178-
var searchIndex = i;
179-
for (var j = 0; j < ISMatches.Count; ++j)
180-
if (searchIndex >= ISMatches[j].Index &&
181-
searchIndex <= ISMatches[j].Index + ISMatches[j].Length)
174+
if (scopeLevel >= 0) continue;
175+
var FoundMatch = false;
176+
var searchIndex = i;
177+
for (var j = 0; j < ISMatches.Count; ++j)
178+
if (searchIndex >= ISMatches[j].Index &&
179+
searchIndex <= ISMatches[j].Index + ISMatches[j].Length)
180+
{
181+
FoundMatch = true;
182+
var testString = ISMatches[j].Groups["name"].Value;
183+
var classString = ISMatches[j].Groups["class"].Value;
184+
if (classString.Length > 0)
182185
{
183-
FoundMatch = true;
184-
var testString = ISMatches[j].Groups["name"].Value;
185-
var classString = ISMatches[j].Groups["class"].Value;
186-
if (classString.Length > 0)
186+
var methodString = ISMatches[j].Groups["method"].Value;
187+
var found = false;
188+
189+
// Match for static methods.
190+
var staticMethodMap = methodMaps.FirstOrDefault(e => e.Name == classString);
191+
var staticMethod =
192+
staticMethodMap?.Methods.FirstOrDefault(e => e.Name == methodString);
193+
if (staticMethod != null)
187194
{
188-
var methodString = ISMatches[j].Groups["method"].Value;
189-
var found = false;
190-
191-
// Match for static methods.
192-
var staticMethodMap = methodMaps.FirstOrDefault(e => e.Name == classString);
193-
var staticMethod =
194-
staticMethodMap?.Methods.FirstOrDefault(e => e.Name == methodString);
195-
if (staticMethod != null)
196-
{
197-
xPos = ISMatches[j].Groups["method"].Index +
198-
ISMatches[j].Groups["method"].Length;
199-
ForwardShowIS = true;
200-
ISFuncNameStr = staticMethod.FullName;
201-
ISFuncDescriptionStr = staticMethod.CommentString;
202-
ForceReSet = true;
203-
found = true;
204-
}
205-
206-
// Try to find declaration
207-
if (!found)
208-
{
209-
var pattern =
210-
$@"\b((?<class>[a-zA-Z_]([a-zA-Z0-9_]?)+))\s+({classString})\s*(;|=)";
211-
var findDecl = new Regex(pattern, RegexOptions.Compiled);
212-
var match = findDecl.Match(editor.Text);
213-
var classMatch = match.Groups["class"].Value;
214-
if (classMatch.Length > 0)
215-
{
216-
var methodMap = methodMaps.FirstOrDefault(e => e.Name == classMatch);
217-
var method =
218-
methodMap?.Methods.FirstOrDefault(e => e.Name == methodString);
219-
if (method != null)
220-
{
221-
xPos = ISMatches[j].Groups["method"].Index +
222-
ISMatches[j].Groups["method"].Length;
223-
ForwardShowIS = true;
224-
ISFuncNameStr = method.FullName;
225-
ISFuncDescriptionStr = method.CommentString;
226-
ForceReSet = true;
227-
found = true;
228-
}
229-
}
230-
}
195+
xPos = ISMatches[j].Groups["method"].Index +
196+
ISMatches[j].Groups["method"].Length;
197+
ForwardShowIS = true;
198+
ISFuncNameStr = staticMethod.FullName;
199+
ISFuncDescriptionStr = staticMethod.CommentString;
200+
ForceReSet = true;
201+
found = true;
202+
}
231203

232-
// Match the first found
233-
if (!found)
204+
// Try to find declaration
205+
if (!found)
206+
{
207+
var pattern =
208+
$@"\b((?<class>[a-zA-Z_]([a-zA-Z0-9_]?)+))\s+({classString})\s*(;|=)";
209+
var findDecl = new Regex(pattern, RegexOptions.Compiled);
210+
var match = findDecl.Match(editor.Text);
211+
var classMatch = match.Groups["class"].Value;
212+
if (classMatch.Length > 0)
234213
{
235-
// Match any methodmap, since the ide is not aware of the types
236-
foreach (var methodMap in methodMaps)
214+
var methodMap = methodMaps.FirstOrDefault(e => e.Name == classMatch);
215+
var method =
216+
methodMap?.Methods.FirstOrDefault(e => e.Name == methodString);
217+
if (method != null)
237218
{
238-
var method =
239-
methodMap.Methods.FirstOrDefault(e => e.Name == methodString);
240-
241-
if (method == null)
242-
continue;
243-
244219
xPos = ISMatches[j].Groups["method"].Index +
245220
ISMatches[j].Groups["method"].Length;
246221
ForwardShowIS = true;
247222
ISFuncNameStr = method.FullName;
248223
ISFuncDescriptionStr = method.CommentString;
249224
ForceReSet = true;
225+
found = true;
250226
}
251227
}
252228
}
253-
else
229+
230+
// Match the first found
231+
if (!found)
254232
{
255-
var func = funcs.FirstOrDefault(e => e.Name == testString);
256-
if (func != null)
233+
// Match any methodmap, since the ide is not aware of the types
234+
foreach (var methodMap in methodMaps)
257235
{
258-
xPos = ISMatches[j].Groups["name"].Index +
259-
ISMatches[j].Groups["name"].Length;
236+
var method =
237+
methodMap.Methods.FirstOrDefault(e => e.Name == methodString);
238+
239+
if (method == null)
240+
continue;
241+
242+
xPos = ISMatches[j].Groups["method"].Index +
243+
ISMatches[j].Groups["method"].Length;
260244
ForwardShowIS = true;
261-
ISFuncNameStr = func.FullName;
262-
ISFuncDescriptionStr = func.CommentString;
245+
ISFuncNameStr = method.FullName;
246+
ISFuncDescriptionStr = method.CommentString;
263247
ForceReSet = true;
264248
}
265249
}
266-
267-
break;
250+
}
251+
else
252+
{
253+
var func = funcs.FirstOrDefault(e => e.Name == testString);
254+
if (func != null)
255+
{
256+
xPos = ISMatches[j].Groups["name"].Index +
257+
ISMatches[j].Groups["name"].Length;
258+
ForwardShowIS = true;
259+
ISFuncNameStr = func.FullName;
260+
ISFuncDescriptionStr = func.CommentString;
261+
ForceReSet = true;
262+
}
268263
}
269264

270-
if (FoundMatch)
271-
{
272-
// ReSharper disable once RedundantAssignment
273-
scopeLevel--; //i have no idea why this works...
274265
break;
275266
}
267+
268+
if (FoundMatch)
269+
{
270+
// ReSharper disable once RedundantAssignment
271+
scopeLevel--; //i have no idea why this works...
272+
break;
276273
}
277274
}
278275

0 commit comments

Comments
 (0)