mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
[red-knot] Implement basic LSP server (#12624)
## Summary This PR adds basic LSP implementation for the Red Knot project. This is basically a fork of the existing `ruff_server` crate into a `red_knot_server` crate. The following are the main differences: 1. The `Session` stores a map from workspace root to the corresponding Red Knot database (`RootDatabase`). 2. The database is initialized with the newly implemented `LSPSystem` (implementation of `System` trait) 3. The `LSPSystem` contains the server index corresponding to each workspace and an underlying OS system implementation. For certain methods, the system first checks if there's an open document in LSP system and returns the information from that. Otherwise, it falls back to the OS system to get that information. These methods are `path_metadata`, `read_to_string` and `read_to_notebook` 4. Add `as_any_mut` method for `System` **Why fork?** Forking allows us to experiment with the functionalities that are specific to Red Knot. The architecture is completely different and so the requirements for an LSP implementation are different as well. For example, Red Knot only supports a single workspace, so the LSP system needs to map the multi-workspace support to each Red Knot instance. In the end, the server code isn't too big, it will be easier to implement Red Knot specific functionality without worrying about existing server limitations and it shouldn't be difficult to port the existing server. ## Review Most of the server files hasn't been changed. I'm going to list down the files that have been changed along with highlight the specific part of the file that's changed from the existing server code. Changed files: * Red Knot CLI implementation: https://github.com/astral-sh/ruff/pull/12624/files#diff-579596339a29d3212a641232e674778c339b446de33b890c7fdad905b5eb50e1 * In https://github.com/astral-sh/ruff/pull/12624/files#diff-b9a9041a8a2bace014bf3687c3ef0512f25e0541f112fad6131b14242f408db6, server capabilities have been updated, dynamic capability registration is removed * In https://github.com/astral-sh/ruff/pull/12624/files#diff-b9a9041a8a2bace014bf3687c3ef0512f25e0541f112fad6131b14242f408db6, the API for `clear_diagnostics` now take in a `Url` instead of `DocumentQuery` as the document version doesn't matter when clearing diagnostics after a document is closed * [`did_close`](https://github.com/astral-sh/ruff/pull/12624/files#diff-9271370102a6f3be8defaca40c82485b0048731942520b491a3bdd2ee0e25493), [`did_close_notebook`](https://github.com/astral-sh/ruff/pull/12624/files#diff-96fb53ffb12c1694356e17313e4bb37b3f0931e887878b5d7c896c19ff60283b), [`did_open`](https://github.com/astral-sh/ruff/pull/12624/files#diff-60e852cf1aa771e993131cabf98eb4c467963a8328f10eccdb43b3e8f0f1fb12), [`did_open_notebook`](https://github.com/astral-sh/ruff/pull/12624/files#diff-ac356eb5e36c3b2c1c135eda9dfbcab5c12574d1cb77c71f7da8dbcfcfb2d2f1) are updated to open / close file from the corresponding Red Knot workspace * The [diagnostic handler](https://github.com/astral-sh/ruff/pull/12624/files#diff-4475f318fd0290d0292834569a7df5699debdcc0a453b411b8c3d329f1b879d9) is updated to request diagnostics from Red Knot * The [`Session::new`] method in https://github.com/astral-sh/ruff/pull/12624/files#diff-55c96201296200c1cab37c8b0407b6c733381374b94be7ae50563bfe95264e4d is updated to construct the Red Knot databases for each workspace. It also contains the `index_mut` and `MutIndexGuard` implementation * And, `LSPSystem` implementation is in https://github.com/astral-sh/ruff/pull/12624/files#diff-4ed62bd359c43b0bf1a13f04349dcd954966934bb8d544de7813f974182b489e ## Test Plan First, configure VS Code to use the `red_knot` binary 1. Build the `red_knot` binary by `cargo build` 2. Update the VS Code extension to specify the path to this binary ```json { "ruff.path": ["/path/to/ruff/target/debug/red_knot"] } ``` 3. Restart VS Code Now, open a file containing red-knot specific diagnostics, close the file and validate that diagnostics disappear.
This commit is contained in:
parent
d2c627efb3
commit
e91a0fe94a
44 changed files with 3912 additions and 1 deletions
|
@ -23,6 +23,7 @@ pub type FxDashSet<K> = dashmap::DashSet<K, BuildHasherDefault<FxHasher>>;
|
|||
pub trait Db: salsa::Database {
|
||||
fn vendored(&self) -> &VendoredFileSystem;
|
||||
fn system(&self) -> &dyn System;
|
||||
fn system_mut(&mut self) -> &mut dyn System;
|
||||
fn files(&self) -> &Files;
|
||||
}
|
||||
|
||||
|
@ -103,6 +104,10 @@ mod tests {
|
|||
&self.system
|
||||
}
|
||||
|
||||
fn system_mut(&mut self) -> &mut dyn System {
|
||||
&mut self.system
|
||||
}
|
||||
|
||||
fn files(&self) -> &Files {
|
||||
&self.files
|
||||
}
|
||||
|
|
|
@ -130,6 +130,8 @@ pub trait System: Debug {
|
|||
fn walk_directory(&self, path: &SystemPath) -> WalkDirectoryBuilder;
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any;
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -140,6 +142,14 @@ pub struct Metadata {
|
|||
}
|
||||
|
||||
impl Metadata {
|
||||
pub fn new(revision: FileRevision, permissions: Option<u32>, file_type: FileType) -> Self {
|
||||
Self {
|
||||
revision,
|
||||
permissions,
|
||||
file_type,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn revision(&self) -> FileRevision {
|
||||
self.revision
|
||||
}
|
||||
|
|
|
@ -112,6 +112,10 @@ impl System for OsSystem {
|
|||
self
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn read_directory(
|
||||
&self,
|
||||
path: &SystemPath,
|
||||
|
|
|
@ -132,6 +132,10 @@ impl System for TestSystem {
|
|||
self
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn read_directory<'a>(
|
||||
&'a self,
|
||||
path: &SystemPath,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue