1 use languageserver_types::{
2 self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
5 use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition};
6 use ra_editor::{LineCol, LineIndex};
7 use ra_text_edit::{AtomTextEdit, TextEdit};
8 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
10 use crate::{req, server_world::ServerWorld, Result};
14 fn conv(self) -> Self::Output;
20 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
23 pub trait TryConvWith {
26 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
29 impl Conv for SyntaxKind {
30 type Output = SymbolKind;
32 fn conv(self) -> <Self as Conv>::Output {
34 SyntaxKind::FN_DEF => SymbolKind::Function,
35 SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
36 SyntaxKind::ENUM_DEF => SymbolKind::Enum,
37 SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
38 SyntaxKind::MODULE => SymbolKind::Module,
39 SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
40 SyntaxKind::STATIC_DEF => SymbolKind::Constant,
41 SyntaxKind::CONST_DEF => SymbolKind::Constant,
42 SyntaxKind::IMPL_ITEM => SymbolKind::Object,
43 _ => SymbolKind::Variable,
48 impl ConvWith for Position {
50 type Output = TextUnit;
52 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
53 let line_col = LineCol {
54 line: self.line as u32,
55 col_utf16: self.character as u32,
57 line_index.offset(line_col)
61 impl ConvWith for TextUnit {
63 type Output = Position;
65 fn conv_with(self, line_index: &LineIndex) -> Position {
66 let line_col = line_index.line_col(self);
67 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
71 impl ConvWith for TextRange {
75 fn conv_with(self, line_index: &LineIndex) -> Range {
77 self.start().conv_with(line_index),
78 self.end().conv_with(line_index),
83 impl ConvWith for Range {
85 type Output = TextRange;
87 fn conv_with(self, line_index: &LineIndex) -> TextRange {
89 self.start.conv_with(line_index),
90 self.end.conv_with(line_index),
95 impl ConvWith for TextEdit {
97 type Output = Vec<languageserver_types::TextEdit>;
99 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
102 .map_conv_with(line_index)
107 impl<'a> ConvWith for &'a AtomTextEdit {
108 type Ctx = LineIndex;
109 type Output = languageserver_types::TextEdit;
111 fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
112 languageserver_types::TextEdit {
113 range: self.delete.conv_with(line_index),
114 new_text: self.insert.clone(),
119 impl<T: ConvWith> ConvWith for Option<T> {
120 type Ctx = <T as ConvWith>::Ctx;
121 type Output = Option<<T as ConvWith>::Output>;
122 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
123 self.map(|x| ConvWith::conv_with(x, ctx))
127 impl<'a> TryConvWith for &'a Url {
128 type Ctx = ServerWorld;
129 type Output = FileId;
130 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
131 world.uri_to_file_id(self)
135 impl TryConvWith for FileId {
136 type Ctx = ServerWorld;
138 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
139 world.file_id_to_uri(self)
143 impl<'a> TryConvWith for &'a TextDocumentItem {
144 type Ctx = ServerWorld;
145 type Output = FileId;
146 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
147 self.uri.try_conv_with(world)
151 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
152 type Ctx = ServerWorld;
153 type Output = FileId;
154 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
155 self.uri.try_conv_with(world)
159 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
160 type Ctx = ServerWorld;
161 type Output = FileId;
162 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
163 world.uri_to_file_id(&self.uri)
167 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
168 type Ctx = ServerWorld;
169 type Output = FilePosition;
170 fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
171 let file_id = self.text_document.try_conv_with(world)?;
172 let line_index = world.analysis().file_line_index(file_id);
173 let offset = self.position.conv_with(&line_index);
174 Ok(FilePosition { file_id, offset })
178 impl<T: TryConvWith> TryConvWith for Vec<T> {
179 type Ctx = <T as TryConvWith>::Ctx;
180 type Output = Vec<<T as TryConvWith>::Output>;
181 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
182 let mut res = Vec::with_capacity(self.len());
184 res.push(item.try_conv_with(ctx)?);
190 impl TryConvWith for SourceChange {
191 type Ctx = ServerWorld;
192 type Output = req::SourceChange;
193 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
194 let cursor_position = match self.cursor_position {
197 let line_index = world.analysis().file_line_index(pos.file_id);
201 .find(|it| it.file_id == pos.file_id)
202 .map(|it| it.edit.as_atoms())
204 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
206 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
207 Some(TextDocumentPositionParams {
208 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
213 let source_file_edits = self.source_file_edits.try_conv_with(world)?;
214 let file_system_edits = self.file_system_edits.try_conv_with(world)?;
215 Ok(req::SourceChange {
224 // HACK: we should translate offset to line/column using linde_index *with edits applied*.
225 // A naive version of this function would be to apply `edits` to the original text,
226 // construct a new line index and use that, but it would be slow.
228 // Writing fast & correct version is issue #105, let's use a quick hack in the meantime
229 fn translate_offset_with_edit(
230 pre_edit_index: &LineIndex,
232 edits: &[AtomTextEdit],
234 let fallback = pre_edit_index.line_col(offset);
235 let edit = match edits.first() {
236 None => return fallback,
239 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
240 if !(edit.delete.start() <= offset && offset <= end_offset) {
243 let rel_offset = offset - edit.delete.start();
244 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
245 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
246 if in_edit_line_col.line == 0 {
248 line: edit_line_col.line,
249 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
253 line: edit_line_col.line + in_edit_line_col.line,
254 col_utf16: in_edit_line_col.col_utf16,
259 impl TryConvWith for SourceFileEdit {
260 type Ctx = ServerWorld;
261 type Output = TextDocumentEdit;
262 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
263 let text_document = VersionedTextDocumentIdentifier {
264 uri: self.file_id.try_conv_with(world)?,
267 let line_index = world.analysis().file_line_index(self.file_id);
272 .map_conv_with(&line_index)
274 Ok(TextDocumentEdit {
281 impl TryConvWith for FileSystemEdit {
282 type Ctx = ServerWorld;
283 type Output = req::FileSystemEdit;
284 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
285 let res = match self {
286 FileSystemEdit::CreateFile { anchor, path } => {
287 let uri = world.file_id_to_uri(anchor)?;
288 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
289 let uri = uri.join(path)?;
290 req::FileSystemEdit::CreateFile { uri }
292 FileSystemEdit::MoveFile { file, path } => {
293 let src = world.file_id_to_uri(file)?;
294 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
295 let dst = src.join(path)?;
296 req::FileSystemEdit::MoveFile { src, dst }
307 line_index: &LineIndex,
308 ) -> Result<Location> {
309 let url = file_id.try_conv_with(world)?;
310 let loc = Location::new(url, range.conv_with(line_index));
314 pub trait MapConvWith<'a>: Sized + 'a {
318 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
319 ConvWithIter { iter: self, ctx }
323 impl<'a, I> MapConvWith<'a> for I
328 type Ctx = <I::Item as ConvWith>::Ctx;
329 type Output = <I::Item as ConvWith>::Output;
332 pub struct ConvWithIter<'a, I, Ctx: 'a> {
337 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
340 I::Item: ConvWith<Ctx = Ctx>,
342 type Item = <I::Item as ConvWith>::Output;
344 fn next(&mut self) -> Option<Self::Item> {
345 self.iter.next().map(|item| item.conv_with(self.ctx))