aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/handlers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/server/src/handlers.rs')
-rw-r--r--crates/server/src/handlers.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/crates/server/src/handlers.rs b/crates/server/src/handlers.rs
new file mode 100644
index 000000000..5ee87a4dd
--- /dev/null
+++ b/crates/server/src/handlers.rs
@@ -0,0 +1,61 @@
1use languageserver_types::{Range, Position};
2use libanalysis::World;
3use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit};
4use {req, Result, FilePath};
5
6pub fn handle_syntax_tree(
7 world: World,
8 params: req::SyntaxTreeParams,
9) -> Result<String> {
10 let path = params.text_document.file_path()?;
11 let file = world.file_syntax(&path)?;
12 Ok(libeditor::syntax_tree(&file))
13}
14
15pub fn handle_extend_selection(
16 world: World,
17 params: req::ExtendSelectionParams,
18) -> Result<req::ExtendSelectionResult> {
19 let path = params.text_document.file_path()?;
20 let file = world.file_syntax(&path)?;
21 let line_index = world.file_line_index(&path)?;
22 let selections = params.selections.into_iter()
23 .map(|r| {
24 let r = to_text_range(&line_index, r);
25 let r = libeditor::extend_selection(&file, r).unwrap_or(r);
26 to_vs_range(&line_index, r)
27 })
28 .collect();
29 Ok(req::ExtendSelectionResult { selections })
30}
31
32
33fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange {
34 TextRange::from_to(
35 to_text_unit(line_index, range.start),
36 to_text_unit(line_index, range.end),
37 )
38}
39
40fn to_text_unit(line_index: &LineIndex, position: Position) -> TextUnit {
41 // TODO: UTF-16
42 let line_col = LineCol {
43 line: position.line as u32,
44 col: (position.character as u32).into(),
45 };
46 line_index.offset(line_col)
47}
48
49
50fn to_vs_range(line_index: &LineIndex, range: TextRange) -> Range {
51 Range::new(
52 to_vs_position(line_index, range.start()),
53 to_vs_position(line_index, range.end()),
54 )
55}
56
57fn to_vs_position(line_index: &LineIndex, offset: TextUnit) -> Position {
58 let line_col = line_index.line_col(offset);
59 // TODO: UTF-16
60 Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
61}