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
[SourceKit] Report cursor info information for literals #67613
Conversation
Adds literals support for the CursorInfo request. Previously CursorInfo just returned error for literals. Implements issue apple#57725
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 a lot for the PR, @barinsim. I’ve got a couple of minor comments inline. On a more general level, I think the problem you’ve been facing is that all of cursor info is designed to always reference a declaration, if you’re doing cursor info on a function call, we report the function declaration or if you’re doing cursor info on a variable, we return the corresponding variable declaration.
For literals, there isn’t such a natural declaration and that’s why you needed to add a LitExpr
to DeclInfo
, which doesn’t really make sense since the information about a declaration (that’s what DeclInfo
is), shouldn’t need an expression. And that’s also why you needed the isLiteral
checks.
What I would suggest is that we report the initializer that actually constructs eg. a Double
from the literal (Double.init(_builtinIntegerLiteral:)
) in cursor info. That initializer returns a value of type Double
so you shouldn’t need all the special isLiteral
handling anymore. You should be able to get that initializer using getBuiltinInitializer
on BuiltinLiteralExpr
.
Let me know if you need help with anything or have any questions about my suggestion or in general.
And one small thing: Could you run git-clang-format
before committing your sources to make sure they are formatted correctly?
Addresses PR review comments.
@ahoppen Thank you very much for your feedback! Reporting the actual initializer definitely fits better 🙂 I hope I addressed everything. 2 quick notes:
struct A: ExpressibleByIntegerLiteral {
init(integerLiteral value: Int) {
self.value = value
}
let value: Int
}
var a: A = 42 for
|
Oh, that’s even better. I didn’t think to look for
Cursor info still works on struct MyOptional: ExpressibleByNilLiteral {
init(nilLiteral: ()) {}
}
let _: MyOptional = nil The issue why we don’t get cursor info when Lines 2628 to 2634 in 0591975
If you wanted to, you could look into setting the initializer here as well but I’m not entirely sure what the trade-off is between performance of looking up |
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.
Looks very good. I just have a few minor comments.
@ahoppen Thank you very much for iterating on this with me! 🙂 I addressed the last changes you proposed. If they look alright to you, could you please run the CI? |
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.
Thank you @barinsim. Looks great to me
@swift-ci Please test |
lib/IDE/IDERequests.cpp
Outdated
CursorInfo = new ResolvedExprStartCursorInfo(CursorInfo->getSourceFile(), | ||
CursorInfo->getLoc(), E); |
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 change breaks test/refactoring/RefactoringKind/basic.swift
:
When you have an interpolated string literal and perform cursor info on it, previously we got an interpolated string literal expression. With this change, we get the string literal expression of the first segment inside the string literal before the first interpolation and thus offer to localize the string, which we shouldn’t. I checked and it looks like this change is no longer necessary to get cursor info for literals. Could you revert it?
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.
@ahoppen Yes, you are right 👍 I added a test case into the CursorInfo suite as well.
During which I found that getting info of the expression segment in the interpolated string does not work. We consider the whole interpolated string as a single token which has nested tokens within the expression segments. For example this causes that getSourceRange()
for ValueDecl
returns such range:
<START>var foo = <END>"Hello \(42) world"
This does not work if you are looking for the node of the 42
. Converting to a char based range fixed this.
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.
Oh, the good old char source range conversion for interpolated string literals. I so wish this wouldn’t be necessary. 🙄 Good catch though.
@swift-ci Please smoke test |
Adds literals support for the CursorInfo request.
Previously CursorInfo just returned error for literals.
Resolves #57725