[ty] Suppress completions when introducing names with as by BurntSushi · Pull Request #21460 · astral-sh/ruff (original) (raw)

@BurntSushi

There are a few places in Python where it is known that new names are being introduced and thus we probably shouldn't offer completions. We already handle this today for things like class <CURSOR> and def <CURSOR>. But we didn't handle as <CURSOR>, which can appear in import, with, except and match statements. Indeed, these are exactly the 4 cases where the as keyword can occur. So we look for the presence of as and suppress completions based on that.

While we're here, we also make the implementation a bit more robust with respect to suppressing completions when the user hasn't typed anything. Namely, previously, we'd still offer completions in a class <CURSOR> context. But it looks like LSP clients (at least, VS Code) doesn't ask for completions here, so we were "saved" incidentally. This PR detects this case and suppresses completions there so we don't rely on LSP client behavior to handle that case correctly.

Fixes astral-sh/ty#1287