Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide/src/inlay_hints/closing_brace.rs')
-rw-r--r--crates/ide/src/inlay_hints/closing_brace.rs43
1 files changed, 38 insertions, 5 deletions
diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs
index 2cefd5acdc..ea96c9504e 100644
--- a/crates/ide/src/inlay_hints/closing_brace.rs
+++ b/crates/ide/src/inlay_hints/closing_brace.rs
@@ -4,20 +4,21 @@
//! } /* fn g */
//! ```
use hir::{HirDisplay, Semantics};
-use ide_db::{base_db::FileRange, RootDatabase};
+use ide_db::{FileRange, RootDatabase};
+use span::EditionedFileId;
use syntax::{
ast::{self, AstNode, HasName},
match_ast, SyntaxKind, SyntaxNode, T,
};
-use crate::{FileId, InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
+use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
pub(super) fn hints(
acc: &mut Vec<InlayHint>,
sema: &Semantics<'_, RootDatabase>,
config: &InlayHintsConfig,
- file_id: FileId,
- node: SyntaxNode,
+ file_id: EditionedFileId,
+ mut node: SyntaxNode,
) -> Option<()> {
let min_lines = config.closing_brace_hints_min_lines?;
@@ -51,6 +52,15 @@ pub(super) fn hints(
let module = ast::Module::cast(list.syntax().parent()?)?;
(format!("mod {}", module.name()?), module.name().map(name))
+ } else if let Some(label) = ast::Label::cast(node.clone()) {
+ // in this case, `ast::Label` could be seen as a part of `ast::BlockExpr`
+ // the actual number of lines in this case should be the line count of the parent BlockExpr,
+ // which the `min_lines` config cares about
+ node = node.parent()?;
+ let block = label.syntax().parent().and_then(ast::BlockExpr::cast)?;
+ closing_token = block.stmt_list()?.r_curly_token()?;
+ let lifetime = label.lifetime().map_or_else(String::new, |it| it.to_string());
+ (lifetime, Some(label.syntax().text_range()))
} else if let Some(block) = ast::BlockExpr::cast(node.clone()) {
closing_token = block.stmt_list()?.r_curly_token()?;
@@ -107,7 +117,7 @@ pub(super) fn hints(
return None;
}
- let linked_location = name_range.map(|range| FileRange { file_id, range });
+ let linked_location = name_range.map(|range| FileRange { file_id: file_id.into(), range });
acc.push(InlayHint {
range: closing_token.text_range(),
kind: InlayKind::ClosingBrace,
@@ -191,4 +201,27 @@ fn f() {
"#,
);
}
+
+ #[test]
+ fn hints_closing_brace_for_block_expr() {
+ check_with_config(
+ InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG },
+ r#"
+fn test() {
+ 'end: {
+ 'do_a: {
+ 'do_b: {
+
+ }
+ //^ 'do_b
+ break 'end;
+ }
+ //^ 'do_a
+ }
+ //^ 'end
+ }
+//^ fn test
+"#,
+ );
+ }
}