|
| 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 | +} |
0 commit comments