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

[Feature] Add endpoints specifically for Organize Imports and Fix All #54

Closed
rockerBOO opened this issue Jan 16, 2023 · 6 comments · Fixed by #56
Closed

[Feature] Add endpoints specifically for Organize Imports and Fix All #54

rockerBOO opened this issue Jan 16, 2023 · 6 comments · Fixed by #56

Comments

@rockerBOO
Copy link
Contributor

Right now, Organize Imports and Fix All code actions fill the code actions list anywhere in any python code. There are settings to disable these code actions (it seems, I'm new to this LSP). I would like to disable the code actions and make each of these separate commands that could be run.

For example, in typescript-language-server has workspace/executeCommand which allows you to run these specific actions. This allows the functionality of these good code actions, but done separately of the code actions list.

Thanks for this LSP and the great ruff linter.

@yaegassy
Copy link
Contributor

yaegassy commented Jan 17, 2023

I believe it can be implemented in your language client using this code from ruff-vscode as a reference.

By the way, I use Vim/Neovim and my LSP client plugin was able to handle this.

@charliermarsh
Copy link
Member

Yeah we do have a dedicated "Fix all" action in the VS Code plugin. I'm not sure that this is solvable in general for all clients since it requires some modifications for the client itself. Where are you using the LSP? (Like, what editor, etc.?)

(Thanks for the kind words :))

@rockerBOO
Copy link
Contributor Author

I use Neovim with the built-in LSP client. I am aware the client will need custom additions to handle these.

Workspace command ruff.executeAutofix should work great for Fix All. Looks like an Organize Imports command would complete this.

Thank you, @yaegassy, I can take a look at those to implement it. @charliermarsh this should be adequate to implement a custom handler/command, thank you.

I will report back with my addition.

@charliermarsh
Copy link
Member

Ok cool -- does the LSP need a ruff.organizeImports action too?

(Separately, I don't think it solves your problem, but note that we do also support source.fixAll.ruff and source.organizeImports.ruff as aliases of source.fixAll and source.organizeImports.)

@rockerBOO
Copy link
Contributor Author

I think this would be about what would be needed. I made edits to the apply_autofix. I001 being the code to organize imports?

@LSP_SERVER.command("ruff.organizeImports")
def apply_organize_imports(arguments: tuple[TextDocument]):
    uri = arguments[0]["uri"]
    text_document = LSP_SERVER.workspace.get_document(uri)
    LSP_SERVER.apply_edit(
        _create_workspace_edits(text_document, _formatting_helper(text_document, only="I001") or []),
        "Ruff: Organize Imports",
    )

Not following what the source things are. Not seeing them in the code for the LSP inside the server.py, or things that could be them.

Thank you!

@charliermarsh
Copy link
Member

Yeah that looks reasonable. Feel free to send a PR.

I was referring to the latter two kinds in this code block:

@LSP_SERVER.feature(
    TEXT_DOCUMENT_CODE_ACTION,
    CodeActionOptions(
        code_action_kinds=[
            CodeActionKind.QuickFix,
            CodeActionKind.SourceFixAll,
            CodeActionKind.SourceOrganizeImports,
            f"{CodeActionKind.SourceFixAll}.ruff",
            f"{CodeActionKind.SourceOrganizeImports}.ruff",
        ],
        resolve_provider=True,
    ),
)
def code_action(params: CodeActionParams) -> list[CodeAction] | None:
    """LSP handler for textDocument/codeAction request."""
    ...

charliermarsh pushed a commit that referenced this issue Jan 19, 2023
Added support for `ruff.organizeImports` as a workspace command.

I couldn't properly test because I got very frustrated due to not being able to disable organize imports and fix all code actions. I didn't get to testing that this command would work properly. Wanted to post it though as others may be able to test easier and/or I can come back a little later and try again.

Fixes #54 

Thanks.
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 a pull request may close this issue.

3 participants