|
| 1 | +use super::return_types; |
| 2 | +use ra_ide_api::{CompletionItemKind, FileId, FilePosition, LineCol, LineIndex}; |
| 3 | +use ra_syntax::TextRange; |
| 4 | + |
| 5 | +pub trait Conv { |
| 6 | + type Output; |
| 7 | + fn conv(self) -> Self::Output; |
| 8 | +} |
| 9 | + |
| 10 | +pub trait ConvWith<CTX> { |
| 11 | + type Output; |
| 12 | + fn conv_with(self, ctx: CTX) -> Self::Output; |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Clone, Copy)] |
| 16 | +pub struct Position { |
| 17 | + pub line_number: u32, |
| 18 | + pub column: u32, |
| 19 | +} |
| 20 | + |
| 21 | +impl ConvWith<(&LineIndex, FileId)> for Position { |
| 22 | + type Output = FilePosition; |
| 23 | + |
| 24 | + fn conv_with(self, (line_index, file_id): (&LineIndex, FileId)) -> Self::Output { |
| 25 | + let line_col = LineCol { line: self.line_number - 1, col_utf16: self.column - 1 }; |
| 26 | + let offset = line_index.offset(line_col); |
| 27 | + FilePosition { file_id, offset } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl ConvWith<&LineIndex> for TextRange { |
| 32 | + type Output = return_types::Range; |
| 33 | + |
| 34 | + fn conv_with(self, line_index: &LineIndex) -> Self::Output { |
| 35 | + let start = line_index.line_col(self.start()); |
| 36 | + let end = line_index.line_col(self.end()); |
| 37 | + |
| 38 | + return_types::Range { |
| 39 | + startLineNumber: start.line + 1, |
| 40 | + startColumn: start.col_utf16 + 1, |
| 41 | + endLineNumber: end.line + 1, |
| 42 | + endColumn: end.col_utf16 + 1, |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl Conv for CompletionItemKind { |
| 48 | + type Output = return_types::CompletionItemKind; |
| 49 | + |
| 50 | + fn conv(self) -> <Self as Conv>::Output { |
| 51 | + use return_types::CompletionItemKind::*; |
| 52 | + match self { |
| 53 | + CompletionItemKind::Keyword => Keyword, |
| 54 | + CompletionItemKind::Snippet => Snippet, |
| 55 | + CompletionItemKind::Module => Module, |
| 56 | + CompletionItemKind::Function => Function, |
| 57 | + CompletionItemKind::Struct => Struct, |
| 58 | + CompletionItemKind::Enum => Enum, |
| 59 | + CompletionItemKind::EnumVariant => EnumMember, |
| 60 | + CompletionItemKind::BuiltinType => Struct, |
| 61 | + CompletionItemKind::Binding => Variable, |
| 62 | + CompletionItemKind::Field => Field, |
| 63 | + CompletionItemKind::Trait => Interface, |
| 64 | + CompletionItemKind::TypeAlias => Struct, |
| 65 | + CompletionItemKind::Const => Constant, |
| 66 | + CompletionItemKind::Static => Value, |
| 67 | + CompletionItemKind::Method => Method, |
| 68 | + CompletionItemKind::TypeParam => TypeParameter, |
| 69 | + CompletionItemKind::Macro => Method, |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments