Skip to content

Commit c1bcef0

Browse files
committed
fix off-by-one error
1 parent 35181e1 commit c1bcef0

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

crates/rust-analyzer/src/handlers/request.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,8 @@ fn run_rustfmt(
22842284
cmd.arg(
22852285
json!([{
22862286
"file": "stdin",
2287-
"range": [start_line, end_line]
2287+
// LineCol is 0-based, but rustfmt is 1-based.
2288+
"range": [start_line + 1, end_line + 1]
22882289
}])
22892290
.to_string(),
22902291
);

crates/rust-analyzer/tests/slow-tests/main.rs

+67-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ use lsp_types::{
2121
notification::DidOpenTextDocument,
2222
request::{
2323
CodeActionRequest, Completion, Formatting, GotoTypeDefinition, HoverRequest,
24-
InlayHintRequest, InlayHintResolveRequest, WillRenameFiles, WorkspaceSymbolRequest,
24+
InlayHintRequest, InlayHintResolveRequest, RangeFormatting, WillRenameFiles,
25+
WorkspaceSymbolRequest,
2526
},
2627
CodeActionContext, CodeActionParams, CompletionParams, DidOpenTextDocumentParams,
27-
DocumentFormattingParams, FileRename, FormattingOptions, GotoDefinitionParams, HoverParams,
28-
InlayHint, InlayHintLabel, InlayHintParams, PartialResultParams, Position, Range,
29-
RenameFilesParams, TextDocumentItem, TextDocumentPositionParams, WorkDoneProgressParams,
28+
DocumentFormattingParams, DocumentRangeFormattingParams, FileRename, FormattingOptions,
29+
GotoDefinitionParams, HoverParams, InlayHint, InlayHintLabel, InlayHintParams,
30+
PartialResultParams, Position, Range, RenameFilesParams, TextDocumentItem,
31+
TextDocumentPositionParams, WorkDoneProgressParams,
3032
};
3133
use rust_analyzer::lsp::ext::{OnEnter, Runnables, RunnablesParams};
3234
use serde_json::json;
@@ -660,6 +662,67 @@ fn main() {}
660662
);
661663
}
662664

665+
#[test]
666+
fn test_format_document_range() {
667+
if skip_slow_tests() {
668+
return;
669+
}
670+
671+
let server = Project::with_fixture(
672+
r#"
673+
//- /Cargo.toml
674+
[package]
675+
name = "foo"
676+
version = "0.0.0"
677+
678+
//- /src/lib.rs
679+
fn main() {
680+
let unit_offsets_cache = collect(dwarf.units ()) ?;
681+
}
682+
"#,
683+
)
684+
.with_config(serde_json::json!({
685+
"rustfmt": { "rangeFormatting": { "enable": true } },
686+
}))
687+
.server()
688+
.wait_until_workspace_is_loaded();
689+
690+
server.request::<RangeFormatting>(
691+
DocumentRangeFormattingParams {
692+
range: Range {
693+
end: Position { line: 1, character: 0 },
694+
start: Position { line: 1, character: 0 },
695+
},
696+
text_document: server.doc_id("src/lib.rs"),
697+
options: FormattingOptions {
698+
tab_size: 4,
699+
insert_spaces: false,
700+
insert_final_newline: None,
701+
trim_final_newlines: None,
702+
trim_trailing_whitespace: None,
703+
properties: HashMap::new(),
704+
},
705+
work_done_progress_params: WorkDoneProgressParams::default(),
706+
},
707+
json!([
708+
{
709+
"newText": "",
710+
"range": {
711+
"start": { "character": 48, "line": 1 },
712+
"end": { "character": 50, "line": 1 },
713+
},
714+
},
715+
{
716+
"newText": "",
717+
"range": {
718+
"start": { "character": 53, "line": 1 },
719+
"end": { "character": 55, "line": 1 },
720+
},
721+
}
722+
]),
723+
);
724+
}
725+
663726
#[test]
664727
fn test_missing_module_code_action() {
665728
if skip_slow_tests() {

0 commit comments

Comments
 (0)