/// <summary>
        /// Creates a solution that will be used as parent for the sources that need to be checked.
        /// </summary>
        /// <param name="projectId">The project identifier to use.</param>
        /// <param name="language">The language for which the solution is being created.</param>
        /// <returns>The created solution.</returns>
        protected virtual Solution CreateSolution(ProjectId projectId, string language)
        {
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);

            var solution = new AdhocWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, language)
                           .WithProjectCompilationOptions(projectId, compilationOptions)
                           .AddMetadataReference(projectId, MetadataReferences.CorlibReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
                           .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference)
                           .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference);

            solution.Workspace.Options =
                solution.Workspace.Options
                .WithChangedOption(FormattingOptions.IndentationSize, language, IndentationSize)
                .WithChangedOption(FormattingOptions.TabSize, language, TabSize)
                .WithChangedOption(FormattingOptions.UseTabs, language, UseTabs);

            var settings = GetSettings();

            var defaultSettings = new StyleCopSettings();

            if (IndentationSize != defaultSettings.Indentation.IndentationSize ||
                UseTabs != defaultSettings.Indentation.UseTabs ||
                TabSize != defaultSettings.Indentation.TabSize)
            {
                var indentationSettings = $@"
{{
  ""settings"": {{
    ""indentation"": {{
      ""indentationSize"": {IndentationSize},
      ""useTabs"": {UseTabs.ToString().ToLowerInvariant()},
      ""tabSize"": {TabSize}
    }}
  }}
}}
";

                if (string.IsNullOrEmpty(settings))
                {
                    settings = indentationSettings;
                }
                else
                {
                    var mergedSettings = JsonConvert.DeserializeObject <JObject>(settings);
                    mergedSettings.Merge(JsonConvert.DeserializeObject <JObject>(indentationSettings));
                    settings = JsonConvert.SerializeObject(mergedSettings);
                }
            }

            if (!string.IsNullOrEmpty(settings))
            {
                var documentId = DocumentId.CreateNewId(projectId);
                solution = solution.AddAdditionalDocument(documentId, SettingsHelper.SettingsFileName, settings);
            }

            var parseOptions = solution.GetProject(projectId).ParseOptions;

            return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose)));
        }
示例#2
0
 new LineFormattingOptions(
     UseTabs: useTabs,
     TabSize: tabSize,
     IndentationSize: indentationSize,
     NewLine: newLine),