mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Apply code actions
This commit is contained in:
parent
25aebb5225
commit
be742a5877
12 changed files with 247 additions and 118 deletions
|
@ -1,23 +1,23 @@
|
|||
use languageserver_types::{Range, SymbolKind, Position};
|
||||
use libeditor::{LineIndex, LineCol};
|
||||
use languageserver_types::{Range, SymbolKind, Position, TextEdit};
|
||||
use libeditor::{LineIndex, LineCol, Edit, AtomEdit};
|
||||
use libsyntax2::{SyntaxKind, TextUnit, TextRange};
|
||||
|
||||
pub trait Conv {
|
||||
type Output;
|
||||
fn conv(&self) -> Self::Output;
|
||||
fn conv(self) -> Self::Output;
|
||||
}
|
||||
|
||||
pub trait ConvWith {
|
||||
type Ctx;
|
||||
type Output;
|
||||
fn conv_with(&self, ctx: &Self::Ctx) -> Self::Output;
|
||||
fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
|
||||
}
|
||||
|
||||
impl Conv for SyntaxKind {
|
||||
type Output = SymbolKind;
|
||||
|
||||
fn conv(&self) -> <Self as Conv>::Output {
|
||||
match *self {
|
||||
fn conv(self) -> <Self as Conv>::Output {
|
||||
match self {
|
||||
SyntaxKind::FUNCTION => SymbolKind::Function,
|
||||
SyntaxKind::STRUCT => SymbolKind::Struct,
|
||||
SyntaxKind::ENUM => SymbolKind::Enum,
|
||||
|
@ -35,7 +35,7 @@ impl ConvWith for Position {
|
|||
type Ctx = LineIndex;
|
||||
type Output = TextUnit;
|
||||
|
||||
fn conv_with(&self, line_index: &LineIndex) -> TextUnit {
|
||||
fn conv_with(self, line_index: &LineIndex) -> TextUnit {
|
||||
// TODO: UTF-16
|
||||
let line_col = LineCol {
|
||||
line: self.line as u32,
|
||||
|
@ -49,8 +49,8 @@ impl ConvWith for TextUnit {
|
|||
type Ctx = LineIndex;
|
||||
type Output = Position;
|
||||
|
||||
fn conv_with(&self, line_index: &LineIndex) -> Position {
|
||||
let line_col = line_index.line_col(*self);
|
||||
fn conv_with(self, line_index: &LineIndex) -> Position {
|
||||
let line_col = line_index.line_col(self);
|
||||
// TODO: UTF-16
|
||||
Position::new(line_col.line as u64, u32::from(line_col.col) as u64)
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ impl ConvWith for TextRange {
|
|||
type Ctx = LineIndex;
|
||||
type Output = Range;
|
||||
|
||||
fn conv_with(&self, line_index: &LineIndex) -> Range {
|
||||
fn conv_with(self, line_index: &LineIndex) -> Range {
|
||||
Range::new(
|
||||
self.start().conv_with(line_index),
|
||||
self.end().conv_with(line_index),
|
||||
|
@ -72,10 +72,70 @@ impl ConvWith for Range {
|
|||
type Ctx = LineIndex;
|
||||
type Output = TextRange;
|
||||
|
||||
fn conv_with(&self, line_index: &LineIndex) -> TextRange {
|
||||
fn conv_with(self, line_index: &LineIndex) -> TextRange {
|
||||
TextRange::from_to(
|
||||
self.start.conv_with(line_index),
|
||||
self.end.conv_with(line_index),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue