aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/conv.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-12 19:02:56 +0100
committerAleksey Kladov <[email protected]>2018-08-12 19:02:56 +0100
commit23c06db9c236db0f9c8ef5117fb1adefd6616c43 (patch)
tree8e265a79b7f8f62761c6ea2dd4fa0d6bc0c74204 /crates/server/src/conv.rs
parent66be735aa98c32fb062d1c756fa9303ff2d13002 (diff)
Half of code-actions
Diffstat (limited to 'crates/server/src/conv.rs')
-rw-r--r--crates/server/src/conv.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/crates/server/src/conv.rs b/crates/server/src/conv.rs
new file mode 100644
index 000000000..c7bea15df
--- /dev/null
+++ b/crates/server/src/conv.rs
@@ -0,0 +1,81 @@
1use languageserver_types::{Range, SymbolKind, Position};
2use libeditor::{LineIndex, LineCol};
3use libsyntax2::{SyntaxKind, TextUnit, TextRange};
4
5pub trait Conv {
6 type Output;
7 fn conv(&self) -> Self::Output;
8}
9
10pub trait ConvWith {
11 type Ctx;
12 type Output;
13 fn conv_with(&self, ctx: &Self::Ctx) -> Self::Output;
14}
15
16impl Conv for SyntaxKind {
17 type Output = SymbolKind;
18
19 fn conv(&self) -> <Self as Conv>::Output {
20 match *self {
21 SyntaxKind::FUNCTION => SymbolKind::Function,
22 SyntaxKind::STRUCT => SymbolKind::Struct,
23 SyntaxKind::ENUM => SymbolKind::Enum,
24 SyntaxKind::TRAIT => SymbolKind::Interface,
25 SyntaxKind::MODULE => SymbolKind::Module,
26 SyntaxKind::TYPE_ITEM => SymbolKind::TypeParameter,
27 SyntaxKind::STATIC_ITEM => SymbolKind::Constant,
28 SyntaxKind::CONST_ITEM => SymbolKind::Constant,
29 _ => SymbolKind::Variable,
30 }
31 }
32}
33
34impl ConvWith for Position {
35 type Ctx = LineIndex;
36 type Output = TextUnit;
37
38 fn conv_with(&self, line_index: &LineIndex) -> TextUnit {
39 // TODO: UTF-16
40 let line_col = LineCol {
41 line: self.line as u32,
42 col: (self.character as u32).into(),
43 };
44 line_index.offset(line_col)
45 }
46}
47
48impl ConvWith for TextUnit {
49 type Ctx = LineIndex;
50 type Output = Position;
51
52 fn conv_with(&self, line_index: &LineIndex) -> Position {
53 let line_col = line_index.line_col(*self);
54 // TODO: UTF-16
55 Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
56 }
57}
58
59impl ConvWith for TextRange {
60 type Ctx = LineIndex;
61 type Output = Range;
62
63 fn conv_with(&self, line_index: &LineIndex) -> Range {
64 Range::new(
65 self.start().conv_with(line_index),
66 self.end().conv_with(line_index),
67 )
68 }
69}
70
71impl ConvWith for Range {
72 type Ctx = LineIndex;
73 type Output = TextRange;
74
75 fn conv_with(&self, line_index: &LineIndex) -> TextRange {
76 TextRange::from_to(
77 self.start.conv_with(line_index),
78 self.end.conv_with(line_index),
79 )
80 }
81}