Skip to content

Commit 97522d1

Browse files
authored
Merge pull request rust-lang#18939 from Veykril/push-wztmylkyqttu
Flip on typing config to be opt-in, better defaults
2 parents 0ed9d1c + 7be6698 commit 97522d1

File tree

7 files changed

+42
-32
lines changed

7 files changed

+42
-32
lines changed

src/tools/rust-analyzer/crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ impl<'db> SemanticsImpl<'db> {
17561756
let file_id = self.lookup(&root_node).unwrap_or_else(|| {
17571757
panic!(
17581758
"\n\nFailed to lookup {:?} in this Semantics.\n\
1759-
Make sure to use only query nodes, derived from this instance of Semantics.\n\
1759+
Make sure to only query nodes derived from this instance of Semantics.\n\
17601760
root node: {:?}\n\
17611761
known nodes: {}\n\n",
17621762
node,

src/tools/rust-analyzer/crates/ide-db/src/search.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,19 @@ impl<'a> FindUsages<'a> {
953953

954954
// Search for occurrences of the items name
955955
for offset in Self::match_indices(&text, finder, search_range) {
956-
tree.token_at_offset(offset).for_each(|token| {
957-
let Some(str_token) = ast::String::cast(token.clone()) else { return };
956+
let ret = tree.token_at_offset(offset).any(|token| {
957+
let Some(str_token) = ast::String::cast(token.clone()) else { return false };
958958
if let Some((range, Some(nameres))) =
959959
sema.check_for_format_args_template(token, offset)
960960
{
961-
if self.found_format_args_ref(file_id, range, str_token, nameres, sink) {}
961+
return self
962+
.found_format_args_ref(file_id, range, str_token, nameres, sink);
962963
}
964+
false
963965
});
966+
if ret {
967+
return;
968+
}
964969

965970
for name in
966971
Self::find_nodes(sema, name, &tree, offset).filter_map(ast::NameLike::cast)

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,11 @@ impl Analysis {
404404
&self,
405405
position: FilePosition,
406406
char_typed: char,
407-
chars_to_exclude: Option<String>,
408407
) -> Cancellable<Option<SourceChange>> {
409408
// Fast path to not even parse the file.
410409
if !typing::TRIGGER_CHARS.contains(char_typed) {
411410
return Ok(None);
412411
}
413-
if let Some(chars_to_exclude) = chars_to_exclude {
414-
if chars_to_exclude.contains(char_typed) {
415-
return Ok(None);
416-
}
417-
}
418412

419413
self.with_db(|db| typing::on_char_typed(db, position, char_typed))
420414
}

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,16 @@ config_data! {
326326
/// Show documentation.
327327
signatureInfo_documentation_enable: bool = true,
328328

329-
/// Specify the characters to exclude from triggering typing assists. The default trigger characters are `.`, `=`, `<`, `>`, `{`, and `(`.
330-
typing_excludeChars: Option<String> = Some("|<".to_owned()),
329+
/// Specify the characters allowed to invoke special on typing triggers.
330+
/// - typing `=` after `let` tries to smartly add `;` if `=` is followed by an existing expression
331+
/// - typing `=` between two expressions adds `;` when in statement position
332+
/// - typing `=` to turn an assignment into an equality comparison removes `;` when in expression position
333+
/// - typing `.` in a chain method call auto-indents
334+
/// - typing `{` or `(` in front of an expression inserts a closing `}` or `)` after the expression
335+
/// - typing `{` in a use item adds a closing `}` in the right place
336+
/// - typing `>` to complete a return type `->` will insert a whitespace after it
337+
/// - typing `<` in a path or type position inserts a closing `>` after the path or type.
338+
typing_triggerChars: Option<String> = Some("=.".to_owned()),
331339

332340

333341
/// Enables automatic discovery of projects using [`DiscoverWorkspaceConfig::command`].
@@ -2251,8 +2259,8 @@ impl Config {
22512259
}
22522260
}
22532261

2254-
pub fn typing_exclude_chars(&self) -> Option<String> {
2255-
self.typing_excludeChars().clone()
2262+
pub fn typing_trigger_chars(&self) -> &str {
2263+
self.typing_triggerChars().as_deref().unwrap_or_default()
22562264
}
22572265

22582266
// VSCode is our reference implementation, so we allow ourselves to work around issues by

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,29 +434,24 @@ pub(crate) fn handle_on_type_formatting(
434434
params: lsp_types::DocumentOnTypeFormattingParams,
435435
) -> anyhow::Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
436436
let _p = tracing::info_span!("handle_on_type_formatting").entered();
437+
let char_typed = params.ch.chars().next().unwrap_or('\0');
438+
if !snap.config.typing_trigger_chars().contains(char_typed) {
439+
return Ok(None);
440+
}
441+
437442
let mut position = from_proto::file_position(&snap, params.text_document_position)?;
438443
let line_index = snap.file_line_index(position.file_id)?;
439444

440445
// in `ide`, the `on_type` invariant is that
441446
// `text.char_at(position) == typed_char`.
442447
position.offset -= TextSize::of('.');
443-
let char_typed = params.ch.chars().next().unwrap_or('\0');
444448

445449
let text = snap.analysis.file_text(position.file_id)?;
446450
if stdx::never!(!text[usize::from(position.offset)..].starts_with(char_typed)) {
447451
return Ok(None);
448452
}
449453

450-
// We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
451-
// but it requires precise cursor positioning to work, and one can't
452-
// position the cursor with on_type formatting. So, let's just toggle this
453-
// feature off here, hoping that we'll enable it one day, 😿.
454-
if char_typed == '>' {
455-
return Ok(None);
456-
}
457-
let chars_to_exclude = snap.config.typing_exclude_chars();
458-
459-
let edit = snap.analysis.on_char_typed(position, char_typed, chars_to_exclude)?;
454+
let edit = snap.analysis.on_char_typed(position, char_typed)?;
460455
let edit = match edit {
461456
Some(it) => it,
462457
None => return Ok(None),

src/tools/rust-analyzer/docs/user/generated_config.adoc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,18 @@ Show full signature of the callable. Only shows parameters if disabled.
10511051
--
10521052
Show documentation.
10531053
--
1054-
[[rust-analyzer.typing.excludeChars]]rust-analyzer.typing.excludeChars (default: `"|<"`)::
1055-
+
1056-
--
1057-
Specify the characters to exclude from triggering typing assists. The default trigger characters are `.`, `=`, `<`, `>`, `{`, and `(`.
1054+
[[rust-analyzer.typing.triggerChars]]rust-analyzer.typing.triggerChars (default: `"=."`)::
1055+
+
1056+
--
1057+
Specify the characters allowed to invoke special on typing triggers.
1058+
- typing `=` after `let` tries to smartly add `;` if `=` is followed by an existing expression
1059+
- typing `=` between two expressions adds `;` when in statement position
1060+
- typing `=` to turn an assignment into an equality comparison removes `;` when in expression position
1061+
- typing `.` in a chain method call auto-indents
1062+
- typing `{` or `(` in front of an expression inserts a closing `}` or `)` after the expression
1063+
- typing `{` in a use item adds a closing `}` in the right place
1064+
- typing `>` to complete a return type `->` will insert a whitespace after it
1065+
- typing `<` in a path or type position inserts a closing `>` after the path or type.
10581066
--
10591067
[[rust-analyzer.vfs.extraIncludes]]rust-analyzer.vfs.extraIncludes (default: `[]`)::
10601068
+

src/tools/rust-analyzer/editors/code/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,9 +2745,9 @@
27452745
{
27462746
"title": "typing",
27472747
"properties": {
2748-
"rust-analyzer.typing.excludeChars": {
2749-
"markdownDescription": "Specify the characters to exclude from triggering typing assists. The default trigger characters are `.`, `=`, `<`, `>`, `{`, and `(`.",
2750-
"default": "|<",
2748+
"rust-analyzer.typing.triggerChars": {
2749+
"markdownDescription": "Specify the characters allowed to invoke special on typing triggers.\n- typing `=` after `let` tries to smartly add `;` if `=` is followed by an existing expression\n- typing `=` between two expressions adds `;` when in statement position\n- typing `=` to turn an assignment into an equality comparison removes `;` when in expression position\n- typing `.` in a chain method call auto-indents\n- typing `{` or `(` in front of an expression inserts a closing `}` or `)` after the expression\n- typing `{` in a use item adds a closing `}` in the right place\n- typing `>` to complete a return type `->` will insert a whitespace after it\n- typing `<` in a path or type position inserts a closing `>` after the path or type.",
2750+
"default": "=.",
27512751
"type": [
27522752
"null",
27532753
"string"

0 commit comments

Comments
 (0)