static void Main(string[] args) { var workspace = Workspace.LoadSolution(@"..\..\..\code\SampleCode1\SampleCode1.sln"); // Take a snapshot of the original solution. var originalSolution = workspace.CurrentSolution; // Declare a variable to store the intermediate solution snapshot at each step. ISolution newSolution = originalSolution; foreach (var project in originalSolution.Projects) { // Note how we can't simply iterate over project.Documents because it will return // IDocument objects from the originalSolution, not from the newSolution. We need to // use the DocumentId (that doesn't change) to look up the corresponding snapshot of // the document in the newSolution. foreach (var documentId in project.DocumentIds) { // Look up the snapshot for the original document in the latest forked solution. var document = newSolution.GetDocument(documentId); // Get a transformed version of the document (a new solution snapshot is created // under the covers to contain it - none of the existing objects are modified). var newDocument = document.OrganizeImports(); // Store the solution implicitly constructed in the previous step as the latest // one so we can continue building it up in the next iteration. newSolution = newDocument.Project.Solution; } } // Actually apply the accumulated changes and save them to disk. At this point // workspace.CurrentSolution is updated to point to the new solution. workspace.ApplyChanges(originalSolution, newSolution); }
private IDocument SetSubmissionDocument(ITextContainer textContainer, IProject project) { DocumentId id; ISolution solution = project.Solution.AddDocument(project.Id, project.Name, textContainer.CurrentText, out id); _workspace.SetCurrentSolution(solution); _workspace.OpenDocument(id, textContainer); return(solution.GetDocument(id)); }
public void CreateWorkspace(String code) { solutionId = SolutionId.CreateNewId("TestSolution"); solution = Solution.Create(solutionId) .AddCSharpProject("TestProject", "TestProject.dll", out projectId) .AddMetadataReference(projectId, MetadataReference.Create("mscorlib")) .AddDocument(projectId, "TestDocument", code, out documentId); document = solution.GetDocument(documentId); cancellationToken = default(CancellationToken); }
public CodeActionEdit GetEdit(CancellationToken cancellationToken) { SyntaxNode root = (SyntaxNode)this.document.GetSyntaxRoot(cancellationToken); ISemanticModel model = this.document.GetSemanticModel(cancellationToken); // Change accessibility if needed. // Considers: // private -> protected // protected -> protected // internal protected -> internal protected // internal -> internal // public -> public FieldDeclarationSyntax newFieldDeclaration = this.fieldDeclaration.ReplaceToken( Syntax.Token(SyntaxKind.PrivateKeyword), Syntax.Token(SyntaxKind.ProtectedKeyword)); ISolution solution = this.document.Project.Solution; if (solution == null) { return(null); } // Find document instance where the base type is declared IDocument baseTypeDocument = solution.GetDocument(this.baseTypeDeclaration.SyntaxTree); if (baseTypeDocument == null) { return(null); } PullUpFieldRewriter visitor = new PullUpFieldRewriter(model, this.fieldDeclaration, newFieldDeclaration, this.containingTypeDeclaration, this.baseTypeDeclaration); SyntaxNode newRoot = visitor.Visit(root); ISolution updatedSolution = this.document.Project.Solution.UpdateDocument(this.document.Id, newRoot); // If base type is declared in other document, syntax rewriter is executed also for this document if (baseTypeDocument.Id != this.document.Id) { PullUpFieldRewriter baseVisitor = new PullUpFieldRewriter(model, this.fieldDeclaration, newFieldDeclaration, this.containingTypeDeclaration, this.baseTypeDeclaration); SyntaxNode newBaseRoot = baseVisitor.Visit((SyntaxNode)baseTypeDocument.GetSyntaxRoot(cancellationToken)); updatedSolution = updatedSolution.UpdateDocument(baseTypeDocument.Id, newBaseRoot); } return(new CodeActionEdit(updatedSolution)); }
public void ProcessCSharpSolutionDocuments(Func <ISolution, IDocument, CompilationUnitSyntax, ISolution> processItem) { bool solutionModified = false; var workspace = Workspace.LoadSolution(_solutionPath); var projects = GetProjects(workspace); // Take a snapshot of the original solution. var originalSolution = workspace.CurrentSolution; // Declare a variable to store the intermediate solution snapshot at each step. ISolution latestForkedSolution = workspace.CurrentSolution; foreach (var project in projects) { foreach (var documentId in project.DocumentIds) { // Look up the snapshot for the original document in the latest forked solution. var document = latestForkedSolution.GetDocument(documentId); if (document.LanguageServices.Language != LanguageNames.CSharp || _ignorableFileEndings.Any(end => document.FilePath.EndsWith(end, StringComparison.OrdinalIgnoreCase))) { continue; } var root = GetDocumentRootIfNamespaceMatches(document); if (root == null) { continue; } var modifiedSolution = processItem(latestForkedSolution, document, root); if (modifiedSolution != null) { latestForkedSolution = modifiedSolution; solutionModified = true; } } } if (solutionModified) { workspace.ApplyChanges(originalSolution, latestForkedSolution); } }
static void Main(string[] args) { var workspace = Workspace.LoadSolution(@"..\..\..\code\SampleCode1\SampleCode1.sln"); var originalSolution = workspace.CurrentSolution; ISolution newSolution = originalSolution; foreach (var project in originalSolution.Projects) { foreach (var documentId in project.DocumentIds) { var document = newSolution.GetDocument(documentId); // Transform the syntax tree of the document and get the root of the new tree CommonSyntaxNode newRoot = TransformSyntaxRoot(document); newSolution = newSolution.UpdateDocument(document.Id, newRoot); } } workspace.ApplyChanges(originalSolution, newSolution); }