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

apple-swift-format linter & fixer #3671

Merged
merged 1 commit into from
Apr 7, 2021
Merged

apple-swift-format linter & fixer #3671

merged 1 commit into from
Apr 7, 2021

Conversation

bosr
Copy link
Contributor

@bosr bosr commented Mar 31, 2021

Hi, thanks for ALE, it is awesome!

In this PR I propose a name change for apple/swift-format from swift-format to apple-swift-format. For instance, the global variable for the executable becomes g:ale_swift_appleswiftformat_executable.

  • I rename (and refactor a little bit) the existing linter support & tests by @klaaspieter
  • I add the apple-swift-format fixer and its test.
  • Both the linter and the fixer can leverage the g:ale_swift_appleswiftformat_use_swiftpm (for users using swift-format as their project dependency).

I hope you will agree with this naming change, as it distinguishes clearly in ALE the SwiftFormat (the community supported project) and the newer apple/swift-format project, enabling both projects to have their own linter and fixer.

Before this PR the status for swift-format was a bit unfortunate:

  • apple/swift-format has a linter, but no fixer
  • SwiftFormat has no linter, but a fixer.
  • naming confusion
  • no promotion in doc/ale-swift.vim

I hope this is now improved.

FYI @w0rp @klaaspieter (#3007) @WhyNotHugo (#3462)

Cheers

@bosr
Copy link
Contributor Author

bosr commented Mar 31, 2021

I'll fix the failing test, my bad. I did not run vader locally...

@bosr
Copy link
Contributor Author

bosr commented Mar 31, 2021

In the Vader linting tests for apple-swift-format, I'm getting [EXECUTE] (X) No linters were loaded. I'm missing something, could you provide a pointer/hint? Thanks!

Edit: fixed

@bosr
Copy link
Contributor Author

bosr commented Apr 1, 2021

I think the PR is now ready.

Copy link
Contributor

@hsanson hsanson left a comment

Choose a reason for hiding this comment

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

I do think the proposed change is very good but also is important to consider that there may be users that already use this tool with the g:swift_swiftformat_executable configuration. Changing this is a breaking change that goes against ALE philosophy:

Never introduce any breaking changes ever, unless users have been previously warned about them.

We cannot remove g:swift_swiftformat_executable variable so both must be supported giving priority to g:swift_apple_swift_format_executable. And in the documentation we mention that g:swift_swiftformat_executable is deprecated by g:swift_apple_swift_format_executable. Also ensure there are tests for falling back to g:swift_swiftformat_executable if defined.

Finally create an issue to completely remove g:swift_swiftformat_executable from the code after a while. Maybe one or two releases in the future.

ale_linters/swift/appleswiftformat.vim Outdated Show resolved Hide resolved
ale_linters/swift/appleswiftformat.vim Outdated Show resolved Hide resolved
call ale#Set('swift_appleswiftformat_executable', 'swift-format')
call ale#Set('swift_appleswiftformat_use_swiftpm', 0)

function! ale#fixers#appleswiftformat#GetExecutable(buffer) abort
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is exactly the same used in the linter. Better move it to autoload/ale/swift.vim file to keep the code DRY.

@bosr
Copy link
Contributor Author

bosr commented Apr 1, 2021

Hi, thanks for the review! I will happily apply the requested changes.

One clarification though: I think swiftformat should not be deprecated; on the contrary it should become available for the community-supported SwiftFormat project to bring linting support in ALE: today, it is quite hard for them because the name is already taken and the CLI is not compatible. Ideally, #3007 should have used a different name. Just as a reminder, the SwiftFormat project has a fixer in fixers/swiftformat.vim.

Back to your point on ALE philosophy (with which I agree), we need a migration path which does not break existing functionality. Here are the steps I have in mind:

  • apply the requested naming changes above
  • hold on factorizing the code in autoload/ale/swift.vim (because it would only help apple/swift-format and be incompatible with SwiftFormat)
  • re-instate the original support for apple/swift-format in ale_linters/swift/swiftformat.vim but also provide basic support for SwiftFormat in the same file, as follows:
    • introduce a new dictionary (or string, not yet sure) variable g:ale_swift_swiftformat_options (empty by default) to provide CLI options to SwiftFormat
    • modify the GetExecutable function to first locate the swiftformat binary (from SwiftFormat), then fallback on searching for the swift-format binary (from apple/swift-format). If swiftformat is found, the options dictionary is used, if swift-format is found the existing behavior (I mean from Add Apple's swift-format as a linter #3007) is maintained. This has to be documented in the doc/ale-swift.txt file.
  • as you said, in a few releases, drop support for apple/swift-format in the ale_linters/swift/swiftformat.vim and only keep it in ale_linters/swift/apple_swift_format.vim

I hope this sounds like a plan to solve this. I'll wait for your feedback to start coding.

@hsanson
Copy link
Contributor

hsanson commented Apr 3, 2021

@bosr I think I understand better what the issue is and what you are trying to do.

The fact that both SwiftFormat (Fixer) and Apple Swift Format (Linter) use the same variable name g:swift_swiftformat_executable to set their executable is unfortunate indeed. I think in this case the breaking change is necessary and can be tolerated.

When refactoring ale_linters#swift#appleswiftformat#GetExecutable() into ale/swift.vim, please name it something like ale#swift#GetAppleSwiftFormatExecutable(). This way in the future we can add ale#swift#GetSwiftFormatExecutable() for the other tool that can also be used as both linter and fixer.

@bosr
Copy link
Contributor Author

bosr commented Apr 5, 2021

@hsanson, before I make the changes, do you have a preference for the variable name?

g:ale_swift_appleswiftformat_use_swiftpm or g:ale_swift_apple_swift_format_use_swiftpm

In retrospect, I find the former to be more readable and aligned with others like g:ale_swift_sourcekitlsp.

@hsanson
Copy link
Contributor

hsanson commented Apr 5, 2021

@bosr I have no strong preference so please use whichever feels best for you that actually use this tool.

I do like to take the official tool name and replace the "/" and "-" chars with "_" to make the variables:

  • apple/swift-format becomes g:ale_swift_apple_swift_format_xxx
  • SwiftFormat becomes g:ale_swift_swiftformat_xxx
  • apple/sourcekit-lsp should have been g:ale_swift_apple_sourcekit_lsp_xxx
  • etc...

@bosr
Copy link
Contributor Author

bosr commented Apr 5, 2021

Hi @hsanson, I made some progress factoring the code in autoload, but I have a regression on the fixer tests which I cannot fix.

Starting Vader: /testplugin/test/fixers/test_appleswiftformat_fixer_callback.vader
    (1/2) [EXECUTE] The swiftformat callback should return the correct default values
    (1/2) [EXECUTE] (X) Vim(function):E127: Cannot redefine function ale#fixers#appleswiftformat#Fix: It is in use
      > /testplugin/autoload/ale/fixers/appleswiftformat.vim, line 16
    (2/2) [EXECUTE] The swiftformat callback should use swiftpm is use_swiftpm is set to 1
    (2/2) [EXECUTE] (X) Vim(function):E127: Cannot redefine function ale#fixers#appleswiftformat#Fix: It is in use
      > /testplugin/autoload/ale/fixers/appleswiftformat.vim, line 16
  Success/Total: 0/2

I've spent hours trying to fix it, so if you could help me, that would be great.

Thanks

@hsanson
Copy link
Contributor

hsanson commented Apr 6, 2021

@bosr this method is being used but does not seem to be defined anywhere: ale#fixers#appleswiftformat#GetArguments() I would start by defining it and see if this fixes that "cannot redefine" error.

@bosr
Copy link
Contributor Author

bosr commented Apr 6, 2021

Thanks a lot, that was right under my nose 🤦‍♂️.

Now all tests pass, the names could be changed, but if it up to me, I like them as they are because they are readable and match with other name.

@bosr
Copy link
Contributor Author

bosr commented Apr 6, 2021

Note: I have some issues using it on my local machine, so I need more time to debug before merging. Thanks.

@bosr
Copy link
Contributor Author

bosr commented Apr 6, 2021

Hello @hsanson, my local problems are solved, tests pass, and the local behavior is correct for the linter and fixer, with/without use_swiftpm, and with/without a .swift-format.

I would like to add one last test for the .swift-format config file. I know the behavior is correct locally: meaning ALE uses the available .swift-format from my project folder for both the linter and the fixer, but in the Vader tests I could not manage to use correctly the SetDirectory and SetFilename for the .swift-format to be located correctly and the --configuration /path/to/.swift-format argument to be passed in the test.

This test is commented in test/linter/test_swift_appleswiftformat.vader). Could you provide guidance on this? Thanks!

Copy link
Contributor

@hsanson hsanson left a comment

Choose a reason for hiding this comment

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

@bosr please check comments for tips on fixing the test.

test/linter/test_swift_appleswiftformat.vader Outdated Show resolved Hide resolved
test/linter/test_swift_appleswiftformat.vader Show resolved Hide resolved
@bosr
Copy link
Contributor Author

bosr commented Apr 7, 2021

Thanks for the hints!

FYI, my last updates were about passing the tests with --configuration on windows.

I think I'm good now.

Copy link
Contributor

@hsanson hsanson left a comment

Choose a reason for hiding this comment

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

Thanks for all the work and patience.

@hsanson hsanson merged commit f0887d3 into dense-analysis:master Apr 7, 2021
@bosr
Copy link
Contributor Author

bosr commented Apr 7, 2021

My pleasure! Thank you for the patience and guidance. It was much more laborious than expected, but thanks to you I learned a lot.

@bosr bosr deleted the apple-swift-format branch April 7, 2021 10:44
jsit added a commit to jsit/ale that referenced this pull request Apr 19, 2021
* origin/master: (219 commits)
  Updated `solhint` linter to be able to use a local installation (dense-analysis#3682)
  Don't exit visual mode on floating preview close (dense-analysis#3689)
  hadolint: fix color output and stdin shown as "-" (dense-analysis#3680)
  feat: Add protolint as linter and fixer (dense-analysis#2911)
  apple-swift-format: linter and fixer with config swiftpm support (dense-analysis#3671)
  improve DMD handler (dense-analysis#3647)
  Add support for V: "v" (compiler) and "vfmt" fixer. (dense-analysis#3622)
  Add nixfmt as a Nix fixer. (dense-analysis#3651)
  Switch to using buildifier's -path option (dense-analysis#3640)
  Add support for `ptop` fixer (dense-analysis#3652)
  Add more parameters to the DMD linting command (dense-analysis#3639)
  dense-analysis#3633 - Move linter tests into test/linter
  Allow more time before PRs become stale
  Add support for clangd with CUDA (dense-analysis#3598)
  add support for svelte via svelteserver language server (dense-analysis#3644)
  dense-analysis#3633 - Put all dummy test files in test/test-files
  Add desktop-file-validate
  Fix a typo in a test filename
  issue 3033 (dense-analysis#3620)
  dense-analysis#3632 Add ale#util#MapMatches
  ...
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