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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
|
//
// MonoDevelopWorkspace.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 Microsoft.CodeAnalysis;
using System.Linq;
using System.IO;
using MonoDevelop.Core;
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.Text;
using System.Threading.Tasks;
using MonoDevelop.Ide.Editor;
using Microsoft.CodeAnalysis.Host;
using MonoDevelop.Core.Text;
using MonoDevelop.Ide.CodeFormatting;
using MonoDevelop.Ide.Editor.Projection;
using System.ComponentModel;
using Mono.Addins;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.SolutionCrawler;
using MonoDevelop.Ide.Composition;
using MonoDevelop.Ide.Projects.FileNesting;
using MonoDevelop.Ide.RoslynServices;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Ide.Gui.Documents;
using MonoDevelop.Ide.Gui;
using Document = Microsoft.CodeAnalysis.Document;
using System.Runtime.CompilerServices;
namespace MonoDevelop.Ide.TypeSystem
{
public partial class MonoDevelopWorkspace : Workspace
{
public const string ServiceLayer = nameof(MonoDevelopWorkspace);
private readonly ServiceProvider serviceProvider;
TypeSystemService typeSystemService;
DesktopService desktopService;
DocumentManager documentManager;
RootWorkspace workspace;
CompositionManager compositionManager;
DynamicFileManager dynamicFileManager;
// Background compiler is used to trigger compilations in the background for the solution and hold onto them
// so in case nothing references the solution in current stacks, they're not collected.
// We previously used to experience pathological GC times on large solutions, and this was caused
// by the compilations being freed out of memory due to only being weakly referenced, and recomputing them on
// a case by case basis.
BackgroundCompiler backgroundCompiler;
// Background parser is an optimized task queue for the roslyn use-case, where a parse that's already in-progress
// is not canceled, but used later on to help incremental parsing.
BackgroundParser backgroundParser;
internal readonly WorkspaceId Id;
CancellationTokenSource src = new CancellationTokenSource ();
bool disposed;
internal readonly SemaphoreSlim LoadLock = new SemaphoreSlim (1, 1);
Lazy<MonoDevelopMetadataReferenceManager> manager;
Lazy<MetadataReferenceHandler> metadataHandler;
ProjectionData Projections { get; }
OpenDocumentsData OpenDocuments { get; }
internal ProjectDataMap ProjectMap { get; }
ProjectSystemHandler ProjectHandler { get; }
public MonoDevelop.Projects.Solution MonoDevelopSolution { get; private set; }
internal MonoDevelopMetadataReferenceManager MetadataReferenceManager => manager.Value;
static MonoDevelopWorkspace ()
{
Tasks.CommentTasksProvider.Initialize ();
}
/// <summary>
/// This bypasses the type system service. Use with care.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal void OpenSolutionInfo (SolutionInfo sInfo)
{
OnSolutionAdded (sInfo);
}
internal MonoDevelopWorkspace (HostServices hostServices, MonoDevelop.Projects.Solution solution, TypeSystemService typeSystemService) : base (hostServices, WorkspaceKind.Host)
{
this.MonoDevelopSolution = solution;
this.serviceProvider = typeSystemService.ServiceProvider ?? Runtime.ServiceProvider;
this.typeSystemService = typeSystemService;
this.Id = WorkspaceId.Next ();
Projections = new ProjectionData ();
OpenDocuments = new OpenDocumentsData ();
ProjectMap = new ProjectDataMap (this);
ProjectHandler = new ProjectSystemHandler (this, ProjectMap, Projections);
manager = new Lazy<MonoDevelopMetadataReferenceManager> (() => Services.GetService<MonoDevelopMetadataReferenceManager> ());
metadataHandler = new Lazy<MetadataReferenceHandler> (() => new MetadataReferenceHandler (MetadataReferenceManager, ProjectMap));
}
internal async Task Initialize ()
{
serviceProvider.WhenServiceInitialized<RootWorkspace> (s => {
workspace = s;
if (MonoDevelopSolution != null)
workspace.ActiveConfigurationChanged += HandleActiveConfigurationChanged;
});
backgroundCompiler = new BackgroundCompiler (this);
backgroundParser = new BackgroundParser (this);
backgroundParser.Start ();
var cacheService = Services.GetService<IWorkspaceCacheService> ();
if (cacheService != null)
cacheService.CacheFlushRequested += OnCacheFlushRequested;
// Trigger running compiler syntax and semantic errors via the diagnostic analyzer engine
TypeSystemService.Preferences.FullSolutionAnalysisRuntimeEnabled = true;
Options = Options.WithChangedOption (Microsoft.CodeAnalysis.Diagnostics.InternalRuntimeDiagnosticOptions.Syntax, true)
.WithChangedOption (Microsoft.CodeAnalysis.Diagnostics.InternalRuntimeDiagnosticOptions.Semantic, true)
// Turn on FSA on a new workspace addition
.WithChangedOption (RuntimeOptions.FullSolutionAnalysis, true)
.WithChangedOption (RuntimeOptions.FullSolutionAnalysisInfoBarShown, false)
// Always use persistent storage regardless of solution size, at least until a consensus is reached
// https://github.com/mono/monodevelop/issues/4149 https://github.com/dotnet/roslyn/issues/25453
.WithChangedOption (Microsoft.CodeAnalysis.Storage.StorageOptions.SolutionSizeThreshold, MonoDevelop.Core.Platform.IsLinux ? int.MaxValue : 0);
if (TypeSystemService.EnableSourceAnalysis) {
var solutionCrawler = Services.GetService<ISolutionCrawlerRegistrationService> ();
solutionCrawler.Register (this);
}
TypeSystemService.EnableSourceAnalysis.Changed += OnEnableSourceAnalysisChanged;
// TODO: Unhack C# here when monodevelop workspace supports more than C#
TypeSystemService.Preferences.FullSolutionAnalysisRuntimeEnabledChanged += OnEnableFullSourceAnalysisChanged;
foreach (var factory in AddinManager.GetExtensionObjects<Microsoft.CodeAnalysis.Options.IDocumentOptionsProviderFactory>("/MonoDevelop/Ide/TypeService/OptionProviders"))
Services.GetRequiredService<Microsoft.CodeAnalysis.Options.IOptionService> ().RegisterDocumentOptionsProvider (factory.TryCreate (this));
desktopService = await serviceProvider.GetService<DesktopService> ().ConfigureAwait (false);
documentManager = await serviceProvider.GetService<DocumentManager> ().ConfigureAwait (false);
compositionManager = await serviceProvider.GetService<CompositionManager> ().ConfigureAwait (false);
if (MonoDevelopSolution != null) {
Runtime.RunInMainThread (() => desktopService.MemoryMonitor.StatusChanged += OnMemoryStatusChanged).Ignore ();
}
this.dynamicFileManager = compositionManager.GetExportedValue<DynamicFileManager> ();
}
bool lowMemoryLogged;
void OnMemoryStatusChanged (object sender, PlatformMemoryStatusEventArgs args)
{
// Disable full solution analysis when the OS triggers a warning about memory pressure.
if (args.MemoryStatus == PlatformMemoryStatus.Normal)
return;
// record that we had hit critical memory barrier
if (!lowMemoryLogged) {
lowMemoryLogged = true;
Logger.Log (FunctionId.VirtualMemory_MemoryLow, KeyValueLogMessage.Create (m => {
// which message we are logging and memory left in bytes when this is called.
m ["MSG"] = args.MemoryStatus;
//m ["MemoryLeft"] = (long)wParam;
}));
}
if (!ShouldTurnOffFullSolutionAnalysis ())
return;
Options = Options.WithChangedOption (RuntimeOptions.FullSolutionAnalysis, false);
TypeSystemService.Preferences.FullSolutionAnalysisRuntimeEnabled = false;
if (IsUserOptionOn ()) {
// let user know full analysis is turned off due to memory concern.
// make sure we show info bar only once for the same solution.
Options = Options.WithChangedOption (RuntimeOptions.FullSolutionAnalysisInfoBarShown, true);
const string LowVMMoreInfoLink = "https://go.microsoft.com/fwlink/?linkid=2003417&clcid=0x409";
Services.GetService<IErrorReportingService> ().ShowGlobalErrorInfo (
GettextCatalog.GetString ("{0} has suspended some advanced features to improve performance", BrandingService.ApplicationName),
new InfoBarUI ("Learn more", InfoBarUI.UIKind.HyperLink, () => desktopService.ShowUrl (LowVMMoreInfoLink), closeAfterAction: false),
new InfoBarUI ("Restore", InfoBarUI.UIKind.Button, () => Options = Options.WithChangedOption (RuntimeOptions.FullSolutionAnalysis, true))
);
}
}
void OnCacheFlushRequested (object sender, EventArgs args)
{
if (backgroundCompiler != null) {
backgroundCompiler.Dispose ();
backgroundCompiler = null; // PartialSemanticsEnabled will now return false
}
// No longer need cache notifications
var cacheService = Services.GetService<IWorkspaceCacheService> ();
if (cacheService != null)
cacheService.CacheFlushRequested -= OnCacheFlushRequested;
}
bool ShouldTurnOffFullSolutionAnalysis ()
{
// conditions
// 1. if our full solution analysis option is on (not user full solution analysis option, but our internal one) and
// 2. if infobar is never shown to users for this solution
return Options.GetOption (RuntimeOptions.FullSolutionAnalysis) && !Options.GetOption (RuntimeOptions.FullSolutionAnalysisInfoBarShown);
}
bool IsUserOptionOn ()
{
// check languages currently on solution. since we only show info bar once, we don't need to track solution changes.
var languages = CurrentSolution.Projects.Select (p => p.Language).Distinct ();
foreach (var language in languages) {
if (TypeSystemService.Preferences.For (language).SolutionCrawlerClosedFileDiagnostic) {
return true;
}
}
return false;
}
void OnEnableSourceAnalysisChanged(object sender, EventArgs args)
{
var solutionCrawler = Services.GetService<ISolutionCrawlerRegistrationService> ();
if (TypeSystemService.EnableSourceAnalysis)
solutionCrawler.Register (this);
else
solutionCrawler.Unregister (this);
var diagnosticAnalyzer = compositionManager.GetExportedValue<Microsoft.CodeAnalysis.Diagnostics.IDiagnosticAnalyzerService> ();
diagnosticAnalyzer.Reanalyze (this);
}
void OnEnableFullSourceAnalysisChanged (object sender, EventArgs args)
{
// we only want to turn on FSA if the option is explicitly enabled,
// we don't want to turn it off here.
if (TypeSystemService.Preferences.FullSolutionAnalysisRuntimeEnabled) {
Options = Options.WithChangedOption (RuntimeOptions.FullSolutionAnalysis, true);
}
}
protected internal override bool PartialSemanticsEnabled => backgroundCompiler != null;
// This is called by OnSolutionRemoved and on Dispose.
protected override void ClearSolutionData ()
{
if (MonoDevelopSolution != null) {
foreach (var prj in MonoDevelopSolution.GetAllProjects ()) {
ProjectMap.RemoveProject (prj);
UnloadMonoProject (prj);
}
}
dynamicFileManager?.UnloadWorkspace (this);
base.ClearSolutionData ();
}
/// <summary>
/// Stores the additional C# buffers added to the workspace by Razor. These usually
/// have the .cshtml.g.cs extension and don't exist on disk.
/// We need to keep track of these separately so that when we reload an ASP.NET project
/// we re-add these manually as they do not come from the project system.
/// See https://devdiv.visualstudio.com/DevDiv/_workitems/edit/889145
/// </summary>
readonly HashSet<DocumentInfo> virtualDocuments = new HashSet<DocumentInfo> ();
/// <summary>
/// Used by WebTools to add a C# buffer from .cshtml as a "file"
/// to the workspace while .cshtml is open
/// </summary>
internal void AddDocument(DocumentInfo documentInfo)
{
lock (virtualDocuments) {
virtualDocuments.Add (documentInfo);
}
OnDocumentAdded (documentInfo);
}
/// <summary>
/// Used by WebTools to remove a C# document from the workspace
/// when the .cshtml file is closed
/// </summary>
internal void RemoveDocument(DocumentId documentId)
{
lock (virtualDocuments) {
virtualDocuments.RemoveWhere (d => d.Id == documentId);
}
OnDocumentRemoved (documentId);
}
/// <summary>
/// Razor (.cshtml) needs to be able to add C# documents to a project that are not backed by a file on disk.
/// As these don't come from the project system, we need to keep track of these documents to readd them
/// manually every time the project is loaded from disk.
/// </summary>
internal IEnumerable<DocumentInfo> GetVirtualDocuments (ProjectId projectId)
{
lock (virtualDocuments) {
return virtualDocuments.Where (d => d.Id.ProjectId == projectId).ToList ();
}
}
// This is called by OnProjectRemoved.
protected override void ClearProjectData (ProjectId projectId)
{
var actualProject = ProjectMap.RemoveProject (projectId);
UnloadMonoProject (actualProject);
dynamicFileManager?.UnloadProject (projectId);
base.ClearProjectData (projectId);
}
protected override void Dispose (bool finalize)
{
if (disposed)
return;
disposed = true;
CancelLoad ();
var cacheService = Services.GetService<IWorkspaceCacheService> ();
if (cacheService != null)
cacheService.CacheFlushRequested -= OnCacheFlushRequested;
var cacheHostService = Services.GetService<IProjectCacheHostService> () as IDisposable;
cacheHostService?.Dispose ();
ProjectHandler.Dispose ();
MetadataReferenceManager.ClearCache ();
dynamicFileManager?.UnloadWorkspace (this);
TypeSystemService.EnableSourceAnalysis.Changed -= OnEnableSourceAnalysisChanged;
TypeSystemService.Preferences.FullSolutionAnalysisRuntimeEnabledChanged -= OnEnableFullSourceAnalysisChanged;
if (desktopService != null)
desktopService.MemoryMonitor.StatusChanged -= OnMemoryStatusChanged;
if (workspace != null) {
workspace.ActiveConfigurationChanged -= HandleActiveConfigurationChanged;
}
var solutionCrawler = Services.GetService<ISolutionCrawlerRegistrationService> ();
solutionCrawler.Unregister (this);
if (backgroundCompiler != null) {
backgroundCompiler.Dispose ();
backgroundCompiler = null; // PartialSemanticsEnabled will now return false
}
base.Dispose (finalize);
// Do this at the end so solution removal from base disposal is done properly.
MonoDevelopSolution = null;
}
internal void InformDocumentTextChange (DocumentId id, SourceText text)
{
base.ApplyDocumentTextChanged (id, text);
}
void CancelLoad ()
{
src.Cancel ();
src = new CancellationTokenSource ();
}
public event EventHandler WorkspaceLoaded;
internal static event EventHandler LoadingFinished;
void BeginLoad ()
{
typeSystemService.BeginWorkspaceLoad ();
}
void EndLoad ()
{
typeSystemService.EndWorkspaceLoad (() => {
LoadingFinished?.Invoke (this, EventArgs.Empty);
WorkspaceLoaded?.Invoke (this, EventArgs.Empty);
});
}
async void HandleActiveConfigurationChanged (object sender, EventArgs e)
{
CancelLoad ();
var token = src.Token;
try {
BeginLoad ();
await Task.Run (async () => {
ProjectHandler.ReloadProjectCache ();
var (solution, si) = await InternalLoadSolution (token).ConfigureAwait (false);
if (si != null)
OnSolutionReloaded (si);
});
} catch (OperationCanceledException) {
} catch (AggregateException ae) {
ae.Flatten ().Handle (x => x is OperationCanceledException);
} catch (Exception ex) {
LoggingService.LogError ("Error while reloading solution.", ex);
} finally {
EndLoad ();
}
}
internal void ReloadModifiedProject (MonoDevelop.Projects.Project project)
{
ProjectHandler.ReloadModifiedProject (project);
}
async Task<(MonoDevelop.Projects.Solution, SolutionInfo)> TryLoadSolution (CancellationToken cancellationToken = default(CancellationToken))
{
using (var cts = CancellationTokenSource.CreateLinkedTokenSource (cancellationToken, src.Token)) {
return await ProjectHandler.CreateSolutionInfo (MonoDevelopSolution, cts.Token);
}
}
async Task<(MonoDevelop.Projects.Solution, SolutionInfo)> TryLoadSolutionFromCache (CancellationToken cancellationToken)
{
using (var cts = CancellationTokenSource.CreateLinkedTokenSource (cancellationToken, src.Token)) {
return await ProjectHandler.CreateSolutionInfoFromCache (MonoDevelopSolution, cts.Token);
}
}
internal async Task<(MonoDevelop.Projects.Solution, SolutionInfo)> LoadSolution (CancellationToken cancellationToken)
{
try {
BeginLoad ();
return await InternalLoadSolution (cancellationToken).ConfigureAwait (false);
} finally {
EndLoad ();
}
}
async Task<(MonoDevelop.Projects.Solution, SolutionInfo)> InternalLoadSolution (CancellationToken cancellationToken)
{
// Try the cache first.
var (solution, solutionInfo) = await TryLoadSolutionFromCache (cancellationToken).ConfigureAwait (false);
if (solutionInfo != null) {
// Start full load of projects in the background.
ReloadProjects (cancellationToken).Ignore ();
return (solution, solutionInfo);
}
// No cache.
await TypeSystemService.SafeFreezeLoad ().ConfigureAwait (false);
if (cancellationToken.IsCancellationRequested)
return (solution, null);
return await TryLoadSolution (cancellationToken).ConfigureAwait (false);
}
async Task ReloadProjects (CancellationToken cancellationToken)
{
try {
using var cts = CancellationTokenSource.CreateLinkedTokenSource (cancellationToken, src.Token);
await TypeSystemService.SafeFreezeLoad ().ConfigureAwait (false);
if (cancellationToken.IsCancellationRequested)
return;
foreach (var project in GetProjectsOrderedByMostRecentlyUsed ()) {
if (cts.IsCancellationRequested)
return;
if (!ProjectHandler.CanLoadProject (project))
continue;
foreach (string framework in MonoDevelopWorkspace.GetFrameworks (project)) {
var projectInfo = await ProjectHandler.LoadProjectIfCacheOutOfDate (project, framework, cts.Token).ConfigureAwait (false);
if (projectInfo == null)
continue;
if (!CurrentSolution.ContainsProject (projectInfo.Id)) {
// Cache did not contain project so add it to the solution.
OnProjectAdded (projectInfo);
} else {
lock (projectModifyLock) {
OnProjectReloaded (projectInfo);
}
}
}
await Runtime.RunInMainThread (IdeServices.TypeSystemService.UpdateRegisteredOpenDocuments).ConfigureAwait (false);
}
} catch (Exception ex) {
LoggingService.LogInternalError (ex);
}
}
/// <summary>
/// Returns an ordered list of projects with the most recently used projects first.
/// </summary>
IEnumerable<MonoDevelop.Projects.Project> GetProjectsOrderedByMostRecentlyUsed ()
{
var projects = MonoDevelopSolution.GetAllProjects ();
var userPrefs = MonoDevelopSolution.UserProperties.GetValue<Gui.WorkbenchUserPrefs> ("MonoDevelop.Ide.Workbench");
if (userPrefs == null || userPrefs.Files.Count == 0)
return projects;
var set = new HashSet<MonoDevelop.Projects.Project> (projects);
var orderedProjects = new List<MonoDevelop.Projects.Project> (set.Count);
foreach (var documentUserPrefs in userPrefs.Files) {
var fileName = MonoDevelopSolution.BaseDirectory.Combine (documentUserPrefs.FileName);
foreach (var project in MonoDevelopSolution.GetProjectsContainingFile (fileName)) {
if (set.Remove (project)) {
orderedProjects.Add (project);
}
}
}
orderedProjects.AddRange (set);
return orderedProjects;
}
internal void UnloadSolution ()
{
OnSolutionRemoved ();
}
void UnloadMonoProject (MonoDevelop.Projects.Project project)
{
if (project == null)
throw new ArgumentNullException (nameof (project));
project.Modified -= OnProjectModified;
}
#region Open documents
public override bool CanOpenDocuments => true;
public override void OpenDocument (DocumentId documentId, bool activate = true)
{
// Roslyn also expects this to be true.
Runtime.AssertMainThread ();
var doc = GetDocument (documentId);
if (doc == null)
return;
var mdProject = GetMonoProject (doc.Project);
if (mdProject == null)
return;
// This method can be called by Roslyn or the editor in a context which is not the GTK UI context
// that MonoDevelop uses. In that case, before starting async an operation that may queue
// task continuations into the current context, we switch to the GTK context, so that
// whatever is queued will be dispatched when we run RunPendingEvents.
var oldContext = SynchronizationContext.Current;
try {
SynchronizationContext.SetSynchronizationContext (Runtime.MainSynchronizationContext);
var task = OpenDocumentWithTextViewAsync (doc, mdProject, activate);
// Can't wait for the task to finish synchronously since doing so would deadlock the UI thread.
while (!task.IsCompleted) {
DispatchService.RunPendingEvents (30);
}
} finally {
SynchronizationContext.SetSynchronizationContext (oldContext);
}
}
async Task OpenDocumentWithTextViewAsync (Document doc, MonoDevelop.Projects.Project mdProject, bool activate)
{
var openTask = new TaskCompletionSource<bool> ();
var shellDoc = await IdeServices.DocumentManager.OpenDocument (new FileOpenInformation (doc.FilePath, mdProject, activate));
shellDoc.RunWhenContentAdded<Microsoft.VisualStudio.Text.Editor.ITextView> (v => {
openTask.SetResult (true);
});
// Wait for the ITextView with a timeout, since the document may not show a text view at all
await Task.WhenAny (openTask.Task, Task.Delay (1000));
}
internal void InformDocumentOpen (DocumentId documentId, SourceTextContainer sourceTextContainer)
{
var document = InternalInformDocumentOpen (documentId, sourceTextContainer, true);
if (document is Document doc) {
foreach (var linkedDoc in doc.GetLinkedDocumentIds ()) {
InternalInformDocumentOpen (linkedDoc, sourceTextContainer, false);
}
}
}
TextDocument InternalInformDocumentOpen (DocumentId documentId, SourceTextContainer sourceTextContainer, bool isCurrentContext)
{
var project = this.CurrentSolution.GetProject (documentId.ProjectId);
if (project == null)
return null;
TextDocument document = project.GetDocument (documentId) ??
project.GetAdditionalDocument (documentId) ??
project.GetAnalyzerConfigDocument (documentId);
if (document == null || OpenDocuments.Contains (documentId)) {
return document;
}
OpenDocuments.Add (documentId, sourceTextContainer);
if (document is Document) {
OnDocumentOpened (documentId, sourceTextContainer, isCurrentContext);
} else if (document is AdditionalDocument) {
OnAdditionalDocumentOpened (documentId, sourceTextContainer, isCurrentContext);
} else if (document is AnalyzerConfigDocument) {
OnAnalyzerConfigDocumentOpened (documentId, sourceTextContainer, isCurrentContext);
}
return document;
}
ProjectChanges projectChanges;
protected override void OnDocumentClosing (DocumentId documentId)
{
base.OnDocumentClosing (documentId);
OpenDocuments.Remove (documentId);
backgroundParser.CancelParse (documentId);
}
// internal override bool CanChangeActiveContextDocument {
// get {
// return true;
// }
// }
internal void InformDocumentClose (DocumentId analysisDocument, SourceTextContainer container)
{
try {
if (!OpenDocuments.Remove (analysisDocument)) {
//Apparently something else opened this file via AddAndOpenDocumentInternal(e.g. .cshtml)
//it's job of whatever opened to also call CloseAndemoveDocumentInternal
return;
}
// Using a source text container
var loader = new SourceTextLoader (container, null);
var document = this.GetDocument (analysisDocument);
if (document == null) {
var ad = this.GetAdditionalDocument (analysisDocument);
if (ad != null)
OnAdditionalDocumentClosed (analysisDocument, loader);
var analyzerDoc = this.GetAnalyzerConfigDocument (analysisDocument);
if (analyzerDoc != null)
OnAnalyzerConfigDocumentClosed (analysisDocument, loader);
return;
}
if (!CurrentSolution.ContainsDocument (analysisDocument))
return;
OnDocumentClosed (analysisDocument, loader);
foreach (var linkedDoc in document.GetLinkedDocumentIds ()) {
OnDocumentClosed (linkedDoc, loader);
}
} catch (Exception e) {
LoggingService.LogError ("Exception while closing document.", e);
}
}
sealed class SourceTextLoader : TextLoader
{
readonly SourceTextContainer _textContainer;
readonly string _filePath;
public SourceTextLoader(SourceTextContainer textContainer, string filePath)
{
_textContainer = textContainer;
_filePath = filePath;
}
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
return Task.FromResult(TextAndVersion.Create(_textContainer.CurrentText, VersionStamp.Create(), _filePath));
}
}
internal static void UpdateText (SourceText newText, Microsoft.VisualStudio.Text.ITextBuffer buffer, Microsoft.VisualStudio.Text.EditOptions options)
{
using (var edit = buffer.CreateEdit (options, reiteratedVersionNumber: null, editTag: null)) {
var oldSnapshot = buffer.CurrentSnapshot;
edit.Replace (0, oldSnapshot.Length, newText.ToString ());
edit.Apply ();
}
}
protected override void OnDocumentTextChanged (Document document)
{
base.OnDocumentTextChanged (document);
backgroundParser.Parse (document);
}
//FIXME: this should NOT be async. our implementation is doing some very expensive things like formatting that it shouldn't need to do.
protected override void ApplyDocumentTextChanged (DocumentId id, SourceText text)
{
lock (projectModifyLock)
tryApplyState_documentTextChangedTasks.Add (ApplyDocumentTextChangedCore (id, text));
}
async Task ApplyDocumentTextChangedCore (DocumentId id, SourceText text)
{
var document = GetDocument (id);
if (document == null)
return;
var hostDocument = MonoDevelopHostDocumentRegistration.FromDocument (document);
if (hostDocument != null) {
hostDocument.UpdateText (text);
return;
}
if (IsDocumentOpen (id)) {
var textBuffer = (await document.GetTextAsync (CancellationToken.None)).Container.TryGetTextBuffer ();
if (textBuffer != null) {
UpdateText (text, textBuffer, Microsoft.VisualStudio.Text.EditOptions.DefaultMinimalChange);
return;
}
}
var (projection, filePath) = Projections.Get (document.FilePath);
var data = TextFileProvider.Instance.GetTextEditorData (filePath, out bool isOpen);
// Guard against already done changes in linked files.
// This shouldn't happen but the roslyn merging seems not to be working correctly in all cases :/
if (document.GetLinkedDocumentIds ().Length > 0 && isOpen && !(text.GetType ().FullName == "Microsoft.CodeAnalysis.Text.ChangedText")) {
return;
}
lock (tryApplyState_documentTextChangedContents) {
if (tryApplyState_documentTextChangedContents.TryGetValue (filePath, out SourceText formerText)) {
if (formerText.Length == text.Length && formerText.ToString () == text.ToString ())
return;
}
tryApplyState_documentTextChangedContents[filePath] = text;
}
if (!isOpen || !document.TryGetText (out SourceText oldFile)) {
oldFile = await document.GetTextAsync ();
}
var changes = text.GetTextChanges (oldFile).OrderByDescending (c => c.Span.Start).ToList ();
int delta = 0;
if (!isOpen) {
delta = ApplyChanges (projection, data, changes);
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
if (formatter != null && formatter.SupportsPartialDocumentFormatting) {
var mp = GetMonoProject (CurrentSolution.GetProject (id.ProjectId));
string currentText = data.Text;
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
if (projection.TryConvertFromProjectionToOriginal (startOffset, out int originalOffset))
startOffset = originalOffset;
}
string str;
if (change.NewText.Length == 0) {
str = formatter.FormatText (mp.Policies, currentText, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
} else {
str = formatter.FormatText (mp.Policies, currentText, new TextSegment (startOffset, change.NewText.Length));
}
data.ReplaceText (startOffset, change.NewText.Length, str);
}
}
data.Save ();
if (projection != null) {
await UpdateProjectionsDocuments (document, data);
} else {
OnDocumentTextChanged (id, new MonoDevelopSourceText (data), PreservationMode.PreserveValue);
}
} else {
var formatter = CodeFormatterService.GetFormatter (data.MimeType);
var documentContext = documentManager.Documents.FirstOrDefault (d => FilePath.PathComparer.Compare (d.FileName, filePath) == 0)?.DocumentContext;
var root = await projectChanges.NewProject.GetDocument (id).GetSyntaxRootAsync ();
var annotatedNode = root.DescendantNodesAndSelf ().FirstOrDefault (n => n.HasAnnotation (typeSystemService.InsertionModeAnnotation));
SyntaxToken? renameTokenOpt = root.GetAnnotatedNodesAndTokens (Microsoft.CodeAnalysis.CodeActions.RenameAnnotation.Kind)
.Where (s => s.IsToken)
.Select (s => s.AsToken ())
.Cast<SyntaxToken?> ()
.FirstOrDefault ();
if (documentContext != null) {
var editor = (TextEditor)data;
await Runtime.RunInMainThread (async () => {
using (var undo = editor.OpenUndoGroup ()) {
var oldVersion = editor.Version;
delta = ApplyChanges (projection, data, changes);
var versionBeforeFormat = editor.Version;
if (formatter != null && formatter.SupportsOnTheFlyFormatting) {
foreach (var change in changes) {
delta -= change.Span.Length - change.NewText.Length;
var startOffset = change.Span.Start - delta;
if (projection != null) {
if (projection.TryConvertFromProjectionToOriginal (startOffset, out int originalOffset))
startOffset = originalOffset;
}
if (change.NewText.Length == 0) {
formatter.OnTheFlyFormat (editor, documentContext, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
} else {
formatter.OnTheFlyFormat (editor, documentContext, new TextSegment (startOffset, change.NewText.Length));
}
}
}
if (annotatedNode != null && GetInsertionPoints != null) {
documentManager.Documents.First (d => d.FileName == editor.FileName).Select ();
var formattedVersion = editor.Version;
int startOffset = versionBeforeFormat.MoveOffsetTo (editor.Version, annotatedNode.Span.Start);
int endOffset = versionBeforeFormat.MoveOffsetTo (editor.Version, annotatedNode.Span.End);
// alway whole line start & delimiter
var startLine = editor.GetLineByOffset (startOffset);
startOffset = startLine.Offset;
var endLine = editor.GetLineByOffset (endOffset);
endOffset = endLine.EndOffsetIncludingDelimiter + 1;
var insertionCursorSegment = TextSegment.FromBounds (startOffset, endOffset);
string textToInsert = editor.GetTextAt (insertionCursorSegment).TrimEnd ();
editor.RemoveText (insertionCursorSegment);
var insertionPoints = await GetInsertionPoints (editor, editor.CaretOffset);
if (insertionPoints.Count == 0) {
// Just to get sure if no insertion points -> go back to the formatted version.
var textChanges = editor.Version.GetChangesTo (formattedVersion).ToList ();
using (var undo2 = editor.OpenUndoGroup ()) {
foreach (var textChange in textChanges) {
foreach (var v in textChange.TextChanges.Reverse ()) {
editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
}
}
}
return;
}
string insertionModeOperation;
const int CSharpMethodKind = 8875;
bool isMethod = annotatedNode.RawKind == CSharpMethodKind;
if (!isMethod) {
// atm only for generate field/property : remove all new lines generated & just insert the plain node.
// for methods it's not so easy because of "extract code" changes.
foreach (var textChange in editor.Version.GetChangesTo (oldVersion).ToList ()) {
foreach (var v in textChange.TextChanges.Reverse ()) {
editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
}
}
}
switch (annotatedNode.RawKind) {
case 8873: // C# field
insertionModeOperation = GettextCatalog.GetString ("Insert Field");
break;
case CSharpMethodKind:
insertionModeOperation = GettextCatalog.GetString ("Insert Method");
break;
case 8892: // C# property
insertionModeOperation = GettextCatalog.GetString ("Insert Property");
break;
default:
insertionModeOperation = GettextCatalog.GetString ("Insert Code");
break;
}
var options = new InsertionModeOptions (
insertionModeOperation,
insertionPoints,
point => {
if (!point.Success)
return;
point.InsertionPoint.Insert (editor, textToInsert);
}
);
options.ModeExitedAction += delegate (InsertionCursorEventArgs args) {
if (!args.Success) {
var textChanges = editor.Version.GetChangesTo (oldVersion).ToList ();
using (var undo2 = editor.OpenUndoGroup ()) {
foreach (var textChange in textChanges) {
foreach (var v in textChange.TextChanges.Reverse ())
editor.ReplaceText (v.Offset, v.RemovalLength, v.InsertedText);
}
}
}
};
for (int i = 0; i < insertionPoints.Count; i++) {
if (insertionPoints [i].Location.Line < editor.CaretLine) {
options.FirstSelectedInsertionPoint = Math.Min (isMethod ? i + 1 : i, insertionPoints.Count - 1);
} else {
break;
}
}
options.ModeExitedAction += delegate {
if (renameTokenOpt.HasValue)
StartRenameSession (editor, documentContext, versionBeforeFormat, renameTokenOpt.Value);
};
editor.StartInsertionMode (options);
}
}
});
}
if (projection != null) {
await UpdateProjectionsDocuments (document, data);
} else {
OnDocumentTextChanged (id, new MonoDevelopSourceText (data), PreservationMode.PreserveValue);
}
}
}
internal static Func<TextEditor, int, Task<List<InsertionPoint>>> GetInsertionPoints;
internal static Action<TextEditor, DocumentContext, ITextSourceVersion, SyntaxToken?> StartRenameSession;
async Task UpdateProjectionsDocuments (Document document, ITextDocument data)
{
var project = typeSystemService.GetMonoProject (document.Project);
var file = project.Files.GetFile (data.FileName);
var node = typeSystemService.GetTypeSystemParserNode (data.MimeType, file.BuildAction);
if (node != null && node.Parser.CanGenerateProjection (data.MimeType, file.BuildAction, project.SupportedLanguages)) {
var options = new ParseOptions {
FileName = file.FilePath,
Project = project,
Content = TextFileProvider.Instance.GetReadOnlyTextEditorData (file.FilePath),
};
var projections = await node.Parser.GenerateProjections (options);
UpdateProjectionEntry (file, projections);
var projectId = GetProjectId (project);
var projectdata = ProjectMap.GetData (projectId);
foreach (var projected in projections) {
OnDocumentTextChanged (projectdata.DocumentData.Get (projected.Document.FileName), new MonoDevelopSourceText (projected.Document), PreservationMode.PreserveValue);
}
}
}
static int ApplyChanges (Projection projection, ITextDocument data, List<Microsoft.CodeAnalysis.Text.TextChange> changes)
{
int delta = 0;
foreach (var change in changes) {
var offset = change.Span.Start;
if (projection != null) {
//If change is outside projection segments don't apply it...
if (projection.TryConvertFromProjectionToOriginal (offset, out int originalOffset)) {
offset = originalOffset;
data.ReplaceText (offset, change.Span.Length, change.NewText);
delta += change.Span.Length - change.NewText.Length;
}
} else {
data.ReplaceText (offset, change.Span.Length, change.NewText);
delta += change.Span.Length - change.NewText.Length;
}
}
return delta;
}
// used to pass additional state from Apply* to TryApplyChanges so it can batch certain operations such as saving projects
HashSet<MonoDevelop.Projects.Project> tryApplyState_changedProjects = new HashSet<MonoDevelop.Projects.Project> ();
List<Task> tryApplyState_documentTextChangedTasks = new List<Task> ();
Dictionary<string, SourceText> tryApplyState_documentTextChangedContents = new Dictionary<string, SourceText> ();
/// <summary>
/// Used by tests to validate that project has been saved.
/// </summary>
/// <value>The task that can be awaited to validate saving has finished.</value>
internal Task ProjectSaveTask { get; private set; } = Task.FromResult<object> (null);
internal override bool TryApplyChanges (Solution newSolution, IProgressTracker progressTracker)
{
// this is supported on the main thread only
// see https://github.com/dotnet/roslyn/pull/18043
// as a result, we can assume that the things it calls are _also_ main thread only
Runtime.CheckMainThread ();
lock (projectModifyLock) {
FileService.FreezeEvents ();
freezeProjectModify = true;
try {
var ret = base.TryApplyChanges (newSolution, progressTracker);
if (tryApplyState_documentTextChangedTasks.Count > 0) {
Task.WhenAll (tryApplyState_documentTextChangedTasks).ContinueWith (t => {
try {
t.Wait ();
} catch (Exception ex) {
LoggingService.LogError ("Error applying changes to documents", ex);
}
var changedFiles = new HashSet<string> (tryApplyState_documentTextChangedContents.Keys, FilePath.PathComparer);
foreach (var w in documentManager.Documents) {
if (w.IsFile && changedFiles.Contains (w.FileName)) {
w.DocumentContext.ReparseDocument ();
}
}
}, CancellationToken.None, TaskContinuationOptions.None, Runtime.MainTaskScheduler);
}
if (tryApplyState_changedProjects.Count > 0)
ProjectSaveTask = IdeApp.IsInitialized ? IdeApp.ProjectOperations.SaveAsync (tryApplyState_changedProjects) : Task.WhenAll (tryApplyState_changedProjects.Select (p => p.SaveAsync (new ProgressMonitor ())));
return ret;
} finally {
tryApplyState_documentTextChangedContents.Clear ();
tryApplyState_documentTextChangedTasks.Clear ();
tryApplyState_changedProjects.Clear ();
freezeProjectModify = false;
FileService.ThawEvents ();
}
}
}
public override bool CanApplyChange (ApplyChangesKind feature)
{
switch (feature) {
case ApplyChangesKind.AddDocument:
case ApplyChangesKind.RemoveDocument:
case ApplyChangesKind.ChangeDocument:
//HACK: we don't actually support adding and removing metadata references from project
//however, our MetadataReferenceCache currently depends on (incorrectly) using TryApplyChanges
case ApplyChangesKind.AddMetadataReference:
case ApplyChangesKind.RemoveMetadataReference:
case ApplyChangesKind.AddProjectReference:
case ApplyChangesKind.RemoveProjectReference:
case ApplyChangesKind.ChangeDocumentInfo:
return true;
default:
return false;
}
}
protected override void ApplyProjectChanges (ProjectChanges projectChanges)
{
this.projectChanges = projectChanges;
base.ApplyProjectChanges (projectChanges);
}
protected override void ApplyDocumentAdded (DocumentInfo info, SourceText text)
{
var id = info.Id;
MonoDevelop.Projects.Project mdProject = null;
if (id.ProjectId != null) {
var project = CurrentSolution.GetProject (id.ProjectId);
mdProject = GetMonoProject (project);
if (mdProject == null)
LoggingService.LogWarning ("Couldn't find project for newly generated file {0} (Project {1}).", info.Name, info.Id.ProjectId);
}
var path = DetermineFilePath (info.Id, info.Name, info.FilePath, info.Folders, mdProject?.FileName.ParentDirectory, true);
// If file is already part of project don't re-add it, example of this is .cshtml
if (mdProject?.IsFileInProject (path) == true) {
this.OnDocumentAdded (info);
return;
}
info = info.WithFilePath (path).WithTextLoader (new MonoDevelopTextLoader (path));
string formattedText;
var formatter = CodeFormatterService.GetFormatter (desktopService.GetMimeTypeForUri (path));
if (formatter != null && mdProject != null) {
formattedText = formatter.FormatText (mdProject.Policies, text.ToString ());
} else {
formattedText = text.ToString ();
}
var textSource = new StringTextSource (formattedText, text.Encoding ?? System.Text.Encoding.UTF8);
try {
textSource.WriteTextTo (path);
} catch (Exception e) {
LoggingService.LogError ("Exception while saving file to " + path, e);
}
if (mdProject != null) {
var data = ProjectMap.GetData (id.ProjectId);
data.DocumentData.Add (info.Id, path);
var file = new MonoDevelop.Projects.ProjectFile (path);
mdProject.Files.Add (file);
tryApplyState_changedProjects.Add (mdProject);
}
this.OnDocumentAdded (info);
}
protected override void ApplyDocumentRemoved (DocumentId documentId)
{
var document = GetDocument (documentId);
var mdProject = GetMonoProject (documentId.ProjectId);
if (document == null || mdProject == null) {
return;
}
FilePath filePath = document.FilePath;
var projectFile = mdProject.Files.GetFile (filePath);
if (projectFile == null) {
return;
}
//force-close the old doc even if it's dirty
var openDoc = documentManager.Documents.FirstOrDefault (d => d.IsFile && filePath.Equals (d.FileName));
if (openDoc != null && openDoc.IsDirty) {
openDoc.Save ().Wait ();
openDoc.Close ().Wait ();
}
//this will fire a OnDocumentRemoved event via OnFileRemoved
mdProject.Files.Remove (projectFile);
FileService.DeleteFile (filePath);
tryApplyState_changedProjects.Add (mdProject);
}
string DetermineFilePath (DocumentId id, string name, string filePath, IReadOnlyList<string> docFolders, string defaultFolder, bool createDirectory = false)
{
var path = filePath;
if (string.IsNullOrEmpty (path)) {
var monoProject = GetMonoProject (id.ProjectId);
// If the first namespace name matches the name of the project, then we don't want to
// generate a folder for that. The project is implicitly a folder with that name.
IEnumerable<string> folders;
if (docFolders != null && monoProject != null && docFolders.FirstOrDefault () == monoProject.Name) {
folders = docFolders.Skip (1);
} else {
folders = docFolders;
}
if (folders.Any ()) {
string baseDirectory = Path.Combine (monoProject?.BaseDirectory ?? MonoDevelopSolution.BaseDirectory, Path.Combine (folders.ToArray ()));
try {
if (createDirectory && !Directory.Exists (baseDirectory))
Directory.CreateDirectory (baseDirectory);
} catch (Exception e) {
LoggingService.LogError ("Error while creating directory for a new file : " + baseDirectory, e);
}
path = Path.Combine (baseDirectory, name);
} else {
path = Path.Combine (defaultFolder, name);
}
}
return path;
}
bool TryGetMetadataReferenceMapping (ProjectId projectId, MetadataReference metadataReference, out MonoDevelop.Projects.DotNetProject mdProject, out string path, out SystemAssembly systemAssemblyOpt)
{
mdProject = GetMonoProject (projectId) as MonoDevelop.Projects.DotNetProject;
path = GetMetadataPath (metadataReference);
systemAssemblyOpt = null;
if (mdProject == null || path == null)
return false;
// PERF: Maybe break IAssemblyContext API and add GetAssemblyFromPath.
// GetPackageFromPath could be implemented by querying the SystemAssembly's package.
var package = mdProject.AssemblyContext.GetPackageFromPath (path);
if (package != null) {
foreach (var asm in package.Assemblies) {
if (asm.Location == path)
systemAssemblyOpt = asm;
}
}
// This code would handle assemblies like glib-sharp.
// Enabling this causes a NRE, as there's no package associated with the SystemAssembly.
//if (systemAssemblyOpt == null) {
// try {
// var aName = AssemblyName.GetAssemblyName (path).FullName;
// var isGac = mdProject.AssemblyContext.AssemblyIsInGac (aName);
// if (isGac) {
// systemAssemblyOpt = new SystemAssembly (path, aName);
// }
// } catch {
// }
//}
return true;
}
protected override void ApplyMetadataReferenceAdded (ProjectId projectId, MetadataReference metadataReference)
{
if (!TryGetMetadataReferenceMapping (projectId, metadataReference, out var mdProject, out string path, out var systemAssemblyOpt))
return;
foreach (var r in mdProject.References) {
if (systemAssemblyOpt != null) {
if (r.ReferenceType == MonoDevelop.Projects.ReferenceType.Package) {
var nameToCheck = r.SpecificVersion ? systemAssemblyOpt.FullName : systemAssemblyOpt.Name;
if (r.Reference == nameToCheck) {
LoggingService.LogWarning ("Warning duplicate reference is added " + path);
return;
}
}
}
if (r.ReferenceType == MonoDevelop.Projects.ReferenceType.Assembly && r.Reference == path) {
LoggingService.LogWarning ("Warning duplicate reference is added " + path);
return;
}
if (r.ReferenceType == MonoDevelop.Projects.ReferenceType.Project) {
foreach (var fn in r.GetReferencedFileNames (MonoDevelop.Projects.ConfigurationSelector.Default)) {
if (fn == path) {
LoggingService.LogWarning ("Warning duplicate reference is added " + path + " for project " + r.Reference);
return;
}
}
}
}
if (systemAssemblyOpt != null) {
mdProject.References.Add (MonoDevelop.Projects.ProjectReference.CreateAssemblyReference (systemAssemblyOpt));
} else {
mdProject.AddReference (path);
}
tryApplyState_changedProjects.Add (mdProject);
this.OnMetadataReferenceAdded (projectId, metadataReference);
}
protected override void ApplyMetadataReferenceRemoved (ProjectId projectId, MetadataReference metadataReference)
{
if (!TryGetMetadataReferenceMapping (projectId, metadataReference, out var mdProject, out string path, out var systemAssemblyOpt))
return;
MonoDevelop.Projects.ProjectReference item;
// if we're trying to remove a system package, try removing a system package first
if (systemAssemblyOpt != null) {
item = mdProject.References.FirstOrDefault (r => {
if (r.ReferenceType != MonoDevelop.Projects.ReferenceType.Package)
return false;
var nameToCheck = r.SpecificVersion ? systemAssemblyOpt.FullName : systemAssemblyOpt.Name;
return r.ReferenceType == MonoDevelop.Projects.ReferenceType.Package && r.Reference == nameToCheck;
});
} else {
// Remove a normal assembly reference.
item = mdProject.References.FirstOrDefault (r => r.ReferenceType == MonoDevelop.Projects.ReferenceType.Assembly && r.Reference == path);
}
if (item == null)
return;
mdProject.References.Remove (item);
tryApplyState_changedProjects.Add (mdProject);
this.OnMetadataReferenceRemoved (projectId, metadataReference);
}
string GetMetadataPath (MetadataReference metadataReference)
{
if (metadataReference is PortableExecutableReference fileMetadata) {
return fileMetadata.FilePath;
}
return null;
}
protected override void ApplyProjectReferenceAdded (ProjectId projectId, ProjectReference projectReference)
{
var mdProject = GetMonoProject (projectId) as MonoDevelop.Projects.DotNetProject;
var projectToReference = GetMonoProject (projectReference.ProjectId);
if (mdProject == null || projectToReference == null)
return;
var mdRef = MonoDevelop.Projects.ProjectReference.CreateProjectReference (projectToReference);
mdProject.References.Add (mdRef);
tryApplyState_changedProjects.Add (mdProject);
this.OnProjectReferenceAdded (projectId, projectReference);
}
protected override void ApplyProjectReferenceRemoved (ProjectId projectId, ProjectReference projectReference)
{
var mdProject = GetMonoProject (projectId) as MonoDevelop.Projects.DotNetProject;
var projectToReference = GetMonoProject (projectReference.ProjectId);
if (mdProject == null || projectToReference == null)
return;
foreach (var pr in mdProject.References.OfType<MonoDevelop.Projects.ProjectReference>()) {
if (pr.ProjectGuid == projectToReference.ItemId) {
mdProject.References.Remove (pr);
tryApplyState_changedProjects.Add (mdProject);
this.OnProjectReferenceRemoved (projectId, projectReference);
break;
}
}
}
protected override void ApplyDocumentInfoChanged (DocumentId id, DocumentInfo info)
{
var currentSolution = CurrentSolution;
var document = currentSolution.GetDocument (id);
FailIfDocumentInfoChangesNotSupported (document, info);
// abort if the name is the same, as there's nothing to do
if (document.Name == info.Name
|| string.IsNullOrEmpty (info.Name)
|| string.IsNullOrEmpty (info.FilePath))
return;
MonoDevelop.Projects.Project mdProject = null;
if (id.ProjectId != null) {
var project = currentSolution.GetProject (id.ProjectId);
mdProject = GetMonoProject (project);
if (mdProject == null) {
LoggingService.LogWarning ("Couldn't find project for newly file file {0} (Project {1}).",
info.Name, info.Id.ProjectId);
return;
}
}
var guiDoc = documentManager
.Documents
.FirstOrDefault (d => d.IsFile
&& document.FilePath.Equals (d.FilePath, FilePath.PathComparison));
DispatchService.PumpingWait (() => guiDoc.IsDirty ? guiDoc.Save () : Task.CompletedTask);
// TODO: the Visual Studio Windows implementation of this also adds an undo unit to the
// global undo manager which we don't currently support.
var newName = NameGenerator.GenerateUniqueName (
Path.GetFileNameWithoutExtension (info.Name),
Path.GetExtension (info.Name),
nx => !mdProject.Files.Any (p => string.Equals (p.Name, nx, FilePath.PathComparison)));
var mdFile = mdProject.GetProjectFile (document.FilePath);
if (mdFile == null) {
LoggingService.LogWarning ($"{document.FilePath} was not found in project in ApplyDocumentInfo");
return;
}
DispatchService.PumpingWait (() => {
return Runtime.RunInMainThread (() => {
FileService.RenameFile (document.FilePath, newName);
var childrenToRename = ProjectOperations.GetDependentFilesToRename (mdFile, newName);
if (childrenToRename != null) {
// we also need to rename children!
foreach (var child in childrenToRename) {
FileService.RenameFile (child.File.FilePath, child.NewName);
}
}
});
});
tryApplyState_changedProjects.Add (mdProject);
}
static void FailIfDocumentInfoChangesNotSupported (Document document, DocumentInfo updatedInfo)
{
if (document.SourceCodeKind != updatedInfo.SourceCodeKind) {
throw new InvalidOperationException (
$"This Workspace does not support changing a document's {nameof (document.SourceCodeKind)}.");
}
if (document.FilePath != updatedInfo.FilePath) {
throw new InvalidOperationException (
$"This Workspace does not support changing a document's {nameof (document.FilePath)}.");
}
if (document.Id != updatedInfo.Id) {
throw new InvalidOperationException (
$"This Workspace does not support changing a document's {nameof (document.Id)}.");
}
if (document.Folders != updatedInfo.Folders) {
throw new InvalidOperationException (
$"This Workspace does not support changing a document's {nameof (document.Folders)}.");
}
if (document.State.Attributes.IsGenerated != updatedInfo.IsGenerated) {
throw new InvalidOperationException (
$"This Workspace does not support changing a document's {nameof (document.State.Attributes.IsGenerated)} state.");
}
}
#endregion
internal Document GetDocument (DocumentId documentId, CancellationToken cancellationToken = default (CancellationToken))
{
var project = CurrentSolution.GetProject (documentId.ProjectId);
if (project == null)
return null;
return project.GetDocument (documentId);
}
internal TextDocument GetAdditionalDocument (DocumentId documentId, CancellationToken cancellationToken = default(CancellationToken))
{
var project = CurrentSolution.GetProject (documentId.ProjectId);
if (project == null)
return null;
return project.GetAdditionalDocument (documentId);
}
internal TextDocument GetAnalyzerConfigDocument (DocumentId documentId, CancellationToken cancellationToken = default (CancellationToken))
{
var project = CurrentSolution.GetProject (documentId.ProjectId);
if (project == null)
return null;
return project.GetAnalyzerConfigDocument (documentId);
}
internal async Task UpdateFileContent (string fileName, string text)
{
SourceText newText = SourceText.From (text);
var tasks = new List<Task> ();
try {
await LoadLock.WaitAsync ();
foreach (var projectId in ProjectMap.GetProjectIds ()) {
var docId = this.GetDocumentId (projectId, fileName);
if (docId != null) {
try {
if (this.GetDocument (docId) != null) {
base.OnDocumentTextChanged (docId, newText, PreservationMode.PreserveIdentity);
} else if (this.GetAdditionalDocument (docId) != null) {
base.OnAdditionalDocumentTextChanged (docId, newText, PreservationMode.PreserveIdentity);
} else if (this.GetAnalyzerConfigDocument (docId) != null) {
base.OnAnalyzerConfigDocumentTextChanged (docId, newText, PreservationMode.PreserveIdentity);
}
} catch (Exception e) {
LoggingService.LogWarning ("Roslyn error on text change", e);
}
}
var monoProject = GetMonoProject (projectId);
if (monoProject != null) {
var pf = monoProject.GetProjectFile (fileName);
if (pf != null) {
var mimeType = desktopService.GetMimeTypeForUri (fileName);
if (typeSystemService.CanParseProjections (monoProject, mimeType, fileName)) {
var parseOptions = new ParseOptions { Project = monoProject, FileName = fileName, Content = new StringTextSource (text), BuildAction = pf.BuildAction };
var task = typeSystemService.ParseProjection (parseOptions, mimeType);
tasks.Add (task);
}
}
}
}
} finally {
LoadLock.Release ();
}
await Task.WhenAll (tasks);
}
internal void RemoveProject (MonoDevelop.Projects.Project project)
{
foreach (var id in GetProjectIds (project)) {
OnProjectRemoved (id);
dynamicFileManager?.UnloadProject (id);
}
}
#region Project modification handlers
List<MonoDevelop.Projects.DotNetProject> modifiedProjects = new List<MonoDevelop.Projects.DotNetProject> ();
readonly object projectModifyLock = new object ();
bool freezeProjectModify;
ConditionalWeakTable<MonoDevelop.Projects.DotNetProject, CancellationTokenSource> projectModifiedCts = new ConditionalWeakTable<MonoDevelop.Projects.DotNetProject, CancellationTokenSource> ();
void OnProjectModified (object sender, MonoDevelop.Projects.SolutionItemModifiedEventArgs args)
{
lock (projectModifyLock) {
if (freezeProjectModify)
return;
try {
if (!args.Any (x => x.Hint == "TargetFramework" || x.Hint == "References" || x.Hint == "CompilerParameters" || x.Hint == "Files"))
return;
var project = sender as MonoDevelop.Projects.DotNetProject;
if (project == null)
return;
var projectId = GetProjectId (project);
if (projectModifiedCts.TryGetValue (project, out var cts)) {
cts.Cancel ();
projectModifiedCts.Remove (project);
}
cts = new CancellationTokenSource ();
projectModifiedCts.Add (project, cts);
if (CurrentSolution.ContainsProject (projectId)) {
foreach (string framework in MonoDevelopWorkspace.GetFrameworks (project)) {
var projectInfo = ProjectHandler.LoadProject (project, cts.Token, null, null, framework).ContinueWith (t => {
if (t.IsFaulted) {
LoggingService.LogError ("Failed to reload project", t.Exception);
return;
}
try {
lock (projectModifyLock) {
ProjectInfo newProjectContents = t.Result;
OnProjectReloaded (newProjectContents);
foreach (var docId in GetOpenDocumentIds (newProjectContents.Id).ToArray ()) {
if (CurrentSolution.GetDocument (docId) == null) {
ClearOpenDocument (docId);
}
}
Runtime.RunInMainThread (() => IdeServices.TypeSystemService.UpdateRegisteredOpenDocuments ()).Ignore();
}
} catch (Exception e) {
LoggingService.LogError ("Error while reloading project " + project.Name, e);
}
}, cts.Token, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
}
} else {
modifiedProjects.Add (project);
}
} catch (Exception ex) {
LoggingService.LogInternalError (ex);
}
}
}
internal ProjectInfo WithDynamicDocuments (MonoDevelop.Projects.Project project, ProjectInfo projectInfo)
{
var projectDirectory = Path.GetDirectoryName (project.FileName);
var contentItems = project.MSBuildProject.EvaluatedItems
.Where (item => item.Name == "Content" && item.Include.EndsWith (".razor", StringComparison.OrdinalIgnoreCase))
.Select (item => GetAbsolutePath(item.Include))
.ToList ();
return dynamicFileManager?.UpdateDynamicFiles (projectInfo, contentItems, this);
string GetAbsolutePath (string relativePath)
{
if (!Path.IsPathRooted (relativePath)) {
relativePath = Path.Combine (projectDirectory, relativePath);
}
// normalize the path separator characters in case they're mixed
relativePath = relativePath.Replace ('\\', Path.DirectorySeparatorChar);
return relativePath;
}
}
internal override void SetDocumentContext (DocumentId documentId)
{
base.OnDocumentContextUpdated (documentId);
}
#endregion
}
// static class MonoDevelopWorkspaceFeatures
// {
// static FeaturePack pack;
//
// public static FeaturePack Features {
// get {
// if (pack == null)
// Interlocked.CompareExchange (ref pack, ComputePack (), null);
// return pack;
// }
// }
//
// static FeaturePack ComputePack ()
// {
// var assemblies = new List<Assembly> ();
// var workspaceCoreAssembly = typeof(Workspace).Assembly;
// assemblies.Add (workspaceCoreAssembly);
//
// LoadAssembly (assemblies, "Microsoft.CodeAnalysis.CSharp.Workspaces");
// //LoadAssembly (assemblies, "Microsoft.CodeAnalysis.VisualBasic.Workspaces");
//
// var catalogs = assemblies.Select (a => new System.ComponentModel.Composition.Hosting.AssemblyCatalog (a));
//
// return new MefExportPack (catalogs);
// }
//
// static void LoadAssembly (List<Assembly> assemblies, string assemblyName)
// {
// try {
// var loadedAssembly = Assembly.Load (assemblyName);
// assemblies.Add (loadedAssembly);
// } catch (Exception e) {
// LoggingService.LogWarning ("Couldn't load assembly:" + assemblyName, e);
// }
// }
// }
public class RoslynProjectEventArgs : EventArgs
{
public ProjectId ProjectId { get; private set; }
public RoslynProjectEventArgs (ProjectId projectId)
{
ProjectId = projectId;
}
}
}
|