当前位置: 首页>>代码示例>>C#>>正文


C# Workspace.TryApplyChanges方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.Workspace.TryApplyChanges方法的典型用法代码示例。如果您正苦于以下问题:C# Workspace.TryApplyChanges方法的具体用法?C# Workspace.TryApplyChanges怎么用?C# Workspace.TryApplyChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.Workspace的用法示例。


在下文中一共展示了Workspace.TryApplyChanges方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FormatAsync

        private async Task FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
        {
            var watch = new Stopwatch();
            watch.Start();

            var originalSolution = workspace.CurrentSolution;
            var solution = await FormatCoreAsync(originalSolution, documentIds, cancellationToken);

            watch.Stop();

            if (!workspace.TryApplyChanges(solution))
            {
                FormatLogger.WriteErrorLine("Unable to save changes to disk");
            }

            FormatLogger.WriteLine("Total time {0}", watch.Elapsed);
        }
开发者ID:OliverKurowski,项目名称:StylecopCodeFormatter,代码行数:17,代码来源:FormattingEngineImplementation.cs

示例2: RunAsync

        public async Task RunAsync(Workspace workspace)
        {
            if (workspace == null) { throw new ArgumentNullException(nameof(workspace)); }
            if (workspace.CurrentSolution == null)
            {
                throw new ArgumentException("Workspace does not have an open CurrentSolution", nameof(workspace));
            }

            // Apply global options
            workspace.Options = this.globalOptions.ApplyOptions(workspace.Options);

            Solution solution = workspace.CurrentSolution;

            foreach (DocumentId documentId in solution.Projects.SelectMany(p => p.DocumentIds))
            {
                Document document = solution.GetDocument(documentId);
                if (await this.ShouldExcludeAsync(document))
                {
                    continue;
                }

                IReadOnlyList<Export<IStyleRule, StyleRuleMetadata>> styleRules;
                if (this.languageRuleMap.TryGetValue(document.Project.Language, out styleRules))
                {
                    foreach (Export<IStyleRule, StyleRuleMetadata> styleRule in styleRules)
                    {
                        // Use GetDocument each time to ensure we get the corresponding document from the potentially
                        // mutated solution.
                        Document currentDocument = solution.GetDocument(documentId);

                        currentDocument.ApplyOptions(styleRule.OptionApplier);

                        using (new PerformanceTracer(styleRule.Metadata.Name, currentDocument.Name))
                        {
                            solution = await styleRule.Part.EnforceAsync(currentDocument);
                        }
                    }
                }
            }

            // If we have an ISourceRepository and we have made changes to the solution, checkout all changed documents
            // before applying the changes.
            if (this.repository != null)
            {
                SolutionChanges solutionChanges = solution.GetChanges(workspace.CurrentSolution);
                foreach (ProjectChanges projectChanges in solutionChanges.GetProjectChanges())
                {
                    foreach (DocumentId changedDocumentId in projectChanges.GetChangedDocuments())
                    {
                        Document changedDocument = solution.GetDocument(changedDocumentId);
                        if (!await this.repository.HasPendingChangeAsync(changedDocument))
                        {
                            await this.repository.CheckOutAsync(changedDocument);
                        }
                    }
                }
            }

            Log.WriteVerbose("Applying solution changes");
            if (!workspace.TryApplyChanges(solution))
            {
                Log.WriteError("Unable to apply the changes to the solution");
            }

            Log.WriteVerbose("Style formatting complete");
        }
开发者ID:nicholjy,项目名称:stylize,代码行数:66,代码来源:StylizeEngine.cs

示例3: CompileHandlebarsFiles

 private static Workspace CompileHandlebarsFiles(Project project, Workspace workspace, List<string> hbsFiles, CompilerOptions options)
 {
     bool successFullCompilation = true;
       while(hbsFiles.Any() && successFullCompilation)
       {
     successFullCompilation = false;
     var nextRound = new List<string>();
     foreach(var file in hbsFiles)
     {
       var fileInfo = new FileInfo(file);
       string @namespace;
       bool compiledVersionExists = File.Exists($"{file}.cs");
       bool compiledVersionIsOlder = true;
       if (compiledVersionExists)
       {//Compiled Version already exists
     var compiledFileInfo = new FileInfo($"{file}.cs");
     compiledVersionIsOlder = (fileInfo.LastWriteTimeUtc > compiledFileInfo.LastWriteTimeUtc);
     @namespace = DetermineNamespace(compiledFileInfo);
       } else
       {
     @namespace = DetermineNamespace(fileInfo, project);
       }
       if (compiledVersionIsOlder || options.ForceRecompilation)
       {
     string content = File.ReadAllText(file);
     string name = Path.GetFileNameWithoutExtension(file);
     var compilationResult = CompileHandlebarsTemplate(content, @namespace, name, project, options);
     if (!options.DryRun)
     {
       if (compilationResult?.Item2?.Any() ?? false)
       {//Errors occured
         if (compilationResult.Item2.OfType<HandlebarsTypeError>().Any(x => x.Kind == HandlebarsTypeErrorKind.UnknownPartial))
         {//Unresolvable Partial... could be due to compiling sequence
           //Console.WriteLine($"Unresolved partial call for template '{name}'. Try again!");
           nextRound.Add(file);
         }
         else
           foreach (var error in compilationResult.Item2)
             PrintError(error);
       }
       else
       {
         successFullCompilation = true;
         //Check if template already exits
         var doc = project.Documents.FirstOrDefault(x => x.Name.Equals(string.Concat(name, ".hbs.cs")));
         if (doc != null)
         {//And change it if it does
           project = doc.WithSyntaxRoot(CSharpSyntaxTree.ParseText(SourceText.From(compilationResult.Item1)).GetRoot()).Project;
         }
         else
         {//Otherwise add a new document
           project = project.AddDocument(string.Concat(name, ".hbs.cs"), SourceText.From(compilationResult.Item1), GetFolderStructureForFile(fileInfo, project)).Project;
         }
         try {
           workspace.TryApplyChanges(project.Solution);
           project = workspace.CurrentSolution.Projects.First(x => x.Id.Equals(project.Id));
         } catch(NotSupportedException)
         {//ProjectJsonWorkspace does not support adding documents (as of 2016-02-17). So just add it manually
           File.WriteAllText($"{file}.cs", compilationResult.Item1);
         }
       }
     }
       }
     }
     hbsFiles = nextRound;
       }
       return workspace;
 }
开发者ID:Noxum,项目名称:CompiledHandlebars,代码行数:68,代码来源:Program.cs


注:本文中的Microsoft.CodeAnalysis.Workspace.TryApplyChanges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。