1 use languageserver_types::{
2 Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3 TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier,
5 use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit};
6 use ra_editor::{AtomEdit, Edit, LineCol, LineIndex};
7 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
9 use crate::{req, server_world::ServerWorld, Result};
13 fn conv(self) -> Self::Output;
19 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
22 pub trait TryConvWith {
25 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
28 impl Conv for SyntaxKind {
29 type Output = SymbolKind;
31 fn conv(self) -> <Self as Conv>::Output {
33 SyntaxKind::FN_DEF => SymbolKind::Function,
34 SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
35 SyntaxKind::ENUM_DEF => SymbolKind::Enum,
36 SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
37 SyntaxKind::MODULE => SymbolKind::Module,
38 SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
39 SyntaxKind::STATIC_DEF => SymbolKind::Constant,
40 SyntaxKind::CONST_DEF => SymbolKind::Constant,
41 SyntaxKind::IMPL_ITEM => SymbolKind::Object,
42 _ => SymbolKind::Variable,
47 impl ConvWith for Position {
49 type Output = TextUnit;
51 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
53 let line_col = LineCol {
54 line: self.line as u32,
55 col: (self.character as u32).into(),
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);
68 Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
72 impl ConvWith for TextRange {
76 fn conv_with(self, line_index: &LineIndex) -> Range {
78 self.start().conv_with(line_index),
79 self.end().conv_with(line_index),
84 impl ConvWith for Range {
86 type Output = TextRange;
88 fn conv_with(self, line_index: &LineIndex) -> TextRange {
90 self.start.conv_with(line_index),
91 self.end.conv_with(line_index),
96 impl ConvWith for Edit {
98 type Output = Vec<TextEdit>;
100 fn conv_with(self, line_index: &LineIndex) -> Vec<TextEdit> {
103 .map_conv_with(line_index)
108 impl ConvWith for AtomEdit {
109 type Ctx = LineIndex;
110 type Output = TextEdit;
112 fn conv_with(self, line_index: &LineIndex) -> TextEdit {
114 range: self.delete.conv_with(line_index),
115 new_text: self.insert,
120 impl<T: ConvWith> ConvWith for Option<T> {
121 type Ctx = <T as ConvWith>::Ctx;
122 type Output = Option<<T as ConvWith>::Output>;
123 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
124 self.map(|x| ConvWith::conv_with(x, ctx))
128 impl<'a> TryConvWith for &'a Url {
129 type Ctx = ServerWorld;
130 type Output = FileId;
131 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
132 world.uri_to_file_id(self)
136 impl TryConvWith for FileId {
137 type Ctx = ServerWorld;
139 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
140 world.file_id_to_uri(self)
144 impl<'a> TryConvWith for &'a TextDocumentItem {
145 type Ctx = ServerWorld;
146 type Output = FileId;
147 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
148 self.uri.try_conv_with(world)
152 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
153 type Ctx = ServerWorld;
154 type Output = FileId;
155 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
156 self.uri.try_conv_with(world)
160 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
161 type Ctx = ServerWorld;
162 type Output = FileId;
163 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
164 world.uri_to_file_id(&self.uri)
168 impl<T: TryConvWith> TryConvWith for Vec<T> {
169 type Ctx = <T as TryConvWith>::Ctx;
170 type Output = Vec<<T as TryConvWith>::Output>;
171 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
172 let mut res = Vec::with_capacity(self.len());
174 res.push(item.try_conv_with(ctx)?);
180 impl TryConvWith for SourceChange {
181 type Ctx = ServerWorld;
182 type Output = req::SourceChange;
183 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
184 let cursor_position = match self.cursor_position {
187 let line_index = world.analysis().file_line_index(pos.file_id);
191 .find(|it| it.file_id == pos.file_id)
192 .map(|it| it.edits.as_slice())
194 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
195 let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64);
196 Some(TextDocumentPositionParams {
197 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
202 let source_file_edits = self.source_file_edits.try_conv_with(world)?;
203 let file_system_edits = self.file_system_edits.try_conv_with(world)?;
204 Ok(req::SourceChange {
213 // HACK: we should translate offset to line/column using linde_index *with edits applied*.
214 // A naive version of this function would be to apply `edits` to the original text,
215 // construct a new line index and use that, but it would be slow.
217 // Writing fast & correct version is issue #105, let's use a quick hack in the meantime
218 fn translate_offset_with_edit(
219 pre_edit_index: &LineIndex,
223 let fallback = pre_edit_index.line_col(offset);
224 let edit = match edits.first() {
225 None => return fallback,
228 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
229 if !(edit.delete.start() <= offset && offset <= end_offset) {
232 let rel_offset = offset - edit.delete.start();
233 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
234 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
235 if in_edit_line_col.line == 0 {
237 line: edit_line_col.line,
238 col: edit_line_col.col + in_edit_line_col.col,
242 line: edit_line_col.line + in_edit_line_col.line,
243 col: in_edit_line_col.col,
248 impl TryConvWith for SourceFileEdit {
249 type Ctx = ServerWorld;
250 type Output = TextDocumentEdit;
251 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
252 let text_document = VersionedTextDocumentIdentifier {
253 uri: self.file_id.try_conv_with(world)?,
256 let line_index = world.analysis().file_line_index(self.file_id);
257 let edits = self.edits.into_iter().map_conv_with(&line_index).collect();
258 Ok(TextDocumentEdit {
265 impl TryConvWith for FileSystemEdit {
266 type Ctx = ServerWorld;
267 type Output = req::FileSystemEdit;
268 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
269 let res = match self {
270 FileSystemEdit::CreateFile { anchor, path } => {
271 let uri = world.file_id_to_uri(anchor)?;
272 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
273 let uri = uri.join(path)?;
274 req::FileSystemEdit::CreateFile { uri }
276 FileSystemEdit::MoveFile { file, path } => {
277 let src = world.file_id_to_uri(file)?;
278 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
279 let dst = src.join(path)?;
280 req::FileSystemEdit::MoveFile { src, dst }
291 line_index: &LineIndex,
292 ) -> Result<Location> {
293 let url = file_id.try_conv_with(world)?;
294 let loc = Location::new(url, range.conv_with(line_index));
298 pub trait MapConvWith<'a>: Sized + 'a {
302 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
303 ConvWithIter { iter: self, ctx }
307 impl<'a, I> MapConvWith<'a> for I
312 type Ctx = <I::Item as ConvWith>::Ctx;
313 type Output = <I::Item as ConvWith>::Output;
316 pub struct ConvWithIter<'a, I, Ctx: 'a> {
321 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
324 I::Item: ConvWith<Ctx = Ctx>,
326 type Item = <I::Item as ConvWith>::Output;
328 fn next(&mut self) -> Option<Self::Item> {
329 self.iter.next().map(|item| item.conv_with(self.ctx))