This commit is contained in:
Aleksey Kladov 2018-08-22 12:58:34 +03:00
parent 641659d5a8
commit e8dfb92641
4 changed files with 10 additions and 18 deletions

View file

@ -11,12 +11,7 @@ use libsyntax2::{
pub struct ActionResult { pub struct ActionResult {
pub edit: Edit, pub edit: Edit,
pub cursor_position: CursorPosition, pub cursor_position: Option<TextUnit>,
}
pub enum CursorPosition {
Same,
Offset(TextUnit),
} }
pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
@ -31,7 +26,7 @@ pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnO
edit.replace(right.range(), left.text()); edit.replace(right.range(), left.text());
ActionResult { ActionResult {
edit: edit.finish(), edit: edit.finish(),
cursor_position: CursorPosition::Same, cursor_position: None,
} }
}) })
} }
@ -58,7 +53,7 @@ pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnO
}; };
ActionResult { ActionResult {
edit: edit.finish(), edit: edit.finish(),
cursor_position: CursorPosition::Offset(offset), cursor_position: Some(offset),
} }
}) })
} }

View file

@ -21,7 +21,7 @@ pub use self::{
symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
edit::{EditBuilder, Edit, AtomEdit}, edit::{EditBuilder, Edit, AtomEdit},
code_actions::{ code_actions::{
ActionResult, CursorPosition, find_node, ActionResult, find_node,
flip_comma, add_derive, flip_comma, add_derive,
}, },
}; };

View file

@ -5,7 +5,7 @@ extern crate assert_eq_text;
use assert_eq_text::{assert_eq_dbg}; use assert_eq_text::{assert_eq_dbg};
use libeditor::{ use libeditor::{
ParsedFile, TextUnit, TextRange, ActionResult, CursorPosition, ParsedFile, TextUnit, TextRange, ActionResult,
highlight, runnables, extend_selection, file_structure, highlight, runnables, extend_selection, file_structure,
flip_comma, add_derive, matching_brace, flip_comma, add_derive, matching_brace,
}; };
@ -177,8 +177,8 @@ fn check_action<F: Fn(&ParsedFile, TextUnit) -> Option<ActionResult>>(
let result = f(&file, before_cursor_pos).expect("code action is not applicable"); let result = f(&file, before_cursor_pos).expect("code action is not applicable");
let actual = result.edit.apply(&before); let actual = result.edit.apply(&before);
let actual_cursor_pos = match result.cursor_position { let actual_cursor_pos = match result.cursor_position {
CursorPosition::Same => result.edit.apply_to_offset(before_cursor_pos).unwrap(), None => result.edit.apply_to_offset(before_cursor_pos).unwrap(),
CursorPosition::Offset(off) => off, Some(off) => off,
}; };
let actual = add_cursor(&actual, actual_cursor_pos); let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);

View file

@ -6,7 +6,7 @@ use languageserver_types::{
SymbolInformation, Position, Location, SymbolInformation, Position, Location,
}; };
use libanalysis::{Query}; use libanalysis::{Query};
use libeditor::{self, CursorPosition}; use libeditor;
use libsyntax2::TextUnit; use libsyntax2::TextUnit;
use serde_json::{to_value, from_value}; use serde_json::{to_value, from_value};
@ -230,11 +230,8 @@ pub fn handle_execute_command(
document_changes: None, document_changes: None,
}; };
let edit = req::ApplyWorkspaceEditParams { edit }; let edit = req::ApplyWorkspaceEditParams { edit };
let cursor_pos = match action_result.cursor_position { let cursor_pos = action_result.cursor_position
CursorPosition::Same => None, .map(|off| off.conv_with(&line_index));
CursorPosition::Offset(offset) => Some(offset.conv_with(&line_index)),
};
Ok((edit, cursor_pos)) Ok((edit, cursor_pos))
} }