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

Improve development experience #192

Merged
merged 3 commits into from
May 22, 2023
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,17 @@ This extension is based on the [Template for VS Code Python tools extensions](ht
- To automatically format the codebase, run: `just fmt`.
- To run lint and type checks, run: `just check`.
- To run tests, run: `just test`.

To run the extension, navigate to `src/extension.ts` and run (`F5`). You should see the LSP output
and Python log messages in the debug console under "Python Server".

### Modifying the LSP

- Clone [ruff-lsp](https://github.com/charliermarsh/ruff-lsp) to e.g. `../ruff-lsp`
- Go to `../ruff-lsp` and run `pip install -t ../ruff-vscode/bundled/libs/ -e .`

### Custom ruff build

- Clone [ruff](https://github.com/charliermarsh/ruff) to e.g. `/home/ferris/ruff`
- `cargo build` in ruff
- In the settings, add `/home/ferris/ruff/target/debug/ruff` to "Ruff: Path"
48 changes: 43 additions & 5 deletions bundled/tool/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,68 @@

from __future__ import annotations

import logging
import logging.config
import os
import pathlib
import site
import sys

BUNDLE_DIR = pathlib.Path(__file__).parent.parent
logger = logging.getLogger(__name__)


# Update sys.path before importing any bundled libraries.
def update_sys_path(path_to_add: str) -> None:
"""Add given path to `sys.path`."""
if path_to_add not in sys.path and os.path.isdir(path_to_add):
# site adds the directory at the end if it is not yet present, we want it to
# be front
sys.path.insert(0, path_to_add)
# Allow dev installs into bundled/libs
site.addsitedir(path_to_add)


# Ensure that we can import LSP libraries, and other bundled libraries.
update_sys_path(os.fspath(BUNDLE_DIR / "libs"))
def main():
import ruff_lsp
from ruff_lsp import server

logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"simple": {"format": "%(asctime)s %(levelname)-4s %(message)s"}
},
"handlers": {
"stderr": {
"class": "logging.StreamHandler",
"formatter": "simple",
},
},
"root": {"level": "INFO", "handlers": ["stderr"]},
"loggers": {
# Don't repeat every message
"pygls.protocol": {
"level": "WARN",
"handlers": ["stderr"],
Copy link
Member

Choose a reason for hiding this comment

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

Is my assumption correct that this writes to stderr and not the output pane?

Copy link
Member Author

Choose a reason for hiding this comment

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

It also writes to the output pane just in red instead of blue

Copy link
Member

Choose a reason for hiding this comment

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

Oh nice. Is this because the vs code extension redirects stderr to the output panel?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, i think they generally just gather all output

"propagate": False,
},
},
}
)

# Start the server.
if __name__ == "__main__":
from ruff_lsp import server
logger.info(f"ruff path: {ruff_lsp.__file__}, sys.path: {sys.path}")

if not hasattr(server, "set_bundle"):
raise RuntimeError("ruff-vscode needs at least ruff-lsp v0.0.6")

server.set_bundle(os.fspath(BUNDLE_DIR / "libs" / "bin" / server.TOOL_MODULE))
server.start()


# Start the server.
if __name__ == "__main__":
# Ensure that we can import LSP libraries, and other bundled libraries.
update_sys_path(os.fspath(BUNDLE_DIR / "libs"))
main()