private static Document CreateTestDocument(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4)
        {
            var workspace = DiagnosticVerifier.CreateWorkspace();

            workspace.Options = workspace.Options
                                .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, indentationSize)
                                .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, useTabs)
                                .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, tabSize);

            var projectId          = ProjectId.CreateNewId();
            var documentId         = DocumentId.CreateNewId(projectId);
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);

            var solution = workspace.CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .WithProjectCompilationOptions(projectId, compilationOptions)
                           .AddMetadataReference(projectId, MetadataReferences.CorlibReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
                           .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference)
                           .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference)
                           .AddDocument(documentId, TestFilename, SourceText.From(source));

            StyleCopSettings defaultSettings = new StyleCopSettings();

            if (indentationSize != defaultSettings.Indentation.IndentationSize ||
                useTabs != defaultSettings.Indentation.UseTabs ||
                tabSize != defaultSettings.Indentation.TabSize)
            {
                string settings           = $@"
{{
  ""settings"": {{
    ""indentation"": {{
      ""indentationSize"": {indentationSize},
      ""useTabs"": {useTabs.ToString().ToLowerInvariant()},
      ""tabSize"": {tabSize}
    }}
  }}
}}
";
                var    settingsDocumentId = DocumentId.CreateNewId(projectId);
                solution = solution.AddAdditionalDocument(documentId, SettingsHelper.SettingsFileName, settings);
            }

            return(solution.GetDocument(documentId));
        }
Example #2
0
        private static async Task <SyntaxTreeAnalysisContext> CreateAnalysisContextAsync(string stylecopJSON, string settingsFileName = SettingsHelper.SettingsFileName)
        {
            var projectId  = ProjectId.CreateNewId();
            var documentId = DocumentId.CreateNewId(projectId);

            var solution = DiagnosticVerifier.CreateWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .AddDocument(documentId, "Test0.cs", SourceText.From(string.Empty));

            var document   = solution.GetDocument(documentId);
            var syntaxTree = await document.GetSyntaxTreeAsync().ConfigureAwait(false);

            var stylecopJSONFile = new AdditionalTextHelper(settingsFileName, stylecopJSON);
            var additionalFiles  = ImmutableArray.Create <AdditionalText>(stylecopJSONFile);
            var analyzerOptions  = new AnalyzerOptions(additionalFiles);

            return(new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, rd => { }, isd => { return true; }, CancellationToken.None));
        }