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

Commit a4b7e77

Browse files
committed
CTRL + LEFTCLICK to find Object Definition
1 parent c9d5132 commit a4b7e77

File tree

4 files changed

+123
-6
lines changed

4 files changed

+123
-6
lines changed

Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public static class Program
3939
[STAThread]
4040
public static void Main(string[] args)
4141
{
42+
#if DEBUG
43+
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level =
44+
System.Diagnostics.SourceLevels.Critical;
45+
#endif
46+
4247
// Init Discord RPC
4348
discordClient.Initialize();
4449

Spcode.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<UseApplicationTrust>false</UseApplicationTrust>
2929
<BootstrapperEnabled>true</BootstrapperEnabled>
3030
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
31+
<LangVersion>latest</LangVersion>
3132
</PropertyGroup>
3233
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3334
<PlatformTarget>AnyCPU</PlatformTarget>

UI/Components/EditorElement.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</UserControl.Resources>
5858
<Grid Background="{DynamicResource WhiteColorBrush}">
5959
<editor:TextEditor x:Name="editor" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,20" ShowLineNumbers="True" FontFamily="Consolas"
60-
TextChanged="editor_TextChanged" Foreground="{DynamicResource BlackColorBrush}">
60+
TextChanged="editor_TextChanged" Foreground="{DynamicResource BlackColorBrush}" >
6161
<editor:TextEditor.ContextMenu>
6262
<ContextMenu Opened="ContextMenu_Opening">
6363
<MenuItem Name="MenuC_Undo" Header="Undo" Tag="0" Click="HandleContextMenuCommand" />
@@ -72,7 +72,7 @@
7272
</editor:TextEditor.ContextMenu>
7373
</editor:TextEditor>
7474
<CheckBox x:Name="CompileBox" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="2,0,0,0" Content="Compile" Foreground="{DynamicResource BlackColorBrush}" />
75-
<TextBlock Name="StatusLine_Work" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="100,0,0,0" Text="HERE" Foreground="{DynamicResource BlackColorBrush}" IsHitTestVisible="False" />
75+
<!-- <TextBlock Name="StatusLine_Work" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="100,0,0,0" Text="HERE" Foreground="{DynamicResource BlackColorBrush}" IsHitTestVisible="False" /> -->
7676
<TextBlock Name="StatusLine_FontSize" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="2" Text="14 pt" Foreground="{DynamicResource BlackColorBrush}" IsHitTestVisible="False" />
7777
<TextBlock Name="StatusLine_Coloumn" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="2,2,100,2" Text="Col 0" Foreground="{DynamicResource BlackColorBrush}" IsHitTestVisible="False" />
7878
<TextBlock Name="StatusLine_Line" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="2,2,200,2" Text="Ln 0" Foreground="{DynamicResource BlackColorBrush}" IsHitTestVisible="False" />

UI/Components/EditorElement.xaml.cs

+115-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
4+
using System.Linq;
35
using System.Text;
46
using System.Threading;
7+
using System.Threading.Tasks;
58
using System.Timers;
69
using System.Windows;
710
using System.Windows.Controls;
11+
using System.Windows.Documents;
812
using System.Windows.Input;
913
using System.Windows.Media;
1014
using System.Windows.Media.Animation;
1115
using ICSharpCode.AvalonEdit.Document;
16+
using ICSharpCode.AvalonEdit.Editing;
1217
using ICSharpCode.AvalonEdit.Folding;
1318
using ICSharpCode.AvalonEdit.Rendering;
1419
using ICSharpCode.AvalonEdit.Utils;
1520
using MahApps.Metro.Controls.Dialogs;
21+
using SourcepawnCondenser.SourcemodDefinition;
1622
using Spcode.Utils.SPSyntaxTidy;
1723
using Xceed.Wpf.AvalonDock.Layout;
1824
using Timer = System.Timers.Timer;
@@ -67,16 +73,20 @@ public EditorElement(string filePath)
6773
FadeJumpGridIn = (Storyboard) Resources["FadeJumpGridIn"];
6874
FadeJumpGridOut = (Storyboard) Resources["FadeJumpGridOut"];
6975

76+
editor.CaptureMouse();
77+
7078
KeyDown += EditorElement_KeyDown;
7179

7280
editor.TextArea.Caret.PositionChanged += Caret_PositionChanged;
7381
editor.TextArea.SelectionChanged += TextArea_SelectionChanged;
7482
editor.TextArea.PreviewKeyDown += TextArea_PreviewKeyDown;
83+
84+
editor.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextArea_MouseDown), true);
85+
7586
editor.PreviewMouseWheel += PrevMouseWheel;
7687
editor.MouseDown += editor_MouseDown;
7788
editor.TextArea.TextEntered += TextArea_TextEntered;
7889
editor.TextArea.TextEntering += TextArea_TextEntering;
79-
8090
var fInfo = new FileInfo(filePath);
8191
if (fInfo.Exists)
8292
{
@@ -158,6 +168,106 @@ public EditorElement(string filePath)
158168
CompileBox.IsChecked = filePath.EndsWith(".sp");
159169
}
160170

171+
private async void TextArea_MouseDown(object sender, MouseButtonEventArgs e)
172+
{
173+
if (!Keyboard.IsKeyDown(Key.LeftCtrl)) return;
174+
var word = GetWordAtMousePosition(e);
175+
Debug.Print($"The word: {word}");
176+
if (word.Trim().Length == 0) return;
177+
178+
e.Handled = true;
179+
var smDef = Program.Configs[Program.SelectedConfig].GetSMDef();
180+
var sm = (SMBaseDefinition) smDef.Functions.FirstOrDefault(i => i.Name == word);
181+
182+
sm ??= smDef.Constants.FirstOrDefault(i =>
183+
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
184+
185+
sm ??= smDef.Defines.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
186+
187+
sm ??= smDef.Enums.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
188+
189+
foreach (var smEnum in smDef.Enums)
190+
{
191+
var str = smEnum.Entries.FirstOrDefault(
192+
i => i.Equals(word, StringComparison.InvariantCultureIgnoreCase));
193+
194+
if (str == null) continue;
195+
sm = smEnum;
196+
break;
197+
}
198+
199+
200+
//TODO: Match EnumStruct and MethodMaps Fields and Methods
201+
sm ??= smDef.EnumStructs.FirstOrDefault(i =>
202+
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
203+
204+
sm ??= smDef.Methodmaps.FirstOrDefault(
205+
i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
206+
207+
sm ??= smDef.Structs.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
208+
209+
sm ??= smDef.Typedefs.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
210+
211+
sm ??= smDef.Variables.FirstOrDefault(i =>
212+
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
213+
214+
Debug.Print($"Function {word} found with {sm}!");
215+
216+
if (sm == null)
217+
{
218+
Debug.Print("Definition not found!");
219+
return;
220+
}
221+
222+
var config = Program.Configs[Program.SelectedConfig].SMDirectories.First();
223+
var file = Path.GetFullPath(Path.Combine(config, "include", sm.File)) + ".inc";
224+
var result = Program.MainWindow.TryLoadSourceFile(file,
225+
true, false, true);
226+
if (!result)
227+
{
228+
Debug.Print("File {file} not found!");
229+
return;
230+
}
231+
232+
var newEditor = Program.MainWindow.GetCurrentEditorElement();
233+
Debug.Assert(newEditor != null);
234+
newEditor.editor.TextArea.Caret.Offset = sm.Index;
235+
newEditor.editor.TextArea.Caret.BringCaretToView();
236+
await Task.Delay(100);
237+
newEditor.editor.TextArea.Selection =
238+
Selection.Create(newEditor.editor.TextArea, sm.Index, sm.Index + sm.Length);
239+
}
240+
241+
private string GetWordAtMousePosition(MouseEventArgs e)
242+
{
243+
var mousePosition = editor.GetPositionFromPoint(e.GetPosition(this));
244+
245+
if (mousePosition == null)
246+
return string.Empty;
247+
248+
var line = mousePosition.Value.Line;
249+
var column = mousePosition.Value.Column;
250+
var offset = editor.TextArea.Document.GetOffset(line, column);
251+
252+
if (offset >= editor.TextArea.Document.TextLength)
253+
offset--;
254+
255+
int offsetStart = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
256+
LogicalDirection.Backward, CaretPositioningMode.WordBorder);
257+
int offsetEnd = TextUtilities.GetNextCaretPosition(editor.TextArea.Document, offset,
258+
LogicalDirection.Forward, CaretPositioningMode.WordBorder);
259+
260+
if (offsetEnd == -1 || offsetStart == -1)
261+
return string.Empty;
262+
263+
var currentChar = editor.TextArea.Document.GetText(offset, 1);
264+
265+
if (string.IsNullOrWhiteSpace(currentChar))
266+
return string.Empty;
267+
268+
return editor.TextArea.Document.GetText(offsetStart, offsetEnd - offsetStart);
269+
}
270+
161271
public string FullFilePath
162272
{
163273
get => _FullFilePath;
@@ -502,7 +612,7 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
502612

503613
Program.MainWindow.EditorsReferences.Remove(this);
504614
// var childs = Program.MainWindow.DockingPaneGroup.Children;
505-
// foreach (var c in childs) (c as LayoutDocumentPane)?.Children.Remove(Parent);
615+
// foreach (var c in childs) (c as LayoutDocumentPane)?.Children.Remove(Parent);
506616

507617
Parent = null; //to prevent a ring depency which disables the GC from work
508618
Program.MainWindow.UpdateWindowTitle();
@@ -522,7 +632,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
522632
var result = bracketSearcher.SearchBracket(editor.Document, editor.CaretOffset);
523633
bracketHighlightRenderer.SetHighlight(result);
524634
//TODO: Remove this in release
525-
StatusLine_Work.Text = editor.CaretOffset.ToString();
635+
// StatusLine_Work.Text = editor.CaretOffset.ToString();
526636
}
527637

528638
private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
@@ -531,7 +641,6 @@ private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
531641
if (e.Text == ";")
532642
if (editor.CaretOffset >= 0)
533643
{
534-
535644
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
536645
var text = editor.Document.GetText(line);
537646

@@ -660,6 +769,8 @@ private void PrevMouseWheel(object sender, MouseWheelEventArgs e)
660769

661770
private void editor_MouseDown(object sender, MouseButtonEventArgs e)
662771
{
772+
e.Handled = true;
773+
Console.WriteLine("PRESSED MOUSE DOWN");
663774
HideISAC();
664775
}
665776

0 commit comments

Comments
 (0)