Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions crates/ty_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ impl<'m> ContextCursor<'m> {
// keywords), but it indicates that the user has typed
// `import`. This is useful to know in some contexts. And this
// applies also to the other keywords.
if !matches!(last.kind(), TokenKind::Name) && !last.kind().is_keyword() {
if !is_name_like_token(last) {
return None;
}
// This one's weird, but if the cursor is beyond
Expand Down Expand Up @@ -1717,7 +1717,6 @@ impl<'t> CompletionTargetTokens<'t> {
/// Look for the best matching token pattern at the given offset.
fn find(cursor: &ContextCursor<'t>) -> Option<CompletionTargetTokens<'t>> {
static OBJECT_DOT_EMPTY: [TokenKind; 1] = [TokenKind::Dot];
static OBJECT_DOT_NON_EMPTY: [TokenKind; 2] = [TokenKind::Dot, TokenKind::Name];

let before = cursor.tokens_before;
Some(
Expand All @@ -1732,10 +1731,10 @@ impl<'t> CompletionTargetTokens<'t> {
object,
attribute: None,
}
} else if let Some([_dot, attribute]) =
token_suffix_by_kinds(before, OBJECT_DOT_NON_EMPTY)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This previously required exactly TokenKind::Name.

} else if let [.., object, dot, attribute] = before
&& dot.kind() == TokenKind::Dot
&& is_name_like_token(attribute)
{
let object = before[..before.len() - 2].last()?;
CompletionTargetTokens::PossibleObjectDot {
object,
attribute: Some(attribute),
Expand Down Expand Up @@ -2439,6 +2438,15 @@ fn token_suffix_by_kinds<const N: usize>(
}))
}

/// Returns `true` if the token is a `Name` or a keyword.
///
/// Keywords are included because the lexer will lex a partially-typed
/// attribute name as a keyword token when it happens to match one
/// (e.g., `{1}.is` lexes `is` as `TokenKind::Is` rather than `TokenKind::Name`).
fn is_name_like_token(token: &Token) -> bool {
matches!(token.kind(), TokenKind::Name) || token.kind().is_keyword()
}

/// Returns the "kind" of a completion using just its type information.
///
/// This is meant to be a very general classification of this completion.
Expand Down Expand Up @@ -4778,6 +4786,20 @@ Re<CURSOR>
builder.build().contains("add").not_contains("values");
}

#[test]
fn attribute_access_set_keyword_prefix() {
let builder = completion_test_builder(
"\
{1}.is<CURSOR>
",
);

builder
.build()
.contains("isdisjoint")
.not_contains("isinstance");
}

#[test]
fn attribute_parens() {
let builder = completion_test_builder(
Expand Down
Loading