elixir-tools / credo-language-server

LSP implementation for Credo.
MIT License
96 stars 11 forks source link

feat: code action for refactoring UnlessWithElse #35

Open CoderDennis opened 1 year ago

CoderDennis commented 1 year ago

Issue #17

This is a very rough draft. Just experimenting with the AST transforms and haven't chosen good variable names for everything yet. The adjust_comment_lines functions represent something I tried that didn't help.

CoderDennis commented 1 year ago

@mhanberg This code assumes that the diagnostic from Credo contains the whole text of the unless and else blocks that would need to be refactored. Based on what I've gathered around the fact that only the first character gets highlighted, I'm guessing that may not be the case. I had started down the path of inspecting the diagnostic struct but ran into issues running the locally built version as we discussed elsewhere.

mhanberg commented 1 year ago

So running it locally should work on main now if you rebase.

You can just do bin/start --port 9000 in the credo-language-server repo and then open your editor to any arbitrary repo.

With regard to the diagnostic, yes, Credo only tells you the line and col where the issue begins.

It might be sufficient to get the AST node that begins at that position (the entire document should be stored in the gen_lsp state) and work from there.

I have been considering patching Credo and upstreaming it, but that would obviously only work for the latest version (as well as the Credo codebase is... a beast, so might be hard to patch it in).

mhanberg commented 1 year ago

@mhanberg This code assumes that the diagnostic from Credo contains the whole text of the unless and else blocks that would need to be refactored. Based on what I've gathered around the fact that only the first character gets highlighted, I'm guessing that may not be the case. I had started down the path of inspecting the diagnostic struct but ran into issues running the locally built version as we discussed elsewhere.

I'd have to double check, but an issue has a trigger field on it that might have the source code you want to manipulate

edit: actually it looks like in this case, it would just be "unless"

mhanberg commented 1 year ago

Related: https://github.com/rrrene/credo/issues/1047