2018-08-13 08:35:17 -05:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use languageserver_types::{Range, SymbolKind, Position, TextEdit, Location, Url};
|
2018-08-12 18:38:34 -05:00
|
|
|
use libeditor::{LineIndex, LineCol, Edit, AtomEdit};
|
2018-08-12 13:02:56 -05:00
|
|
|
use libsyntax2::{SyntaxKind, TextUnit, TextRange};
|
|
|
|
|
2018-08-13 08:35:17 -05:00
|
|
|
use Result;
|
|
|
|
|
2018-08-12 13:02:56 -05:00
|
|
|
pub trait Conv {
|
|
|
|
type Output;
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv(self) -> Self::Output;
|
2018-08-12 13:02:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ConvWith {
|
|
|
|
type Ctx;
|
|
|
|
type Output;
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
|
2018-08-12 13:02:56 -05:00
|
|
|
}
|
|
|
|
|
2018-08-13 08:35:17 -05:00
|
|
|
pub trait TryConvWith {
|
|
|
|
type Ctx;
|
|
|
|
type Output;
|
|
|
|
fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
|
|
|
|
}
|
|
|
|
|
2018-08-12 13:02:56 -05:00
|
|
|
impl Conv for SyntaxKind {
|
|
|
|
type Output = SymbolKind;
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv(self) -> <Self as Conv>::Output {
|
|
|
|
match self {
|
2018-08-12 13:02:56 -05:00
|
|
|
SyntaxKind::FUNCTION => SymbolKind::Function,
|
|
|
|
SyntaxKind::STRUCT => SymbolKind::Struct,
|
|
|
|
SyntaxKind::ENUM => SymbolKind::Enum,
|
|
|
|
SyntaxKind::TRAIT => SymbolKind::Interface,
|
|
|
|
SyntaxKind::MODULE => SymbolKind::Module,
|
|
|
|
SyntaxKind::TYPE_ITEM => SymbolKind::TypeParameter,
|
|
|
|
SyntaxKind::STATIC_ITEM => SymbolKind::Constant,
|
|
|
|
SyntaxKind::CONST_ITEM => SymbolKind::Constant,
|
|
|
|
_ => SymbolKind::Variable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConvWith for Position {
|
|
|
|
type Ctx = LineIndex;
|
|
|
|
type Output = TextUnit;
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> TextUnit {
|
2018-08-12 13:02:56 -05:00
|
|
|
// 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;
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> Position {
|
|
|
|
let line_col = line_index.line_col(self);
|
2018-08-12 13:02:56 -05:00
|
|
|
// 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;
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> Range {
|
2018-08-12 13:02:56 -05:00
|
|
|
Range::new(
|
|
|
|
self.start().conv_with(line_index),
|
|
|
|
self.end().conv_with(line_index),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConvWith for Range {
|
|
|
|
type Ctx = LineIndex;
|
|
|
|
type Output = TextRange;
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> TextRange {
|
2018-08-12 13:02:56 -05:00
|
|
|
TextRange::from_to(
|
|
|
|
self.start.conv_with(line_index),
|
|
|
|
self.end.conv_with(line_index),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 18:38:34 -05:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 08:35:17 -05:00
|
|
|
impl<'a> TryConvWith for (&'a Path, TextRange) {
|
|
|
|
type Ctx = LineIndex;
|
|
|
|
type Output = Location;
|
|
|
|
|
|
|
|
fn try_conv_with(self, line_index: &LineIndex) -> Result<Location> {
|
|
|
|
let loc = Location::new(
|
|
|
|
Url::from_file_path(self.0)
|
|
|
|
.map_err(|()| format_err!("can't convert path to url: {}", self.0.display()))?,
|
|
|
|
self.1.conv_with(line_index),
|
|
|
|
);
|
|
|
|
Ok(loc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 18:38:34 -05:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|