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

Add initial EditorConfig support #102

Merged
merged 2 commits into from
Apr 8, 2021
Merged

Add initial EditorConfig support #102

merged 2 commits into from
Apr 8, 2021

Conversation

ralish
Copy link
Contributor

@ralish ralish commented Jun 21, 2020

Projects may define shfmt settings as part of an .editorconfig file. Where these settings are present we probably want to use them so shell formatting is consistent with the project settings.

This commit introduces a new setting to enable usage of settings defined in an .editorconfig file: useEditorConfig (boolean). The default is false to preserve existing behaviour.

Enabling this setting does three things:

  1. Disables addition of any custom shfmt flags defined in the flag setting. If any custom flags are provided to shfmt it will ignore .editorconfig settings, making this setting mutually exclusive with the new setting.
  2. Disables addition of the -i (indent) flag for the same reason as above.
  3. Launches shfmt in the directory of the file we're formatting. This ensures shfmt is able to locate any .editorconfig file in the project (which may be further up the directory hierarchy).

@ralish
Copy link
Contributor Author

ralish commented Jun 21, 2020

This PR is flawed. It works in my configuration, but won't in many other cases due to the way shfmt is called. I'll see if I can make it a bit more durable to handle these other typical cases.

@ralish
Copy link
Contributor Author

ralish commented Jun 21, 2020

OK, new commit pushed which reworks my original approach of simply invoking shfmt in the same directory as the file being formatted. That worked on my setup because the shfmt settings in my .editorconfig were configured to apply to all files (i.e. [*]). It won't work for settings scoped to specific paths or extensions as the content to format is passed in via stdin.

New approach instead retrieves the EditorConfig configuration for the specific file to be formatted. It does so by introducing a dependency on the editorconfig package, with which it retrieves the configuration for the file via the parseSync() function. It then sets up the appropriate shfmt flags before invocation. All of this still only occurs if the user has enabled the useEditorConfig setting.

This commit also introduces a broader refactoring of the formatDocumentWithContent() function. It fixes a few miscellaneous issues and I think is now cleaner and easier to read. Hope that's OK!

@ralish
Copy link
Contributor Author

ralish commented Jan 16, 2021

Any chance this could be merged? I've just rebased it with all the recent commits :)

@foxundermoon foxundermoon merged commit 2afac93 into foxundermoon:master Apr 8, 2021
@ralish
Copy link
Contributor Author

ralish commented Apr 9, 2021

Thank-you for merging! Looking forward to this being in the next release :-)

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.

None yet

2 participants