-
Notifications
You must be signed in to change notification settings - Fork 307
Don’t offer text edits from inlay hints if they are syntactically invalid #770
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,35 @@ | |
import Dispatch | ||
import LanguageServerProtocol | ||
import SourceKitD | ||
import SwiftSyntax | ||
|
||
fileprivate extension TokenSyntax { | ||
/// Returns `false` if it is known that this token can’t be followed by a type | ||
/// annotation. | ||
var canBeFollowedByTypeAnnotation: Bool { | ||
var node = Syntax(self) | ||
LOOP: while let parent = node.parent { | ||
switch parent.kind { | ||
case .caseItem, .closureParam: | ||
// case items (inside a switch) and closure parameters can’t have type | ||
// annotations. | ||
return false | ||
case .codeBlockItem, .memberDeclListItem: | ||
// Performance optimization. If we walked the parents up to code block item, | ||
// we can’t enter a case item or closure param anymore. No need walking | ||
// the tree any further. | ||
break LOOP | ||
default: | ||
break | ||
} | ||
node = parent | ||
} | ||
|
||
// By default, assume that the token can be followed by a type annotation as | ||
// most locations that produce a variable type info can. | ||
return true | ||
} | ||
} | ||
|
||
/// A typed variable as returned by sourcekitd's CollectVariableType. | ||
struct VariableTypeInfo { | ||
|
@@ -22,6 +51,9 @@ struct VariableTypeInfo { | |
var printedType: String | ||
/// Whether the variable has an explicit type annotation in the source file. | ||
var hasExplicitType: Bool | ||
/// Whether we should suggest making an edit to add the type annotation to the | ||
/// source file. | ||
var canBeFollowedByTypeAnnotation: Bool | ||
|
||
init?(_ dict: SKDResponseDictionary, in snapshot: DocumentSnapshot) { | ||
let keys = dict.sourcekitd.keys | ||
|
@@ -34,10 +66,12 @@ struct VariableTypeInfo { | |
let hasExplicitType: Bool = dict[keys.variable_type_explicit] else { | ||
return nil | ||
} | ||
let tokenAtOffset = snapshot.tokens.syntaxTree?.token(at: AbsolutePosition(utf8Offset: offset)) | ||
|
||
self.range = startIndex..<endIndex | ||
self.printedType = printedType | ||
self.hasExplicitType = hasExplicitType | ||
self.canBeFollowedByTypeAnnotation = tokenAtOffset?.canBeFollowedByTypeAnnotation ?? true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. |
||
} | ||
} | ||
|
||
|
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.
I was going to ask if this handles the case that the param is in
()
. But apparently it does, becauseclosureParam
is only the non-signature case.x in
is aClosureParamListSyntax
where as(x) in
is aClosureParameterClauseSyntax
. The former hasClosureParam
s and the latterClosureParameter
s 😅.Not something that needs fixing here, but worth knowing. I don't have a great name for the former case 🤔.