Skip to content

Commit 5ce2b48

Browse files
bors[bot]kjeremy
andcommitted
Merge #1504
1504: Simplify LSP handlers r=matklad a=kjeremy Takes advantage of protocol inheritance via composition and simplifies some responses via the `From`/`Into` traits. Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
2 parents 5b19825 + 9c6e93c commit 5ce2b48

File tree

5 files changed

+34
-42
lines changed

5 files changed

+34
-42
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/gen_lsp_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
description = "Generic LSP server scaffold."
99

1010
[dependencies]
11-
lsp-types = "0.58.0"
11+
lsp-types = "0.59.0"
1212
log = "0.4.3"
1313
serde_json = "1.0.34"
1414
serde = { version = "1.0.83", features = ["derive"] }

crates/ra_lsp_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crossbeam-channel = "0.3.5"
1313
flexi_logger = "0.13.0"
1414
log = "0.4.3"
1515
url_serde = "0.2.0"
16-
lsp-types = { version = "0.58.0", features = ["proposed"] }
16+
lsp-types = { version = "0.59.0", features = ["proposed"] }
1717
rustc-hash = "1.0"
1818
parking_lot = "0.8.0"
1919

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::{fmt::Write as _, io::Write as _};
22

33
use gen_lsp_server::ErrorCode;
44
use lsp_types::{
5-
CodeAction, CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity,
6-
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeKind,
7-
FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position,
8-
PrepareRenameResponse, Range, RenameParams, SymbolInformation, TextDocumentIdentifier,
9-
TextEdit, WorkspaceEdit,
5+
CodeAction, CodeActionResponse, CodeLens, Command, CompletionItem, Diagnostic,
6+
DiagnosticSeverity, DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange,
7+
FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent,
8+
MarkupKind, Position, PrepareRenameResponse, Range, RenameParams, SymbolInformation,
9+
TextDocumentIdentifier, TextEdit, WorkspaceEdit,
1010
};
1111
use ra_ide_api::{
1212
AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo,
@@ -153,14 +153,12 @@ pub fn handle_on_type_formatting(
153153
params: req::DocumentOnTypeFormattingParams,
154154
) -> Result<Option<Vec<TextEdit>>> {
155155
let _p = profile("handle_on_type_formatting");
156-
let file_id = params.text_document.try_conv_with(&world)?;
157-
let line_index = world.analysis().file_line_index(file_id);
158-
let position = FilePosition {
159-
file_id,
160-
/// in `ra_ide_api`, the `on_type` invariant is that
161-
/// `text.char_at(position) == typed_char`.
162-
offset: params.position.conv_with(&line_index) - TextUnit::of_char('.'),
163-
};
156+
let mut position = params.text_document_position.try_conv_with(&world)?;
157+
let line_index = world.analysis().file_line_index(position.file_id);
158+
159+
// in `ra_ide_api`, the `on_type` invariant is that
160+
// `text.char_at(position) == typed_char`.
161+
position.offset = position.offset - TextUnit::of_char('.');
164162

165163
let edit = match params.ch.as_str() {
166164
"=" => world.analysis().on_eq_typed(position),
@@ -214,7 +212,7 @@ pub fn handle_document_symbol(
214212
}
215213
}
216214

217-
Ok(Some(req::DocumentSymbolResponse::Nested(res)))
215+
Ok(Some(res.into()))
218216
}
219217

220218
pub fn handle_workspace_symbol(
@@ -277,7 +275,7 @@ pub fn handle_goto_definition(
277275
.map(|nav| RangeInfo::new(nav_range, nav))
278276
.map(|nav| to_location_link(&nav, &world, &line_index))
279277
.collect::<Result<Vec<_>>>()?;
280-
Ok(Some(req::GotoDefinitionResponse::Link(res)))
278+
Ok(Some(res.into()))
281279
}
282280

283281
pub fn handle_goto_implementation(
@@ -297,7 +295,7 @@ pub fn handle_goto_implementation(
297295
.map(|nav| RangeInfo::new(nav_range, nav))
298296
.map(|nav| to_location_link(&nav, &world, &line_index))
299297
.collect::<Result<Vec<_>>>()?;
300-
Ok(Some(req::GotoDefinitionResponse::Link(res)))
298+
Ok(Some(res.into()))
301299
}
302300

303301
pub fn handle_goto_type_definition(
@@ -317,7 +315,7 @@ pub fn handle_goto_type_definition(
317315
.map(|nav| RangeInfo::new(nav_range, nav))
318316
.map(|nav| to_location_link(&nav, &world, &line_index))
319317
.collect::<Result<Vec<_>>>()?;
320-
Ok(Some(req::GotoDefinitionResponse::Link(res)))
318+
Ok(Some(res.into()))
321319
}
322320

323321
pub fn handle_parent_module(
@@ -407,12 +405,7 @@ pub fn handle_completion(
407405
params: req::CompletionParams,
408406
) -> Result<Option<req::CompletionResponse>> {
409407
let _p = profile("handle_completion");
410-
let position = {
411-
let file_id = params.text_document.try_conv_with(&world)?;
412-
let line_index = world.analysis().file_line_index(file_id);
413-
let offset = params.position.conv_with(&line_index);
414-
FilePosition { file_id, offset }
415-
};
408+
let position = params.text_document_position.try_conv_with(&world)?;
416409
let completion_triggered_after_single_colon = {
417410
let mut res = false;
418411
if let Some(ctx) = params.context {
@@ -440,9 +433,10 @@ pub fn handle_completion(
440433
Some(items) => items,
441434
};
442435
let line_index = world.analysis().file_line_index(position.file_id);
443-
let items = items.into_iter().map(|item| item.conv_with(&line_index)).collect();
436+
let items: Vec<CompletionItem> =
437+
items.into_iter().map(|item| item.conv_with(&line_index)).collect();
444438

445-
Ok(Some(req::CompletionResponse::Array(items)))
439+
Ok(Some(items.into()))
446440
}
447441

448442
pub fn handle_folding_range(
@@ -543,9 +537,7 @@ pub fn handle_prepare_rename(
543537
}
544538

545539
pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
546-
let file_id = params.text_document.try_conv_with(&world)?;
547-
let line_index = world.analysis().file_line_index(file_id);
548-
let offset = params.position.conv_with(&line_index);
540+
let position = params.text_document_position.try_conv_with(&world)?;
549541

550542
if params.new_name.is_empty() {
551543
return Err(LspError::new(
@@ -555,8 +547,7 @@ pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Optio
555547
.into());
556548
}
557549

558-
let optional_change =
559-
world.analysis().rename(FilePosition { file_id, offset }, &*params.new_name)?;
550+
let optional_change = world.analysis().rename(position, &*params.new_name)?;
560551
let change = match optional_change {
561552
None => return Ok(None),
562553
Some(it) => it,
@@ -571,11 +562,10 @@ pub fn handle_references(
571562
world: WorldSnapshot,
572563
params: req::ReferenceParams,
573564
) -> Result<Option<Vec<Location>>> {
574-
let file_id = params.text_document.try_conv_with(&world)?;
575-
let line_index = world.analysis().file_line_index(file_id);
576-
let offset = params.position.conv_with(&line_index);
565+
let position = params.text_document_position.try_conv_with(&world)?;
566+
let line_index = world.analysis().file_line_index(position.file_id);
577567

578-
let refs = match world.analysis().find_all_refs(FilePosition { file_id, offset })? {
568+
let refs = match world.analysis().find_all_refs(position)? {
579569
None => return Ok(None),
580570
Some(refs) => refs,
581571
};

crates/ra_lsp_server/tests/heavy_tests/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ use std::collections::Spam;
3737
eprintln!("loading took {:?}", project_start.elapsed());
3838
let completion_start = Instant::now();
3939
let res = server.send_request::<Completion>(CompletionParams {
40-
text_document: server.doc_id("src/lib.rs"),
40+
text_document_position: TextDocumentPositionParams::new(
41+
server.doc_id("src/lib.rs"),
42+
Position::new(0, 23),
43+
),
4144
context: None,
42-
position: Position::new(0, 23),
4345
});
4446
assert!(format!("{}", res).contains("HashMap"));
4547
eprintln!("completion took {:?}", completion_start.elapsed());

0 commit comments

Comments
 (0)