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
|
//
// TextEditorViewContent.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Components.Commands;
using MonoDevelop.Ide.Commands;
using System.Collections;
using System.Collections.Generic;
using MonoDevelop.Ide.TypeSystem;
using System.IO;
using MonoDevelop.Core.Text;
using System.Text;
using Gtk;
using ICSharpCode.NRefactory.TypeSystem;
using System.Linq;
using MonoDevelop.Ide.Editor.Extension;
using ICSharpCode.NRefactory.Refactoring;
using MonoDevelop.Components;
using MonoDevelop.Core;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.CodeAnalysis;
using Gdk;
using MonoDevelop.Ide.CodeFormatting;
using System.Collections.Immutable;
using Microsoft.VisualStudio.CodingConventions;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
namespace MonoDevelop.Ide.Editor
{
/// <summary>
/// The TextEditor object needs to be available through BaseViewContent.GetContent therefore we need to insert a
/// decorator in between.
/// </summary>
[Obsolete]
class TextEditorViewContent : ViewContent, ICommandRouter
{
readonly TextEditor textEditor;
readonly ITextEditorImpl textEditorImpl;
MonoDevelop.Projects.Policies.PolicyContainer policyContainer;
public TextEditorViewContent (TextEditor textEditor, ITextEditorImpl textEditorImpl)
{
if (textEditor == null)
throw new ArgumentNullException (nameof (textEditor));
if (textEditorImpl == null)
throw new ArgumentNullException (nameof (textEditorImpl));
this.textEditor = textEditor;
this.textEditorImpl = textEditorImpl;
this.textEditor.MimeTypeChanged += UpdateTextEditorOptions;
DefaultSourceEditorOptions.Instance.Changed += UpdateTextEditorOptions;
textEditorImpl.ViewContent.ContentNameChanged += ViewContent_ContentNameChanged;
textEditorImpl.ViewContent.DirtyChanged += ViewContent_DirtyChanged;
textEditor.Options = DefaultSourceEditorOptions.Instance.Create ();
}
protected override void OnContentNameChanged ()
{
base.OnContentNameChanged ();
if (ContentName != textEditorImpl.ContentName && !string.IsNullOrEmpty (textEditorImpl.ContentName))
AutoSave.RemoveAutoSaveFile (textEditorImpl.ContentName);
if (ContentName != null) // Happens when a file is converted to an untitled file, but even in that case the text editor should be associated with the old location, otherwise typing can be messed up due to change of .editconfig settings etc.
textEditor.FileName = ContentName;
if (this.WorkbenchWindow?.Document != null)
textEditor.InitializeExtensionChain (this.WorkbenchWindow.Document);
UpdateTextEditorOptions (null, null);
}
void ViewContent_ContentNameChanged (object sender, EventArgs e)
{
this.ContentName = textEditorImpl.ViewContent.ContentName;
}
void ViewContent_DirtyChanged (object sender, EventArgs e)
{
OnDirtyChanged ();
}
void HandleDirtyChanged (object sender, EventArgs e)
{
IsDirty = textEditorImpl.ViewContent.IsDirty;
InformAutoSave ();
}
void HandleTextChanged (object sender, TextChangeEventArgs e)
{
InformAutoSave ();
}
CancellationTokenSource editorOptionsUpdateCancellationSource;
void UpdateTextEditorOptions (object sender, EventArgs e)
{
editorOptionsUpdateCancellationSource?.Cancel ();
editorOptionsUpdateCancellationSource = new CancellationTokenSource ();
UpdateStyleParent (Project, textEditor.MimeType, editorOptionsUpdateCancellationSource.Token).Ignore ();
}
uint autoSaveTimer = 0;
Task autoSaveTask;
void InformAutoSave ()
{
if (isDisposed)
return;
RemoveAutoSaveTimer ();
autoSaveTimer = GLib.Timeout.Add (500, delegate {
autoSaveTimer = 0;
if (autoSaveTask != null && !autoSaveTask.IsCompleted)
return false;
autoSaveTask = AutoSave.InformAutoSaveThread (textEditor.CreateSnapshot (), textEditor.FileName, IsDirty);
return false;
});
}
void RemoveAutoSaveTimer ()
{
if (autoSaveTimer == 0)
return;
GLib.Source.Remove (autoSaveTimer);
autoSaveTimer = 0;
}
void RemovePolicyChangeHandler ()
{
if (policyContainer != null)
policyContainer.PolicyChanged -= HandlePolicyChanged;
}
async Task UpdateStyleParent (MonoDevelop.Projects.Project styleParent, string mimeType, CancellationToken token)
{
RemovePolicyChangeHandler ();
if (string.IsNullOrEmpty (mimeType))
mimeType = "text/plain";
var mimeTypes = DesktopService.GetMimeTypeInheritanceChain (mimeType);
if (styleParent != null)
policyContainer = styleParent.Policies;
else
policyContainer = MonoDevelop.Projects.Policies.PolicyService.DefaultPolicies;
var currentPolicy = policyContainer.Get<TextStylePolicy> (mimeTypes);
policyContainer.PolicyChanged += HandlePolicyChanged;
((DefaultSourceEditorOptions)textEditor.Options).UpdateStylePolicy (currentPolicy);
var context = await EditorConfigService.GetEditorConfigContext (textEditor.FileName, token);
if (context == null)
return;
((DefaultSourceEditorOptions)textEditor.Options).SetContext (context);
}
void HandlePolicyChanged (object sender, MonoDevelop.Projects.Policies.PolicyChangedEventArgs args)
{
var mimeTypes = DesktopService.GetMimeTypeInheritanceChain (textEditor.MimeType);
var currentPolicy = policyContainer.Get<TextStylePolicy> (mimeTypes);
((DefaultSourceEditorOptions)textEditor.Options).UpdateStylePolicy (currentPolicy);
}
void CancelDocumentParsedUpdate ()
{
src.Cancel ();
src = new CancellationTokenSource ();
}
CancellationTokenSource src = new CancellationTokenSource ();
async Task RunFirstTimeFoldUpdate (string text)
{
if (string.IsNullOrEmpty (text))
return;
try {
ParsedDocument parsedDocument = null;
var foldingParser = TypeSystemService.GetFoldingParser (textEditor.MimeType);
if (foldingParser != null) {
parsedDocument = foldingParser.Parse (textEditor.FileName, text);
} else {
var normalParser = TypeSystemService.GetParser (textEditor.MimeType);
if (normalParser != null) {
parsedDocument = await normalParser.Parse(
new TypeSystem.ParseOptions {
FileName = textEditor.FileName,
Content = new StringTextSource(text),
Project = Project
});
}
}
if (parsedDocument != null) {
await FoldingTextEditorExtension.UpdateFoldings (textEditor, parsedDocument, textEditor.CaretLocation, true);
}
} catch (Exception e) {
LoggingService.LogError ("Error running first time fold update", e);
}
}
#region IViewFContent implementation
public override async Task Load (FileOpenInformation fileOpenInformation)
{
textEditorImpl.ViewContent.DirtyChanged -= HandleDirtyChanged;
textEditor.TextChanged -= HandleTextChanged;
await textEditorImpl.ViewContent.Load (fileOpenInformation);
await RunFirstTimeFoldUpdate (textEditor.Text);
textEditorImpl.InformLoadComplete ();
textEditor.TextChanged += HandleTextChanged;
textEditorImpl.ViewContent.DirtyChanged += HandleDirtyChanged;
}
public override async Task LoadNew (Stream content, string mimeType)
{
textEditor.MimeType = mimeType;
string text = null;
if (content != null) {
var res = await TextFileUtility.GetTextAsync (content);
text = textEditor.Text = res.Text;
textEditor.Encoding = res.Encoding;
}
await RunFirstTimeFoldUpdate (text);
textEditorImpl.InformLoadComplete ();
}
public override Task Save (FileSaveInformation fileSaveInformation)
{
if (!string.IsNullOrEmpty (fileSaveInformation.FileName))
AutoSave.RemoveAutoSaveFile (fileSaveInformation.FileName);
return textEditorImpl.ViewContent.Save (fileSaveInformation);
}
public override Task Save ()
{
if (!string.IsNullOrEmpty (textEditorImpl.ContentName))
AutoSave.RemoveAutoSaveFile (textEditorImpl.ContentName);
return textEditorImpl.ViewContent.Save ();
}
public override void DiscardChanges ()
{
if (autoSaveTask != null)
autoSaveTask.Wait (TimeSpan.FromSeconds (5));
RemoveAutoSaveTimer ();
if (!string.IsNullOrEmpty (textEditorImpl.ContentName))
AutoSave.RemoveAutoSaveFile (textEditorImpl.ContentName);
textEditorImpl.ViewContent.DiscardChanges ();
}
protected override void OnSetProject (MonoDevelop.Projects.Project project)
{
base.OnSetProject (project);
textEditorImpl.ViewContent.Project = project;
UpdateTextEditorOptions (null, null);
}
public override ProjectReloadCapability ProjectReloadCapability {
get {
return textEditorImpl.ViewContent.ProjectReloadCapability;
}
}
#endregion
#region BaseViewContent implementation
protected override IEnumerable<object> OnGetContents (Type type)
{
foreach (var r in base.OnGetContents (type))
yield return r;
if (type == typeof(ITextBuffer)) {
yield return textEditor.TextView.TextBuffer;
yield break;
}
if (type == typeof (ITextView)) {
yield return textEditor.TextView;
yield break;
}
if (type.IsAssignableFrom (typeof (TextEditor))) {
yield return textEditor;
yield break;
}
var ext = textEditorImpl.EditorExtension;
while (ext != null) {
foreach (var r in ext.OnGetContents (type))
yield return r;
ext = ext.Next;
}
foreach (var r in textEditorImpl.ViewContent.GetContents (type))
yield return r;
}
protected override void OnWorkbenchWindowChanged ()
{
base.OnWorkbenchWindowChanged ();
textEditorImpl.ViewContent.WorkbenchWindow = WorkbenchWindow;
}
public override Control Control {
get {
return textEditor;
}
}
public override string TabPageLabel {
get {
return textEditorImpl.ViewContent.TabPageLabel;
}
}
public override string TabAccessibilityDescription {
get {
return textEditorImpl.ViewContent.TabAccessibilityDescription;
}
}
public override bool IsDirty {
get { return textEditorImpl.ViewContent.IsDirty; }
set {
textEditorImpl.ViewContent.IsDirty = value;
}
}
#endregion
#region IDisposable implementation
bool isDisposed;
public override void Dispose ()
{
if (isDisposed)
return;
base.Dispose ();
isDisposed = true;
EditorConfigService.RemoveEditConfigContext (textEditor.FileName).Ignore ();
CancelDocumentParsedUpdate ();
textEditorImpl.ViewContent.DirtyChanged -= HandleDirtyChanged;
textEditor.MimeTypeChanged -= UpdateTextEditorOptions;
textEditor.TextChanged -= HandleTextChanged;
textEditorImpl.ViewContent.ContentNameChanged -= ViewContent_ContentNameChanged;
textEditorImpl.ViewContent.DirtyChanged -= ViewContent_DirtyChanged; ;
DefaultSourceEditorOptions.Instance.Changed -= UpdateTextEditorOptions;
RemovePolicyChangeHandler ();
editorOptionsUpdateCancellationSource?.Cancel ();
RemoveAutoSaveTimer ();
textEditor.Dispose ();
}
#endregion
#region ICommandRouter implementation
object ICommandRouter.GetNextCommandTarget ()
{
return textEditor;
}
#endregion
public override void GrabFocus ()
{
textEditor.GrabFocus ();
DefaultSourceEditorOptions.SetUseAsyncCompletion (false);
}
}
}
|