Skip to content

Commit e6285a7

Browse files
committed
Fix formatting request parameters not being respected
1 parent 8f6aa2b commit e6285a7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/cli/lsp.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ 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, OneOf, Position, Range,
8-
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit, Uri,
7+
DocumentFormattingParams, DocumentRangeFormattingParams, FormattingOptions, OneOf,
8+
Position, Range, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
9+
TextDocumentSyncKind, TextEdit, Uri,
910
};
10-
use stylua_lib::{format_code, OutputVerification};
11+
use stylua_lib::{format_code, IndentType, OutputVerification};
1112

1213
use crate::{config::ConfigResolver, opt};
1314

@@ -16,17 +17,28 @@ fn handle_formatting(
1617
document: &FullTextDocument,
1718
range: Option<stylua_lib::Range>,
1819
config_resolver: &mut ConfigResolver,
20+
formatting_options: FormattingOptions,
1921
) -> Option<Vec<TextEdit>> {
2022
if document.language_id() != "lua" && document.language_id() != "luau" {
2123
return None;
2224
}
2325

2426
let contents = document.get_content(None);
2527

26-
let config = config_resolver
28+
let mut config = config_resolver
2729
.load_configuration(uri.path().as_str().as_ref())
2830
.unwrap_or_default();
2931

32+
config.indent_width = formatting_options
33+
.tab_size
34+
.try_into()
35+
.expect("usize fits into u32");
36+
config.indent_type = if formatting_options.insert_spaces {
37+
IndentType::Spaces
38+
} else {
39+
IndentType::Tabs
40+
};
41+
3042
let formatted_contents = format_code(contents, config, range, OutputVerification::None).ok()?;
3143

3244
let last_line_idx = document.line_count().saturating_sub(1);
@@ -68,6 +80,7 @@ fn handle_request(
6880
document,
6981
None,
7082
config_resolver,
83+
params.options,
7184
) {
7285
Some(edits) => Response::new_ok(request.id, edits),
7386
None => Response::new_ok(request.id, serde_json::Value::Null),
@@ -104,6 +117,7 @@ fn handle_request(
104117
document,
105118
Some(range),
106119
config_resolver,
120+
params.options,
107121
) {
108122
Some(edits) => Response::new_ok(request.id, edits),
109123
None => Response::new_ok(request.id, serde_json::Value::Null),

0 commit comments

Comments
 (0)