-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: boost keyword suggestions in some locations, adds
extends
in …
…type contexts
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters