Skip to content

Commit 9549e47

Browse files
authored
Merge pull request #18627 from Veykril/push-tzvnrnytrksr
Improve heuristics for on typing semicolon insertion
2 parents 2a06000 + a086560 commit 9549e47

File tree

1 file changed

+25
-2
lines changed
  • src/tools/rust-analyzer/crates/ide/src

1 file changed

+25
-2
lines changed

src/tools/rust-analyzer/crates/ide/src/typing.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
1616
mod on_enter;
1717

18+
use std::iter;
19+
1820
use ide_db::{base_db::SourceDatabase, FilePosition, RootDatabase};
1921
use span::{Edition, EditionedFileId};
2022
use syntax::{
@@ -120,7 +122,8 @@ fn on_opening_delimiter_typed(
120122
'(' => (
121123
')',
122124
SyntaxKind::L_PAREN,
123-
&[ast::Expr::can_cast, ast::Pat::can_cast, ast::Type::can_cast] as &[FilterFn],
125+
&[ast::Expr::can_cast as FilterFn, ast::Pat::can_cast, ast::Type::can_cast]
126+
as &[FilterFn],
124127
),
125128
'<' => ('>', SyntaxKind::L_ANGLE, &[ast::Type::can_cast as FilterFn] as &[FilterFn]),
126129
_ => return None,
@@ -208,7 +211,18 @@ fn on_delimited_node_typed(
208211
/// this works when adding `let =`.
209212
// FIXME: use a snippet completion instead of this hack here.
210213
fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
211-
if !stdx::always!(file.syntax().text().char_at(offset) == Some('=')) {
214+
let text = file.syntax().text();
215+
if !stdx::always!(text.char_at(offset) == Some('=')) {
216+
return None;
217+
}
218+
219+
let has_newline = iter::successors(Some(offset), |&offset| Some(offset + TextSize::new(1)))
220+
.filter_map(|offset| text.char_at(offset))
221+
.find(|&c| !c.is_whitespace() || c == '\n')
222+
== Some('n');
223+
// don't attempt to add `;` if there is a newline after the `=`, the intent is likely to write
224+
// out the expression afterwards!
225+
if has_newline {
212226
return None;
213227
}
214228

@@ -466,6 +480,15 @@ fn foo() {
466480
let foo =$0
467481
let bar = 1;
468482
}
483+
",
484+
);
485+
type_char_noop(
486+
'=',
487+
r"
488+
fn foo() {
489+
let foo =$0
490+
1 + 1
491+
}
469492
",
470493
);
471494
}

0 commit comments

Comments
 (0)