mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Don't insert }
when typing {
in string
This commit is contained in:
parent
3898387f3b
commit
8d2a33da05
1 changed files with 33 additions and 3 deletions
|
@ -23,7 +23,7 @@ use syntax::{
|
||||||
algo::find_node_at_offset,
|
algo::find_node_at_offset,
|
||||||
ast::{self, edit::IndentLevel, AstToken},
|
ast::{self, edit::IndentLevel, AstToken},
|
||||||
AstNode, Parse, SourceFile,
|
AstNode, Parse, SourceFile,
|
||||||
SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR},
|
SyntaxKind::{self, FIELD_EXPR, METHOD_CALL_EXPR},
|
||||||
TextRange, TextSize,
|
TextRange, TextSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,9 +95,16 @@ fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<
|
||||||
}
|
}
|
||||||
|
|
||||||
let brace_token = file.tree().syntax().token_at_offset(offset).right_biased()?;
|
let brace_token = file.tree().syntax().token_at_offset(offset).right_biased()?;
|
||||||
|
if brace_token.kind() != SyntaxKind::L_CURLY {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the `{` to get a better parse tree, and reparse
|
// Remove the `{` to get a better parse tree, and reparse.
|
||||||
let file = file.reparse(&Indel::delete(brace_token.text_range()));
|
let range = brace_token.text_range();
|
||||||
|
if !stdx::always!(range.len() == TextSize::of('{')) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let file = file.reparse(&Indel::delete(range));
|
||||||
|
|
||||||
if let Some(edit) = brace_expr(&file.tree(), offset) {
|
if let Some(edit) = brace_expr(&file.tree(), offset) {
|
||||||
return Some(edit);
|
return Some(edit);
|
||||||
|
@ -549,6 +556,29 @@ fn f() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noop_in_string_literal() {
|
||||||
|
// Regression test for #9351
|
||||||
|
type_char_noop(
|
||||||
|
'{',
|
||||||
|
r##"
|
||||||
|
fn check_with(ra_fixture: &str, expect: Expect) {
|
||||||
|
let base = r#"
|
||||||
|
enum E { T(), R$0, C }
|
||||||
|
use self::E::X;
|
||||||
|
const Z: E = E::C;
|
||||||
|
mod m {}
|
||||||
|
asdasdasdasdasdasda
|
||||||
|
sdasdasdasdasdasda
|
||||||
|
sdasdasdasdasd
|
||||||
|
"#;
|
||||||
|
let actual = completion_list(&format!("{}\n{}", base, ra_fixture));
|
||||||
|
expect.assert_eq(&actual)
|
||||||
|
}
|
||||||
|
"##,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn adds_closing_brace_for_use_tree() {
|
fn adds_closing_brace_for_use_tree() {
|
||||||
type_char(
|
type_char(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue