Skip to content

Commit

Permalink
feat: boost keyword suggestions in some locations, adds extends in …
Browse files Browse the repository at this point in the history
…type contexts
  • Loading branch information
zardoy committed Oct 2, 2022
1 parent 4722e67 commit b38a3fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions typescript/src/completions/boostKeywordSuggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { findChildContainingPosition } from '../utils'

export default (entries: ts.CompletionEntry[], position: number, node: ts.Node): ts.CompletionEntry[] | undefined => {
// todo-not-sure for now, requires explicit completion trigger
const prevCharIsSpace = node.getSourceFile().getFullText()[position - 1] === ' '
if (!prevCharIsSpace) return
let extendsKeyword = ts.isInterfaceDeclaration(node) && node.end === position - 1
const addOrBoostKeywords = [] as string[]
if (!extendsKeyword) {
const leftNode = findChildContainingPosition(ts, node.getSourceFile(), position - 2)
if (leftNode && ts.isIdentifier(leftNode) && ts.isTypeParameterDeclaration(leftNode.parent)) {
if (!leftNode.parent.constraint) extendsKeyword = true
} else if (leftNode) {
if (ts.isBlock(leftNode)) {
if (ts.isTryStatement(leftNode.parent) && leftNode.parent.tryBlock === leftNode) addOrBoostKeywords.push(...['catch', 'finally'])
else if (ts.isCatchClause(leftNode.parent) && leftNode.parent.block === leftNode) addOrBoostKeywords.push('finally')
}
if (leftNode.kind === ts.SyntaxKind.ExportKeyword) {
addOrBoostKeywords.push(...['const', 'function', 'default', 'from', 'let'])
}
}
}
if (extendsKeyword) addOrBoostKeywords.push('extends')
if (addOrBoostKeywords.length === 0) return
return [
...addOrBoostKeywords.map(keyword => ({ name: keyword, kind: ts.ScriptElementKind.keyword, sortText: '07' })),
...entries.filter(({ name }) => !addOrBoostKeywords.includes(name)),
]
}
2 changes: 2 additions & 0 deletions typescript/src/completionsAtPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import prepareTextForEmmet from './specialCommands/prepareTextForEmmet'
import objectLiteralHelpers from './completions/objectLiteralHelpers'
import switchCaseExcludeCovered from './completions/switchCaseExcludeCovered'
import additionalTypesSuggestions from './completions/additionalTypesSuggestions'
import boostKeywordSuggestions from './completions/boostKeywordSuggestions'

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

Expand Down Expand Up @@ -100,6 +101,7 @@ export const getCompletionsAtPosition = (
if (!prior) return

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

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

0 comments on commit b38a3fc

Please sign in to comment.