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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
//
// Document.cs
//
// Author:
// Lluis Sanchez Gual
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.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 System.Collections;
using System.Collections.Generic;
using System.IO;
using Gtk;
using MonoDevelop.Core;
using MonoDevelop.Core.Execution;
using MonoDevelop.Components;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Text;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Ide.Gui.Dialogs;
using MonoDevelop.Ide.Tasks;
using Mono.Addins;
using MonoDevelop.Ide.Extensions;
using System.Linq;
using System.Threading;
using MonoDevelop.Ide.TypeSystem;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
using MonoDevelop.Ide.Editor;
using MonoDevelop.Ide.Editor.Highlighting;
using MonoDevelop.Core.Text;
using MonoDevelop.Components.Extensions;
using MonoDevelop.Projects.SharedAssetsProjects;
using MonoDevelop.Ide.Editor.Extension;
using System.Collections.Immutable;
using MonoDevelop.Ide.Editor.TextMate;
using MonoDevelop.Core.Assemblies;
using Roslyn.Utilities;
namespace MonoDevelop.Ide.Gui
{
public class Document : DocumentContext
{
internal object MemoryProbe = Counters.DocumentsInMemory.CreateMemoryProbe ();
IWorkbenchWindow window;
ParsedDocument parsedDocument;
Microsoft.CodeAnalysis.DocumentId analysisDocument;
const int ParseDelay = 600;
public IWorkbenchWindow Window {
get { return window; }
}
internal DateTime LastTimeActive {
get;
set;
}
/// <summary>
/// Returns the roslyn document for this document. This may return <c>null</c> if it's no compileable document.
/// Even if it's a C# file.
/// </summary>
public override Microsoft.CodeAnalysis.Document AnalysisDocument {
get {
if (analysisDocument == null)
return null;
return RoslynWorkspace.CurrentSolution.GetDocument (analysisDocument);
}
}
public override T GetContent<T> ()
{
if (window == null)
return null;
//check whether the ViewContent can return the type directly
T ret = Window.ActiveViewContent.GetContent (typeof(T)) as T;
if (ret != null)
return ret;
//check the primary viewcontent
//not sure if this is the right thing to do, but things depend on this behaviour
if (Window.ViewContent != Window.ActiveViewContent) {
ret = Window.ViewContent.GetContent (typeof(T)) as T;
if (ret != null)
return ret;
}
//If we didn't find in ActiveView or ViewContent... Try in SubViews
foreach (var subView in window.SubViewContents) {
foreach (var cnt in subView.GetContents<T> ()) {
return cnt;
}
}
return null;
}
internal ProjectReloadCapability ProjectReloadCapability {
get {
return Window.ViewContent.ProjectReloadCapability;
}
}
public override IEnumerable<T> GetContents<T> ()
{
foreach (var cnt in window.ViewContent.GetContents<T> ()) {
yield return cnt;
}
foreach (var subView in window.SubViewContents) {
foreach (var cnt in subView.GetContents<T> ()) {
yield return cnt;
}
}
}
static Document ()
{
if (IdeApp.Workbench != null) {
IdeApp.Workbench.ActiveDocumentChanged += delegate {
// reparse on document switch to update the current file with changes done in other files.
var doc = IdeApp.Workbench.ActiveDocument;
if (doc == null || doc.Editor == null)
return;
doc.StartReparseThread ();
};
}
}
public Document (IWorkbenchWindow window)
{
Counters.OpenDocuments++;
LastTimeActive = DateTime.Now;
this.window = window;
window.Closed += OnClosed;
window.ActiveViewContentChanged += OnActiveViewContentChanged;
if (IdeApp.Workspace != null)
IdeApp.Workspace.ItemRemovedFromSolution += OnEntryRemoved;
if (window.ViewContent.Project != null)
window.ViewContent.Project.Modified += HandleProjectModified;
window.ViewsChanged += HandleViewsChanged;
window.ViewContent.ContentNameChanged += delegate {
UnsubscribeAnalysisDocument ();
UnloadAdhocProject();
};
MonoDevelopWorkspace.LoadingFinished += TypeSystemService_WorkspaceItemLoaded;
}
void TypeSystemService_WorkspaceItemLoaded (object sender, EventArgs e)
{
UnsubscribeAnalysisDocument ();
UnloadAdhocProject ();
EnsureAnalysisDocumentIsOpen ().ContinueWith (delegate {
if (analysisDocument != null)
StartReparseThread ();
});
}
/* void UpdateRegisteredDom (object sender, ProjectDomEventArgs e)
{
if (dom == null || dom.Project == null)
return;
var project = e.ITypeResolveContext != null ? e.ITypeResolveContext.Project : null;
if (project != null && project.FileName == dom.Project.FileName)
dom = e.ITypeResolveContext;
}*/
public FilePath FileName {
get {
if (Window == null || !Window.ViewContent.IsFile)
return null;
return Window.ViewContent.IsUntitled ? Window.ViewContent.UntitledName : Window.ViewContent.ContentName;
}
}
public bool IsFile {
get { return Window.ViewContent.IsFile; }
}
public bool IsDirty {
get { return !Window.ViewContent.IsViewOnly && (Window.ViewContent.ContentName == null || Window.ViewContent.IsDirty); }
set { Window.ViewContent.IsDirty = value; }
}
public object GetDocumentObject ()
{
return Window?.ViewContent?.GetDocumentObject ();
}
FilePath adHocFile;
Project adhocProject;
Solution adhocSolution;
public override Project Project {
get { return (Window != null ? Window.ViewContent.Project : null) ?? adhocProject; }
/* set {
Window.ViewContent.Project = value;
if (value != null)
singleFileContext = null;
// File needs to be in sync with the project, otherwise the parsed document at start may be invalid.
// better solution: create the document with the project attached.
StartReparseThread ();
}*/
}
internal override bool IsAdHocProject {
get { return adhocProject != null; }
}
public override bool IsCompileableInProject {
get {
var project = Project;
if (project == null)
return false;
var solution = project.ParentSolution;
if (solution != null && IdeApp.Workspace != null) {
var config = IdeApp.Workspace.ActiveConfiguration;
if (config != null) {
var sc = solution.GetConfiguration (config);
if (sc != null && !sc.BuildEnabledForItem (project))
return false;
}
}
var pf = project.GetProjectFile (FileName);
return pf != null && pf.BuildAction != BuildAction.None;
}
}
public Task<Microsoft.CodeAnalysis.Compilation> GetCompilationAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var project = TypeSystemService.GetCodeAnalysisProject (adhocProject ?? Project);
if (project == null)
return new Task<Microsoft.CodeAnalysis.Compilation> (() => null);
return project.GetCompilationAsync (cancellationToken);
}
public override ParsedDocument ParsedDocument {
get {
return parsedDocument;
}
}
public string PathRelativeToProject {
get { return Window.ViewContent.PathRelativeToProject; }
}
public void Select ()
{
window.SelectWindow ();
}
public DocumentView ActiveView {
get {
LoadViews (true);
return WrapView (window.ActiveViewContent);
}
}
public DocumentView PrimaryView {
get {
LoadViews (true);
return WrapView (window.ViewContent);
}
}
public ReadOnlyCollection<DocumentView> Views {
get {
LoadViews (true);
if (viewsRO == null)
viewsRO = new ReadOnlyCollection<DocumentView> (views);
return viewsRO;
}
}
ReadOnlyCollection<DocumentView> viewsRO;
List<DocumentView> views = new List<DocumentView> ();
void HandleViewsChanged (object sender, EventArgs e)
{
LoadViews (false);
}
void LoadViews (bool force)
{
if (!force && views == null)
return;
var newList = new List<DocumentView> ();
newList.Add (WrapView (window.ViewContent));
foreach (var v in window.SubViewContents)
newList.Add (WrapView (v));
views = newList;
viewsRO = null;
}
DocumentView WrapView (BaseViewContent content)
{
if (content == null)
return null;
if (views != null)
return views.FirstOrDefault (v => v.BaseContent == content) ?? new DocumentView (this, content);
else
return new DocumentView (this, content);
}
public override string Name {
get {
ViewContent view = Window.ViewContent;
return view.IsUntitled ? view.UntitledName : view.ContentName;
}
}
public TextEditor Editor {
get {
return GetContent <TextEditor> ();
}
}
public bool IsViewOnly {
get { return Window.ViewContent.IsViewOnly; }
}
public override bool IsUntitled {
get {
return Window.ViewContent.IsUntitled;
}
}
Task currentOperationTask = Task.FromResult (true);
Task RunAsyncOperation (Func<Task> action)
{
Runtime.AssertMainThread ();
return currentOperationTask = currentOperationTask.ContinueWith (t => action(), Runtime.MainTaskScheduler).Unwrap ();
}
public Task Reload ()
{
return RunAsyncOperation (ReloadTask);
}
async Task ReloadTask ()
{
ICustomXmlSerializer memento = null;
IMementoCapable mc = GetContent<IMementoCapable> ();
if (mc != null) {
memento = mc.Memento;
}
window.ViewContent.DiscardChanges ();
await window.ViewContent.Load (new FileOpenInformation (window.ViewContent.ContentName) { IsReloadOperation = true });
if (memento != null) {
mc.Memento = memento;
}
}
public Task Save ()
{
return RunAsyncOperation (SaveTask);
}
async Task SaveTask ()
{
// suspend type service "check all file loop" since we have already a parsed document.
// Or at least one that updates "soon".
try {
// Freeze the file change events. There can be several such events, and sending them all together
// is more efficient
FileService.FreezeEvents ();
if (Window.ViewContent.IsViewOnly || !Window.ViewContent.IsDirty)
return;
if (!Window.ViewContent.IsFile) {
await Window.ViewContent.Save ();
return;
}
if (IsUntitled) {
await SaveAs ();
} else {
try {
FileService.RequestFileEdit ((FilePath)Window.ViewContent.ContentName, true);
} catch (Exception ex) {
MessageService.ShowError (GettextCatalog.GetString ("The file could not be saved."), ex.Message, ex);
}
FileAttributes attr = FileAttributes.ReadOnly | FileAttributes.Directory | FileAttributes.Offline | FileAttributes.System;
if (!File.Exists ((string)Window.ViewContent.ContentName) || (File.GetAttributes ((string)window.ViewContent.ContentName) & attr) != 0) {
await SaveAs();
} else {
string fileName = Window.ViewContent.ContentName;
// save backup first
if (IdeApp.Preferences.CreateFileBackupCopies) {
await Window.ViewContent.Save (fileName + "~");
FileService.NotifyFileChanged (fileName + "~");
}
await Window.ViewContent.Save (fileName);
FileService.NotifyFileChanged (fileName);
OnSaved(EventArgs.Empty);
}
}
} finally {
// Send all file change notifications
FileService.ThawEvents ();
// Set the file time of the current document after the file time of the written file, to prevent double file updates.
// Note that the parsed document may be overwritten by a background thread to a more recent one.
var doc = parsedDocument;
if (doc != null) {
string fileName = Window.ViewContent.ContentName;
try {
// filename could be null if the user cancelled SaveAs and this is a new & unsaved file
if (fileName != null)
doc.LastWriteTimeUtc = File.GetLastWriteTimeUtc (fileName);
} catch (Exception e) {
doc.LastWriteTimeUtc = DateTime.UtcNow;
LoggingService.LogWarning ("Exception while getting the write time from " + fileName, e);
}
}
}
}
public Task<bool> SaveAs ()
{
return SaveAs (null);
}
public Task<bool> SaveAs (string filename)
{
return Runtime.RunInMainThread (() => SaveAsTask (filename));
}
async Task<bool> SaveAsTask (string filename)
{
if (Window.ViewContent.IsViewOnly || !Window.ViewContent.IsFile)
return false;
Encoding encoding = null;
var tbuffer = GetContent <ITextSource> ();
if (tbuffer != null) {
encoding = tbuffer.Encoding;
if (encoding == null)
encoding = Encoding.UTF8;
}
if (filename == null) {
var dlg = new OpenFileDialog (GettextCatalog.GetString ("Save as..."), MonoDevelop.Components.FileChooserAction.Save) {
TransientFor = IdeApp.Workbench.RootWindow,
Encoding = encoding,
ShowEncodingSelector = (tbuffer != null),
};
if (Window.ViewContent.IsUntitled)
dlg.InitialFileName = Window.ViewContent.UntitledName;
else {
dlg.CurrentFolder = Path.GetDirectoryName ((string)Window.ViewContent.ContentName);
dlg.InitialFileName = Path.GetFileName ((string)Window.ViewContent.ContentName);
}
if (!dlg.Run ())
return false;
filename = dlg.SelectedFile;
encoding = dlg.Encoding;
}
if (!FileService.IsValidPath (filename)) {
MessageService.ShowMessage (GettextCatalog.GetString ("File name {0} is invalid", filename));
return false;
}
// detect preexisting file
if (File.Exists (filename)) {
if (!MessageService.Confirm (GettextCatalog.GetString ("File {0} already exists. Overwrite?", filename), AlertButton.OverwriteFile))
return false;
}
// save backup first
if (IdeApp.Preferences.CreateFileBackupCopies) {
if (tbuffer != null && encoding != null)
TextFileUtility.WriteText (filename + "~", tbuffer.Text, encoding);
else
await Window.ViewContent.Save (new FileSaveInformation (filename + "~", encoding));
}
TypeSystemService.RemoveSkippedfile (FileName);
// do actual save
Window.ViewContent.ContentName = filename;
Window.ViewContent.Project = Workbench.GetProjectContainingFile (filename);
await Window.ViewContent.Save (new FileSaveInformation (filename, encoding));
DesktopService.RecentFiles.AddFile (filename, (Project)null);
OnSaved (EventArgs.Empty);
await UpdateParseDocument ();
return true;
}
public async Task<bool> Close ()
{
return await ((SdiWorkspaceWindow)Window).CloseWindow (false, true);
}
protected override void OnSaved (EventArgs e)
{
IdeApp.Workbench.SaveFileStatus ();
base.OnSaved (e);
}
public void CancelParseTimeout ()
{
lock (reparseTimeoutLock) {
var timeout = parseTimeout;
if (timeout != 0) {
GLib.Source.Remove (timeout);
parseTimeout = 0;
}
}
}
bool isClosed;
void OnClosed (object s, EventArgs a)
{
isClosed = true;
// TypeSystemService.DomRegistered -= UpdateRegisteredDom;
CancelParseTimeout ();
ClearTasks ();
TypeSystemService.RemoveSkippedfile (FileName);
try {
OnClosed (a);
} catch (Exception ex) {
LoggingService.LogError ("Exception while calling OnClosed.", ex);
}
// Parse the file when the document is closed. In this way if the document
// is closed without saving the changes, the saved compilation unit
// information will be restored
/* if (currentParseFile != null) {
TypeSystemService.QueueParseJob (dom, delegate (string name, IProgressMonitor monitor) {
TypeSystemService.Parse (curentParseProject, currentParseFile);
}, FileName);
}
if (isFileDom) {
TypeSystemService.RemoveFileDom (FileName);
dom = null;
}*/
Counters.OpenDocuments--;
}
internal void DisposeDocument ()
{
UnsubscribeAnalysisDocument ();
UnsubscribeRoslynWorkspace ();
UnloadAdhocProject ();
if (window is SdiWorkspaceWindow)
((SdiWorkspaceWindow)window).DetachFromPathedDocument ();
window.Closed -= OnClosed;
window.ActiveViewContentChanged -= OnActiveViewContentChanged;
if (IdeApp.Workspace != null)
IdeApp.Workspace.ItemRemovedFromSolution -= OnEntryRemoved;
// Unsubscribe project events
if (window.ViewContent.Project != null)
window.ViewContent.Project.Modified -= HandleProjectModified;
window.ViewsChanged += HandleViewsChanged;
MonoDevelopWorkspace.LoadingFinished -= TypeSystemService_WorkspaceItemLoaded;
window = null;
parsedDocument = null;
views = null;
viewsRO = null;
}
void UnsubscribeAnalysisDocument ()
{
if (analysisDocument != null) {
TypeSystemService.InformDocumentClose (analysisDocument, FileName);
analysisDocument = null;
}
}
#region document tasks
object lockObj = new object ();
void ClearTasks ()
{
lock (lockObj) {
TaskService.Errors.ClearByOwner (this);
}
}
// void CompilationUnitUpdated (object sender, ParsedDocumentEventArgs args)
// {
// if (this.FileName == args.FileName) {
//// if (!args.Unit.HasErrors)
// parsedDocument = args.ParsedDocument;
///* TODO: Implement better task update algorithm.
//
// ClearTasks ();
// lock (lockObj) {
// foreach (Error error in args.Unit.Errors) {
// tasks.Add (new Task (this.FileName, error.Message, error.Column, error.Line, error.ErrorType == ErrorType.Error ? TaskType.Error : TaskType.Warning, this.Project));
// }
// IdeApp.Services.TaskService.AddRange (tasks);
// }*/
// }
// }
#endregion
void OnActiveViewContentChanged (object s, EventArgs args)
{
OnViewChanged (args);
}
void OnClosed (EventArgs args)
{
if (Closed != null)
Closed (this, args);
}
void OnViewChanged (EventArgs args)
{
if (ViewChanged != null)
ViewChanged (this, args);
}
bool wasEdited;
void InitializeExtensionChain ()
{
Editor.InitializeExtensionChain (this);
if (window is SdiWorkspaceWindow)
((SdiWorkspaceWindow)window).AttachToPathedDocument (GetContent<MonoDevelop.Ide.Gui.Content.IPathedDocument> ());
}
void InitializeEditor ()
{
Editor.TextChanged += (o, a) => {
if (parsedDocument != null)
parsedDocument.IsInvalid = true;
if (Editor.IsInAtomicUndo) {
wasEdited = true;
} else {
StartReparseThread ();
}
};
Editor.BeginAtomicUndoOperation += delegate {
wasEdited = false;
};
Editor.EndAtomicUndoOperation += delegate {
if (wasEdited)
StartReparseThread ();
};
// Editor.Undone += (o, a) => StartReparseThread ();
// Editor.Redone += (o, a) => StartReparseThread ();
InitializeExtensionChain ();
}
internal void OnDocumentAttached ()
{
if (Editor != null) {
InitializeEditor ();
RunWhenRealized (delegate { ListenToProjectLoad (Project); });
}
window.Document = this;
}
/// <summary>
/// Performs an action when the content is loaded.
/// </summary>
/// <param name='action'>
/// The action to run.
/// </param>
public void RunWhenLoaded (System.Action action)
{
var e = Editor;
if (e == null) {
action ();
return;
}
e.RunWhenLoaded (action);
}
public void RunWhenRealized (System.Action action)
{
var e = Editor;
if (e == null) {
action ();
return;
}
e.RunWhenRealized (action);
}
public override void AttachToProject (Project project)
{
SetProject (project);
}
internal void SetProject (Project project)
{
if (Window == null || Window.ViewContent == null || Window.ViewContent.Project == project || project == adhocProject)
return;
UnloadAdhocProject ();
if (adhocProject == null)
UnsubscribeAnalysisDocument ();
// Unsubscribe project events
if (Window.ViewContent.Project != null)
Window.ViewContent.Project.Modified -= HandleProjectModified;
Window.ViewContent.Project = project;
if (project != null)
project.Modified += HandleProjectModified;
InitializeExtensionChain ();
ListenToProjectLoad (project);
}
void ListenToProjectLoad (Project project)
{
StartReparseThread ();
}
void HandleInLoadChanged (object sender, EventArgs e)
{
StartReparseThread ();
}
void HandleProjectModified (object sender, SolutionItemModifiedEventArgs e)
{
if (!e.Any (x => x.Hint == "TargetFramework" || x.Hint == "References"))
return;
StartReparseThread ();
}
/// <summary>
/// This method can take some time to finish. It's not threaded
/// </summary>
/// <returns>
/// A <see cref="ParsedDocument"/> that contains the current dom.
/// </returns>
public override async Task<ParsedDocument> UpdateParseDocument ()
{
try {
await EnsureAnalysisDocumentIsOpen ();
string currentParseFile = GetCurrentParseFileName();
var editor = Editor;
if (editor == null || string.IsNullOrEmpty (currentParseFile))
return null;
TypeSystemService.AddSkippedFile (currentParseFile);
var currentParseText = editor.CreateDocumentSnapshot ();
CancelOldParsing();
var project = adhocProject ?? Project;
var options = new ParseOptions {
Project = project,
Content = currentParseText,
FileName = currentParseFile,
OldParsedDocument = parsedDocument,
RoslynDocument = AnalysisDocument
};
if (project != null && TypeSystemService.CanParseProjections (project, Editor.MimeType, FileName)) {
var projectFile = project.GetProjectFile (currentParseFile);
if (projectFile != null)
options.BuildAction = projectFile.BuildAction;
var p = await TypeSystemService.ParseProjection (options, editor.MimeType);
if (p != null) {
this.parsedDocument = p.ParsedDocument;
var projections = p.Projections;
foreach (var p2 in projections)
p2.CreateProjectedEditor (this);
Editor.SetOrUpdateProjections (this, projections, p.DisabledProjectionFeatures);
}
} else {
this.parsedDocument = await TypeSystemService.ParseFile (options, editor.MimeType) ?? this.parsedDocument;
}
} finally {
OnDocumentParsed (EventArgs.Empty);
}
return this.parsedDocument;
}
uint parseTimeout = 0;
CancellationTokenSource analysisDocumentSrc = new CancellationTokenSource ();
void CancelEnsureAnalysisDocumentIsOpen ()
{
analysisDocumentSrc.Cancel ();
analysisDocumentSrc = new CancellationTokenSource ();
}
Task EnsureAnalysisDocumentIsOpen ()
{
if (analysisDocument != null) {
Microsoft.CodeAnalysis.Document doc;
try {
doc = RoslynWorkspace.CurrentSolution.GetDocument (analysisDocument);
} catch (Exception) {
doc = null;
}
if (doc != null)
return Task.CompletedTask;
}
if (Editor == null) {
UnsubscribeAnalysisDocument ();
return Task.CompletedTask;
}
if (Project != null && !IsUnreferencedSharedProject(Project)) {
UnsubscribeRoslynWorkspace ();
RoslynWorkspace = TypeSystemService.GetWorkspace (this.Project.ParentSolution);
if (RoslynWorkspace == null) // Solution not loaded yet
return Task.CompletedTask;
SubscribeRoslynWorkspace ();
analysisDocument = FileName != null ? TypeSystemService.GetDocumentId (this.Project, this.FileName) : null;
if (analysisDocument != null) {
TypeSystemService.InformDocumentOpen (analysisDocument, Editor);
return Task.CompletedTask;
}
}
lock (adhocProjectLock) {
var token = analysisDocumentSrc.Token;
if (adhocProject != null) {
return Task.CompletedTask;
}
if (Editor != null) {
var node = TypeSystemService.GetTypeSystemParserNode (Editor.MimeType, BuildAction.Compile);
if (Editor.MimeType == "text/x-csharp" || node?.Parser.CanGenerateAnalysisDocument (Editor.MimeType, BuildAction.Compile, new string[0]) == true) {
var newProject = Services.ProjectService.CreateDotNetProject ("C#");
this.adhocProject = newProject;
newProject.Name = "InvisibleProject";
newProject.References.Add (ProjectReference.CreateAssemblyReference ("mscorlib"));
newProject.References.Add (ProjectReference.CreateAssemblyReference ("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
newProject.References.Add (ProjectReference.CreateAssemblyReference ("System.Core"));
newProject.FileName = "test.csproj";
if (!Window.ViewContent.IsUntitled) {
adHocFile = Editor.FileName;
} else {
adHocFile = (Platform.IsWindows ? "C:\\" : "/") + Window.ViewContent.UntitledName + ".cs";
}
newProject.Files.Add (new ProjectFile (adHocFile, BuildAction.Compile));
adhocSolution = new Solution ();
adhocSolution.AddConfiguration ("", true);
adhocSolution.DefaultSolutionFolder.AddItem (newProject);
return TypeSystemService.Load (adhocSolution, new ProgressMonitor (), token, false).ContinueWith (task => {
if (token.IsCancellationRequested)
return;
UnsubscribeRoslynWorkspace ();
RoslynWorkspace = task.Result.FirstOrDefault (); // 1 solution loaded ->1 workspace as result
SubscribeRoslynWorkspace ();
analysisDocument = RoslynWorkspace.CurrentSolution.Projects.First ().DocumentIds.First ();
TypeSystemService.InformDocumentOpen (RoslynWorkspace, analysisDocument, Editor);
});
}
}
}
return Task.CompletedTask;
}
void UnsubscribeRoslynWorkspace ()
{
var ws = RoslynWorkspace as MonoDevelopWorkspace;
if (ws != null) {
ws.ProjectReloaded -= HandleRoslynProjectReload;
}
}
void SubscribeRoslynWorkspace ()
{
var ws = RoslynWorkspace as MonoDevelopWorkspace;
if (ws != null) {
ws.ProjectReloaded += HandleRoslynProjectReload;
}
}
void HandleRoslynProjectReload (object sender, RoslynProjectEventArgs e)
{
StartReparseThread ();
}
bool IsUnreferencedSharedProject (Project project)
{
return project is SharedAssetsProject;
}
object adhocProjectLock = new object();
void UnloadAdhocProject ()
{
CancelEnsureAnalysisDocumentIsOpen ();
lock (adhocProjectLock) {
if (adhocProject == null)
return;
if (adhocSolution != null) {
TypeSystemService.Unload (adhocSolution);
adhocSolution.Dispose ();
adhocSolution = null;
}
adhocProject = null;
}
}
CancellationTokenSource parseTokenSource = new CancellationTokenSource();
void CancelOldParsing()
{
parseTokenSource.Cancel ();
parseTokenSource = new CancellationTokenSource ();
}
object reparseTimeoutLock = new object ();
internal void StartReparseThread ()
{
RunWhenRealized (() => {
string currentParseFile = GetCurrentParseFileName ();
if (string.IsNullOrEmpty (currentParseFile))
return;
lock (reparseTimeoutLock) {
CancelParseTimeout ();
parseTimeout = GLib.Timeout.Add (ParseDelay, delegate {
StartReparseThreadDelayed (currentParseFile);
parseTimeout = 0;
return false;
});
}
});
}
string GetCurrentParseFileName ()
{
var editor = Editor;
string result = adhocProject != null ? adHocFile : editor?.FileName;
return result ?? FileName;
}
async void StartReparseThreadDelayed (FilePath currentParseFile)
{
var editor = Editor;
if (editor == null)
return;
// Don't directly parse the document because doing it at every key press is
// very inefficient. Do it after a small delay instead, so several changes can
// be parsed at the same time.
await EnsureAnalysisDocumentIsOpen ();
var currentParseText = editor.CreateSnapshot ();
string mimeType = editor.MimeType;
CancelOldParsing ();
var token = parseTokenSource.Token;
var project = adhocProject ?? Project;
var projectFile = project?.GetProjectFile (currentParseFile);
ThreadPool.QueueUserWorkItem (delegate {
TypeSystemService.AddSkippedFile (currentParseFile);
var options = new ParseOptions {
Project = project,
Content = currentParseText,
FileName = currentParseFile,
OldParsedDocument = parsedDocument,
RoslynDocument = AnalysisDocument
};
if (projectFile != null)
options.BuildAction = projectFile.BuildAction;
if (project != null && TypeSystemService.CanParseProjections (project, mimeType, currentParseFile)) {
TypeSystemService.ParseProjection (options, mimeType, token).ContinueWith (task => {
if (token.IsCancellationRequested)
return;
Application.Invoke (delegate {
// this may be called after the document has closed, in that case the OnDocumentParsed event shouldn't be invoked.
var taskResult = task.Result;
if (isClosed || taskResult == null || token.IsCancellationRequested)
return;
this.parsedDocument = taskResult.ParsedDocument;
var projections = taskResult.Projections;
foreach (var p2 in projections)
p2.CreateProjectedEditor (this);
Editor.SetOrUpdateProjections (this, projections, taskResult.DisabledProjectionFeatures);
OnDocumentParsed (EventArgs.Empty);
});
}, TaskContinuationOptions.OnlyOnRanToCompletion);
} else {
TypeSystemService.ParseFile (options, mimeType, token).ContinueWith (task => {
if (token.IsCancellationRequested)
return;
Application.Invoke (delegate {
// this may be called after the document has closed, in that case the OnDocumentParsed event shouldn't be invoked.
if (isClosed || task.Result == null || token.IsCancellationRequested)
return;
this.parsedDocument = task.Result;
OnDocumentParsed (EventArgs.Empty);
});
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
});
}
/// <summary>
/// This method kicks off an async document parser and should be used instead of
/// <see cref="UpdateParseDocument"/> unless you need the parsed document immediately.
/// </summary>
public override void ReparseDocument ()
{
StartReparseThread ();
}
void OnEntryRemoved (object sender, SolutionItemEventArgs args)
{
if (args.SolutionItem == window.ViewContent.Project)
window.ViewContent.Project = null;
}
public event EventHandler Closed;
public event EventHandler ViewChanged;
public string[] CommentTags {
get {
if (IsFile)
return GetCommentTags (FileName);
else
return null;
}
}
public static string[] GetCommentTags (string fileName)
{
//Document doc = IdeApp.Workbench.ActiveDocument;
var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (fileName));
if (lang.LineComments.Count > 0)
return lang.LineComments.ToArray ();
if (lang.BlockComments.Count> 0)
return new [] { lang.BlockComments[0].Item1, lang.BlockComments[0].Item2 };
return null;
}
public override T GetPolicy<T> (IEnumerable<string> types)
{
if (adhocProject != null)
return MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<T> (types);
return base.GetPolicy<T> (types);
}
// public MonoDevelop.Projects.CodeGeneration.CodeGenerator CreateCodeGenerator ()
// {
// return MonoDevelop.Projects.CodeGeneration.CodeGenerator.CreateGenerator (Editor.Document.MimeType,
// Editor.Options.TabsToSpaces, Editor.Options.TabSize, Editor.EolMarker);
// }
/// <summary>
/// If the document shouldn't restore the settings after the load it can be disabled with this method.
/// That is useful when opening a document and programmatically scrolling to a specified location.
/// </summary>
public void DisableAutoScroll ()
{
if (IsFile)
FileSettingsStore.Remove (FileName);
}
public override OptionSet GetOptionSet ()
{
return TypeSystemService.Workspace.Options;
}
internal override Task<IReadOnlyList<Editor.Projection.Projection>> GetPartialProjectionsAsync (CancellationToken cancellationToken = default(CancellationToken))
{
var parser = TypeSystemService.GetParser (Editor.MimeType);
if (parser == null)
return null;
var projectFile = Project.GetProjectFile (Editor.FileName);
if (projectFile == null)
return null;
if (!parser.CanGenerateProjection (Editor.MimeType, projectFile.BuildAction, Project.SupportedLanguages))
return null;
try {
return parser.GetPartialProjectionsAsync (this, Editor, parsedDocument, cancellationToken);
} catch (NotSupportedException) {
return null;
}
}
}
[Serializable]
public sealed class DocumentEventArgs : EventArgs
{
public Document Document {
get;
set;
}
public DocumentEventArgs (Document document)
{
this.Document = document;
}
}
}
|