diff options
author | Aleksey Kladov <[email protected]> | 2018-08-12 19:02:56 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-12 19:02:56 +0100 |
commit | 23c06db9c236db0f9c8ef5117fb1adefd6616c43 (patch) | |
tree | 8e265a79b7f8f62761c6ea2dd4fa0d6bc0c74204 /crates/server/src/conv.rs | |
parent | 66be735aa98c32fb062d1c756fa9303ff2d13002 (diff) |
Half of code-actions
Diffstat (limited to 'crates/server/src/conv.rs')
-rw-r--r-- | crates/server/src/conv.rs | 81 |
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 @@ | |||
1 | use languageserver_types::{Range, SymbolKind, Position}; | ||
2 | use libeditor::{LineIndex, LineCol}; | ||
3 | use libsyntax2::{SyntaxKind, TextUnit, TextRange}; | ||
4 | |||
5 | pub trait Conv { | ||
6 | type Output; | ||
7 | fn conv(&self) -> Self::Output; | ||
8 | } | ||
9 | |||
10 | pub trait ConvWith { | ||
11 | type Ctx; | ||
12 | type Output; | ||
13 | fn conv_with(&self, ctx: &Self::Ctx) -> Self::Output; | ||
14 | } | ||
15 | |||
16 | impl 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 | |||
34 | impl 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 | |||
48 | impl 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 | |||
59 | impl 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 | |||
71 | impl 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 | } | ||