-
-
Notifications
You must be signed in to change notification settings - Fork 842
feat(language_server): introduce ServerFormatter
#13700
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
Merged
graphite-app
merged 1 commit into
main
from
09-11-refactor_language_server_introduce_dummy_serverformatter_
Sep 20, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod options; | ||
| pub mod server_formatter; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,69 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde::{Deserialize, Deserializer, Serialize, de::Error}; | ||
| use serde_json::Value; | ||
|
|
||
| #[derive(Debug, Default, Serialize, Deserialize, Clone)] | ||
| #[derive(Debug, Default, Serialize, Clone)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct FormatOptions; | ||
| pub struct FormatOptions { | ||
| pub experimental: bool, | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for FormatOptions { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let value = Value::deserialize(deserializer)?; | ||
| FormatOptions::try_from(value).map_err(Error::custom) | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<Value> for FormatOptions { | ||
| type Error = String; | ||
|
|
||
| fn try_from(value: Value) -> Result<Self, Self::Error> { | ||
| let Some(object) = value.as_object() else { | ||
| return Err("no object passed".to_string()); | ||
| }; | ||
|
|
||
| Ok(Self { | ||
| experimental: object | ||
| .get("fmt.experimental") | ||
| .is_some_and(|run| serde_json::from_value::<bool>(run.clone()).unwrap_or_default()), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use serde_json::json; | ||
|
|
||
| use super::FormatOptions; | ||
|
|
||
| #[test] | ||
| fn test_valid_options_json() { | ||
| let json = json!({ | ||
| "fmt.experimental": true, | ||
| }); | ||
|
|
||
| let options = FormatOptions::try_from(json).unwrap(); | ||
| assert!(options.experimental); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_empty_options_json() { | ||
| let json = json!({}); | ||
|
|
||
| let options = FormatOptions::try_from(json).unwrap(); | ||
| assert!(!options.experimental); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_options_json() { | ||
| let json = json!({ | ||
| "fmt.experimental": "what", // should be bool | ||
| }); | ||
|
|
||
| let options = FormatOptions::try_from(json).unwrap(); | ||
| assert!(!options.experimental); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
crates/oxc_language_server/src/formatter/server_formatter.rs
This file contains hidden or 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,57 @@ | ||
| use oxc_allocator::Allocator; | ||
| use oxc_formatter::{FormatOptions, Formatter, get_supported_source_type}; | ||
| use oxc_parser::{ParseOptions, Parser}; | ||
| use tower_lsp_server::{ | ||
| UriExt, | ||
| lsp_types::{Position, Range, TextEdit, Uri}, | ||
| }; | ||
|
|
||
| use crate::LSP_MAX_INT; | ||
|
|
||
| pub struct ServerFormatter; | ||
|
|
||
| impl ServerFormatter { | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
|
|
||
| #[expect(clippy::unused_self)] | ||
| pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<TextEdit>> { | ||
| let path = uri.to_file_path()?; | ||
| let source_type = get_supported_source_type(&path)?; | ||
| let source_text = if let Some(content) = content { | ||
| content | ||
| } else { | ||
| std::fs::read_to_string(&path).ok()? | ||
| }; | ||
|
|
||
| let allocator = Allocator::new(); | ||
| let ret = Parser::new(&allocator, &source_text, source_type) | ||
| .with_options(ParseOptions { | ||
| parse_regular_expression: false, | ||
| // Enable all syntax features | ||
| allow_v8_intrinsics: true, | ||
| allow_return_outside_function: true, | ||
| // `oxc_formatter` expects this to be false | ||
| preserve_parens: false, | ||
| }) | ||
| .parse(); | ||
|
|
||
| if !ret.errors.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| let options = FormatOptions::default(); | ||
| let code = Formatter::new(&allocator, options).build(&ret.program); | ||
|
|
||
| // nothing has changed | ||
| if code == source_text { | ||
| return Some(vec![]); | ||
| } | ||
|
|
||
| Some(vec![TextEdit::new( | ||
| Range::new(Position::new(0, 0), Position::new(LSP_MAX_INT, 0)), | ||
| code, | ||
| )]) | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.