forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruff server
- A new built-in LSP for Ruff, written in Rust (astral-…
…sh#10158) <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary This PR introduces the `ruff_server` crate and a new `ruff server` command. `ruff_server` is a re-implementation of [`ruff-lsp`](https://github.com/astral-sh/ruff-lsp), written entirely in Rust. It brings significant performance improvements, much tighter integration with Ruff, a foundation for supporting entirely new language server features, and more! This PR is an early version of `ruff_lsp` that we're calling the **pre-release** version. Anyone is more than welcome to use it and submit bug reports for any issues they encounter - we'll have some documentation on how to set it up with a few common editors, and we'll also provide a pre-release VSCode extension for those interested. This pre-release version supports: - **Diagnostics for `.py` files** - **Quick fixes** - **Full-file formatting** - **Range formatting** - **Multiple workspace folders** - **Automatic linter/formatter configuration** - taken from any `pyproject.toml` files in the workspace. Many thanks to @MichaReiser for his [proof-of-concept work](astral-sh#7262), which was important groundwork for making this PR possible. ## Architectural Decisions I've made an executive choice to go with `lsp-server` as a base framework for the LSP, in favor of `tower-lsp`. There were several reasons for this: 1. I would like to avoid `async` in our implementation. LSPs are mostly computationally bound rather than I/O bound, and `async` adds a lot of complexity to the API, while also making harder to reason about execution order. This leads into the second reason, which is... 2. Any handlers that mutate state should be blocking and run in the event loop, and the state should be lock-free. This is the approach that `rust-analyzer` uses (also with the `lsp-server`/`lsp-types` crates as a framework), and it gives us assurances about data mutation and execution order. `tower-lsp` doesn't support this, which has caused some [issues](ebkalderon/tower-lsp#284) around data races and out-of-order handler execution. 3. In general, I think it makes sense to have tight control over scheduling and the specifics of our implementation, in exchange for a slightly higher up-front cost of writing it ourselves. We'll be able to fine-tune it to our needs and support future LSP features without depending on an upstream maintainer. ## Test Plan The pre-release of `ruff_server` will have snapshot tests for common document editing scenarios. An expanded test suite is on the roadmap for future version of `ruff_server`.
- Loading branch information
1 parent
a892fc7
commit 0c84fbb
Showing
45 changed files
with
5,425 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::ExitStatus; | ||
use anyhow::Result; | ||
use ruff_linter::logging::LogLevel; | ||
use ruff_server::Server; | ||
use tracing::{level_filters::LevelFilter, metadata::Level, subscriber::Interest, Metadata}; | ||
use tracing_subscriber::{ | ||
layer::{Context, Filter, SubscriberExt}, | ||
Layer, Registry, | ||
}; | ||
use tracing_tree::time::Uptime; | ||
|
||
pub(crate) fn run_server(log_level: LogLevel) -> Result<ExitStatus> { | ||
let trace_level = if log_level == LogLevel::Verbose { | ||
Level::TRACE | ||
} else { | ||
Level::DEBUG | ||
}; | ||
|
||
let subscriber = Registry::default().with( | ||
tracing_tree::HierarchicalLayer::default() | ||
.with_indent_lines(true) | ||
.with_indent_amount(2) | ||
.with_bracketed_fields(true) | ||
.with_targets(true) | ||
.with_writer(|| Box::new(std::io::stderr())) | ||
.with_timer(Uptime::default()) | ||
.with_filter(LoggingFilter { trace_level }), | ||
); | ||
|
||
tracing::subscriber::set_global_default(subscriber)?; | ||
|
||
let server = Server::new()?; | ||
|
||
server.run().map(|()| ExitStatus::Success) | ||
} | ||
|
||
struct LoggingFilter { | ||
trace_level: Level, | ||
} | ||
|
||
impl LoggingFilter { | ||
fn is_enabled(&self, meta: &Metadata<'_>) -> bool { | ||
let filter = if meta.target().starts_with("ruff") { | ||
self.trace_level | ||
} else { | ||
Level::INFO | ||
}; | ||
|
||
meta.level() <= &filter | ||
} | ||
} | ||
|
||
impl<S> Filter<S> for LoggingFilter { | ||
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool { | ||
self.is_enabled(meta) | ||
} | ||
|
||
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { | ||
if self.is_enabled(meta) { | ||
Interest::always() | ||
} else { | ||
Interest::never() | ||
} | ||
} | ||
|
||
fn max_level_hint(&self) -> Option<LevelFilter> { | ||
Some(LevelFilter::from_level(self.trace_level)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.