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

Implement codeAction/resolve #3

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ A [Language Server Protocol](https://microsoft.github.io/language-server-protoco
[Ruff](https://github.com/charliermarsh/ruff), an extremely fast Python linter and code transformation
tool, written in Rust.

Enables Ruff to be used in any editor that supports the LSP, including Neovim, Emacs, Sublime Text,
and more.
Enables Ruff to be used in any editor that supports the LSP, including [Neovim](#example--neovim),
[Sublime Text](#example--sublime-text), Emacs and more.

For Visual Studio Code users, check out the [Ruff VS Code extension](https://github.com/charliermarsh/ruff-vscode).
For Visual Studio Code, check out the [Ruff VS Code extension](https://github.com/charliermarsh/ruff-vscode).

## Highlights

Expand Down Expand Up @@ -83,13 +83,18 @@ if not configs.ruff_lsp then
cmd = { "ruff-lsp" },
filetypes = {'python'},
root_dir = require('lspconfig').util.find_git_ancestor,
settings = {},
settings = {
ruff_lsp = {
-- Any extra CLI arguments for `ruff` go here.
args = {}
}
}
}
}
end

require('lspconfig').ruff_lsp.setup {
on_attach = on_attach,
on_attach = on_attach,
}
```

Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ classifiers = [
ruff-lsp = "ruff_lsp.__main__:main"

[tool.poetry.dependencies]
python = "<3.12,>=3.7"
python = ">=3.7,<3.12"
pygls = ">1.0.0a3"
ruff = "*"
ruff = ">0.0.150"
typing_extensions = "*"

[tool.poetry.dev-dependencies]
Expand Down
22 changes: 21 additions & 1 deletion ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, Sequence, cast

from lsprotocol.types import (
CODE_ACTION_RESOLVE,
EXIT,
INITIALIZE,
TEXT_DOCUMENT_CODE_ACTION,
Expand Down Expand Up @@ -228,7 +229,7 @@ def apply_autofix(arguments: tuple[TextDocument]):
),
)
def code_action(params: CodeActionParams) -> list[CodeAction] | None:

"""LSP handler for textDocument/codeAction request."""
text_document = LSP_SERVER.workspace.get_document(params.text_document.uri)

if utils.is_stdlib_file(text_document.path):
Expand Down Expand Up @@ -331,6 +332,25 @@ def code_action(params: CodeActionParams) -> list[CodeAction] | None:
return actions if actions else None


@LSP_SERVER.feature(CODE_ACTION_RESOLVE)
def resolve_code_action(params: CodeAction) -> CodeAction:
"""LSP handler for codeAction/resolve request."""
text_document = LSP_SERVER.workspace.get_document(cast(str, params.data))

if params.kind == CodeActionKind.SourceOrganizeImports:
# Generate the "Ruff: Organize Imports" edit
params.edit = _create_workspace_edits(
text_document, _formatting_helper(text_document, select="I001") or []
)
elif params.kind == CodeActionKind.SourceFixAll:
# Generate the "Ruff: Fix All" edit.
params.edit = _create_workspace_edits(
text_document, _formatting_helper(text_document) or []
)

return params


def _formatting_helper(
document: workspace.Document, *, select: str | None = None
) -> list[TextEdit] | None:
Expand Down