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

Adding support for editorconfig #927

Merged
merged 14 commits into from
Sep 1, 2023
Merged

Adding support for editorconfig #927

merged 14 commits into from
Sep 1, 2023

Conversation

belav
Copy link
Owner

@belav belav commented Jul 14, 2023

closes #630

@belav belav marked this pull request as ready for review July 15, 2023 15:47
@belav
Copy link
Owner Author

belav commented Jul 15, 2023

I still need to update the doc, but this is ready for review..... actually looks like there are some test failures. Possibly a linux vs windows path issue.
When in the same directory, csharpierrc takes precedence over editorconfig.
Otherwise if no configuration is in the same directory the first parent configuration (csharpierrc or editorconfig) will win.
Includes support for editorconfig inheritance (not sure if they call it that). Rules from parent files can apply if a child file doesn't supply a value for that rule.

@belav
Copy link
Owner Author

belav commented Aug 11, 2023

@shocklateboy92 this was ready for review if you wanted to take a gander. It is a bigger change so I didn't want to skip over your watchful eye.

Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

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

I haven't read through the whole PR yet, but I figured the one comment I had so far was important enough to commit before going to bed tonight 🙂


internal static class ConfigFileParser
{
private static readonly Regex SectionRegex = new(@"^\s*\[(([^#;]|\\#|\\;)+)\]\s*([#;].*)?$");
Copy link
Collaborator

Choose a reason for hiding this comment

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

So, I'm not a super big fan of parsing source using regex.
People have built libraries that use actual parsers and lexers to handle all the weird corner cases properly.
editorconfig.org recommends:
https://github.com/editorconfig/editorconfig-core-net#readme

However, editorconfig is actually an INI file and there are plenty of well known libraries for those.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I was originally using that project, but it didn't support IFileSystem - https://github.com/editorconfig/editorconfig-core-net/pulls

I also realized it was built to find a editorconfig for a given file, which would be too slow for what csharpier needs.

That regex comes out of editorconfig-core-net, I did find https://github.com/rickyah/ini-parser just now. I will see if it is easy to pull in.

Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

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

Okay, I actually did have time to finish the review tonight (finally).

@belav
Copy link
Owner Author

belav commented Aug 25, 2023

Using ini-parser cleaned up the code and should do a much better job of parsing the files correctly.

Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

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

minor nit: feel free to complete this PR yourself without waiting for another review if you choose to address it.

var sections = new List<Section>();
foreach (var section in configData.Sections)
{
sections.Add(new Section(section, directory));
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I feel like this whole loop could have been a .Select().

}

var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(directoryName);
var editorConfigFiles = directoryInfo
Copy link
Contributor

Choose a reason for hiding this comment

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

Thus only a part of the .editorconfig of the project is considered. Since the project system / Visual Studio with <EditorConfigFiles /> allows to load further files. In addition Visual Studios extension the Global AnalyzerConfig.

Copy link
Owner Author

@belav belav Aug 31, 2023

Choose a reason for hiding this comment

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

This comes from the doc on AnalyzerConfig, so I don't think there need to be any changes to support it.

global configuration files can't be used to configure editor style settings for IDEs, such as indent size or whether to trim trailing whitespace. Instead, they are designed purely for specifying project-level analyzer configuration options.

I wasn't aware of EditorConfigFiles but I'll look into it.

Copy link
Owner Author

Choose a reason for hiding this comment

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

It sounds like usage of EditorConfigFiles is not recommended and Global AnalyzerConfig should be used instead. https://github.com/MicrosoftDocs/visualstudio-docs/issues/6051#issuecomment-1092262347 Which makes it sounds like this is more intended to be used for configuring analyzers.

There is an easy workaround of just adding an .editorconfig or .csahrpierrc to the root, so it doesn't seem critical to support EditorConfigFiles.

@belav belav merged commit e3c5fdd into main Sep 1, 2023
3 checks passed
@belav belav deleted the editorconfig branch September 1, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Csharpier .editorconfig
3 participants