mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-27 18:26:19 +00:00
Merge pull request #18952 from lh123/add-raw-keyword-complete
feat: complete raw, const keyword
This commit is contained in:
commit
903bc8133c
8 changed files with 168 additions and 8 deletions
|
|
@ -62,6 +62,7 @@ pub(crate) fn complete_expr_path(
|
|||
in_condition,
|
||||
incomplete_let,
|
||||
ref ref_expr_parent,
|
||||
after_amp,
|
||||
ref is_func_update,
|
||||
ref innermost_ret_ty,
|
||||
ref impl_,
|
||||
|
|
@ -69,8 +70,23 @@ pub(crate) fn complete_expr_path(
|
|||
..
|
||||
} = expr_ctx;
|
||||
|
||||
let wants_mut_token =
|
||||
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false);
|
||||
let (has_raw_token, has_const_token, has_mut_token) = ref_expr_parent
|
||||
.as_ref()
|
||||
.map(|it| (it.raw_token().is_some(), it.const_token().is_some(), it.mut_token().is_some()))
|
||||
.unwrap_or((false, false, false));
|
||||
|
||||
let wants_raw_token = ref_expr_parent.is_some() && !has_raw_token && after_amp;
|
||||
let wants_const_token =
|
||||
ref_expr_parent.is_some() && has_raw_token && !has_const_token && !has_mut_token;
|
||||
let wants_mut_token = if ref_expr_parent.is_some() {
|
||||
if has_raw_token {
|
||||
!has_const_token && !has_mut_token
|
||||
} else {
|
||||
!has_mut_token
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let scope_def_applicable = |def| match def {
|
||||
ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) => false,
|
||||
|
|
@ -354,6 +370,12 @@ pub(crate) fn complete_expr_path(
|
|||
add_keyword("else if", "else if $1 {\n $0\n}");
|
||||
}
|
||||
|
||||
if wants_raw_token {
|
||||
add_keyword("raw", "raw ");
|
||||
}
|
||||
if wants_const_token {
|
||||
add_keyword("const", "const ");
|
||||
}
|
||||
if wants_mut_token {
|
||||
add_keyword("mut", "mut ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -146,6 +146,7 @@ pub(crate) struct PathExprCtx {
|
|||
pub(crate) in_condition: bool,
|
||||
pub(crate) incomplete_let: bool,
|
||||
pub(crate) ref_expr_parent: Option<ast::RefExpr>,
|
||||
pub(crate) after_amp: bool,
|
||||
/// The surrounding RecordExpression we are completing a functional update
|
||||
pub(crate) is_func_update: Option<ast::RecordExpr>,
|
||||
pub(crate) self_param: Option<hir::SelfParam>,
|
||||
|
|
|
|||
|
|
@ -1151,6 +1151,9 @@ fn classify_name_ref(
|
|||
let after_if_expr = after_if_expr(it.clone());
|
||||
let ref_expr_parent =
|
||||
path.as_single_name_ref().and_then(|_| it.parent()).and_then(ast::RefExpr::cast);
|
||||
let after_amp = non_trivia_sibling(it.clone().into(), Direction::Prev)
|
||||
.map(|it| it.kind() == SyntaxKind::AMP)
|
||||
.unwrap_or(false);
|
||||
let (innermost_ret_ty, self_param) = {
|
||||
let find_ret_ty = |it: SyntaxNode| {
|
||||
if let Some(item) = ast::Item::cast(it.clone()) {
|
||||
|
|
@ -1220,6 +1223,7 @@ fn classify_name_ref(
|
|||
after_if_expr,
|
||||
in_condition,
|
||||
ref_expr_parent,
|
||||
after_amp,
|
||||
is_func_update,
|
||||
innermost_ret_ty,
|
||||
self_param,
|
||||
|
|
|
|||
|
|
@ -66,6 +66,7 @@ fn baz() {
|
|||
kw loop
|
||||
kw match
|
||||
kw mut
|
||||
kw raw
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
|
|
@ -436,6 +437,114 @@ fn completes_in_let_initializer() {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_after_ref_expr() {
|
||||
check(
|
||||
r#"fn main() { let _ = &$0 }"#,
|
||||
expect![[r#"
|
||||
fn main() fn()
|
||||
bt u32 u32
|
||||
kw crate::
|
||||
kw false
|
||||
kw for
|
||||
kw if
|
||||
kw if let
|
||||
kw loop
|
||||
kw match
|
||||
kw mut
|
||||
kw raw
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
kw unsafe
|
||||
kw while
|
||||
kw while let
|
||||
"#]],
|
||||
);
|
||||
check(
|
||||
r#"fn main() { let _ = &raw $0 }"#,
|
||||
expect![[r#"
|
||||
fn main() fn()
|
||||
bt u32 u32
|
||||
kw const
|
||||
kw crate::
|
||||
kw false
|
||||
kw for
|
||||
kw if
|
||||
kw if let
|
||||
kw loop
|
||||
kw match
|
||||
kw mut
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
kw unsafe
|
||||
kw while
|
||||
kw while let
|
||||
"#]],
|
||||
);
|
||||
check(
|
||||
r#"fn main() { let _ = &raw const $0 }"#,
|
||||
expect![[r#"
|
||||
fn main() fn()
|
||||
bt u32 u32
|
||||
kw crate::
|
||||
kw false
|
||||
kw for
|
||||
kw if
|
||||
kw if let
|
||||
kw loop
|
||||
kw match
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
kw unsafe
|
||||
kw while
|
||||
kw while let
|
||||
"#]],
|
||||
);
|
||||
check(
|
||||
r#"fn main() { let _ = &raw mut $0 }"#,
|
||||
expect![[r#"
|
||||
fn main() fn()
|
||||
bt u32 u32
|
||||
kw crate::
|
||||
kw false
|
||||
kw for
|
||||
kw if
|
||||
kw if let
|
||||
kw loop
|
||||
kw match
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
kw unsafe
|
||||
kw while
|
||||
kw while let
|
||||
"#]],
|
||||
);
|
||||
check(
|
||||
r#"fn main() { let _ = &mut $0 }"#,
|
||||
expect![[r#"
|
||||
fn main() fn()
|
||||
bt u32 u32
|
||||
kw crate::
|
||||
kw false
|
||||
kw for
|
||||
kw if
|
||||
kw if let
|
||||
kw loop
|
||||
kw match
|
||||
kw return
|
||||
kw self::
|
||||
kw true
|
||||
kw unsafe
|
||||
kw while
|
||||
kw while let
|
||||
"#]],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn struct_initializer_field_expr() {
|
||||
check(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue