Skip to content

Commit 20dd2f4

Browse files
Ilya Golovinzardoy
andcommitted
feat(suggestions.displayImportedInfo): add hover support
Co-authored-by: Vitaly <vital2580@icloud.com>
1 parent a9b729c commit 20dd2f4

3 files changed

Lines changed: 62 additions & 19 deletions

File tree

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import getImportPath from '../utils/getImportPath'
12
import { sharedCompletionContext } from './sharedContext'
23

34
export default (entries: ts.CompletionEntry[]) => {
@@ -9,24 +10,13 @@ export default (entries: ts.CompletionEntry[]) => {
910
for (const entry of entries) {
1011
const { symbol } = entry
1112
if (!symbol) continue
12-
const [node] = symbol.declarations ?? []
13-
if (!node) continue
14-
let importDeclaration: ts.ImportDeclaration | undefined
15-
if (ts.isImportSpecifier(node) && ts.isNamedImports(node.parent) && ts.isImportDeclaration(node.parent.parent.parent)) {
16-
importDeclaration = node.parent.parent.parent
17-
} else if (ts.isImportClause(node) && ts.isImportDeclaration(node.parent)) {
18-
importDeclaration = node.parent
19-
} else if (ts.isNamespaceImport(node) && ts.isImportClause(node.parent) && ts.isImportDeclaration(node.parent.parent)) {
20-
// todo-low(builtin) maybe reformat text
21-
importDeclaration = node.parent.parent
22-
}
23-
if (importDeclaration) {
24-
prevCompletionsMap[entry.name] ??= {}
25-
let importPath = importDeclaration.moduleSpecifier.getText()
26-
const symbolsLimit = 40
27-
if (importPath.length > symbolsLimit) importPath = `${importPath.slice(0, symbolsLimit / 2)}...${importPath.slice(-symbolsLimit / 2)}`
28-
const detailPrepend = displayImportedInfo === 'short-format' ? `(from ${importPath}) ` : `Imported from ${importPath}\n\n`
29-
prevCompletionsMap[entry.name]!.detailPrepend = detailPrepend
30-
}
13+
let { quotedPath: importPath } = getImportPath(symbol) ?? {}
14+
if (!importPath) continue
15+
16+
prevCompletionsMap[entry.name] ??= {}
17+
const symbolsLimit = 40
18+
if (importPath.length > symbolsLimit) importPath = `${importPath.slice(0, symbolsLimit / 2)}...${importPath.slice(-symbolsLimit / 2)}`
19+
const detailPrepend = displayImportedInfo === 'short-format' ? `(from ${importPath}) ` : `Imported from ${importPath}\n\n`
20+
prevCompletionsMap[entry.name]!.detailPrepend = detailPrepend
3121
}
3222
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
import { GetConfig } from './types'
2+
import { findChildContainingExactPosition } from './utils'
3+
import getImportPath from './utils/getImportPath'
24

35
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, languageServiceHost: ts.LanguageServiceHost, c: GetConfig) => {
46
proxy.getQuickInfoAtPosition = (...args) => {
57
const [fileName, position] = args
68
const prior = languageService.getQuickInfoAtPosition(...args)
79
if (!prior) return
10+
const program = languageService.getProgram()!
11+
const sourceFile = program.getSourceFile(fileName)!
12+
13+
if (c('suggestions.displayImportedInfo') !== 'disable') {
14+
const node = findChildContainingExactPosition(sourceFile, position)
15+
const possiblyImportKeywords = prior.displayParts?.at(-3)
16+
if (possiblyImportKeywords?.text === 'import' && node) {
17+
const symbolAtLocation = program.getTypeChecker().getSymbolAtLocation(node)
18+
if (symbolAtLocation) {
19+
const result = getImportPath(symbolAtLocation)
20+
if (result) {
21+
const { quotedPath: importPath, importKind } = result
22+
23+
prior.displayParts!.at(-3)!.text =
24+
{
25+
[ts.SyntaxKind.NamespaceImport]: 'import * as',
26+
[ts.SyntaxKind.NamedImports]: 'import {',
27+
}[importKind] ?? possiblyImportKeywords.text
28+
29+
prior.displayParts = [
30+
...(prior.displayParts || []),
31+
{ kind: 'text', text: `${importKind === ts.SyntaxKind.NamedImports ? ' }' : ''} from ${importPath}` },
32+
]
33+
}
34+
}
35+
}
36+
}
837
return prior
938
}
1039
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default (symbol: ts.Symbol) => {
2+
const [node] = symbol.declarations ?? []
3+
if (!node) return
4+
5+
if (!node) return undefined
6+
let importDeclaration: ts.ImportDeclaration | undefined
7+
let importKind!: ts.SyntaxKind
8+
if (ts.isImportSpecifier(node) && ts.isNamedImports(node.parent) && ts.isImportDeclaration(node.parent.parent.parent)) {
9+
importDeclaration = node.parent.parent.parent
10+
importKind = ts.SyntaxKind.NamedImports
11+
} else if (ts.isImportClause(node) && ts.isImportDeclaration(node.parent)) {
12+
importDeclaration = node.parent
13+
} else if (ts.isNamespaceImport(node) && ts.isImportClause(node.parent) && ts.isImportDeclaration(node.parent.parent)) {
14+
// todo-low(builtin) maybe reformat text
15+
importDeclaration = node.parent.parent
16+
importKind = ts.SyntaxKind.NamespaceImport
17+
}
18+
19+
if (!importDeclaration) return undefined
20+
return {
21+
quotedPath: importDeclaration.moduleSpecifier.getText(),
22+
importKind,
23+
}
24+
}

0 commit comments

Comments
 (0)