This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMainWindowCommands.cs
More file actions
655 lines (609 loc) · 18.3 KB
/
Copy pathMainWindowCommands.cs
File metadata and controls
655 lines (609 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using SPCode.UI.Components;
using SPCode.UI.Windows;
using SPCode.Utils;
using SPCode.Utils.SPSyntaxTidy;
using static SPCode.Interop.TranslationProvider;
namespace SPCode.UI;
public partial class MainWindow
{
/// <summary>
/// Gets the current editor element.
/// </summary>
/// <returns></returns>
public EditorElement GetCurrentEditorElement()
{
if (Application.Current != null)
{
foreach (Window win in Application.Current.Windows)
{
if (win is NewFileWindow newFileWin)
{
return newFileWin.PreviewBox;
}
}
}
if (DockingPane.SelectedContent?.Content != null)
{
var possElement = DockingManager.ActiveContent;
if (possElement is EditorElement element)
{
return element;
}
}
return null;
}
/// <summary>
/// Gets the current DASM element.
/// </summary>
/// <returns></returns>
public DASMElement GetCurrentDASMElement()
{
DASMElement outElement = null;
if (DockingPane.SelectedContent?.Content != null)
{
var possElement = DockingManager.ActiveContent;
if (possElement is DASMElement element)
{
outElement = element;
}
}
return outElement;
}
/// <summary>
/// Creates a new SourcePawn Script file and loads it.
/// </summary>
private void Command_New()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && ee.IsTemplateEditor)
{
return;
}
string newFilePath;
string rootPath;
var newFileNum = 0;
if (Program.Configs[Program.SelectedConfig].SMDirectories.Count > 0)
{
rootPath = Program.Configs[Program.SelectedConfig].SMDirectories[0];
}
else
{
rootPath = Environment.CurrentDirectory;
}
do
{
newFilePath = Path.Combine(rootPath, $"New Plugin ({++newFileNum}).sp");
} while (File.Exists(newFilePath));
File.Create(newFilePath).Close();
AddEditorElement(new FileInfo(newFilePath), $"New Plugin ({newFileNum}).sp", true, out _);
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens a new window for the user to create a new SourcePawn Script from a template.
/// </summary>
private void Command_NewFromTemplate()
{
try
{
var nfWindow = new NewFileWindow
{
Owner = this,
ShowInTaskbar = false
};
DimmMainWindow();
nfWindow.ShowDialog();
RestoreMainWindow();
UpdateWindowTitle();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Open File window for the user to load a file into the editor.
/// </summary>
private void Command_Open()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && ee.IsTemplateEditor)
{
return;
}
var ofd = new OpenFileDialog
{
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
Filter = Constants.FileOpenFilters,
Multiselect = true,
Title = Translate("OpenNewFile")
};
var result = ofd.ShowDialog(this);
if (result.Value)
{
var AnyFileLoaded = false;
if (ofd.FileNames.Length > 0)
{
for (var i = 0; i < ofd.FileNames.Length; ++i)
{
AnyFileLoaded |= TryLoadSourceFile(ofd.FileNames[i], out _, true, i == 0);
}
if (!AnyFileLoaded)
{
MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Theme;
this.ShowMessageAsync(Translate("NoFileOpened"),
Translate("NoFileOpenedCap"), MessageDialogStyle.Affirmative,
MetroDialogOptions);
}
}
}
Activate();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Saves the current file.
/// </summary>
private void Command_Save()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && !ee.IsTemplateEditor)
{
ee.Save(true);
BlendEffect();
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Save As window.
/// </summary>
private void Command_SaveAs()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && !ee.IsTemplateEditor)
{
var sfd = new SaveFileDialog { AddExtension = true, Filter = Constants.FileSaveFilters, OverwritePrompt = true, Title = Translate("SaveFileAs"), FileName = ee.Parent.Title.Trim('*') };
var result = sfd.ShowDialog(this);
if (result.Value && !string.IsNullOrWhiteSpace(sfd.FileName))
{
ee.FullFilePath = sfd.FileName;
ee.Save(true);
BlendEffect();
}
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Go To Line window.
/// </summary>
private void Command_GoToLine()
{
try
{
if (EditorReferences.Any())
{
var goToLineWindow = new GoToLineWindow();
goToLineWindow.ShowDialog();
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Deletes the line the caret is in.
/// </summary>
private void Command_DeleteLine()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && !ee.editor.IsReadOnly)
{
ee.DeleteLine();
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Search window.
/// </summary>
private void Command_FindReplace()
{
try
{
if (!EditorReferences.Any())
{
return;
}
var selection = GetCurrentEditorElement().editor.TextArea.Selection.GetText();
foreach (Window win in Application.Current.Windows)
{
if (win is SearchWindow findWin)
{
findWin.Activate();
findWin.FindBox.Text = selection;
findWin.FindBox.SelectAll();
findWin.FindBox.Focus();
return;
}
}
var findWindow = new SearchWindow(selection) { Owner = this };
findWindow.Show();
findWindow.FindBox.Focus();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Saves all opened files.
/// </summary>
private void Command_SaveAll()
{
try
{
if (!EditorReferences.Any() || GetCurrentEditorElement().IsTemplateEditor)
{
return;
}
if (EditorReferences.Count > 0)
{
foreach (var editor in EditorReferences)
{
editor.Save();
}
BlendEffect();
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Closes the current file opened.
/// </summary>
private void Command_Close()
{
try
{
var ee = GetCurrentEditorElement();
var de = GetCurrentDASMElement();
if (ee != null && (ee.IsTemplateEditor || ee.ClosingPromptOpened))
{
return;
}
ee?.Close();
de?.Close();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Closes all files opened.
/// </summary>
private async void Command_CloseAll()
{
try
{
// We create a new list because we can't delete elements of the list we're iterating through
foreach (var editor in EditorReferences.ToList()) editor.Close();
foreach (var editor in DASMReferences.ToList()) editor.Close();
}
catch (Exception ex)
{
await this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Undoes the previous action.
/// </summary>
private void Command_Undo()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null)
{
if (ee.editor.CanUndo)
{
ee.editor.Undo();
}
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Redoes the latter action.
/// </summary>
private void Command_Redo()
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null)
{
if (ee.editor.CanRedo)
{
ee.editor.Redo();
}
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Cut command.
/// </summary>
private void Command_Cut()
{
try
{
var ee = GetCurrentEditorElement();
ee?.editor.Cut();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Copy command.
/// </summary>
private void Command_Copy()
{
try
{
var ee = GetCurrentEditorElement();
ee?.editor.Copy();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Paste command.
/// </summary>
private void Command_Paste()
{
try
{
var ee = GetCurrentEditorElement();
ee?.editor.Paste();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Collapses or expands all foldings in the editor.
/// </summary>
/// <param name="folded">Whether to fold all foldings (true to collapse all).</param>
private void Command_FlushFoldingState(bool folded)
{
try
{
var ee = GetCurrentEditorElement();
if (ee?.foldingManager != null)
{
var foldings = ee.foldingManager.AllFoldings;
foreach (var folding in foldings)
{
folding.IsFolded = folded;
}
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Select all command.
/// </summary>
private void Command_SelectAll()
{
try
{
var ee = GetCurrentEditorElement();
ee?.editor.SelectAll();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Comment or uncomment the line the caret is in.
/// </summary>
private void Command_ToggleCommentLine(bool comment)
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && !ee.editor.IsReadOnly)
{
ee.ToggleComment(comment);
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Change the case of the current selection.
/// </summary>
/// <param name="toUpper">Whether to transform to uppercase</param>
public void Command_ChangeCase(bool toUpper)
{
try
{
var ee = GetCurrentEditorElement();
if (ee != null && !ee.editor.IsReadOnly)
{
ee.ChangeCase(toUpper);
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Perform a code reformat to clean loose whitespaces/wrongly indented code.
/// </summary>
/// <param name="all"></param>
private void Command_TidyCode(bool all)
{
try
{
var editors = all ? EditorReferences : new() { GetCurrentEditorElement() };
foreach (var editor in editors)
{
if (editor == null || editor.editor.IsReadOnly)
{
continue;
}
int currentCaret = editor.editor.TextArea.Caret.Offset, numOfSpacesOrTabsBefore = 0;
var line = editor.editor.Document.GetLineByOffset(currentCaret);
var lineNumber = line.LineNumber;
// 0 - start | any other - middle | -1 - EOS
var cursorLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;
if (cursorLinePos > 0)
{
numOfSpacesOrTabsBefore = editor.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
}
// Formatting Start //
editor.editor.Document.BeginUpdate();
var source = editor.editor.Text;
editor.editor.Document.Replace(0, source.Length, SPSyntaxTidy.TidyUp(source));
editor.editor.Document.EndUpdate();
// Formatting End //
line = editor.editor.Document.GetLineByNumber(lineNumber);
var newCaretPos = line.Offset;
if (cursorLinePos == -1)
{
newCaretPos += line.Length;
}
else if (cursorLinePos != 0)
{
var numOfSpacesOrTabsAfter = editor.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
newCaretPos += cursorLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
}
editor.editor.TextArea.Caret.Offset = newCaretPos;
}
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Open File window for the user to select a file to decompile.
/// </summary>
private async void Command_Decompile()
{
try
{
var file = DecompileUtil.GetFile();
if (file != null)
{
var msg = await this.ShowProgressAsync(Translate("Decompiling") + "...", file.Name, false, MetroDialogOptions);
msg.SetIndeterminate();
ProcessUITasks();
TryLoadSourceFile(DecompileUtil.GetDecompiledPlugin(file), out _, SelectMe: true);
await msg.CloseAsync();
}
}
catch (Exception ex)
{
await this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Opens the Search Definition window.
/// </summary>
private void Command_OpenSPDef()
{
try
{
var spDefinitionWindow = new SPDefinitionWindow
{
Owner = this,
ShowInTaskbar = false
};
DimmMainWindow();
spDefinitionWindow.ShowDialog();
RestoreMainWindow();
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
/// <summary>
/// Re-opens the last closed tab
/// </summary>
private void Command_ReopenLastClosedTab()
{
try
{
if (Program.RecentFilesStack.Count > 0)
{
TryLoadSourceFile(Program.RecentFilesStack.Pop(), out _, false, true);
}
MenuI_ReopenLastClosedTab.IsEnabled = Program.RecentFilesStack.Count > 0;
}
catch (Exception ex)
{
this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
}
}