2018-08-15 09:24:20 -05:00
|
|
|
use languageserver_types::{
|
2019-01-11 07:01:48 -06:00
|
|
|
self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink,
|
2019-01-03 07:14:36 -06:00
|
|
|
Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
|
|
|
|
TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
|
|
|
|
WorkspaceEdit,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
use ra_ide_api::{
|
2019-01-03 07:14:36 -06:00
|
|
|
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
2019-01-11 05:14:09 -06:00
|
|
|
InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo,
|
2019-01-08 11:31:43 -06:00
|
|
|
LineCol, LineIndex, translate_offset_with_edit
|
2019-01-03 07:14:36 -06:00
|
|
|
};
|
2018-10-15 16:44:23 -05:00
|
|
|
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
2019-01-03 07:14:36 -06:00
|
|
|
use ra_text_edit::{AtomTextEdit, TextEdit};
|
2018-10-15 16:44:23 -05:00
|
|
|
|
|
|
|
use crate::{req, server_world::ServerWorld, Result};
|
2018-08-13 08:35:17 -05:00
|
|
|
|
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-13 10:27:26 -05:00
|
|
|
SyntaxKind::FN_DEF => SymbolKind::Function,
|
|
|
|
SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
|
|
|
|
SyntaxKind::ENUM_DEF => SymbolKind::Enum,
|
|
|
|
SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
|
2018-08-12 13:02:56 -05:00
|
|
|
SyntaxKind::MODULE => SymbolKind::Module,
|
2018-08-13 10:27:26 -05:00
|
|
|
SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
|
|
|
|
SyntaxKind::STATIC_DEF => SymbolKind::Constant,
|
|
|
|
SyntaxKind::CONST_DEF => SymbolKind::Constant,
|
2018-12-26 14:24:49 -06:00
|
|
|
SyntaxKind::IMPL_BLOCK => SymbolKind::Object,
|
2018-08-12 13:02:56 -05:00
|
|
|
_ => SymbolKind::Variable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:02:47 -06:00
|
|
|
impl Conv for CompletionItemKind {
|
|
|
|
type Output = ::languageserver_types::CompletionItemKind;
|
|
|
|
|
|
|
|
fn conv(self) -> <Self as Conv>::Output {
|
2019-01-03 07:14:36 -06:00
|
|
|
use languageserver_types::CompletionItemKind::*;
|
2018-12-21 17:02:47 -06:00
|
|
|
match self {
|
|
|
|
CompletionItemKind::Keyword => Keyword,
|
|
|
|
CompletionItemKind::Snippet => Snippet,
|
2018-12-21 17:20:14 -06:00
|
|
|
CompletionItemKind::Module => Module,
|
|
|
|
CompletionItemKind::Function => Function,
|
2018-12-24 13:32:39 -06:00
|
|
|
CompletionItemKind::Struct => Struct,
|
|
|
|
CompletionItemKind::Enum => Enum,
|
2018-12-28 12:06:08 -06:00
|
|
|
CompletionItemKind::EnumVariant => EnumMember,
|
2018-12-21 17:24:59 -06:00
|
|
|
CompletionItemKind::Binding => Variable,
|
2018-12-25 08:15:40 -06:00
|
|
|
CompletionItemKind::Field => Field,
|
2018-12-21 17:02:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:59:32 -06:00
|
|
|
impl Conv for CompletionItem {
|
|
|
|
type Output = ::languageserver_types::CompletionItem;
|
|
|
|
|
|
|
|
fn conv(self) -> <Self as Conv>::Output {
|
|
|
|
let mut res = ::languageserver_types::CompletionItem {
|
|
|
|
label: self.label().to_string(),
|
2019-01-09 09:09:49 -06:00
|
|
|
detail: self.detail().map(|it| it.to_string()),
|
2018-12-21 16:59:32 -06:00
|
|
|
filter_text: Some(self.lookup().to_string()),
|
2018-12-21 17:02:47 -06:00
|
|
|
kind: self.kind().map(|it| it.conv()),
|
2018-12-21 16:59:32 -06:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
match self.insert_text() {
|
|
|
|
InsertText::PlainText { text } => {
|
|
|
|
res.insert_text = Some(text);
|
|
|
|
res.insert_text_format = Some(InsertTextFormat::PlainText);
|
|
|
|
}
|
|
|
|
InsertText::Snippet { text } => {
|
|
|
|
res.insert_text = Some(text);
|
|
|
|
res.insert_text_format = Some(InsertTextFormat::Snippet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 13:02:56 -05:00
|
|
|
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
|
|
|
let line_col = LineCol {
|
|
|
|
line: self.line as u32,
|
2018-11-16 05:02:45 -06:00
|
|
|
col_utf16: self.character as u32,
|
2018-08-12 13:02:56 -05:00
|
|
|
};
|
|
|
|
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-11-29 14:30:49 -06:00
|
|
|
Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
|
2018-08-12 13:02:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-11 12:07:17 -06:00
|
|
|
impl ConvWith for TextEdit {
|
2018-08-12 18:38:34 -05:00
|
|
|
type Ctx = LineIndex;
|
2018-12-11 12:07:17 -06:00
|
|
|
type Output = Vec<languageserver_types::TextEdit>;
|
2018-08-12 18:38:34 -05:00
|
|
|
|
2018-12-11 12:07:17 -06:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
|
2018-12-21 02:24:16 -06:00
|
|
|
self.as_atoms()
|
2018-08-12 18:38:34 -05:00
|
|
|
.into_iter()
|
|
|
|
.map_conv_with(line_index)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 02:24:16 -06:00
|
|
|
impl<'a> ConvWith for &'a AtomTextEdit {
|
2018-08-12 18:38:34 -05:00
|
|
|
type Ctx = LineIndex;
|
2018-12-11 12:07:17 -06:00
|
|
|
type Output = languageserver_types::TextEdit;
|
2018-08-12 18:38:34 -05:00
|
|
|
|
2018-12-11 12:07:17 -06:00
|
|
|
fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
|
|
|
|
languageserver_types::TextEdit {
|
2018-08-12 18:38:34 -05:00
|
|
|
range: self.delete.conv_with(line_index),
|
2018-12-21 02:24:16 -06:00
|
|
|
new_text: self.insert.clone(),
|
2018-08-12 18:38:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:23:22 -05:00
|
|
|
impl<T: ConvWith> ConvWith for Option<T> {
|
|
|
|
type Ctx = <T as ConvWith>::Ctx;
|
|
|
|
type Output = Option<<T as ConvWith>::Output>;
|
|
|
|
fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
|
|
|
|
self.map(|x| ConvWith::conv_with(x, ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:24:20 -05:00
|
|
|
impl<'a> TryConvWith for &'a Url {
|
2018-08-17 11:54:08 -05:00
|
|
|
type Ctx = ServerWorld;
|
2018-08-15 09:24:20 -05:00
|
|
|
type Output = FileId;
|
2018-08-17 11:54:08 -05:00
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
|
|
|
|
world.uri_to_file_id(self)
|
2018-08-15 09:24:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryConvWith for FileId {
|
2018-08-17 11:54:08 -05:00
|
|
|
type Ctx = ServerWorld;
|
2018-08-15 09:24:20 -05:00
|
|
|
type Output = Url;
|
2018-08-17 11:54:08 -05:00
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
|
|
|
|
world.file_id_to_uri(self)
|
2018-08-15 09:24:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryConvWith for &'a TextDocumentItem {
|
2018-08-17 11:54:08 -05:00
|
|
|
type Ctx = ServerWorld;
|
2018-08-15 09:24:20 -05:00
|
|
|
type Output = FileId;
|
2018-08-17 11:54:08 -05:00
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
|
|
|
|
self.uri.try_conv_with(world)
|
2018-08-15 09:24:20 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 08:35:17 -05:00
|
|
|
|
2018-08-15 09:24:20 -05:00
|
|
|
impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
|
2018-08-17 11:54:08 -05:00
|
|
|
type Ctx = ServerWorld;
|
2018-08-15 09:24:20 -05:00
|
|
|
type Output = FileId;
|
2018-08-17 11:54:08 -05:00
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
|
|
|
|
self.uri.try_conv_with(world)
|
2018-08-15 09:24:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryConvWith for &'a TextDocumentIdentifier {
|
2018-08-17 11:54:08 -05:00
|
|
|
type Ctx = ServerWorld;
|
2018-08-15 09:24:20 -05:00
|
|
|
type Output = FileId;
|
2018-08-17 11:54:08 -05:00
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
|
|
|
|
world.uri_to_file_id(&self.uri)
|
2018-08-15 09:24:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 05:57:41 -06:00
|
|
|
impl<'a> TryConvWith for &'a TextDocumentPositionParams {
|
|
|
|
type Ctx = ServerWorld;
|
|
|
|
type Output = FilePosition;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
|
|
|
|
let file_id = self.text_document.try_conv_with(world)?;
|
|
|
|
let line_index = world.analysis().file_line_index(file_id);
|
|
|
|
let offset = self.position.conv_with(&line_index);
|
|
|
|
Ok(FilePosition { file_id, offset })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 09:15:19 -06:00
|
|
|
impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
|
|
|
|
type Ctx = ServerWorld;
|
|
|
|
type Output = FileRange;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<FileRange> {
|
|
|
|
let file_id = self.0.try_conv_with(world)?;
|
|
|
|
let line_index = world.analysis().file_line_index(file_id);
|
|
|
|
let range = self.1.conv_with(&line_index);
|
|
|
|
Ok(FileRange { file_id, range })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:03:14 -05:00
|
|
|
impl<T: TryConvWith> TryConvWith for Vec<T> {
|
|
|
|
type Ctx = <T as TryConvWith>::Ctx;
|
|
|
|
type Output = Vec<<T as TryConvWith>::Output>;
|
|
|
|
fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
|
|
|
|
let mut res = Vec::with_capacity(self.len());
|
|
|
|
for item in self {
|
|
|
|
res.push(item.try_conv_with(ctx)?);
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryConvWith for SourceChange {
|
|
|
|
type Ctx = ServerWorld;
|
|
|
|
type Output = req::SourceChange;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
|
|
|
|
let cursor_position = match self.cursor_position {
|
|
|
|
None => None,
|
|
|
|
Some(pos) => {
|
|
|
|
let line_index = world.analysis().file_line_index(pos.file_id);
|
2018-12-25 13:49:18 -06:00
|
|
|
let edit = self
|
2018-10-15 16:44:23 -05:00
|
|
|
.source_file_edits
|
|
|
|
.iter()
|
|
|
|
.find(|it| it.file_id == pos.file_id)
|
2018-12-25 13:49:18 -06:00
|
|
|
.map(|it| &it.edit);
|
|
|
|
let line_col = match edit {
|
|
|
|
Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
|
|
|
|
None => line_index.line_col(pos.offset),
|
|
|
|
};
|
2018-11-29 14:30:49 -06:00
|
|
|
let position =
|
|
|
|
Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
|
2018-08-29 10:03:14 -05:00
|
|
|
Some(TextDocumentPositionParams {
|
|
|
|
text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
|
2018-10-09 08:00:20 -05:00
|
|
|
position,
|
2018-08-29 10:03:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2019-01-03 07:14:36 -06:00
|
|
|
let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
|
|
|
|
for resource_op in self.file_system_edits.try_conv_with(world)? {
|
|
|
|
document_changes.push(DocumentChangeOperation::Op(resource_op));
|
|
|
|
}
|
|
|
|
for text_document_edit in self.source_file_edits.try_conv_with(world)? {
|
|
|
|
document_changes.push(DocumentChangeOperation::Edit(text_document_edit));
|
|
|
|
}
|
|
|
|
let workspace_edit = WorkspaceEdit {
|
|
|
|
changes: None,
|
|
|
|
document_changes: Some(DocumentChanges::Operations(document_changes)),
|
|
|
|
};
|
2018-08-29 10:03:14 -05:00
|
|
|
Ok(req::SourceChange {
|
|
|
|
label: self.label,
|
2019-01-03 07:14:36 -06:00
|
|
|
workspace_edit,
|
2018-08-29 10:03:14 -05:00
|
|
|
cursor_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 02:15:23 -06:00
|
|
|
impl TryConvWith for SourceFileEdit {
|
2018-08-29 10:03:14 -05:00
|
|
|
type Ctx = ServerWorld;
|
|
|
|
type Output = TextDocumentEdit;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
|
|
|
|
let text_document = VersionedTextDocumentIdentifier {
|
|
|
|
uri: self.file_id.try_conv_with(world)?,
|
|
|
|
version: None,
|
|
|
|
};
|
|
|
|
let line_index = world.analysis().file_line_index(self.file_id);
|
2018-12-21 02:24:16 -06:00
|
|
|
let edits = self
|
|
|
|
.edit
|
|
|
|
.as_atoms()
|
|
|
|
.iter()
|
|
|
|
.map_conv_with(&line_index)
|
|
|
|
.collect();
|
2018-10-15 16:44:23 -05:00
|
|
|
Ok(TextDocumentEdit {
|
|
|
|
text_document,
|
|
|
|
edits,
|
|
|
|
})
|
2018-08-29 10:03:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryConvWith for FileSystemEdit {
|
|
|
|
type Ctx = ServerWorld;
|
2019-01-03 07:14:36 -06:00
|
|
|
type Output = ResourceOp;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
|
2018-08-29 10:03:14 -05:00
|
|
|
let res = match self {
|
2018-12-21 03:18:14 -06:00
|
|
|
FileSystemEdit::CreateFile { source_root, path } => {
|
2019-01-03 07:14:36 -06:00
|
|
|
let uri = world.path_to_uri(source_root, &path)?.to_string();
|
|
|
|
ResourceOp::Create(CreateFile { uri, options: None })
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
2018-12-21 03:18:14 -06:00
|
|
|
FileSystemEdit::MoveFile {
|
|
|
|
src,
|
|
|
|
dst_source_root,
|
|
|
|
dst_path,
|
|
|
|
} => {
|
2019-01-03 07:14:36 -06:00
|
|
|
let old_uri = world.file_id_to_uri(src)?.to_string();
|
|
|
|
let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
|
|
|
|
ResourceOp::Rename(RenameFile {
|
|
|
|
old_uri,
|
|
|
|
new_uri,
|
|
|
|
options: None,
|
|
|
|
})
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
2018-08-29 10:03:14 -05:00
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 08:09:39 -06:00
|
|
|
impl TryConvWith for &NavigationTarget {
|
|
|
|
type Ctx = ServerWorld;
|
|
|
|
type Output = Location;
|
|
|
|
fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
|
|
|
|
let line_index = world.analysis().file_line_index(self.file_id());
|
2019-01-11 09:17:20 -06:00
|
|
|
let range = self.focus_range().unwrap_or(self.full_range());
|
|
|
|
to_location(self.file_id(), range, &world, &line_index)
|
2019-01-11 05:14:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 07:01:48 -06:00
|
|
|
pub fn to_location_link(
|
|
|
|
target: &RangeInfo<NavigationTarget>,
|
|
|
|
world: &ServerWorld,
|
|
|
|
// line index for original range file
|
|
|
|
line_index: &LineIndex,
|
|
|
|
) -> Result<LocationLink> {
|
|
|
|
let url = target.info.file_id().try_conv_with(world)?;
|
|
|
|
let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
|
|
|
|
|
|
|
|
let res = LocationLink {
|
|
|
|
origin_selection_range: Some(target.range.conv_with(line_index)),
|
|
|
|
target_uri: url.to_string(),
|
2019-01-11 09:17:20 -06:00
|
|
|
target_range: target.info.full_range().conv_with(&tgt_line_index),
|
2019-01-11 07:01:48 -06:00
|
|
|
target_selection_range: target
|
|
|
|
.info
|
|
|
|
.focus_range()
|
|
|
|
.map(|it| it.conv_with(&tgt_line_index)),
|
|
|
|
};
|
|
|
|
Ok(res)
|
2019-01-02 08:09:39 -06:00
|
|
|
}
|
|
|
|
|
2018-08-15 09:24:20 -05:00
|
|
|
pub fn to_location(
|
|
|
|
file_id: FileId,
|
|
|
|
range: TextRange,
|
2018-08-17 11:54:08 -05:00
|
|
|
world: &ServerWorld,
|
2018-08-15 09:24:20 -05:00
|
|
|
line_index: &LineIndex,
|
|
|
|
) -> Result<Location> {
|
2018-10-15 16:44:23 -05:00
|
|
|
let url = file_id.try_conv_with(world)?;
|
|
|
|
let loc = Location::new(url, range.conv_with(line_index));
|
|
|
|
Ok(loc)
|
2018-08-13 08:35:17 -05:00
|
|
|
}
|
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
pub trait MapConvWith<'a>: Sized + 'a {
|
2018-08-12 18:38:34 -05:00
|
|
|
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
|
2018-10-15 16:44:23 -05:00
|
|
|
where
|
|
|
|
I: Iterator + 'a,
|
|
|
|
I::Item: ConvWith,
|
2018-08-12 18:38:34 -05:00
|
|
|
{
|
|
|
|
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>
|
2018-10-15 16:44:23 -05:00
|
|
|
where
|
|
|
|
I: Iterator,
|
|
|
|
I::Item: ConvWith<Ctx = Ctx>,
|
2018-08-12 18:38:34 -05:00
|
|
|
{
|
|
|
|
type Item = <I::Item as ConvWith>::Output;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.iter.next().map(|item| item.conv_with(self.ctx))
|
|
|
|
}
|
|
|
|
}
|