From 72eb9de74739887874bebb08c71b574106ae80f1 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Wed, 26 Dec 2018 00:45:13 +0800 Subject: add fix for removing unnecessary braces in use statements --- crates/ra_editor/src/code_actions.rs | 7 +++++++ crates/ra_editor/src/lib.rs | 26 +++++++++++++++++++++----- crates/ra_editor/src/typing.rs | 31 +++++++++++++------------------ 3 files changed, 41 insertions(+), 23 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs index 1d78cb7e8..7615f37a6 100644 --- a/crates/ra_editor/src/code_actions.rs +++ b/crates/ra_editor/src/code_actions.rs @@ -12,6 +12,7 @@ use crate::{find_node_at_offset, TextEdit, TextEditBuilder}; #[derive(Debug)] pub struct LocalEdit { + pub label: String, pub edit: TextEdit, pub cursor_position: Option, } @@ -30,6 +31,7 @@ pub fn flip_comma<'a>( edit.replace(prev.range(), next.text().to_string()); edit.replace(next.range(), prev.text().to_string()); LocalEdit { + label: "flip comma".to_string(), edit: edit.finish(), cursor_position: None, } @@ -58,6 +60,7 @@ pub fn add_derive<'a>( Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'), }; LocalEdit { + label: "add `#[derive]`".to_string(), edit: edit.finish(), cursor_position: Some(offset), } @@ -109,6 +112,7 @@ pub fn add_impl<'a>( buf.push_str("\n}"); edit.insert(start_offset, buf); LocalEdit { + label: "add impl".to_string(), edit: edit.finish(), cursor_position: Some(offset), } @@ -148,6 +152,7 @@ pub fn introduce_variable<'a>( } let cursor_position = anchor_stmt.range().start() + TextUnit::of_str("let "); LocalEdit { + label: "introduce variable".to_string(), edit: edit.finish(), cursor_position: Some(cursor_position), } @@ -194,6 +199,7 @@ pub fn make_pub_crate<'a>( || parent.children().any(|child| child.kind() == VISIBILITY) { return LocalEdit { + label: "make pub crate".to_string(), edit: edit.finish(), cursor_position: Some(offset), }; @@ -201,6 +207,7 @@ pub fn make_pub_crate<'a>( edit.insert(node_start, "pub(crate) ".to_string()); LocalEdit { + label: "make pub crate".to_string(), edit: edit.finish(), cursor_position: Some(node_start), } diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7a689b0f2..898d9b8c7 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -42,6 +42,7 @@ pub struct Diagnostic { pub range: TextRange, pub msg: String, pub severity: Severity, + pub fix: Option, } #[derive(Debug)] @@ -111,6 +112,7 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec { range: location_to_range(err.location()), msg: format!("Syntax Error: {}", err), severity: Severity::Error, + fix: None, }) .collect(); @@ -124,11 +126,27 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec LocalEdit { let pos = match text.find('\n') { None => { return LocalEdit { + label: "join lines".to_string(), edit: TextEditBuilder::new().finish(), cursor_position: None, }; @@ -51,6 +54,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { } LocalEdit { + label: "join lines".to_string(), edit: edit.finish(), cursor_position: None, } @@ -76,6 +80,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option { let mut edit = TextEditBuilder::new(); edit.insert(offset, inserted); Some(LocalEdit { + label: "on enter".to_string(), edit: edit.finish(), cursor_position: Some(cursor_position), }) @@ -126,6 +131,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option let mut edit = TextEditBuilder::new(); edit.insert(offset, ";".to_string()); Some(LocalEdit { + label: "add semicolon".to_string(), edit: edit.finish(), cursor_position: None, }) @@ -248,24 +254,13 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti Some(()) } -fn single_use_tree(tree_list: ast::UseTreeList) -> Option { - let mut res = None; - for child in tree_list.syntax().children() { - if let Some(tree) = ast::UseTree::cast(child) { - if tree.syntax().text().contains('\n') { - return None; - } - if mem::replace(&mut res, Some(tree)).is_some() { - return None; - } - } else { - match child.kind() { - WHITESPACE | L_CURLY | R_CURLY | COMMA => (), - _ => return None, - } - } +pub(crate) fn single_use_tree(tree_list: ast::UseTreeList) -> Option { + let sub_use_trees = tree_list.use_trees().count(); + if sub_use_trees != 1 { + return None; } - res + + tree_list.use_trees().next() } fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { -- cgit v1.2.3