Skip to content

Commit

Permalink
Merge #8784
Browse files Browse the repository at this point in the history
8784: feat: auto-insert `}` when typing `{` in use item r=jonas-schievink a=jonas-schievink

![Peek 2021-05-09 22-14](https://user-images.githubusercontent.com/1786438/117585742-45983f80-b114-11eb-80fc-d44f480fd012.gif)

cc #8636

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
  • Loading branch information
bors[bot] and jonas-schievink committed May 9, 2021
2 parents 75a5c0a + 64f97fb commit fd109fb
Showing 1 changed file with 131 additions and 19 deletions.
150 changes: 131 additions & 19 deletions crates/ide/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn on_char_typed_inner(
}

/// Inserts a closing `}` when the user types an opening `{`, wrapping an existing expression in a
/// block.
/// block, or a part of a `use` item.
fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<TextEdit> {
if !stdx::always!(file.tree().syntax().text().char_at(offset) == Some('{')) {
return None;
Expand All @@ -99,30 +99,59 @@ fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<
// Remove the `{` to get a better parse tree, and reparse
let file = file.reparse(&Indel::delete(brace_token.text_range()));

let mut expr: ast::Expr = find_node_at_offset(file.tree().syntax(), offset)?;
if expr.syntax().text_range().start() != offset {
return None;
if let Some(edit) = brace_expr(&file.tree(), offset) {
return Some(edit);
}

// Enclose the outermost expression starting at `offset`
while let Some(parent) = expr.syntax().parent() {
if parent.text_range().start() != expr.syntax().text_range().start() {
break;
}
if let Some(edit) = brace_use_path(&file.tree(), offset) {
return Some(edit);
}

match ast::Expr::cast(parent) {
Some(parent) => expr = parent,
None => break,
return None;

fn brace_use_path(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
let segment: ast::PathSegment = find_node_at_offset(file.syntax(), offset)?;
if segment.syntax().text_range().start() != offset {
return None;
}
}

// If it's a statement in a block, we don't know how many statements should be included
if ast::ExprStmt::can_cast(expr.syntax().parent()?.kind()) {
return None;
let tree: ast::UseTree = find_node_at_offset(file.syntax(), offset)?;

Some(TextEdit::insert(
tree.syntax().text_range().end() + TextSize::of("{"),
"}".to_string(),
))
}

// Insert `}` right after the expression.
Some(TextEdit::insert(expr.syntax().text_range().end() + TextSize::of("{"), "}".to_string()))
fn brace_expr(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
let mut expr: ast::Expr = find_node_at_offset(file.syntax(), offset)?;
if expr.syntax().text_range().start() != offset {
return None;
}

// Enclose the outermost expression starting at `offset`
while let Some(parent) = expr.syntax().parent() {
if parent.text_range().start() != expr.syntax().text_range().start() {
break;
}

match ast::Expr::cast(parent) {
Some(parent) => expr = parent,
None => break,
}
}

// If it's a statement in a block, we don't know how many statements should be included
if ast::ExprStmt::can_cast(expr.syntax().parent()?.kind()) {
return None;
}

// Insert `}` right after the expression.
Some(TextEdit::insert(
expr.syntax().text_range().end() + TextSize::of("{"),
"}".to_string(),
))
}
}

/// Returns an edit which should be applied after `=` was typed. Primarily,
Expand Down Expand Up @@ -440,7 +469,7 @@ fn foo() -> { 92 }
}

#[test]
fn adds_closing_brace() {
fn adds_closing_brace_for_expr() {
type_char(
'{',
r#"
Expand Down Expand Up @@ -519,4 +548,87 @@ fn f() {
"#,
);
}

#[test]
fn adds_closing_brace_for_use_tree() {
type_char(
'{',
r#"
use some::$0Path;
"#,
r#"
use some::{Path};
"#,
);
type_char(
'{',
r#"
use some::{Path, $0Other};
"#,
r#"
use some::{Path, {Other}};
"#,
);
type_char(
'{',
r#"
use some::{$0Path, Other};
"#,
r#"
use some::{{Path}, Other};
"#,
);
type_char(
'{',
r#"
use some::path::$0to::Item;
"#,
r#"
use some::path::{to::Item};
"#,
);
type_char(
'{',
r#"
use some::$0path::to::Item;
"#,
r#"
use some::{path::to::Item};
"#,
);
type_char(
'{',
r#"
use $0some::path::to::Item;
"#,
r#"
use {some::path::to::Item};
"#,
);
type_char(
'{',
r#"
use some::path::$0to::{Item};
"#,
r#"
use some::path::{to::{Item}};
"#,
);
type_char(
'{',
r#"
use $0Thing as _;
"#,
r#"
use {Thing as _};
"#,
);

type_char_noop(
'{',
r#"
use some::pa$0th::to::Item;
"#,
);
}
}

0 comments on commit fd109fb

Please sign in to comment.