Repository navigation
Expand file tree
/
Copy pathRefactor.cs
More file actions
116 lines (105 loc) · 3.81 KB
/
Copy pathRefactor.cs
File metadata and controls
116 lines (105 loc) · 3.81 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
using FastColoredTextBoxNS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Trax {
/// <summary>
/// Global symbol replace form
/// </summary>
public partial class Refactor : Form {
FastColoredTextBox E;
Main M;
string S;
string R;
/// <summary>
/// Creates global replace dialog
/// </summary>
/// <param name="main"></param>
public Refactor(Main main) {
M = main;
E = M.CurrentEditor;
InitializeComponent();
Width = Symbol.Width + Rename.Width + 4;
if (E != null) {
PreviewKeyDown += Refactor_PreviewKeyDown;
Symbol.KeyDown += Symbol_KeyDown;
GetSymbol();
Symbol.Focus();
} else Dispose();
}
/// <summary>
/// Gets symbol from current editor selection and decides if it's applicable
/// </summary>
void GetSymbol() {
var originalSelection = new Range(E, E.Selection.Start, E.Selection.End);
S = E.SelectWord();
E.Selection.Normalize();
var point = E.PlaceToPoint(E.Selection.Start);
var target = E.PointToScreen(point);
target.X -= E.Left;
target.Y -= Height - ClientRectangle.Height - E.Left + 3;
Location = target;
Symbol.Text = S;
var style = E.Selection.Chars.First().style;
var notApplicableForStyles = new StyleIndex[] {
EditorSyntax.StyleMap.Command,
EditorSyntax.StyleMap.Comment,
EditorSyntax.StyleMap.Keyword0,
EditorSyntax.StyleMap.Keyword1,
EditorSyntax.StyleMap.Keyword2,
EditorSyntax.StyleMap.Keyword3,
EditorSyntax.StyleMap.Keyword4,
EditorSyntax.StyleMap.Number
};
var applicable = !notApplicableForStyles.Contains(style);
if (applicable) ShowDialog();
else { E.Selection = originalSelection; Dispose(); }
}
/// <summary>
/// Handles form's preview keys
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Refactor_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Escape: if (!IsDisposed) Close(); break;
case Keys.Enter: if (!IsDisposed) Close(); break;
}
}
/// <summary>
/// Handles keys pressed in text box
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Symbol_KeyDown(object sender, KeyEventArgs e) {
switch (e.KeyCode) {
case Keys.Escape: Close(); e.Handled = true; break;
case Keys.Enter: R = Symbol.Text; DoReplace(S, R); e.Handled = true; break;
}
}
/// <summary>
/// Handles rename click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Rename_Click(object sender, EventArgs e) {
DoReplace(S, Symbol.Text);
}
/// <summary>
/// Performs replace
/// </summary>
/// <param name="original"></param>
/// <param name="updated"></param>
private void DoReplace(string original, string updated) {
E.BeginInvoke(new MethodInvoker(() => { ProjectFile.ReplaceSymbol(original, updated); }));
if (!IsDisposed) Close();
}
}
}