Skip to content

Commit 9603f99

Browse files
committed
fix: improve Copy Full Type command for indexed access
1 parent 0fa6b0b commit 9603f99

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { findChildContainingExactPosition } from '../utils'
2+
3+
export default (languageService: ts.LanguageService, sourceFile: ts.SourceFile, position: number) => {
4+
let node = findChildContainingExactPosition(sourceFile, position)
5+
if (!node) return
6+
node = getNodeForQuickInfo(node)
7+
const checker = languageService.getProgram()!.getTypeChecker()!
8+
const symbol = getSymbolAtLocationForQuickInfo(node, checker)
9+
if (!symbol) return
10+
const type = checker.getTypeOfSymbol(symbol)
11+
return checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.NoTypeReduction)
12+
}
13+
14+
function getNodeForQuickInfo(node: ts.Node): ts.Node {
15+
if (ts.isNewExpression(node.parent) && node.pos === node.parent.pos) {
16+
return node.parent.expression
17+
}
18+
if (ts.isNamedTupleMember(node.parent) && node.pos === node.parent.pos) {
19+
return node.parent
20+
}
21+
//@ts-expect-error
22+
if (tsFull.isImportMeta(node.parent) && node.parent.name === node) {
23+
return node.parent
24+
}
25+
if (ts.isJsxNamespacedName(node.parent)) {
26+
return node.parent
27+
}
28+
return node
29+
}
30+
31+
function getSymbolAtLocationForQuickInfo(node: ts.Node, checker: ts.TypeChecker): ts.Symbol | undefined {
32+
const object = tsFull.getContainingObjectLiteralElement(node as any) as any
33+
if (object) {
34+
const contextualType = checker.getContextualType(object.parent)
35+
const properties = contextualType && tsFull.getPropertySymbolsFromContextualType(object, checker as any, contextualType as any, /*unionSymbolOk*/ false)
36+
if (properties && properties.length === 1) {
37+
return properties[0]! as any
38+
}
39+
}
40+
return checker.getSymbolAtLocation(node)
41+
}

typescript/src/specialCommands/handle.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { lastResolvedCompletion } from '../completionEntryDetails'
77
import { overrideRenameRequest } from '../decorateFindRenameLocations'
88
import getEmmetCompletions from './emmet'
99
import objectIntoArrayConverters from './objectIntoArrayConverters'
10+
import getFullType from './getFullType'
1011

1112
export const previousGetCodeActionsResult = {
1213
value: undefined as undefined | Array<Record<'description' | 'name', string>>,
@@ -238,12 +239,10 @@ export default (
238239
return lastResolvedCompletion.value
239240
}
240241
if (specialCommand === 'getFullType') {
241-
const node = findChildContainingExactPosition(sourceFile, position)
242-
if (!node) return
243-
const checker = languageService.getProgram()!.getTypeChecker()!
244-
const type = checker.getTypeAtLocation(node)
242+
const text = getFullType(languageService, sourceFile, position)
243+
if (!text) return
245244
return {
246-
text: checker.typeToString(type, undefined, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.NoTypeReduction),
245+
text,
247246
}
248247
}
249248
if (specialCommand === 'getArgumentReferencesFromCurrentParameter') {

0 commit comments

Comments
 (0)