-
Notifications
You must be signed in to change notification settings - Fork 137
/
SciTextEditorControl.cs
243 lines (188 loc) · 8.41 KB
/
SciTextEditorControl.cs
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
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ScintillaNET;
using SwqlStudio.Metadata;
using SwqlStudio.Properties;
namespace SwqlStudio
{
internal class SciTextEditorControl : Scintilla, ILexerDataSource
{
private ContextMenuStrip editorContextMenu;
string ILexerDataSource.Text => Text;
public string FileName { get; set; }
public LexerService LexerService { get; }
public SciTextEditorControl()
{
Lexer = Lexer.Sql;
LexerService = new LexerService(this);
StyleResetDefault();
Styles[Style.Default].Font = "Consolas";
Styles[Style.Default].Size = 10;
StyleClearAll(); // Propagates the settings from Style.Default to all language-specific lexer styles
// TODO: wait for newest nuget package, it will ocntains this method!!!
//this.AutoCSetFillUps(" {}[]().,:;+-*/%&|^!~=<>?@#'\"\\");
foreach (var words in LexerService.LexerKeywords)
SetKeywords(words.Item1, string.Join(" ", words.Item2));
//// Default (whitespace) style index.
//Styles[Style.Sql.Default].ForeColor = Color.Black;
//// Comment style index.
//Styles[Style.Sql.Comment].ForeColor = Color.Green;
// Line comment style index.
Styles[Style.Sql.CommentLine].ForeColor = Color.Green;
//// Documentation comment style index.
//Styles[Style.Sql.CommentDoc].ForeColor = Color.DarkOliveGreen;
//// Number style index.
//Styles[Style.Sql.Number].ForeColor = Color.Blue;
// Keyword list 1 style index.
Styles[Style.Sql.Word].ForeColor = Color.Blue;
//// Double-quoted string style index.
//Styles[Style.Sql.String].ForeColor = Color.Red;
// Single-quoted string style index.
Styles[Style.Sql.Character].ForeColor = Color.Red;
//// Keyword from the SQL*Plus list style index.
//Styles[Style.Sql.SqlPlus].ForeColor = Color.HotPink;
//// SQL*Plus prompt style index.
//Styles[Style.Sql.SqlPlusPrompt].ForeColor = Color.DodgerBlue;
// Operator style index.
Styles[Style.Sql.Operator].ForeColor = Color.DarkGray;
// Identifier style index.
Styles[Style.Sql.Identifier].ForeColor = Color.Black;
//// SQL*Plus comment style index.
//Styles[Style.Sql.SqlPlusComment].ForeColor = Color.LightSeaGreen;
//// Documentation line comment style index.
//Styles[Style.Sql.CommentLineDoc].ForeColor = Color.RoyalBlue;
//// Keyword list 2 style index.
//Styles[Style.Sql.Word2].ForeColor = Color.ForestGreen;
//// Documentation (Doxygen) keyword style index.
//Styles[Style.Sql.CommentDocKeyword].ForeColor = Color.Black;
//// Documentation (Doxygen) keyword error style index.
//Styles[Style.Sql.CommentDocKeywordError].ForeColor = Color.Fuchsia;
// Keyword user-list 1 style index. (scalar functions)
Styles[Style.Sql.User1].ForeColor = Color.Fuchsia;
// Keyword user-list 2 style index. (aggregate functions)
Styles[Style.Sql.User2].ForeColor = Color.Salmon;
//// Keyword user-list 3 style index.
//Styles[Style.Sql.User3].ForeColor = Color.Salmon;
//// Keyword user-list 4 style index.
//Styles[Style.Sql.User4].ForeColor = Color.Peru;
//// Quoted identifier style index.
//Styles[Style.Sql.QuotedIdentifier].ForeColor = Color.Brown;
//// Q operator style index.
//Styles[Style.Sql.QOperator].ForeColor = Color.DarkGray;
InitializeCustomContextMenu();
}
private void InitializeCustomContextMenu()
{
editorContextMenu = new ContextMenuStrip();
editorContextMenu.SuspendLayout();
ToolStripMenuItem undoMenuItem = new ToolStripMenuItem();
undoMenuItem.Text = "Undo";
undoMenuItem.Image = Resources.Undo_16x;
undoMenuItem.ShortcutKeys = Keys.Control | Keys.Z;
undoMenuItem.Click += new EventHandler(UndoMenuClick);
ToolStripMenuItem redoMenuItem = new ToolStripMenuItem();
redoMenuItem.Text = "Redo";
redoMenuItem.Image = Resources.Redo_16x;
redoMenuItem.ShortcutKeys = Keys.Control | Keys.Y;
redoMenuItem.Click += new EventHandler(RedodoMenuClick);
ToolStripMenuItem cutMenuItem = new ToolStripMenuItem();
cutMenuItem.Text = "Cut";
cutMenuItem.Image = Resources.Cut_16x;
cutMenuItem.ShortcutKeys = Keys.Control | Keys.X;
cutMenuItem.Click += new EventHandler(CutMenuClick);
ToolStripMenuItem copyMenuItem = new ToolStripMenuItem();
copyMenuItem.Text = "Copy";
copyMenuItem.Image = Resources.ASX_Copy_blue_16x;
copyMenuItem.ShortcutKeys = Keys.Control | Keys.C;
copyMenuItem.Click += new EventHandler(CopyMenuClick);
ToolStripMenuItem pasteMenuItem = new ToolStripMenuItem();
pasteMenuItem.Text = "Paste";
pasteMenuItem.Image = Resources.ASX_Paste_blue_16x;
pasteMenuItem.ShortcutKeys = Keys.Control | Keys.V;
pasteMenuItem.Click += new EventHandler(PasteMenuClick);
editorContextMenu.Items.AddRange(new ToolStripItem[]
{
undoMenuItem,
redoMenuItem,
cutMenuItem,
copyMenuItem,
pasteMenuItem
});
ContextMenuStrip = editorContextMenu;
editorContextMenu.ResumeLayout(false);
}
private void PasteMenuClick(object sender, EventArgs e)
{
Paste();
}
public void CopyMenuClick(object sender, EventArgs e)
{
Copy(CopyFormat.Text | CopyFormat.Rtf | CopyFormat.Html);
}
private void CutMenuClick(object sender, EventArgs e)
{
Cut();
}
private void RedodoMenuClick(object sender, EventArgs e)
{
Redo();
}
private void UndoMenuClick(object sender, EventArgs e)
{
Undo();
}
public void SetMetadata(IMetadataProvider provider)
{
if (provider != null)
LexerService.SetMetadata(provider);
}
public string GetSelectedOrAllText()
{
var text = SelectedText;
if (string.IsNullOrEmpty(text))
text = Text;
return text;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyChar == (char)Keys.Space)
{
var currentPos = CurrentPosition;
var wordStartPos = WordStartPosition(currentPos, true);
var lenEntered = currentPos - wordStartPos;
this.DisplayAutocomplete(currentPos, lenEntered);
e.Handled = true;
}
base.OnKeyPress(e);
}
protected override void OnCharAdded(CharAddedEventArgs e)
{
base.OnCharAdded(e);
if (!Settings.Default.AutocompleteEnabled)
return;
var currentPos = CurrentPosition;
var wordStartPos = WordStartPosition(currentPos, true);
var lenEntered = currentPos - wordStartPos;
if (lenEntered <= 0 && e.Char != '.')
return;
this.DisplayAutocomplete(currentPos, lenEntered);
}
private void DisplayAutocomplete(int currentPos, int lenEntered)
{
var wordStartPos = WordStartPosition(currentPos, true);
var currentWord = GetWordFromPosition(wordStartPos) ?? string.Empty;
var source = LexerService.GetAutoCompletionKeywords(currentPos)
.Where(x => x.StartsWith(currentWord, StringComparison.OrdinalIgnoreCase))
.OrderBy(x => x);
var keywords = string.Join(" ", source);
this.AutoCShow(lenEntered, keywords);
}
protected override void Dispose(bool disposing)
{
LexerService.Dispose();
base.Dispose(disposing);
}
}
}