Skip to content

Commit 432f7d9

Browse files
committed
Add initialization options to respect editor's formatting options
1 parent 6a997f3 commit 432f7d9

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/cli/lsp.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use lsp_server::{Connection, ErrorCode, Message, Response};
44
use lsp_textdocument::{FullTextDocument, TextDocuments};
55
use lsp_types::{
66
request::{Formatting, RangeFormatting, Request},
7-
DocumentFormattingParams, DocumentRangeFormattingParams, InitializeResult, OneOf, Range,
8-
ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit,
9-
Uri,
7+
DocumentFormattingParams, DocumentRangeFormattingParams, FormattingOptions, InitializeParams,
8+
InitializeResult, OneOf, Range, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
9+
TextDocumentSyncKind, TextEdit, Uri,
1010
};
1111
use similar::{DiffOp, TextDiff};
12-
use stylua_lib::{format_code, OutputVerification};
12+
use stylua_lib::{format_code, IndentType, OutputVerification};
1313

1414
use crate::{config::ConfigResolver, opt};
1515

@@ -64,17 +64,30 @@ fn handle_formatting(
6464
document: &FullTextDocument,
6565
range: Option<stylua_lib::Range>,
6666
config_resolver: &mut ConfigResolver,
67+
formatting_options: Option<&FormattingOptions>,
6768
) -> Option<Vec<TextEdit>> {
6869
if document.language_id() != "lua" && document.language_id() != "luau" {
6970
return None;
7071
}
7172

7273
let contents = document.get_content(None);
7374

74-
let config = config_resolver
75+
let mut config = config_resolver
7576
.load_configuration(uri.path().as_str().as_ref())
7677
.unwrap_or_default();
7778

79+
if let Some(formatting_options) = formatting_options {
80+
config.indent_width = formatting_options
81+
.tab_size
82+
.try_into()
83+
.expect("u32 fits into usize");
84+
config.indent_type = if formatting_options.insert_spaces {
85+
IndentType::Spaces
86+
} else {
87+
IndentType::Tabs
88+
};
89+
}
90+
7891
let formatted_contents = format_code(contents, config, range, OutputVerification::None).ok()?;
7992

8093
let operations =
@@ -94,6 +107,7 @@ fn handle_request(
94107
request: lsp_server::Request,
95108
documents: &TextDocuments,
96109
config_resolver: &mut ConfigResolver,
110+
respect_editor_formatting_options: bool,
97111
) -> Response {
98112
match request.method.as_str() {
99113
Formatting::METHOD => {
@@ -115,6 +129,7 @@ fn handle_request(
115129
document,
116130
None,
117131
config_resolver,
132+
respect_editor_formatting_options.then_some(&params.options),
118133
) {
119134
Some(edits) => Response::new_ok(request.id, edits),
120135
None => Response::new_ok(request.id, serde_json::Value::Null),
@@ -151,6 +166,7 @@ fn handle_request(
151166
document,
152167
Some(range),
153168
config_resolver,
169+
respect_editor_formatting_options.then_some(&params.options),
154170
) {
155171
Some(edits) => Response::new_ok(request.id, edits),
156172
None => Response::new_ok(request.id, serde_json::Value::Null),
@@ -187,7 +203,17 @@ fn main_loop(connection: Connection, config_resolver: &mut ConfigResolver) -> an
187203
}),
188204
};
189205

190-
let (id, _) = connection.initialize_start()?;
206+
let (id, initialize_params) = connection.initialize_start()?;
207+
208+
let respect_editor_formatting_options =
209+
serde_json::from_value::<InitializeParams>(initialize_params)?
210+
.initialization_options
211+
.as_ref()
212+
.and_then(|init_options| init_options.as_object())
213+
.and_then(|map| map.get("respect_editor_formatting_options"))
214+
.and_then(|value| value.as_bool())
215+
.unwrap_or(false);
216+
191217
connection.initialize_finish(id, serde_json::to_value(initialize_result)?)?;
192218

193219
let mut documents = TextDocuments::new();
@@ -198,7 +224,12 @@ fn main_loop(connection: Connection, config_resolver: &mut ConfigResolver) -> an
198224
break;
199225
}
200226

201-
let response = handle_request(req, &documents, config_resolver);
227+
let response = handle_request(
228+
req,
229+
&documents,
230+
config_resolver,
231+
respect_editor_formatting_options,
232+
);
202233
connection.sender.send(Message::Response(response))?
203234
}
204235
Message::Response(_) => {}

0 commit comments

Comments
 (0)