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

Honour .editorconfig files when formatting new projects #60450

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ private async Task FormatDocumentCreatedFromTemplateAsync(IVsHierarchy hierarchy
name: nameof(FormatDocumentCreatedFromTemplate),
assemblyName: nameof(FormatDocumentCreatedFromTemplate),
language: LanguageName);

// We have to discover .editorconfig files ourselves to ensure that code style rules are followed.
// Normally the project system would tell us about these.
projectToAddTo = AddEditorConfigFiles(projectToAddTo, Path.GetDirectoryName(filePath));
}

// We need to ensure that decisions made during new document formatting are based on the right language
Expand Down Expand Up @@ -361,5 +365,63 @@ private async Task FormatDocumentCreatedFromTemplateAsync(IVsHierarchy hierarchy
formattedText.Write(textWriter, cancellationToken: CancellationToken.None);
});
}

private static Project AddEditorConfigFiles(Project projectToAddTo, string projectFolder)
{
do
{
projectToAddTo = AddEditorConfigFile(projectToAddTo, projectFolder, out var foundRoot);

if (foundRoot)
{
break;
}
projectFolder = Path.GetDirectoryName(projectFolder);
}
while (projectFolder is not null);

return projectToAddTo;

static Project AddEditorConfigFile(Project project, string folder, out bool foundRoot)
{
const string EditorConfigFileName = ".editorconfig";

foundRoot = false;

var editorConfigFile = Path.Combine(folder, EditorConfigFileName);
davidwengier marked this conversation as resolved.
Show resolved Hide resolved

var text = IOUtilities.PerformIO(() =>
{
using var stream = File.OpenRead(editorConfigFile);
return SourceText.From(stream);
});

if (text is null)
{
return project;
}

// We need to stop looking in the file system if the .editorconfig file is a root file
// The root property must be at the top of the file, barring comments and empty lines,
// so we can just do a simple parse. If this misses some odd cases we're okay with it as
// it's only used for the temporary project anyway.
foreach (var line in text.Lines)
{
if (line.Span.Length == 0 ||
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
text[line.Start] == '#')
{
continue;
}
else if (line.Span.Contains(text.IndexOf("root", line.Start, true)))
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
{
foundRoot = true;
}

break;
}

return project.AddAnalyzerConfigDocument(EditorConfigFileName, text, filePath: editorConfigFile).Project;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable disable

using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
Expand Down Expand Up @@ -54,5 +55,55 @@ public void CreateSDKProjectWithFileScopedNamespaces()
VisualStudio.ErrorList.ShowErrorList();
VisualStudio.ErrorList.Verify.NoErrors();
}

[WpfFact]
[WorkItem(60449, "https://github.com/dotnet/roslyn/issues/60449")]
public void CreateSDKProjectWithBlockScopedNamespacesFromEditorConfig()
{
var project = new ProjectUtils.Project("TestProj");

VisualStudio.Workspace.SetFileScopedNamespaces(true);

var editorConfigFilePath = Path.Combine(VisualStudio.SolutionExplorer.DirectoryName, ".editorconfig");
File.WriteAllText(editorConfigFilePath,
@"
root = true

[*.cs]
csharp_style_namespace_declarations = block_scoped
");

VisualStudio.SolutionExplorer.AddProject(project, WellKnownProjectTemplates.CSharpNetCoreClassLibrary, LanguageNames.CSharp);

VisualStudio.ErrorList.ShowErrorList();
VisualStudio.ErrorList.Verify.NoErrors();

VisualStudio.Editor.Verify.TextContains("namespace TestProj\r\n{");
}

[WpfFact]
[WorkItem(60449, "https://github.com/dotnet/roslyn/issues/60449")]
public void CreateSDKProjectWithFileScopedNamespacesFromEditorConfig()
{
var project = new ProjectUtils.Project("TestProj");

VisualStudio.Workspace.SetFileScopedNamespaces(false);

var editorConfigFilePath = Path.Combine(VisualStudio.SolutionExplorer.DirectoryName, ".editorconfig");
File.WriteAllText(editorConfigFilePath,
@"
root = true

[*.cs]
csharp_style_namespace_declarations = file_scoped
");

VisualStudio.SolutionExplorer.AddProject(project, WellKnownProjectTemplates.CSharpNetCoreClassLibrary, LanguageNames.CSharp);

VisualStudio.ErrorList.ShowErrorList();
VisualStudio.ErrorList.Verify.NoErrors();

VisualStudio.Editor.Verify.TextContains("namespace TestProj;");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public SolutionExplorer_OutOfProc(VisualStudioInstance visualStudioInstance)
_inProc = CreateInProcComponent<SolutionExplorer_InProc>(visualStudioInstance);
}

public string DirectoryName
=> _inProc.DirectoryName;

public void CloseSolution(bool saveFirst = false)
=> _inProc.CloseSolution(saveFirst);

Expand Down