-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Solution.cs
1972 lines (1652 loc) · 80.4 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Collections.Immutable;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents a set of projects and their source code documents.
/// </summary>
public partial class Solution
{
// SolutionState that doesn't hold onto Project/Document
private readonly SolutionState _state;
// Values for all these are created on demand.
private ImmutableHashMap<ProjectId, Project> _projectIdToProjectMap;
private Solution(SolutionState state)
{
_projectIdToProjectMap = ImmutableHashMap<ProjectId, Project>.Empty;
_state = state;
}
internal Solution(Workspace workspace, SolutionInfo.SolutionAttributes solutionAttributes, SerializableOptionSet options, IReadOnlyList<AnalyzerReference> analyzerReferences)
: this(new SolutionState(workspace.PrimaryBranchId, new SolutionServices(workspace), solutionAttributes, options, analyzerReferences))
{
}
internal SolutionState State => _state;
internal int WorkspaceVersion => _state.WorkspaceVersion;
internal SolutionServices Services => _state.Services;
internal BranchId BranchId => _state.BranchId;
internal ProjectState? GetProjectState(ProjectId projectId) => _state.GetProjectState(projectId);
/// <summary>
/// The Workspace this solution is associated with.
/// </summary>
public Workspace Workspace => _state.Workspace;
/// <summary>
/// The Id of the solution. Multiple solution instances may share the same Id.
/// </summary>
public SolutionId Id => _state.Id;
/// <summary>
/// The path to the solution file or null if there is no solution file.
/// </summary>
public string? FilePath => _state.FilePath;
/// <summary>
/// The solution version. This equates to the solution file's version.
/// </summary>
public VersionStamp Version => _state.Version;
/// <summary>
/// A list of all the ids for all the projects contained by the solution.
/// </summary>
public IReadOnlyList<ProjectId> ProjectIds => _state.ProjectIds;
/// <summary>
/// A list of all the projects contained by the solution.
/// </summary>
public IEnumerable<Project> Projects => ProjectIds.Select(id => GetProject(id)!);
/// <summary>
/// The version of the most recently modified project.
/// </summary>
public VersionStamp GetLatestProjectVersion() => _state.GetLatestProjectVersion();
/// <summary>
/// True if the solution contains a project with the specified project ID.
/// </summary>
public bool ContainsProject([NotNullWhen(returnValue: true)] ProjectId? projectId) => _state.ContainsProject(projectId);
/// <summary>
/// Gets the project in this solution with the specified project ID.
///
/// If the id is not an id of a project that is part of this solution the method returns null.
/// </summary>
public Project? GetProject(ProjectId? projectId)
{
if (this.ContainsProject(projectId))
{
return ImmutableHashMapExtensions.GetOrAdd(ref _projectIdToProjectMap, projectId, s_createProjectFunction, this);
}
return null;
}
private static readonly Func<ProjectId, Solution, Project> s_createProjectFunction = CreateProject;
private static Project CreateProject(ProjectId projectId, Solution solution)
{
var state = solution.State.GetProjectState(projectId);
Contract.ThrowIfNull(state);
return new Project(solution, state);
}
#pragma warning disable IDE0060 // Remove unused parameter 'cancellationToken' - shipped public API
/// <summary>
/// Gets the <see cref="Project"/> associated with an assembly symbol.
/// </summary>
public Project? GetProject(IAssemblySymbol assemblySymbol,
CancellationToken cancellationToken = default)
#pragma warning restore IDE0060 // Remove unused parameter
{
var projectState = _state.GetProjectState(assemblySymbol);
return projectState == null ? null : GetProject(projectState.Id);
}
/// <summary>
/// Given a <paramref name="symbol"/> returns the <see cref="ProjectId"/> of the <see cref="Project"/> it came
/// from. Returns <see langword="null"/> if <paramref name="symbol"/> does not come from any project in this solution.
/// </summary>
/// <remarks>
/// This function differs from <see cref="GetProject(IAssemblySymbol, CancellationToken)"/> in terms of how it
/// treats <see cref="IAssemblySymbol"/>s. Specifically, say there is the following:
///
/// <c>
/// Project-A, containing Symbol-A.<para/>
/// Project-B, with a reference to Project-A, and usage of Symbol-A.
/// </c>
///
/// It is possible (with retargeting, and other complex cases) that Symbol-A from Project-B will be a different
/// symbol than Symbol-A from Project-A. However, <see cref="GetProject(IAssemblySymbol, CancellationToken)"/>
/// will always try to return Project-A for either of the Symbol-A's, as it prefers to return the original
/// Source-Project of the original definition, not the project that actually produced the symbol. For many
/// features this is an acceptable abstraction. However, for some cases (Find-References in particular) it is
/// necessary to resolve symbols back to the actual project/compilation that produced them for correctness.
/// </remarks>
internal ProjectId? GetOriginatingProjectId(ISymbol symbol)
=> _state.GetOriginatingProjectId(symbol);
/// <inheritdoc cref="GetOriginatingProjectId"/>
internal Project? GetOriginatingProject(ISymbol symbol)
=> GetProject(GetOriginatingProjectId(symbol));
/// <summary>
/// True if the solution contains the document in one of its projects
/// </summary>
public bool ContainsDocument([NotNullWhen(returnValue: true)] DocumentId? documentId) => _state.ContainsDocument(documentId);
/// <summary>
/// True if the solution contains the additional document in one of its projects
/// </summary>
public bool ContainsAdditionalDocument([NotNullWhen(returnValue: true)] DocumentId? documentId) => _state.ContainsAdditionalDocument(documentId);
/// <summary>
/// True if the solution contains the analyzer config document in one of its projects
/// </summary>
public bool ContainsAnalyzerConfigDocument([NotNullWhen(returnValue: true)] DocumentId? documentId) => _state.ContainsAnalyzerConfigDocument(documentId);
/// <summary>
/// Gets the documentId in this solution with the specified syntax tree.
/// </summary>
public DocumentId? GetDocumentId(SyntaxTree? syntaxTree) => GetDocumentId(syntaxTree, projectId: null);
/// <summary>
/// Gets the documentId in this solution with the specified syntax tree.
/// </summary>
public DocumentId? GetDocumentId(SyntaxTree? syntaxTree, ProjectId? projectId)
{
return State.GetDocumentState(syntaxTree, projectId)?.Id;
}
/// <summary>
/// Gets the document in this solution with the specified document ID.
/// </summary>
public Document? GetDocument(DocumentId? documentId)
=> GetProject(documentId?.ProjectId)?.GetDocument(documentId!);
/// <summary>
/// Gets a document or a source generated document in this solution with the specified document ID.
/// </summary>
internal ValueTask<Document?> GetDocumentAsync(DocumentId? documentId, bool includeSourceGenerated = false, CancellationToken cancellationToken = default)
{
var project = GetProject(documentId?.ProjectId);
if (project == null)
{
return default;
}
Contract.ThrowIfNull(documentId);
return project.GetDocumentAsync(documentId, includeSourceGenerated, cancellationToken);
}
/// <summary>
/// Gets a document, additional document, analyzer config document or a source generated document in this solution with the specified document ID.
/// </summary>
internal ValueTask<TextDocument?> GetTextDocumentAsync(DocumentId? documentId, CancellationToken cancellationToken = default)
{
var project = GetProject(documentId?.ProjectId);
if (project == null)
{
return default;
}
Contract.ThrowIfNull(documentId);
return project.GetTextDocumentAsync(documentId, cancellationToken);
}
/// <summary>
/// Gets the additional document in this solution with the specified document ID.
/// </summary>
public TextDocument? GetAdditionalDocument(DocumentId? documentId)
{
if (this.ContainsAdditionalDocument(documentId))
{
return this.GetProject(documentId.ProjectId)!.GetAdditionalDocument(documentId);
}
return null;
}
/// <summary>
/// Gets the analyzer config document in this solution with the specified document ID.
/// </summary>
public AnalyzerConfigDocument? GetAnalyzerConfigDocument(DocumentId? documentId)
{
if (this.ContainsAnalyzerConfigDocument(documentId))
{
return this.GetProject(documentId.ProjectId)!.GetAnalyzerConfigDocument(documentId);
}
return null;
}
public ValueTask<SourceGeneratedDocument?> GetSourceGeneratedDocumentAsync(DocumentId documentId, CancellationToken cancellationToken)
{
var project = GetProject(documentId.ProjectId);
if (project == null)
{
return new(result: null);
}
else
{
return project.GetSourceGeneratedDocumentAsync(documentId, cancellationToken);
}
}
/// <summary>
/// Gets the document in this solution with the specified syntax tree.
/// </summary>
public Document? GetDocument(SyntaxTree? syntaxTree)
=> this.GetDocument(syntaxTree, projectId: null);
internal Document? GetDocument(SyntaxTree? syntaxTree, ProjectId? projectId)
{
if (syntaxTree != null)
{
var documentState = State.GetDocumentState(syntaxTree, projectId);
if (documentState is SourceGeneratedDocumentState)
{
// We have the underlying state, but we need to get the wrapper SourceGeneratedDocument object. The wrapping is maintained by
// the Project object, so we'll now fetch the project and ask it to get the SourceGeneratedDocument wrapper. Under the covers this
// implicity may call to fetch the SourceGeneratedDocumentState a second time but that's not expensive.
var generatedDocument = this.GetRequiredProject(documentState.Id.ProjectId).TryGetSourceGeneratedDocumentForAlreadyGeneratedId(documentState.Id);
Contract.ThrowIfNull(generatedDocument, "The call to GetDocumentState found a SourceGeneratedDocumentState, so we should have found it now.");
return generatedDocument;
}
else if (documentState is DocumentState)
{
return GetDocument(documentState.Id)!;
}
}
return null;
}
/// <summary>
/// Creates a new solution instance that includes a project with the specified language and names.
/// Returns the new project.
/// </summary>
public Project AddProject(string name, string assemblyName, string language)
{
var id = ProjectId.CreateNewId(debugName: name);
return this.AddProject(id, name, assemblyName, language).GetProject(id)!;
}
/// <summary>
/// Creates a new solution instance that includes a project with the specified language and names.
/// </summary>
public Solution AddProject(ProjectId projectId, string name, string assemblyName, string language)
=> this.AddProject(ProjectInfo.Create(projectId, VersionStamp.Create(), name, assemblyName, language));
/// <summary>
/// Create a new solution instance that includes a project with the specified project information.
/// </summary>
public Solution AddProject(ProjectInfo projectInfo)
{
var newState = _state.AddProject(projectInfo);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance without the project specified.
/// </summary>
public Solution RemoveProject(ProjectId projectId)
{
var newState = _state.RemoveProject(projectId);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the new
/// assembly name.
/// </summary>
public Solution WithProjectAssemblyName(ProjectId projectId, string assemblyName)
{
CheckContainsProject(projectId);
if (assemblyName == null)
{
throw new ArgumentNullException(nameof(assemblyName));
}
var newState = _state.WithProjectAssemblyName(projectId, assemblyName);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the output file path.
/// </summary>
public Solution WithProjectOutputFilePath(ProjectId projectId, string? outputFilePath)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectOutputFilePath(projectId, outputFilePath);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the reference assembly output file path.
/// </summary>
public Solution WithProjectOutputRefFilePath(ProjectId projectId, string? outputRefFilePath)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectOutputRefFilePath(projectId, outputRefFilePath);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the compiler output file path.
/// </summary>
public Solution WithProjectCompilationOutputInfo(ProjectId projectId, in CompilationOutputInfo info)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectCompilationOutputInfo(projectId, info);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the default namespace.
/// </summary>
public Solution WithProjectDefaultNamespace(ProjectId projectId, string? defaultNamespace)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectDefaultNamespace(projectId, defaultNamespace);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the name.
/// </summary>
public Solution WithProjectName(ProjectId projectId, string name)
{
CheckContainsProject(projectId);
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var newState = _state.WithProjectName(projectId, name);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project specified updated to have the project file path.
/// </summary>
public Solution WithProjectFilePath(ProjectId projectId, string? filePath)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectFilePath(projectId, filePath);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to have
/// the specified compilation options.
/// </summary>
public Solution WithProjectCompilationOptions(ProjectId projectId, CompilationOptions options)
{
CheckContainsProject(projectId);
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var newState = _state.WithProjectCompilationOptions(projectId, options);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to have
/// the specified parse options.
/// </summary>
public Solution WithProjectParseOptions(ProjectId projectId, ParseOptions options)
{
CheckContainsProject(projectId);
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var newState = _state.WithProjectParseOptions(projectId, options);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to have
/// the specified hasAllInformation.
/// </summary>
// TODO: https://github.com/dotnet/roslyn/issues/42449 make it public
internal Solution WithHasAllInformation(ProjectId projectId, bool hasAllInformation)
{
CheckContainsProject(projectId);
var newState = _state.WithHasAllInformation(projectId, hasAllInformation);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to have
/// the specified runAnalyzers.
/// </summary>
// TODO: https://github.com/dotnet/roslyn/issues/42449 make it public
internal Solution WithRunAnalyzers(ProjectId projectId, bool runAnalyzers)
{
CheckContainsProject(projectId);
var newState = _state.WithRunAnalyzers(projectId, runAnalyzers);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the project documents in the order by the specified document ids.
/// The specified document ids must be the same as what is already in the project; no adding or removing is allowed.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="documentIds"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="ArgumentException">The number of documents specified in <paramref name="documentIds"/> is not equal to the number of documents in project <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">Document specified in <paramref name="documentIds"/> does not exist in project <paramref name="projectId"/>.</exception>
public Solution WithProjectDocumentsOrder(ProjectId projectId, ImmutableList<DocumentId> documentIds)
{
CheckContainsProject(projectId);
if (documentIds == null)
{
throw new ArgumentNullException(nameof(documentIds));
}
var newState = _state.WithProjectDocumentsOrder(projectId, documentIds);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to include
/// the specified project reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectReference"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project already references the target project.</exception>
public Solution AddProjectReference(ProjectId projectId, ProjectReference projectReference)
{
return AddProjectReferences(projectId,
SpecializedCollections.SingletonEnumerable(
projectReference ?? throw new ArgumentNullException(nameof(projectReference))));
}
/// <summary>
/// Create a new solution instance with the project specified updated to include
/// the specified project references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="projectReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project already references the target project.</exception>
/// <exception cref="InvalidOperationException">Adding the project reference would create a circular dependency.</exception>
public Solution AddProjectReferences(ProjectId projectId, IEnumerable<ProjectReference> projectReferences)
{
CheckContainsProject(projectId);
// avoid enumerating multiple times:
var collection = projectReferences?.ToCollection();
PublicContract.RequireUniqueNonNullItems(collection, nameof(projectReferences));
foreach (var projectReference in collection)
{
if (_state.ContainsProjectReference(projectId, projectReference))
{
throw new InvalidOperationException(WorkspacesResources.The_project_already_references_the_target_project);
}
}
CheckCircularProjectReferences(projectId, collection);
CheckSubmissionProjectReferences(projectId, collection, ignoreExistingReferences: false);
var newState = _state.AddProjectReferences(projectId, collection);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to no longer
/// include the specified project reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectReference"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">The solution does not contain <paramref name="projectId"/>.</exception>
public Solution RemoveProjectReference(ProjectId projectId, ProjectReference projectReference)
{
if (projectReference == null)
{
throw new ArgumentNullException(nameof(projectReference));
}
CheckContainsProject(projectId);
var newState = _state.RemoveProjectReference(projectId, projectReference);
if (newState == _state)
{
throw new ArgumentException(WorkspacesResources.Project_does_not_contain_specified_reference, nameof(projectReference));
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to contain
/// the specified list of project references.
/// </summary>
/// <param name="projectId">Id of the project whose references to replace with <paramref name="projectReferences"/>.</param>
/// <param name="projectReferences">New project references.</param>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="projectReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
public Solution WithProjectReferences(ProjectId projectId, IEnumerable<ProjectReference>? projectReferences)
{
CheckContainsProject(projectId);
// avoid enumerating multiple times:
var collection = PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(projectReferences, nameof(projectReferences));
CheckCircularProjectReferences(projectId, collection);
CheckSubmissionProjectReferences(projectId, collection, ignoreExistingReferences: true);
var newState = _state.WithProjectReferences(projectId, collection);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to include the
/// specified metadata reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="metadataReference"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project already contains the specified reference.</exception>
public Solution AddMetadataReference(ProjectId projectId, MetadataReference metadataReference)
{
return AddMetadataReferences(projectId,
SpecializedCollections.SingletonEnumerable(
metadataReference ?? throw new ArgumentNullException(nameof(metadataReference))));
}
/// <summary>
/// Create a new solution instance with the project specified updated to include the
/// specified metadata references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="metadataReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="metadataReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project already contains the specified reference.</exception>
public Solution AddMetadataReferences(ProjectId projectId, IEnumerable<MetadataReference> metadataReferences)
{
CheckContainsProject(projectId);
// avoid enumerating multiple times:
var collection = metadataReferences?.ToCollection();
PublicContract.RequireUniqueNonNullItems(collection, nameof(metadataReferences));
foreach (var metadataReference in collection)
{
if (_state.ContainsMetadataReference(projectId, metadataReference))
{
throw new InvalidOperationException(WorkspacesResources.The_project_already_contains_the_specified_reference);
}
}
var newState = _state.AddMetadataReferences(projectId, collection);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to no longer include
/// the specified metadata reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="metadataReference"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project does not contain the specified reference.</exception>
public Solution RemoveMetadataReference(ProjectId projectId, MetadataReference metadataReference)
{
CheckContainsProject(projectId);
if (metadataReference == null)
{
throw new ArgumentNullException(nameof(metadataReference));
}
var newState = _state.RemoveMetadataReference(projectId, metadataReference);
if (newState == _state)
{
throw new InvalidOperationException(WorkspacesResources.Project_does_not_contain_specified_reference);
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to include only the
/// specified metadata references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="metadataReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="metadataReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
public Solution WithProjectMetadataReferences(ProjectId projectId, IEnumerable<MetadataReference> metadataReferences)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectMetadataReferences(
projectId,
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(metadataReferences, nameof(metadataReferences)));
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to include the
/// specified analyzer reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReference"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
public Solution AddAnalyzerReference(ProjectId projectId, AnalyzerReference analyzerReference)
{
return AddAnalyzerReferences(projectId,
SpecializedCollections.SingletonEnumerable(
analyzerReference ?? throw new ArgumentNullException(nameof(analyzerReference))));
}
/// <summary>
/// Create a new solution instance with the project specified updated to include the
/// specified analyzer references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="analyzerReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project already contains the specified reference.</exception>
public Solution AddAnalyzerReferences(ProjectId projectId, IEnumerable<AnalyzerReference> analyzerReferences)
{
CheckContainsProject(projectId);
if (analyzerReferences is null)
{
throw new ArgumentNullException(nameof(analyzerReferences));
}
var collection = analyzerReferences.ToImmutableArray();
PublicContract.RequireUniqueNonNullItems(collection, nameof(analyzerReferences));
foreach (var analyzerReference in collection)
{
if (_state.ContainsAnalyzerReference(projectId, analyzerReference))
{
throw new InvalidOperationException(WorkspacesResources.The_project_already_contains_the_specified_reference);
}
}
var newState = _state.AddAnalyzerReferences(projectId, collection);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to no longer include
/// the specified analyzer reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReference"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
/// <exception cref="InvalidOperationException">The project does not contain the specified reference.</exception>
public Solution RemoveAnalyzerReference(ProjectId projectId, AnalyzerReference analyzerReference)
{
CheckContainsProject(projectId);
if (analyzerReference == null)
{
throw new ArgumentNullException(nameof(analyzerReference));
}
var newState = _state.RemoveAnalyzerReference(projectId, analyzerReference);
if (newState == _state)
{
throw new InvalidOperationException(WorkspacesResources.Project_does_not_contain_specified_reference);
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to include only the
/// specified analyzer references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="projectId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="analyzerReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain <paramref name="projectId"/>.</exception>
public Solution WithProjectAnalyzerReferences(ProjectId projectId, IEnumerable<AnalyzerReference> analyzerReferences)
{
CheckContainsProject(projectId);
var newState = _state.WithProjectAnalyzerReferences(
projectId,
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(analyzerReferences, nameof(analyzerReferences)));
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance updated to include the specified analyzer reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReference"/> is <see langword="null"/>.</exception>
public Solution AddAnalyzerReference(AnalyzerReference analyzerReference)
{
return AddAnalyzerReferences(
SpecializedCollections.SingletonEnumerable(
analyzerReference ?? throw new ArgumentNullException(nameof(analyzerReference))));
}
/// <summary>
/// Create a new solution instance updated to include the specified analyzer references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="analyzerReferences"/> contains duplicate items.</exception>
/// <exception cref="InvalidOperationException">The solution already contains the specified reference.</exception>
public Solution AddAnalyzerReferences(IEnumerable<AnalyzerReference> analyzerReferences)
{
// avoid enumerating multiple times:
var collection = analyzerReferences?.ToCollection();
PublicContract.RequireUniqueNonNullItems(collection, nameof(analyzerReferences));
foreach (var analyzerReference in collection)
{
if (_state.AnalyzerReferences.Contains(analyzerReference))
{
throw new InvalidOperationException(WorkspacesResources.The_solution_already_contains_the_specified_reference);
}
}
var newState = _state.AddAnalyzerReferences(collection);
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
/// <summary>
/// Create a new solution instance with the project specified updated to no longer include
/// the specified analyzer reference.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReference"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The solution does not contain the specified reference.</exception>
public Solution RemoveAnalyzerReference(AnalyzerReference analyzerReference)
{
if (analyzerReference == null)
{
throw new ArgumentNullException(nameof(analyzerReference));
}
var newState = _state.RemoveAnalyzerReference(analyzerReference);
if (newState == _state)
{
throw new InvalidOperationException(WorkspacesResources.Solution_does_not_contain_specified_reference);
}
return new Solution(newState);
}
/// <summary>
/// Creates a new solution instance with the specified analyzer references.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="analyzerReferences"/> contains <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="analyzerReferences"/> contains duplicate items.</exception>
public Solution WithAnalyzerReferences(IEnumerable<AnalyzerReference> analyzerReferences)
{
var newState = _state.WithAnalyzerReferences(
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(analyzerReferences, nameof(analyzerReferences)));
if (newState == _state)
{
return this;
}
return new Solution(newState);
}
private static SourceCodeKind GetSourceCodeKind(ProjectState project)
=> project.ParseOptions != null ? project.ParseOptions.Kind : SourceCodeKind.Regular;
/// <summary>
/// Creates a new solution instance with the corresponding project updated to include a new
/// document instance defined by its name and text.
/// </summary>
public Solution AddDocument(DocumentId documentId, string name, string text, IEnumerable<string>? folders = null, string? filePath = null)
=> this.AddDocument(documentId, name, SourceText.From(text), folders, filePath);
/// <summary>
/// Creates a new solution instance with the corresponding project updated to include a new
/// document instance defined by its name and text.
/// </summary>
public Solution AddDocument(DocumentId documentId, string name, SourceText text, IEnumerable<string>? folders = null, string? filePath = null, bool isGenerated = false)
{
if (documentId == null)
{
throw new ArgumentNullException(nameof(documentId));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
var project = _state.GetProjectState(documentId.ProjectId);
if (project == null)
{
throw new InvalidOperationException(string.Format(WorkspacesResources._0_is_not_part_of_the_workspace, documentId.ProjectId));
}
var version = VersionStamp.Create();
var loader = TextLoader.From(TextAndVersion.Create(text, version, name));
var info = DocumentInfo.Create(
documentId,
name: name,