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 all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ All changes to the project will be documented in this file.

## [1.35.1] - not yet released
* Fixed not supported exception when trying to decompile a BCL assembly on Mono. For now we do not try to resolve implementation assembly from a ref assembly (PR: [#1767](https://github.com/OmniSharp/omnisharp-roslyn/pull/1767))
* Added support for generic classes in test runner ([omnisharp-vscode#3722](https://github.com/OmniSharp/omnisharp-vscode/issues/3722), PR: [#1768](https://github.com/OmniSharp/omnisharp-roslyn/pull/1768))
* Added support for generic classes in test runner ([omnisharp-vscode#3722](https://github.com/OmniSharp/omnisharp-vscode/issues/3722), PR: [#1768](https://github.com/OmniSharp/omnisharp-roslyn/pull/1768))
* Improved autocompletion performance (PR: [#1761](https://github.com/OmniSharp/omnisharp-roslyn/pull/1761))
* Move to Roslyn's .editorconfig support ([#1657](https://github.com/OmniSharp/omnisharp-roslyn/issues/1657), PR: [#1771](https://github.com/OmniSharp/omnisharp-roslyn/pull/1771))

## [1.35.0] - 2020-04-10
* Support for `<RunAnalyzers />` and `<RunAnalyzersDuringLiveAnalysis />` (PR: [#1739](https://github.com/OmniSharp/omnisharp-roslyn/pull/1739))
Expand Down
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
21 changes: 19 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,21 @@ 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 = _workspace.EditorConfigEnabled
? EditorConfigFinder
.GetEditorConfigPaths(filePath)
.Select(path =>
DocumentInfo.Create(
DocumentId.CreateNewId(projectId),
name: ".editorconfig",
loader: new FileTextLoader(path, Encoding.UTF8),
filePath: path))
.ToImmutableArray()
: ImmutableArray<DocumentInfo>.Empty;

return ProjectInfo.Create(
id: ProjectId.CreateNewId(Guid.NewGuid().ToString()),
id: projectId,
version: VersionStamp.Create(),
name: name,
filePath: filePath,
Expand All @@ -292,7 +308,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
2 changes: 2 additions & 0 deletions src/OmniSharp.Host/WorkspaceInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static void Initialize(IServiceProvider serviceProvider, CompositionHost
projectEventForwarder.Initialize();
var projectSystems = compositionHost.GetExports<IProjectSystem>();

workspace.EditorConfigEnabled = options.CurrentValue.FormattingOptions.EnableEditorConfigSupport;
Copy link
Member

Choose a reason for hiding this comment

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

it would be good to also reset this flag here https://github.com/OmniSharp/omnisharp-roslyn/blob/master/src/OmniSharp.Host/WorkspaceInitializer.cs#L62-L65
this way it should be effective in real time when options change (since they are watched)

Copy link
Member Author

Choose a reason for hiding this comment

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

This would be useful if there were a way to force all the projects to update. Is this option worth all this extra complexity? I would assume the presence of an .editorconfig would be the indicator of whether a user wanted .editorconfig support.

Copy link
Member

Choose a reason for hiding this comment

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

you are right we can solve this systematically separately as there are other areas that would benefit from that


foreach (var projectSystem in projectSystems)
{
try
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
39 changes: 35 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,14 @@ private void WatchProjectFiles(ProjectFileInfo projectFileInfo)
QueueProjectUpdate(projectFileInfo.FilePath, allowAutoRestore: true, projectFileInfo.ProjectIdInfo);
});

if (_workspace.EditorConfigEnabled)
{
_fileSystemWatcher.Watch(".editorconfig", (file, changeType) =>
{
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 +444,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 +492,32 @@ 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

{
if (!_workspace.EditorConfigEnabled)
{
return;
}

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