Skip to content

Commit ff34e99

Browse files
Copilotjakebailey
andcommitted
Refactor: Extract findPropertyInType helper and improve comments
Co-authored-by: jakebailey <[email protected]>
1 parent daf041f commit ff34e99

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

internal/ls/hover.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (l *LanguageService) getQuickInfoAndDocumentationForSymbol(c *checker.Check
7171
}
7272

7373
func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, symbol *ast.Symbol, declaration *ast.Node, location *ast.Node, contentFormat lsproto.MarkupKind) string {
74-
// Handle binding elements specially - we need to get the documentation from the property type
74+
// Handle binding elements specially (variables created from destructuring) - we need to get the documentation from the property type
7575
// The declaration passed in might be the binding element itself, but we need the interface property declaration
7676
if symbol != nil && symbol.ValueDeclaration != nil && ast.IsBindingElement(symbol.ValueDeclaration) && ast.IsIdentifier(location) {
7777
bindingElement := symbol.ValueDeclaration
@@ -84,19 +84,7 @@ func (l *LanguageService) getDocumentationFromDeclaration(c *checker.Checker, sy
8484
propertyName := name.Text()
8585
objectType := c.GetTypeAtLocation(parent)
8686
if objectType != nil {
87-
// For union types, try to find the property in any of the constituent types
88-
var propertySymbol *ast.Symbol
89-
if objectType.Flags()&checker.TypeFlagsUnion != 0 {
90-
for _, t := range objectType.AsUnionType().Types() {
91-
prop := c.GetPropertyOfType(t, propertyName)
92-
if prop != nil {
93-
propertySymbol = prop
94-
break
95-
}
96-
}
97-
} else {
98-
propertySymbol = c.GetPropertyOfType(objectType, propertyName)
99-
}
87+
propertySymbol := findPropertyInType(c, objectType, propertyName)
10088
if propertySymbol != nil && propertySymbol.ValueDeclaration != nil {
10189
declaration = propertySymbol.ValueDeclaration
10290
}
@@ -615,6 +603,20 @@ func writeQuotedString(b *strings.Builder, str string, quote bool) {
615603
}
616604
}
617605

606+
// findPropertyInType finds a property in a type, handling union types by searching constituent types
607+
func findPropertyInType(c *checker.Checker, objectType *checker.Type, propertyName string) *ast.Symbol {
608+
// For union types, try to find the property in any of the constituent types
609+
if objectType.Flags()&checker.TypeFlagsUnion != 0 {
610+
for _, t := range objectType.AsUnionType().Types() {
611+
if prop := c.GetPropertyOfType(t, propertyName); prop != nil {
612+
return prop
613+
}
614+
}
615+
return nil
616+
}
617+
return c.GetPropertyOfType(objectType, propertyName)
618+
}
619+
618620
func getEntityNameString(name *ast.Node) string {
619621
var b strings.Builder
620622
writeEntityNameParts(&b, name)

0 commit comments

Comments
 (0)