1 use languageserver_types::{
2 Range, SymbolKind, Position, TextEdit, Location, Url,
3 TextDocumentIdentifier, VersionedTextDocumentIdentifier, TextDocumentItem,
4 TextDocumentPositionParams, TextDocumentEdit,
6 use ra_editor::{LineIndex, LineCol, Edit, AtomEdit};
7 use ra_syntax::{SyntaxKind, TextUnit, TextRange};
8 use ra_analysis::{FileId, SourceChange, SourceFileEdit, FileSystemEdit};
12 server_world::ServerWorld,
18 fn conv(self) -> Self::Output;
24 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
27 pub trait TryConvWith {
30 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
33 impl Conv for SyntaxKind {
34 type Output = SymbolKind;
36 fn conv(self) -> <Self as Conv>::Output {
38 SyntaxKind::FN_DEF => SymbolKind::Function,
39 SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
40 SyntaxKind::ENUM_DEF => SymbolKind::Enum,
41 SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
42 SyntaxKind::MODULE => SymbolKind::Module,
43 SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
44 SyntaxKind::STATIC_DEF => SymbolKind::Constant,
45 SyntaxKind::CONST_DEF => SymbolKind::Constant,
46 SyntaxKind::IMPL_ITEM => SymbolKind::Object,
47 _ => SymbolKind::Variable,
52 impl ConvWith for Position {
54 type Output = TextUnit;
56 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
58 let line_col = LineCol {
59 line: self.line as u32,
60 col: (self.character as u32).into(),
62 line_index.offset(line_col)
66 impl ConvWith for TextUnit {
68 type Output = Position;
70 fn conv_with(self, line_index: &LineIndex) -> Position {
71 let line_col = line_index.line_col(self);
73 Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
77 impl ConvWith for TextRange {
81 fn conv_with(self, line_index: &LineIndex) -> Range {
83 self.start().conv_with(line_index),
84 self.end().conv_with(line_index),
89 impl ConvWith for Range {
91 type Output = TextRange;
93 fn conv_with(self, line_index: &LineIndex) -> TextRange {
95 self.start.conv_with(line_index),
96 self.end.conv_with(line_index),
101 impl ConvWith for Edit {
102 type Ctx = LineIndex;
103 type Output = Vec<TextEdit>;
105 fn conv_with(self, line_index: &LineIndex) -> Vec<TextEdit> {
108 .map_conv_with(line_index)
113 impl ConvWith for AtomEdit {
114 type Ctx = LineIndex;
115 type Output = TextEdit;
117 fn conv_with(self, line_index: &LineIndex) -> TextEdit {
119 range: self.delete.conv_with(line_index),
120 new_text: self.insert,
125 impl<T: ConvWith> ConvWith for Option<T> {
126 type Ctx = <T as ConvWith>::Ctx;
127 type Output = Option<<T as ConvWith>::Output>;
128 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
129 self.map(|x| ConvWith::conv_with(x, ctx))
133 impl<'a> TryConvWith for &'a Url {
134 type Ctx = ServerWorld;
135 type Output = FileId;
136 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
137 world.uri_to_file_id(self)
141 impl TryConvWith for FileId {
142 type Ctx = ServerWorld;
144 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
145 world.file_id_to_uri(self)
149 impl<'a> TryConvWith for &'a TextDocumentItem {
150 type Ctx = ServerWorld;
151 type Output = FileId;
152 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
153 self.uri.try_conv_with(world)
157 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
158 type Ctx = ServerWorld;
159 type Output = FileId;
160 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
161 self.uri.try_conv_with(world)
165 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
166 type Ctx = ServerWorld;
167 type Output = FileId;
168 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
169 world.uri_to_file_id(&self.uri)
173 impl<T: TryConvWith> TryConvWith for Vec<T> {
174 type Ctx = <T as TryConvWith>::Ctx;
175 type Output = Vec<<T as TryConvWith>::Output>;
176 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
177 let mut res = Vec::with_capacity(self.len());
179 res.push(item.try_conv_with(ctx)?);
185 impl TryConvWith for SourceChange {
186 type Ctx = ServerWorld;
187 type Output = req::SourceChange;
188 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
189 let cursor_position = match self.cursor_position {
192 let line_index = world.analysis().file_line_index(pos.file_id);
193 let edits = self.source_file_edits.iter().find(|it| it.file_id == pos.file_id)
194 .map(|it| it.edits.as_slice()).unwrap_or(&[]);
195 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
196 let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64);
197 Some(TextDocumentPositionParams {
198 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
203 let source_file_edits = self.source_file_edits.try_conv_with(world)?;
204 let file_system_edits = self.file_system_edits.try_conv_with(world)?;
205 Ok(req::SourceChange {
214 // HACK: we should translate offset to line/column using linde_index *with edits applied*.
215 // A naive version of this function would be to apply `edits` to the original text,
216 // construct a new line index and use that, but it would be slow.
218 // Writing fast & correct version is issue #105, let's use a quick hack in the meantime
219 fn translate_offset_with_edit(
220 pre_edit_index: &LineIndex,
224 let fallback = pre_edit_index.line_col(offset);
225 let edit = match edits.first() {
226 None => return fallback,
229 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
230 if !(edit.delete.start() <= offset && offset <= end_offset) {
233 let rel_offset = offset - edit.delete.start();
234 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
235 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
236 if in_edit_line_col.line == 0 {
238 line: edit_line_col.line,
239 col: edit_line_col.col + in_edit_line_col.col,
243 line: edit_line_col.line + in_edit_line_col.line,
244 col: in_edit_line_col.col,
249 impl TryConvWith for SourceFileEdit {
250 type Ctx = ServerWorld;
251 type Output = TextDocumentEdit;
252 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
253 let text_document = VersionedTextDocumentIdentifier {
254 uri: self.file_id.try_conv_with(world)?,
257 let line_index = world.analysis().file_line_index(self.file_id);
258 let edits = self.edits
260 .map_conv_with(&line_index)
262 Ok(TextDocumentEdit { text_document, edits })
266 impl TryConvWith for FileSystemEdit {
267 type Ctx = ServerWorld;
268 type Output = req::FileSystemEdit;
269 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
270 let res = match self {
271 FileSystemEdit::CreateFile { anchor, path } => {
272 let uri = world.file_id_to_uri(anchor)?;
273 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
274 let uri = uri.join(path)?;
275 req::FileSystemEdit::CreateFile { uri }
277 FileSystemEdit::MoveFile { file, path } => {
278 let src = world.file_id_to_uri(file)?;
279 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
280 let dst = src.join(path)?;
281 req::FileSystemEdit::MoveFile { src, dst }
292 line_index: &LineIndex,
293 ) -> Result<Location> {
294 let url = file_id.try_conv_with(world)?;
295 let loc = Location::new(
297 range.conv_with(line_index),
302 pub trait MapConvWith<'a>: Sized + 'a {
306 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
307 ConvWithIter { iter: self, ctx }
311 impl<'a, I> MapConvWith<'a> for I
312 where I: Iterator + 'a,
315 type Ctx = <I::Item as ConvWith>::Ctx;
316 type Output = <I::Item as ConvWith>::Output;
319 pub struct ConvWithIter<'a, I, Ctx: 'a> {
324 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
327 I::Item: ConvWith<Ctx=Ctx>,
329 type Item = <I::Item as ConvWith>::Output;
331 fn next(&mut self) -> Option<Self::Item> {
332 self.iter.next().map(|item| item.conv_with(self.ctx))