Skip to content

Commit 86419e2

Browse files
committed
Create the ruff_server crate and the ruff server command.
1 parent a284c71 commit 86419e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+5374
-1
lines changed

Cargo.lock

Lines changed: 136 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ console_error_panic_hook = { version = "0.1.7" }
3232
console_log = { version = "1.0.0" }
3333
countme = { version ="3.0.1"}
3434
criterion = { version = "0.5.1", default-features = false }
35+
crossbeam = { version = "0.8.4" }
3536
dirs = { version = "5.0.0" }
3637
drop_bomb = { version = "0.1.5" }
3738
env_logger = { version ="0.10.1"}
@@ -52,10 +53,14 @@ is-macro = { version = "0.3.5" }
5253
is-wsl = { version = "0.4.0" }
5354
itertools = { version = "0.12.1" }
5455
js-sys = { version = "0.3.67" }
56+
jod-thread = { version = "0.1.2" }
5557
lalrpop-util = { version = "0.20.0", default-features = false }
5658
lexical-parse-float = { version = "0.8.0", features = ["format"] }
59+
libc = { version = "0.2.153" }
5760
libcst = { version = "1.1.0", default-features = false }
5861
log = { version = "0.4.17" }
62+
lsp-server = { version = "0.7.6" }
63+
lsp-types = { version = "0.95.0", features = ["proposed"] }
5964
memchr = { version = "2.7.1" }
6065
mimalloc = { version ="0.1.39"}
6166
natord = { version = "1.0.9" }
@@ -64,6 +69,7 @@ once_cell = { version = "1.19.0" }
6469
path-absolutize = { version = "3.1.1" }
6570
pathdiff = { version = "0.2.1" }
6671
pep440_rs = { version = "0.4.0", features = ["serde"] }
72+
phf = { version = "0.11.2", features = ["macros"] }
6773
pretty_assertions = "1.3.0"
6874
proc-macro2 = { version = "1.0.78" }
6975
pyproject-toml = { version = "0.9.0" }
@@ -98,6 +104,7 @@ toml = { version = "0.8.9" }
98104
tracing = { version = "0.1.40" }
99105
tracing-indicatif = { version = "0.3.6" }
100106
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
107+
tracing-tree = { version = "0.2.4" }
101108
typed-arena = { version = "2.0.2" }
102109
unic-ucd-category = { version ="0.9"}
103110
unicode-ident = { version = "1.0.12" }

crates/ruff/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ruff_macros = { path = "../ruff_macros" }
2020
ruff_notebook = { path = "../ruff_notebook" }
2121
ruff_python_ast = { path = "../ruff_python_ast" }
2222
ruff_python_formatter = { path = "../ruff_python_formatter" }
23+
ruff_server = { path = "../ruff_server" }
2324
ruff_source_file = { path = "../ruff_source_file" }
2425
ruff_text_size = { path = "../ruff_text_size" }
2526
ruff_workspace = { path = "../ruff_workspace" }
@@ -52,6 +53,8 @@ tempfile = { workspace = true }
5253
thiserror = { workspace = true }
5354
toml = { workspace = true }
5455
tracing = { workspace = true, features = ["log"] }
56+
tracing-subscriber = { workspace = true, features = ["registry"]}
57+
tracing-tree = { workspace = true }
5558
walkdir = { workspace = true }
5659
wild = { workspace = true }
5760

crates/ruff/src/args.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub enum Command {
8888
GenerateShellCompletion { shell: clap_complete_command::Shell },
8989
/// Run the Ruff formatter on the given files or directories.
9090
Format(FormatCommand),
91+
/// Run the language server.
92+
Server(ServerCommand),
9193
/// Display Ruff's version
9294
Version {
9395
#[arg(long, value_enum, default_value = "text")]
@@ -506,6 +508,9 @@ pub struct FormatCommand {
506508
pub range: Option<FormatRange>,
507509
}
508510

511+
#[derive(Clone, Debug, clap::Parser)]
512+
pub struct ServerCommand;
513+
509514
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
510515
pub enum HelpFormat {
511516
Text,

crates/ruff/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub(crate) mod format;
77
pub(crate) mod format_stdin;
88
pub(crate) mod linter;
99
pub(crate) mod rule;
10+
pub(crate) mod server;
1011
pub(crate) mod show_files;
1112
pub(crate) mod show_settings;
1213
pub(crate) mod version;

crates/ruff/src/commands/server.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use crate::ExitStatus;
2+
use anyhow::Result;
3+
use ruff_linter::logging::LogLevel;
4+
use ruff_server::Server;
5+
use tracing::{level_filters::LevelFilter, metadata::Level, subscriber::Interest, Metadata};
6+
use tracing_subscriber::{
7+
layer::{Context, Filter, SubscriberExt},
8+
Layer, Registry,
9+
};
10+
use tracing_tree::time::Uptime;
11+
12+
pub(crate) fn run_server(log_level: LogLevel) -> Result<ExitStatus> {
13+
let trace_level = if log_level == LogLevel::Verbose {
14+
Level::TRACE
15+
} else {
16+
Level::DEBUG
17+
};
18+
19+
let subscriber = Registry::default().with(
20+
tracing_tree::HierarchicalLayer::default()
21+
.with_indent_lines(true)
22+
.with_indent_amount(2)
23+
.with_bracketed_fields(true)
24+
.with_targets(true)
25+
.with_writer(|| Box::new(std::io::stderr()))
26+
.with_timer(Uptime::default())
27+
.with_filter(LoggingFilter { trace_level }),
28+
);
29+
30+
tracing::subscriber::set_global_default(subscriber)?;
31+
32+
let server = Server::new()?;
33+
34+
server.run().map(|()| ExitStatus::Success)
35+
}
36+
37+
struct LoggingFilter {
38+
trace_level: Level,
39+
}
40+
41+
impl LoggingFilter {
42+
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
43+
let filter = if meta.target().starts_with("ruff") {
44+
self.trace_level
45+
} else {
46+
Level::INFO
47+
};
48+
49+
meta.level() <= &filter
50+
}
51+
}
52+
53+
impl<S> Filter<S> for LoggingFilter {
54+
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool {
55+
self.is_enabled(meta)
56+
}
57+
58+
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest {
59+
if self.is_enabled(meta) {
60+
Interest::always()
61+
} else {
62+
Interest::never()
63+
}
64+
}
65+
66+
fn max_level_hint(&self) -> Option<LevelFilter> {
67+
Some(LevelFilter::from_level(self.trace_level))
68+
}
69+
}

0 commit comments

Comments
 (0)