-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Add stub mapping support to signature help #19570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
|
||
| // Create a range from the offset for the covering_node function. | ||
| let range = TextRange::new(offset, offset); | ||
| // Use length 1 if it fits within the root node, otherwise use zero-length range. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the benefit of using a 1 byte offset? I think this is a bit dangerous as it might panic if anyone ends up using the passed range to take a peek at the source text (or calls any method on tokens where some methods enforce that the range falls directly at a token boundary).
It might be better to use the actual len of the character at offset over using an incorrect range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently required to "trick" covering_node to not return the node to the left of the cursor position. Is there a reason why covering_node returns a node to the left of a zero-length range? Do other callers rely on this behavior? If not, perhaps the best fix here is to modify covering_node to not return the node to the left of a zero-length range?
Is there an existing function that retrieves the length of the character at offset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get the size of a character at a given offset:
a[offset..].chars().next().map(TextLen::text_len);Is there a reason why covering_node returns a node to the left of a zero-length range?
covering_node uses contains to test if two ranges overlap. Which I find the correct behavior in case we start with a range (in which case the nodes don't overlap).
The idea behind the API is that covering_node is very low level and unopinionated. It's also not the idea that it is used directly with an offset because there are cases where your offset falls directly between two nodes, in which case it requires some prioritization. The way we've implemented this in other LSP methods is by using tokens.at
let token = parsed
.tokens()
.at_offset(offset)
.max_by_key(|token| match token.kind() {
TokenKind::Name
| TokenKind::String
| TokenKind::Complex
| TokenKind::Float
| TokenKind::Int => 1,
_ => 0,
})?;This allows the caller to customize which tokens should be preferred. I'm not sure what the heuristic should be for signature help. We can consider extracting the above logic from find_goto_target if it is the same or we can deploy our own here.
dhruvmanila
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
* main: (24 commits) Add `Checker::context` method, deduplicate Unicode checks (#19609) [`flake8-pyi`] Preserve inline comment in ellipsis removal (`PYI013`) (#19399) [ty] Add flow diagram for import resolution [ty] Add comments to some core resolver functions [ty] Add missing ticks and use consistent quoting [ty] Reflow some long lines [ty] Unexport helper function [ty] Remove offset from `CompletionTargetTokens::Unknown` [`pyupgrade`] Fix `UP030` to avoid modifying double curly braces in format strings (#19378) [ty] fix a typo (#19621) [ty] synthesize `__replace__` for dataclasses (>=3.13) (#19545) [ty] Discard `Definition`s when normalizing `Signature`s (#19615) [ty] Fix empty spans following a line terminator and unprintable character spans in diagnostics (#19535) Add `LinterContext::settings` to avoid passing separate settings (#19608) Support `.pyi` files in ruff analyze graph (#19611) [ty] Sync vendored typeshed stubs (#19607) [ty] Bump docstring-adder pin (#19606) [`perflint`] Ignore rule if target is `global` or `nonlocal` (`PERF401`) (#19539) Add license classifier back to pyproject.toml (#19599) [ty] Add stub mapping support to signature help (#19570) ...
This PR improves the "signature help" language server feature in two ways: