mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 13:18:47 +00:00
move
This commit is contained in:
parent
2d41bc3e6c
commit
5211e7d977
3 changed files with 37 additions and 30 deletions
|
@ -3,7 +3,7 @@ extern crate libsyntax2;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate test_utils;
|
extern crate test_utils;
|
||||||
|
|
||||||
use test_utils::{assert_eq_dbg};
|
use test_utils::{assert_eq_dbg, add_cursor, extract_offset, extract_range};
|
||||||
use libsyntax2::{File, TextUnit, TextRange};
|
use libsyntax2::{File, TextUnit, TextRange};
|
||||||
use libeditor::{
|
use libeditor::{
|
||||||
ActionResult,
|
ActionResult,
|
||||||
|
@ -15,7 +15,7 @@ use libeditor::{
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extend_selection() {
|
fn test_extend_selection() {
|
||||||
fn do_check(before: &str, afters: &[&str]) {
|
fn do_check(before: &str, afters: &[&str]) {
|
||||||
let (cursor, before) = extract_cursor(before);
|
let (cursor, before) = extract_offset(before);
|
||||||
let file = file(&before);
|
let file = file(&before);
|
||||||
let mut range = TextRange::offset_len(cursor, 0.into());
|
let mut range = TextRange::offset_len(cursor, 0.into());
|
||||||
for &after in afters {
|
for &after in afters {
|
||||||
|
@ -163,7 +163,7 @@ fn test_add_impl() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_matching_brace() {
|
fn test_matching_brace() {
|
||||||
fn do_check(before: &str, after: &str) {
|
fn do_check(before: &str, after: &str) {
|
||||||
let (pos, before) = extract_cursor(before);
|
let (pos, before) = extract_offset(before);
|
||||||
let file = file(&before);
|
let file = file(&before);
|
||||||
let new_pos = match matching_brace(&file, pos) {
|
let new_pos = match matching_brace(&file, pos) {
|
||||||
None => pos,
|
None => pos,
|
||||||
|
@ -215,9 +215,7 @@ pub fn reparse(&self, edit: &AtomEdit) -> File {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_join_lines_selection() {
|
fn test_join_lines_selection() {
|
||||||
fn do_check(before: &str, after: &str) {
|
fn do_check(before: &str, after: &str) {
|
||||||
let (sel_start, before) = extract_cursor(before);
|
let (sel, before) = extract_range(&before);
|
||||||
let (sel_end, before) = extract_cursor(&before);
|
|
||||||
let sel = TextRange::from_to(sel_start, sel_end);
|
|
||||||
let file = file(&before);
|
let file = file(&before);
|
||||||
let result = join_lines(&file, sel);
|
let result = join_lines(&file, sel);
|
||||||
let actual = result.edit.apply(&before);
|
let actual = result.edit.apply(&before);
|
||||||
|
@ -255,7 +253,7 @@ fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
|
||||||
after: &str,
|
after: &str,
|
||||||
f: F,
|
f: F,
|
||||||
) {
|
) {
|
||||||
let (before_cursor_pos, before) = extract_cursor(before);
|
let (before_cursor_pos, before) = extract_offset(before);
|
||||||
let file = file(&before);
|
let file = file(&before);
|
||||||
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);
|
||||||
|
@ -266,26 +264,3 @@ fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_cursor(text: &str) -> (TextUnit, String) {
|
|
||||||
let cursor = "<|>";
|
|
||||||
let cursor_pos = match text.find(cursor) {
|
|
||||||
None => panic!("text should contain cursor marker"),
|
|
||||||
Some(pos) => pos,
|
|
||||||
};
|
|
||||||
let mut new_text = String::with_capacity(text.len() - cursor.len());
|
|
||||||
new_text.push_str(&text[..cursor_pos]);
|
|
||||||
new_text.push_str(&text[cursor_pos + cursor.len()..]);
|
|
||||||
let cursor_pos = TextUnit::from(cursor_pos as u32);
|
|
||||||
(cursor_pos, new_text)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_cursor(text: &str, offset: TextUnit) -> String {
|
|
||||||
let offset: u32 = offset.into();
|
|
||||||
let offset: usize = offset as usize;
|
|
||||||
let mut res = String::new();
|
|
||||||
res.push_str(&text[..offset]);
|
|
||||||
res.push_str("<|>");
|
|
||||||
res.push_str(&text[offset..]);
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,3 +6,4 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
difference = "2.0.0"
|
difference = "2.0.0"
|
||||||
itertools = "0.7.8"
|
itertools = "0.7.8"
|
||||||
|
text_unit = "0.1.2"
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
extern crate difference;
|
extern crate difference;
|
||||||
extern crate itertools;
|
extern crate itertools;
|
||||||
|
extern crate text_unit;
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use text_unit::{TextUnit, TextRange};
|
||||||
|
|
||||||
pub use self::difference::Changeset as __Changeset;
|
pub use self::difference::Changeset as __Changeset;
|
||||||
|
|
||||||
|
@ -34,3 +36,32 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) {
|
||||||
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn extract_offset(text: &str) -> (TextUnit, String) {
|
||||||
|
let cursor = "<|>";
|
||||||
|
let cursor_pos = match text.find(cursor) {
|
||||||
|
None => panic!("text should contain cursor marker"),
|
||||||
|
Some(pos) => pos,
|
||||||
|
};
|
||||||
|
let mut new_text = String::with_capacity(text.len() - cursor.len());
|
||||||
|
new_text.push_str(&text[..cursor_pos]);
|
||||||
|
new_text.push_str(&text[cursor_pos + cursor.len()..]);
|
||||||
|
let cursor_pos = TextUnit::from(cursor_pos as u32);
|
||||||
|
(cursor_pos, new_text)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extract_range(text: &str) -> (TextRange, String) {
|
||||||
|
let (start, text) = extract_offset(text);
|
||||||
|
let (end, text) = extract_offset(&text);
|
||||||
|
(TextRange::from_to(start, end), text)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_cursor(text: &str, offset: TextUnit) -> String {
|
||||||
|
let offset: u32 = offset.into();
|
||||||
|
let offset: usize = offset as usize;
|
||||||
|
let mut res = String::new();
|
||||||
|
res.push_str(&text[..offset]);
|
||||||
|
res.push_str("<|>");
|
||||||
|
res.push_str(&text[offset..]);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue