aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/conv.rs
blob: c7bea15dfc608e138efba2f6f2a3d8cd0a7e2a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use languageserver_types::{Range, SymbolKind, Position};
use libeditor::{LineIndex, LineCol};
use libsyntax2::{SyntaxKind, TextUnit, TextRange};

pub trait Conv {
    type Output;
    fn conv(&self) -> Self::Output;
}

pub trait ConvWith {
    type Ctx;
    type Output;
    fn conv_with(&self, ctx: &Self::Ctx) -> Self::Output;
}

impl Conv for SyntaxKind {
    type Output = SymbolKind;

    fn conv(&self) -> <Self as Conv>::Output {
        match *self {
            SyntaxKind::FUNCTION => SymbolKind::Function,
            SyntaxKind::STRUCT => SymbolKind::Struct,
            SyntaxKind::ENUM => SymbolKind::Enum,
            SyntaxKind::TRAIT => SymbolKind::Interface,
            SyntaxKind::MODULE => SymbolKind::Module,
            SyntaxKind::TYPE_ITEM => SymbolKind::TypeParameter,
            SyntaxKind::STATIC_ITEM => SymbolKind::Constant,
            SyntaxKind::CONST_ITEM => SymbolKind::Constant,
            _ => SymbolKind::Variable,
        }
    }
}

impl ConvWith for Position {
    type Ctx = LineIndex;
    type Output = TextUnit;

    fn conv_with(&self, line_index: &LineIndex) -> TextUnit {
        // TODO: UTF-16
        let line_col = LineCol {
            line: self.line as u32,
            col: (self.character as u32).into(),
        };
        line_index.offset(line_col)
    }
}

impl ConvWith for TextUnit {
    type Ctx = LineIndex;
    type Output = Position;

    fn conv_with(&self, line_index: &LineIndex) -> Position {
        let line_col = line_index.line_col(*self);
        // TODO: UTF-16
        Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
    }
}

impl ConvWith for TextRange {
    type Ctx = LineIndex;
    type Output = Range;

    fn conv_with(&self, line_index: &LineIndex) -> Range {
        Range::new(
            self.start().conv_with(line_index),
            self.end().conv_with(line_index),
        )
    }
}

impl ConvWith for Range {
    type Ctx = LineIndex;
    type Output = TextRange;

    fn conv_with(&self, line_index: &LineIndex) -> TextRange {
        TextRange::from_to(
            self.start.conv_with(line_index),
            self.end.conv_with(line_index),
        )
    }
}