diff --git a/crates/service/Cargo.toml b/crates/service/Cargo.toml index b5f0824a..1b04e03f 100644 --- a/crates/service/Cargo.toml +++ b/crates/service/Cargo.toml @@ -18,7 +18,9 @@ rustc-hash = "2.0" salsa = "0.16" serde.workspace = true smallvec = "1.13" -wat_formatter = { path = "../formatter", version = "0.1" } +wat_formatter = { path = "../formatter", version = "0.1", features = [ + "config_serde", +] } wat_parser = { path = "../parser", version = "0.1" } wat_syntax = { path = "../syntax", version = "0.1" } diff --git a/crates/service/src/config.rs b/crates/service/src/config.rs index c6a66b3b..c60d3d62 100644 --- a/crates/service/src/config.rs +++ b/crates/service/src/config.rs @@ -3,5 +3,6 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Default, Serialize, Deserialize)] /// Language service configuration. This can be different for each document. pub struct ServiceConfig { - // + /// Configuration about formatting. + pub format: wat_formatter::config::LanguageOptions, } diff --git a/crates/service/src/features/formatting.rs b/crates/service/src/features/formatting.rs index e6f24dc4..a33bba6a 100644 --- a/crates/service/src/features/formatting.rs +++ b/crates/service/src/features/formatting.rs @@ -4,7 +4,7 @@ use lsp_types::{ DocumentFormattingParams, DocumentRangeFormattingParams, FormattingOptions, TextEdit, }; use rowan::ast::AstNode; -use wat_formatter::config::{FormatOptions, LayoutOptions}; +use wat_formatter::config::{FormatOptions, LanguageOptions, LayoutOptions}; use wat_syntax::{ast::Root, SyntaxNode}; impl LanguageService { @@ -13,7 +13,16 @@ impl LanguageService { let uri = self.uri(params.text_document.uri); let line_index = self.line_index(uri); let root = Root::cast(SyntaxNode::new_root(self.root(uri)))?; - let formatted = wat_formatter::format(&root, &build_options(¶ms.options)); + let formatted = wat_formatter::format( + &root, + &build_options( + ¶ms.options, + self.configs + .get(&uri) + .map(|config| config.format.clone()) + .unwrap_or_default(), + ), + ); let text_edit = TextEdit { range: helpers::rowan_range_to_lsp_range(&line_index, root.syntax().text_range()), new_text: formatted, @@ -28,7 +37,13 @@ impl LanguageService { let root = Root::cast(SyntaxNode::new_root(self.root(uri)))?; let (formatted, range) = wat_formatter::format_range( &root, - &build_options(¶ms.options), + &build_options( + ¶ms.options, + self.configs + .get(&uri) + .map(|config| config.format.clone()) + .unwrap_or_default(), + ), helpers::lsp_range_to_rowan_range(&line_index, params.range)?, &line_index, )?; @@ -40,13 +55,13 @@ impl LanguageService { } } -fn build_options(options: &FormattingOptions) -> FormatOptions { +fn build_options(layout: &FormattingOptions, language: LanguageOptions) -> FormatOptions { FormatOptions { layout: LayoutOptions { - indent_width: options.tab_size as usize, - use_tabs: !options.insert_spaces, + indent_width: layout.tab_size as usize, + use_tabs: !layout.insert_spaces, ..Default::default() }, - language: Default::default(), + language, } } diff --git a/crates/service/tests/formatting/full.rs b/crates/service/tests/formatting/full.rs index 1c07ff58..c5a7a594 100644 --- a/crates/service/tests/formatting/full.rs +++ b/crates/service/tests/formatting/full.rs @@ -1,6 +1,6 @@ use insta::assert_json_snapshot; use lsp_types::{DocumentFormattingParams, FormattingOptions, TextDocumentIdentifier, Uri}; -use wat_service::LanguageService; +use wat_service::{LanguageService, ServiceConfig}; fn create_params(uri: Uri, options: FormattingOptions) -> DocumentFormattingParams { DocumentFormattingParams { @@ -78,3 +78,30 @@ fn tab() { )); assert_json_snapshot!(response); } + +#[test] +fn format_comments() { + let uri = "untitled:test".parse::().unwrap(); + let source = ";;comment"; + let mut service = LanguageService::default(); + service.commit(uri.clone(), source.into()); + service.set_config( + uri.clone(), + ServiceConfig { + format: wat_formatter::config::LanguageOptions { + format_comments: true, + ..Default::default() + }, + ..Default::default() + }, + ); + let response = service.formatting(create_params( + uri, + FormattingOptions { + tab_size: 2, + insert_spaces: true, + ..Default::default() + }, + )); + assert_json_snapshot!(response); +} diff --git a/crates/service/tests/formatting/range.rs b/crates/service/tests/formatting/range.rs index 1082c795..ab26c4ab 100644 --- a/crates/service/tests/formatting/range.rs +++ b/crates/service/tests/formatting/range.rs @@ -2,7 +2,7 @@ use insta::assert_json_snapshot; use lsp_types::{ DocumentRangeFormattingParams, FormattingOptions, Position, Range, TextDocumentIdentifier, Uri, }; -use wat_service::LanguageService; +use wat_service::{LanguageService, ServiceConfig}; fn create_params(uri: Uri, range: Range) -> DocumentRangeFormattingParams { DocumentRangeFormattingParams { @@ -63,3 +63,32 @@ fn overlap() { )); assert_json_snapshot!(response); } + +#[test] +fn format_comments() { + let uri = "untitled:test".parse::().unwrap(); + let source = " +(module + (func + ;;comment + ) +) +"; + let mut service = LanguageService::default(); + service.commit(uri.clone(), source.into()); + service.set_config( + uri.clone(), + ServiceConfig { + format: wat_formatter::config::LanguageOptions { + format_comments: true, + ..Default::default() + }, + ..Default::default() + }, + ); + let response = service.range_formatting(create_params( + uri, + Range::new(Position::new(3, 4), Position::new(3, 13)), + )); + assert_json_snapshot!(response); +} diff --git a/crates/service/tests/formatting/snapshots/features__formatting__full__format_comments.snap b/crates/service/tests/formatting/snapshots/features__formatting__full__format_comments.snap new file mode 100644 index 00000000..4e20a6a2 --- /dev/null +++ b/crates/service/tests/formatting/snapshots/features__formatting__full__format_comments.snap @@ -0,0 +1,19 @@ +--- +source: crates/service/tests/formatting/full.rs +expression: response +--- +[ + { + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 9 + } + }, + "newText": ";; comment\n" + } +] diff --git a/crates/service/tests/formatting/snapshots/features__formatting__range__format_comments.snap b/crates/service/tests/formatting/snapshots/features__formatting__range__format_comments.snap new file mode 100644 index 00000000..f5ebdf41 --- /dev/null +++ b/crates/service/tests/formatting/snapshots/features__formatting__range__format_comments.snap @@ -0,0 +1,19 @@ +--- +source: crates/service/tests/formatting/range.rs +expression: response +--- +[ + { + "range": { + "start": { + "line": 2, + "character": 2 + }, + "end": { + "line": 4, + "character": 3 + } + }, + "newText": "(func ;; comment\n )" + } +]