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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
use languageserver_types::{
Range, SymbolKind, Position, TextEdit, Location, Url,
TextDocumentIdentifier, VersionedTextDocumentIdentifier, TextDocumentItem,
};
use libeditor::{LineIndex, LineCol, Edit, AtomEdit};
use libsyntax2::{SyntaxKind, TextUnit, TextRange};
use libanalysis::FileId;
use {Result, PathMap};
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;
}
pub trait TryConvWith {
type Ctx;
type Output;
fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
}
impl Conv for SyntaxKind {
type Output = SymbolKind;
fn conv(self) -> <Self as Conv>::Output {
match self {
SyntaxKind::FN_DEF => SymbolKind::Function,
SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
SyntaxKind::ENUM_DEF => SymbolKind::Enum,
SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
SyntaxKind::MODULE => SymbolKind::Module,
SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
SyntaxKind::STATIC_DEF => SymbolKind::Constant,
SyntaxKind::CONST_DEF => SymbolKind::Constant,
SyntaxKind::IMPL_ITEM => SymbolKind::Object,
_ => 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),
)
}
}
impl ConvWith for Edit {
type Ctx = LineIndex;
type Output = Vec<TextEdit>;
fn conv_with(self, line_index: &LineIndex) -> Vec<TextEdit> {
self.into_atoms()
.into_iter()
.map_conv_with(line_index)
.collect()
}
}
impl ConvWith for AtomEdit {
type Ctx = LineIndex;
type Output = TextEdit;
fn conv_with(self, line_index: &LineIndex) -> TextEdit {
TextEdit {
range: self.delete.conv_with(line_index),
new_text: self.insert,
}
}
}
impl<T: ConvWith> ConvWith for Option<T> {
type Ctx = <T as ConvWith>::Ctx;
type Output = Option<<T as ConvWith>::Output>;
fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
self.map(|x| ConvWith::conv_with(x, ctx))
}
}
impl<'a> TryConvWith for &'a Url {
type Ctx = PathMap;
type Output = FileId;
fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> {
let path = self.to_file_path()
.map_err(|()| format_err!("invalid uri: {}", self))?;
path_map.get_id(&path).ok_or_else(|| format_err!("unknown file: {}", path.display()))
}
}
impl TryConvWith for FileId {
type Ctx = PathMap;
type Output = Url;
fn try_conv_with(self, path_map: &PathMap) -> Result<Url> {
let path = path_map.get_path(self);
let url = Url::from_file_path(path)
.map_err(|()| format_err!("can't convert path to url: {}", path.display()))?;
Ok(url)
}
}
impl<'a> TryConvWith for &'a TextDocumentItem {
type Ctx = PathMap;
type Output = FileId;
fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> {
self.uri.try_conv_with(path_map)
}
}
impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
type Ctx = PathMap;
type Output = FileId;
fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> {
self.uri.try_conv_with(path_map)
}
}
impl<'a> TryConvWith for &'a TextDocumentIdentifier {
type Ctx = PathMap;
type Output = FileId;
fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> {
self.uri.try_conv_with(path_map)
}
}
pub fn to_location(
file_id: FileId,
range: TextRange,
path_map: &PathMap,
line_index: &LineIndex,
) -> Result<Location> {
let url = file_id.try_conv_with(path_map)?;
let loc = Location::new(
url,
range.conv_with(line_index),
);
Ok(loc)
}
pub trait MapConvWith<'a>: Sized {
type Ctx;
type Output;
fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
ConvWithIter { iter: self, ctx }
}
}
impl<'a, I> MapConvWith<'a> for I
where I: Iterator,
I::Item: ConvWith
{
type Ctx = <I::Item as ConvWith>::Ctx;
type Output = <I::Item as ConvWith>::Output;
}
pub struct ConvWithIter<'a, I, Ctx: 'a> {
iter: I,
ctx: &'a Ctx,
}
impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
where
I: Iterator,
I::Item: ConvWith<Ctx=Ctx>,
{
type Item = <I::Item as ConvWith>::Output;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| item.conv_with(self.ctx))
}
}
|