-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEditorTextView.cs
293 lines (269 loc) · 9.63 KB
/
EditorTextView.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
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
using System;
using System.Collections.Generic;
using System.Management.Automation.Runspaces;
using Terminal.Gui;
using System.Collections.Concurrent;
namespace psedit
{
public class EditorTextView : TextView
{
public ConcurrentDictionary<Point, string> Errors { get; set; } = new ConcurrentDictionary<Point, string>();
public ConcurrentDictionary<Point, string> ColumnErrors { get; set; } = new ConcurrentDictionary<Point, string>();
public bool modified = false;
public Runspace _runspace;
public List<List<Rune>> Runes { get; private set; }
private EditorContext editorContext;
public bool CanFormat = false;
public bool CanRun = false;
public bool CanSyntaxHighlight = false;
public LanguageEnum _language = LanguageEnum.Powershell;
public EditorTextView(Runspace runspace)
{
AllowsTab = false;
_runspace = runspace;
SetLanguage(LanguageEnum.Powershell);
}
public void SetLanguage(LanguageEnum language)
{
_language = language;
// initialize autocomplete for selected language
if (language == LanguageEnum.Powershell)
{
editorContext = new PowerShellEditorContext(TabWidth, _runspace);
Autocomplete = new PowershellAutocomplete(_runspace);
Autocomplete.MaxWidth = 30;
Autocomplete.MaxHeight = 10;
Autocomplete.HostControl = this;
Autocomplete.SelectionKey = Key.Enter;
}
else if (language == LanguageEnum.JSON)
{
editorContext = new JSONEditorContext(TabWidth);
}
else
{
editorContext = null;
}
// reset formatting
if (editorContext != null)
{
CanFormat = editorContext.CanFormat;
CanRun = editorContext.CanRun;
CanSyntaxHighlight = editorContext.CanSyntaxHighlight;
}
else
{
CanFormat = false;
CanRun = false;
CanSyntaxHighlight = false;
}
}
public void Format()
{
Text = editorContext.Format(Text.ToString());
}
public string Run(string path, bool exit = false)
{
var output = String.Empty;
if (exit == true)
{
editorContext.RunCurrentRunspace(path);
}
else
{
output = editorContext.Run(path);
}
return output;
}
public string RunText(string text, bool exit = false)
{
var output = String.Empty;
if (editorContext.CanRun)
{
if (exit == true)
{
editorContext.RunTextCurrentRunspace(text);
}
else
{
output = editorContext.RunText(text);
}
}
return output;
}
private void ColorNormal()
{
// this is default color / background when there is no content
Driver.SetAttribute(Terminal.Gui.Attribute.Make(Color.Green, Color.Black));
}
private void ColorSelected()
{
// this is default color / background when content is selected
Driver.SetAttribute(Terminal.Gui.Attribute.Make(Color.Green, Color.Blue));
}
public override void Redraw(Rect bounds)
{
if (IsDirty)
{
modified = true;
}
var text = Text.ToString();
Runes = EditorExtensions.StringToRunes(text);
ColorNormal();
var offB = OffSetBackground();
int right = Frame.Width + offB.width + RightOffset;
int bottom = Frame.Height + offB.height + BottomOffset;
var row = 0;
if (editorContext != null)
{
editorContext.ParseText(bounds.Height, TopRow, LeftColumn, LeftColumn + right, Text.ToString(), Runes);
ColumnErrors = editorContext.ColumnErrors;
Errors = editorContext.Errors;
}
for (int idxRow = TopRow; idxRow < Runes.Count; idxRow++)
{
if (row > bottom)
{
break;
}
var line = EditorExtensions.GetLine(Runes, idxRow);
int lineRuneCount = line.Count;
var col = 0;
// identify token for specific row
for (int idxCol = LeftColumn; idxCol < lineRuneCount; idxCol++)
{
var rune = idxCol >= lineRuneCount ? ' ' : line[idxCol];
var cols = Rune.ColumnWidth(rune);
if (editorContext != null)
{
var point = new Point(idxCol, row);
var errorPoint = new Point(idxCol, idxRow);
var color = editorContext.GetColorByPoint(point);
if (Selecting && PointInSelection(idxCol, idxRow))
{
Driver.SetAttribute(Terminal.Gui.Attribute.Make(color, Color.Blue));
}
else if (ColumnErrors.ContainsKey(errorPoint))
{
Driver.SetAttribute(Terminal.Gui.Attribute.Make(color, Color.Red));
}
else
{
Driver.SetAttribute(Terminal.Gui.Attribute.Make(color, Color.Black));
}
}
else if (Selecting && PointInSelection(idxCol, idxRow))
{
ColorSelected();
}
else
{
ColorNormal();
}
// add rune with previously set color
if (rune == '\t')
{
cols += TabWidth + 1;
if (col + cols > right)
{
cols = right - col;
}
for (int i = 0; i < cols; i++)
{
if (col + i < right)
{
AddRune(col + i, row, ' ');
}
}
}
else
{
AddRune(col, row, rune);
}
if (!EditorExtensions.SetCol(ref col, bounds.Right, cols))
{
break;
}
if (idxCol + 1 < lineRuneCount && col + Rune.ColumnWidth(line[idxCol + 1]) > right)
{
break;
}
}
Move(0, row);
if (col < right)
{
ColorNormal();
ClearRegion(col, row, right, row + 1);
}
row++;
}
if (row < bottom)
{
ColorNormal();
ClearRegion(bounds.Left, row, right, bottom);
}
PositionCursor();
if (SelectedLength > 0)
return;
if (editorContext != null)
{
if (editorContext.CanAutocomplete)
{
// draw autocomplete
Autocomplete.GenerateSuggestions();
var renderAt = new Point(
CursorPosition.X - LeftColumn,
Autocomplete.PopupInsideContainer
? (CursorPosition.Y + 1) - TopRow
: 0);
Autocomplete.RenderOverlay(renderAt);
}
}
}
void ClearRegion(int left, int top, int right, int bottom)
{
for (int row = top; row < bottom; row++)
{
Move(left, row);
for (int col = left; col < right; col++)
AddRune(col, row, ' ');
}
}
bool PointInSelection(int col, int row)
{
long start, end;
GetEncodedRegionBounds(out start, out end);
var q = ((long)(uint)row << 32) | (uint)col;
return q >= start && q <= end - 1;
}
void GetEncodedRegionBounds(out long start, out long end)
{
long selection = ((long)(uint)SelectionStartRow << 32) | (uint)SelectionStartColumn;
long point = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
if (selection > point)
{
start = point;
end = selection;
}
else
{
start = selection;
end = point;
}
}
(int width, int height) OffSetBackground()
{
int w = 0;
int h = 0;
if (SuperView?.Frame.Right - Frame.Right < 0)
{
w = SuperView.Frame.Right - Frame.Right - 1;
}
if (SuperView?.Frame.Bottom - Frame.Bottom < 0)
{
h = SuperView.Frame.Bottom - Frame.Bottom - 1;
}
return (w, h);
}
}
}