Skip to content

Commit b38a3fc

Browse files
committed
feat: boost keyword suggestions in some locations, adds extends in type contexts
1 parent 4722e67 commit b38a3fc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { findChildContainingPosition } from '../utils'
2+
3+
export default (entries: ts.CompletionEntry[], position: number, node: ts.Node): ts.CompletionEntry[] | undefined => {
4+
// todo-not-sure for now, requires explicit completion trigger
5+
const prevCharIsSpace = node.getSourceFile().getFullText()[position - 1] === ' '
6+
if (!prevCharIsSpace) return
7+
let extendsKeyword = ts.isInterfaceDeclaration(node) && node.end === position - 1
8+
const addOrBoostKeywords = [] as string[]
9+
if (!extendsKeyword) {
10+
const leftNode = findChildContainingPosition(ts, node.getSourceFile(), position - 2)
11+
if (leftNode && ts.isIdentifier(leftNode) && ts.isTypeParameterDeclaration(leftNode.parent)) {
12+
if (!leftNode.parent.constraint) extendsKeyword = true
13+
} else if (leftNode) {
14+
if (ts.isBlock(leftNode)) {
15+
if (ts.isTryStatement(leftNode.parent) && leftNode.parent.tryBlock === leftNode) addOrBoostKeywords.push(...['catch', 'finally'])
16+
else if (ts.isCatchClause(leftNode.parent) && leftNode.parent.block === leftNode) addOrBoostKeywords.push('finally')
17+
}
18+
if (leftNode.kind === ts.SyntaxKind.ExportKeyword) {
19+
addOrBoostKeywords.push(...['const', 'function', 'default', 'from', 'let'])
20+
}
21+
}
22+
}
23+
if (extendsKeyword) addOrBoostKeywords.push('extends')
24+
if (addOrBoostKeywords.length === 0) return
25+
return [
26+
...addOrBoostKeywords.map(keyword => ({ name: keyword, kind: ts.ScriptElementKind.keyword, sortText: '07' })),
27+
...entries.filter(({ name }) => !addOrBoostKeywords.includes(name)),
28+
]
29+
}

typescript/src/completionsAtPosition.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import prepareTextForEmmet from './specialCommands/prepareTextForEmmet'
1313
import objectLiteralHelpers from './completions/objectLiteralHelpers'
1414
import switchCaseExcludeCovered from './completions/switchCaseExcludeCovered'
1515
import additionalTypesSuggestions from './completions/additionalTypesSuggestions'
16+
import boostKeywordSuggestions from './completions/boostKeywordSuggestions'
1617

1718
export type PrevCompletionMap = Record<string, { originalName?: string; documentationOverride?: string | ts.SymbolDisplayPart[] }>
1819

@@ -100,6 +101,7 @@ export const getCompletionsAtPosition = (
100101
if (!prior) return
101102

102103
if (c('fixSuggestionsSorting')) prior.entries = fixPropertiesSorting(prior.entries, leftNode, sourceFile, program) ?? prior.entries
104+
if (node) prior.entries = boostKeywordSuggestions(prior.entries, position, node) ?? prior.entries
103105

104106
const entryNames = new Set(prior.entries.map(({ name }) => name))
105107
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))

0 commit comments

Comments
 (0)