Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Roslyn's editorconfig support #1771

Merged
merged 6 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<PackageReference Update="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Update="Microsoft.TestPlatform.TranslationLayer" Version="15.7.2" />
<PackageReference Update="Microsoft.VisualStudio.CodingConventions" Version="1.1.20180503.2" />
<PackageReference Update="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="1.14.114" />
<PackageReference Update="Microsoft.VisualStudio.SDK.EmbedInteropTypes" Version="15.0.12" />

Expand Down
19 changes: 17 additions & 2 deletions src/OmniSharp.Cake/CakeProjectSystem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Cake.Scripting.Abstractions.Models;
using Microsoft.CodeAnalysis;
Expand All @@ -18,6 +20,7 @@
using OmniSharp.Helpers;
using OmniSharp.Mef;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.Roslyn.EditorConfig;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Services;

Expand Down Expand Up @@ -280,8 +283,19 @@ private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
throw new InvalidOperationException($"Could not get host object type: {cakeScript.Host.TypeName}.");
}

var projectId = ProjectId.CreateNewId(Guid.NewGuid().ToString());
var analyzerConfigDocuments = EditorConfigFinder
.GetEditorConfigPaths(filePath)
.Select(path =>
DocumentInfo.Create(
DocumentId.CreateNewId(projectId),
name: ".editorconfig",
loader: new FileTextLoader(path, Encoding.UTF8),
filePath: path))
.ToImmutableArray();

return ProjectInfo.Create(
id: ProjectId.CreateNewId(Guid.NewGuid().ToString()),
id: projectId,
version: VersionStamp.Create(),
name: name,
filePath: filePath,
Expand All @@ -292,7 +306,8 @@ private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
metadataReferences: GetMetadataReferences(cakeScript.References),
// TODO: projectReferences?
isSubmission: true,
hostObjectType: hostObjectType);
hostObjectType: hostObjectType)
.WithAnalyzerConfigDocuments(analyzerConfigDocuments);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same should be applied here

public static ProjectInfo CreateProjectInfo(this ProjectFileInfo projectFileInfo, IAnalyzerAssemblyLoader analyzerAssemblyLoader)
as well?

}

private IEnumerable<MetadataReference> GetMetadataReferences(IEnumerable<string> references)
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ItemNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal static class ItemNames
public const string Analyzer = nameof(Analyzer);
public const string AdditionalFiles = nameof(AdditionalFiles);
public const string Compile = nameof(Compile);
public const string EditorConfigFiles = nameof(EditorConfigFiles);
public const string PackageReference = nameof(PackageReference);
public const string ProjectReference = nameof(ProjectReference);
public const string ReferencePath = nameof(ReferencePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ private class ProjectData
public ImmutableArray<PackageReference> PackageReferences { get; }
public ImmutableArray<string> Analyzers { get; }
public ImmutableArray<string> AdditionalFiles { get; }
public ImmutableArray<string> AnalyzerConfigFiles { get; }

public RuleSet RuleSet { get; }
public ImmutableDictionary<string, string> ReferenceAliases { get; }
public ImmutableDictionary<string, string> ProjectReferenceAliases { get; }
Expand All @@ -70,6 +72,7 @@ private ProjectData()
PackageReferences = ImmutableArray<PackageReference>.Empty;
Analyzers = ImmutableArray<string>.Empty;
AdditionalFiles = ImmutableArray<string>.Empty;
AnalyzerConfigFiles = ImmutableArray<string>.Empty;
ReferenceAliases = ImmutableDictionary<string, string>.Empty;
ProjectReferenceAliases = ImmutableDictionary<string, string>.Empty;
}
Expand Down Expand Up @@ -154,6 +157,7 @@ private ProjectData(
ImmutableArray<PackageReference> packageReferences,
ImmutableArray<string> analyzers,
ImmutableArray<string> additionalFiles,
ImmutableArray<string> analyzerConfigFiles,
bool treatWarningsAsErrors,
string defaultNamespace,
bool runAnalyzers,
Expand All @@ -171,6 +175,7 @@ private ProjectData(
PackageReferences = packageReferences.EmptyIfDefault();
Analyzers = analyzers.EmptyIfDefault();
AdditionalFiles = additionalFiles.EmptyIfDefault();
AnalyzerConfigFiles = analyzerConfigFiles.EmptyIfDefault();
ReferenceAliases = referenceAliases;
ProjectReferenceAliases = projectReferenceAliases;
}
Expand Down Expand Up @@ -317,13 +322,14 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
var analyzers = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
var additionalFiles = GetFullPaths(projectInstance.GetItems(ItemNames.AdditionalFiles));
var editorConfigFiles = GetFullPaths(projectInstance.GetItems(ItemNames.EditorConfigFiles));

return new ProjectData(guid, name,
assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks,
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds,
signAssembly, assemblyOriginatorKeyFile,
sourceFiles, projectReferences.ToImmutable(), references.ToImmutable(), packageReferences, analyzers, additionalFiles, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset,
sourceFiles, projectReferences.ToImmutable(), references.ToImmutable(), packageReferences, analyzers, additionalFiles, editorConfigFiles, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset,
referenceAliases.ToImmutableDictionary(), projectReferenceAliases.ToImmutable());
}

Expand Down
7 changes: 4 additions & 3 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal partial class ProjectFileInfo
public ImmutableArray<PackageReference> PackageReferences => _data.PackageReferences;
public ImmutableArray<string> Analyzers => _data.Analyzers;
public ImmutableArray<string> AdditionalFiles => _data.AdditionalFiles;
public ImmutableArray<string> AnalyzerConfigFiles => _data.AnalyzerConfigFiles;
public ImmutableDictionary<string, string> ReferenceAliases => _data.ReferenceAliases;
public ImmutableDictionary<string, string> ProjectReferenceAliases => _data.ProjectReferenceAliases;
public bool TreatWarningsAsErrors => _data.TreatWarningsAsErrors;
Expand All @@ -77,15 +78,15 @@ internal static ProjectFileInfo CreateEmpty(string filePath)
{
var id = ProjectId.CreateNewId(debugName: filePath);

return new ProjectFileInfo(new ProjectIdInfo(id, isDefinedInSolution:false), filePath, data: null);
return new ProjectFileInfo(new ProjectIdInfo(id, isDefinedInSolution: false), filePath, data: null);
}

internal static ProjectFileInfo CreateNoBuild(string filePath, ProjectLoader loader)
{
var id = ProjectId.CreateNewId(debugName: filePath);
var project = loader.EvaluateProjectFile(filePath);
var data = ProjectData.Create(project);
//we are not reading the solution here
//we are not reading the solution here
var projectIdInfo = new ProjectIdInfo(id, isDefinedInSolution: false);

return new ProjectFileInfo(projectIdInfo, filePath, data);
Expand Down Expand Up @@ -127,7 +128,7 @@ public static (ProjectFileInfo, ImmutableArray<MSBuildDiagnostic>, ProjectLoaded

var data = ProjectData.Create(projectInstance);
var projectFileInfo = new ProjectFileInfo(ProjectIdInfo, FilePath, data);
var eventArgs = new ProjectLoadedEventArgs(Id, projectInstance, diagnostics, isReload: true, ProjectIdInfo.IsDefinedInSolution,data.References);
var eventArgs = new ProjectLoadedEventArgs(Id, projectInstance, diagnostics, isReload: true, ProjectIdInfo.IsDefinedInSolution, data.References);

return (projectFileInfo, diagnostics, eventArgs);
}
Expand Down
31 changes: 27 additions & 4 deletions src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private async Task ProcessLoopAsync(CancellationToken cancellationToken)
await Task.Delay(LoopDelay, cancellationToken);
ProcessQueue(cancellationToken);
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError($"Error occurred while processing project updates: {ex}");
}
Expand Down Expand Up @@ -377,6 +377,11 @@ private void WatchProjectFiles(ProjectFileInfo projectFileInfo)
QueueProjectUpdate(projectFileInfo.FilePath, allowAutoRestore: true, projectFileInfo.ProjectIdInfo);
});

_fileSystemWatcher.Watch(".editorconfig", (file, changeType) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this watch on projectFileInfo.AnalyzerConfigFiles instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it still need a catch-all to handle new .editorconfigs being added to subfolders? I expected this would be easy way of handling Add and Update.

{
QueueProjectUpdate(projectFileInfo.FilePath, allowAutoRestore: false, projectFileInfo.ProjectIdInfo);
});

if (projectFileInfo.RuleSet?.FilePath != null)
{
_fileSystemWatcher.Watch(projectFileInfo.RuleSet.FilePath, (file, changeType) =>
Expand Down Expand Up @@ -436,6 +441,7 @@ private void UpdateProject(string projectFilePath)
UpdateReferences(project, projectFileInfo.ProjectReferences, projectFileInfo.References);
UpdateAnalyzerReferences(project, projectFileInfo);
UpdateAdditionalFiles(project, projectFileInfo.AdditionalFiles);
UpdateAnalyzerConfigFiles(project, projectFileInfo.AnalyzerConfigFiles);
UpdateProjectProperties(project, projectFileInfo);

_workspace.TryPromoteMiscellaneousDocumentsToProject(project);
Expand Down Expand Up @@ -483,10 +489,27 @@ private void UpdateAdditionalFiles(Project project, IList<string> additionalFile

foreach (var file in additionalFiles)
{
if (File.Exists(file))
{
if (File.Exists(file))
{
_workspace.AddAdditionalDocument(project.Id, file);
}
}
}
}

private void UpdateAnalyzerConfigFiles(Project project, IList<string> analyzerConfigFiles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this code be conditioned on editorconfig being enabled in OmniSharp?
otherwise, if you disable it, it would be respected on formatting, but the analyzer options would still apply

{
var currentAnalyzerConfigDocuments = project.AnalyzerConfigDocuments;
JoeRobich marked this conversation as resolved.
Show resolved Hide resolved
foreach (var document in currentAnalyzerConfigDocuments)
{
_workspace.RemoveAnalyzerConfigDocument(document.Id);
}

foreach (var file in analyzerConfigFiles)
{
if (File.Exists(file))
{
_workspace.AddAnalyzerConfigDocument(project.Id, file);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/OmniSharp.Roslyn.CSharp/OmniSharp.Roslyn.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.VisualStudio.CodingConventions" />
</ItemGroup>
</Project>

This file was deleted.

This file was deleted.

Loading