Skip to content

Comments

[ty] Fix completions at end of file#20993

Merged
MichaReiser merged 5 commits intomainfrom
micha/fix-completion-at-end-of-file
Oct 21, 2025
Merged

[ty] Fix completions at end of file#20993
MichaReiser merged 5 commits intomainfrom
micha/fix-completion-at-end-of-file

Conversation

@MichaReiser
Copy link
Member

Summary

Fixes astral-sh/ty#1392

The methods on Tokens incorrectly assumed that there can be at most one token starting or ending at offset.
Unfortunately, this isn't the case because Ruff's lexer can produce zero-length tokens. The most common
zero length tokens are the Newline at the end of the file (The lexer must emit this token because every logical line must end with a newline token),
or Dedent tokens (which are emitted when there are "fewer" indents, they're always zero length).

The fix in this PR is to make the methods on Tokens aware of this and return the first token starting at
or last token ending at a given offset (instead of any token starting or ending at).

Test Plan

Added test

@MichaReiser MichaReiser added bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference labels Oct 20, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 20, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 20, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 20, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you!

Initially, I was hoping that something like

self.binary_search_by_key(&(offset, 0), |token: &Token| {
    (token.start(), std::ptr::from_ref(token) as usize)
})

would also work. It does always find the first element, but it obviously changes all Ok(…)s into Err(…)s, and some callers rely on that distinction.

@AlexWaygood AlexWaygood removed their request for review October 21, 2025 08:05
@MichaReiser MichaReiser enabled auto-merge (squash) October 21, 2025 09:19
@MichaReiser MichaReiser merged commit 69ce064 into main Oct 21, 2025
40 checks passed
@MichaReiser MichaReiser deleted the micha/fix-completion-at-end-of-file branch October 21, 2025 09:24
dcreager added a commit that referenced this pull request Oct 22, 2025
* main: (65 commits)
  [ty] Some more simplifications when rendering constraint sets (#21009)
  [ty] Make `attributes.md` mdtests faster (#21030)
  [ty] Set `INSTA_FORCE_PASS` and `INSTA_OUTPUT` environment variables from mdtest.py (#21029)
  [ty] Fall back to `Divergent` for deeply nested specializations (#20988)
  [`ruff`] Autogenerate TypeParam nodes (#21028)
  [ty] Add assertions to ensure that we never call `KnownClass::Tuple.to_instance()` or similar (#21027)
  [`ruff`] Auto generate ast Pattern nodes (#21024)
  [`flake8-simplify`] Skip `SIM911` when unknown arguments are present (#20697)
  Render a diagnostic for syntax errors introduced in formatter tests (#21021)
  [ty] Support goto-definition on vendored typeshed stubs (#21020)
  [ty] Implement go-to for binary and unary operators (#21001)
  [ty] Avoid ever-growing default types (#20991)
  [syntax-errors] Name is parameter and global (#20426)
  [ty] Disable panicking mdtest (#21016)
  [ty] Fix completions at end of file (#20993)
  [ty] Fix out-of-order semantic token for function with regular argument after kwargs (#21013)
  [ty] Fix auto import for files with `from __future__` import (#20987)
  [`fastapi`] Handle ellipsis defaults in FAST002 autofix (`FAST002`) (#20810)
  [`ruff`] Skip autofix for keyword and `__debug__` path params (#20960)
  [`flake8-bugbear`] Skip `B905` and `B912` if <2 iterables and no starred arguments (#20998)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing completions if cursor is at the end of the file

3 participants