Skip to content

Commit

Permalink
feat(service): custom formatting options
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 11, 2024
1 parent e95264c commit d64924a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 11 deletions.
4 changes: 3 additions & 1 deletion crates/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
3 changes: 2 additions & 1 deletion crates/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
29 changes: 22 additions & 7 deletions crates/service/src/features/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(&params.options));
let formatted = wat_formatter::format(
&root,
&build_options(
&params.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,
Expand All @@ -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(&params.options),
&build_options(
&params.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,
)?;
Expand All @@ -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,
}
}
29 changes: 28 additions & 1 deletion crates/service/tests/formatting/full.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -78,3 +78,30 @@ fn tab() {
));
assert_json_snapshot!(response);
}

#[test]
fn format_comments() {
let uri = "untitled:test".parse::<Uri>().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);
}
31 changes: 30 additions & 1 deletion crates/service/tests/formatting/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -63,3 +63,32 @@ fn overlap() {
));
assert_json_snapshot!(response);
}

#[test]
fn format_comments() {
let uri = "untitled:test".parse::<Uri>().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);
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
Original file line number Diff line number Diff line change
@@ -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 )"
}
]

0 comments on commit d64924a

Please sign in to comment.