Skip to content

Commit cb54a80

Browse files
autofix-ci[bot]Sysix
authored andcommitted
[autofix.ci] apply automated fixes
1 parent 15c2922 commit cb54a80

File tree

10 files changed

+245
-87
lines changed

10 files changed

+245
-87
lines changed

Cargo.lock

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

crates/oxc_language_server/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ oxc_diagnostics = { workspace = true }
2727
oxc_formatter = { workspace = true }
2828
oxc_linter = { workspace = true, features = ["language_server"] }
2929
oxc_parser = { workspace = true }
30-
oxc_span = { workspace = true }
3130

3231
#
3332
env_logger = { workspace = true, features = ["humantime"] }

crates/oxc_language_server/src/capabilities.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,19 @@ impl From<ClientCapabilities> for Capabilities {
4040
watched_files.dynamic_registration.is_some_and(|dynamic| dynamic)
4141
})
4242
});
43-
// TODO: enable it when we support formatting
44-
// let formatting = value.text_document.as_ref().is_some_and(|text_document| {
45-
// text_document.formatting.is_some_and(|formatting| {
46-
// formatting.dynamic_registration.is_some_and(|dynamic| dynamic)
47-
// })
48-
// });
43+
let dynamic_formatting = value.text_document.as_ref().is_some_and(|text_document| {
44+
text_document.formatting.is_some_and(|formatting| {
45+
formatting.dynamic_registration.is_some_and(|dynamic| dynamic)
46+
})
47+
});
4948

5049
Self {
5150
code_action_provider,
5251
workspace_apply_edit,
5352
workspace_execute_command,
5453
workspace_configuration,
5554
dynamic_watchers,
56-
dynamic_formatting: false,
55+
dynamic_formatting,
5756
}
5857
}
5958
}
@@ -100,9 +99,8 @@ impl From<Capabilities> for ServerCapabilities {
10099
} else {
101100
None
102101
},
103-
// ToDo: should by dynamic registered with workspace configuration
104-
// THIS MUST NOT BE COMMITTED TO MAIN!!!
105-
document_formatting_provider: Some(OneOf::Left(true)),
102+
// the server supports formatting, but it will tell the client if he enabled the setting
103+
document_formatting_provider: None,
106104
..ServerCapabilities::default()
107105
}
108106
}

crates/oxc_language_server/src/file_system.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ impl LSPFileSystem {
2121
self.files.pin().insert(uri.clone(), content);
2222
}
2323

24-
#[expect(dead_code)] // used for the oxc_formatter in the future
2524
pub fn get(&self, uri: &Uri) -> Option<String> {
2625
self.files.pin().get(uri).cloned()
2726
}
Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
1-
use serde::{Deserialize, Serialize};
1+
use serde::{Deserialize, Deserializer, Serialize, de::Error};
2+
use serde_json::Value;
23

3-
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
4+
#[derive(Debug, Default, Serialize, Clone)]
45
#[serde(rename_all = "camelCase")]
5-
pub struct FormatOptions;
6+
pub struct FormatOptions {
7+
pub experimental: bool,
8+
}
9+
10+
impl<'de> Deserialize<'de> for FormatOptions {
11+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
12+
where
13+
D: Deserializer<'de>,
14+
{
15+
let value = Value::deserialize(deserializer)?;
16+
FormatOptions::try_from(value).map_err(Error::custom)
17+
}
18+
}
19+
20+
impl TryFrom<Value> for FormatOptions {
21+
type Error = String;
22+
23+
fn try_from(value: Value) -> Result<Self, Self::Error> {
24+
let Some(object) = value.as_object() else {
25+
return Err("no object passed".to_string());
26+
};
27+
28+
Ok(Self {
29+
experimental: object
30+
.get("fmt.experimental")
31+
.is_some_and(|run| serde_json::from_value::<bool>(run.clone()).unwrap_or_default()),
32+
})
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod test {
38+
use serde_json::json;
39+
40+
use super::FormatOptions;
41+
42+
#[test]
43+
fn test_valid_options_json() {
44+
let json = json!({
45+
"fmt.experimental": true,
46+
});
47+
48+
let options = FormatOptions::try_from(json).unwrap();
49+
assert!(options.experimental);
50+
}
51+
52+
#[test]
53+
fn test_empty_options_json() {
54+
let json = json!({});
55+
56+
let options = FormatOptions::try_from(json).unwrap();
57+
assert!(!options.experimental);
58+
}
59+
60+
#[test]
61+
fn test_invalid_options_json() {
62+
let json = json!({
63+
"fmt.experimental": "what", // should be bool
64+
});
65+
66+
let options = FormatOptions::try_from(json).unwrap();
67+
assert!(!options.experimental);
68+
}
69+
}

crates/oxc_language_server/src/formatter/server_formatter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ impl ServerFormatter {
1616
}
1717

1818
#[expect(clippy::unused_self)]
19-
pub fn run_single(&self, uri: &Uri) -> Option<Vec<TextEdit>> {
19+
pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<TextEdit>> {
2020
let path = uri.to_file_path()?;
2121
let source_type = get_supported_source_type(&path)?;
22-
let source_text = std::fs::read_to_string(path).expect("Failed to read file");
22+
let source_text = if let Some(content) = content {
23+
content
24+
} else {
25+
std::fs::read_to_string(&path).ok()?
26+
};
2327

2428
let allocator = Allocator::new();
2529
let ret = Parser::new(&allocator, &source_text, source_type)
@@ -37,11 +41,7 @@ impl ServerFormatter {
3741
return None;
3842
}
3943

40-
let options = FormatOptions {
41-
// semicolons: "always".parse().unwrap(),
42-
semicolons: "as-needed".parse().unwrap(),
43-
..FormatOptions::default()
44-
};
44+
let options = FormatOptions::default();
4545
let code = Formatter::new(&allocator, options).build(&ret.program);
4646

4747
Some(vec![TextEdit::new(

0 commit comments

Comments
 (0)