1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . IO ;
4
+ using System . Linq ;
3
5
using System . Text ;
4
6
using System . Threading ;
7
+ using System . Threading . Tasks ;
5
8
using System . Timers ;
6
9
using System . Windows ;
7
10
using System . Windows . Controls ;
11
+ using System . Windows . Documents ;
8
12
using System . Windows . Input ;
9
13
using System . Windows . Media ;
10
14
using System . Windows . Media . Animation ;
11
15
using ICSharpCode . AvalonEdit . Document ;
16
+ using ICSharpCode . AvalonEdit . Editing ;
12
17
using ICSharpCode . AvalonEdit . Folding ;
13
18
using ICSharpCode . AvalonEdit . Rendering ;
14
19
using ICSharpCode . AvalonEdit . Utils ;
15
20
using MahApps . Metro . Controls . Dialogs ;
21
+ using SourcepawnCondenser . SourcemodDefinition ;
16
22
using Spcode . Utils . SPSyntaxTidy ;
17
23
using Xceed . Wpf . AvalonDock . Layout ;
18
24
using Timer = System . Timers . Timer ;
@@ -67,16 +73,20 @@ public EditorElement(string filePath)
67
73
FadeJumpGridIn = ( Storyboard ) Resources [ "FadeJumpGridIn" ] ;
68
74
FadeJumpGridOut = ( Storyboard ) Resources [ "FadeJumpGridOut" ] ;
69
75
76
+ editor . CaptureMouse ( ) ;
77
+
70
78
KeyDown += EditorElement_KeyDown ;
71
79
72
80
editor . TextArea . Caret . PositionChanged += Caret_PositionChanged ;
73
81
editor . TextArea . SelectionChanged += TextArea_SelectionChanged ;
74
82
editor . TextArea . PreviewKeyDown += TextArea_PreviewKeyDown ;
83
+
84
+ editor . AddHandler ( MouseLeftButtonDownEvent , new MouseButtonEventHandler ( TextArea_MouseDown ) , true ) ;
85
+
75
86
editor . PreviewMouseWheel += PrevMouseWheel ;
76
87
editor . MouseDown += editor_MouseDown ;
77
88
editor . TextArea . TextEntered += TextArea_TextEntered ;
78
89
editor . TextArea . TextEntering += TextArea_TextEntering ;
79
-
80
90
var fInfo = new FileInfo ( filePath ) ;
81
91
if ( fInfo . Exists )
82
92
{
@@ -158,6 +168,106 @@ public EditorElement(string filePath)
158
168
CompileBox . IsChecked = filePath . EndsWith ( ".sp" ) ;
159
169
}
160
170
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
+
161
271
public string FullFilePath
162
272
{
163
273
get => _FullFilePath ;
@@ -502,7 +612,7 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
502
612
503
613
Program . MainWindow . EditorsReferences . Remove ( this ) ;
504
614
// 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);
506
616
507
617
Parent = null ; //to prevent a ring depency which disables the GC from work
508
618
Program . MainWindow . UpdateWindowTitle ( ) ;
@@ -522,7 +632,7 @@ private void Caret_PositionChanged(object sender, EventArgs e)
522
632
var result = bracketSearcher . SearchBracket ( editor . Document , editor . CaretOffset ) ;
523
633
bracketHighlightRenderer . SetHighlight ( result ) ;
524
634
//TODO: Remove this in release
525
- StatusLine_Work . Text = editor . CaretOffset . ToString ( ) ;
635
+ // StatusLine_Work.Text = editor.CaretOffset.ToString();
526
636
}
527
637
528
638
private void TextArea_TextEntered ( object sender , TextCompositionEventArgs e )
@@ -531,7 +641,6 @@ private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
531
641
if ( e . Text == ";" )
532
642
if ( editor . CaretOffset >= 0 )
533
643
{
534
-
535
644
var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
536
645
var text = editor . Document . GetText ( line ) ;
537
646
@@ -660,6 +769,8 @@ private void PrevMouseWheel(object sender, MouseWheelEventArgs e)
660
769
661
770
private void editor_MouseDown ( object sender , MouseButtonEventArgs e )
662
771
{
772
+ e . Handled = true ;
773
+ Console . WriteLine ( "PRESSED MOUSE DOWN" ) ;
663
774
HideISAC ( ) ;
664
775
}
665
776
0 commit comments