feat: Import Statement Completions (#291)
https://devblogs.microsoft.com/typescript/announcing-typescript-4-3/#import-statement-completions
This commit is contained in:
parent
ed3886f4c8
commit
cd624aa05e
7 changed files with 66 additions and 12 deletions
src/server
|
@ -49,7 +49,7 @@ class ApplyCompletionCodeActionCommand implements CommandItem {
|
|||
|
||||
export default class TypeScriptCompletionItemProvider implements CompletionItemProvider {
|
||||
|
||||
public static readonly triggerCharacters = ['.', '"', '\'', '`', '/', '@', '<', '#']
|
||||
public static readonly triggerCharacters = ['.', '"', '\'', '`', '/', '@', '<', '#', ' ']
|
||||
private completeOption: SuggestOptions
|
||||
|
||||
constructor(
|
||||
|
@ -148,7 +148,7 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
|
|||
dotAccessorContext = { range, text }
|
||||
}
|
||||
}
|
||||
isIncomplete = (response as any).metadata && (response as any).metadata.isIncomplete
|
||||
isIncomplete = !!response.body.isIncomplete || (response as any).metadata && (response as any).metadata.isIncomplete
|
||||
entries = response.body.entries
|
||||
} catch (e) {
|
||||
if (e.message == 'No content available.') {
|
||||
|
@ -194,6 +194,8 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
|
|||
|
||||
case '#': // Workaround for https://github.com/microsoft/TypeScript/issues/36367
|
||||
return this.client.apiVersion.lt(API.v381) ? undefined : '#'
|
||||
case ' ':
|
||||
return this.client.apiVersion.gte(API.v430) ? ' ' : undefined
|
||||
|
||||
case '.':
|
||||
case '"':
|
||||
|
@ -338,6 +340,13 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
|
|||
}
|
||||
}
|
||||
|
||||
if (triggerCharacter === ' ') {
|
||||
if (!this.completeOption.importStatementSuggestions || !this.client.apiVersion.lt(API.v430)) {
|
||||
return false
|
||||
}
|
||||
return pre === 'import ';
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,9 @@ export interface SuggestOptions {
|
|||
readonly completeFunctionCalls: boolean
|
||||
readonly autoImports: boolean
|
||||
readonly includeAutomaticOptionalChainCompletions: boolean
|
||||
readonly importStatementSuggestions: boolean
|
||||
readonly includeCompletionsForImportStatements: boolean
|
||||
readonly includeCompletionsWithSnippetText: boolean
|
||||
}
|
||||
|
||||
export default class FileConfigurationManager {
|
||||
|
@ -156,6 +159,9 @@ export default class FileConfigurationManager {
|
|||
paths: config.get<boolean>('paths', true),
|
||||
completeFunctionCalls: config.get<boolean>('completeFunctionCalls', true),
|
||||
autoImports: config.get<boolean>('autoImports', true),
|
||||
importStatementSuggestions: config.get<boolean>('importStatements', true),
|
||||
includeCompletionsForImportStatements: config.get<boolean>('includeCompletionsForImportStatements', true),
|
||||
includeCompletionsWithSnippetText: config.get<boolean>('includeCompletionsWithSnippetText', true),
|
||||
includeAutomaticOptionalChainCompletions: config.get<boolean>('includeAutomaticOptionalChainCompletions', true)
|
||||
}
|
||||
}
|
||||
|
@ -173,6 +179,8 @@ export default class FileConfigurationManager {
|
|||
allowTextChangesInNewFiles: uri.startsWith('file:'),
|
||||
allowRenameOfImportPath: true,
|
||||
providePrefixAndSuffixTextForRename: config.get<boolean>('renameShorthandProperties', true) === false ? false : config.get<boolean>('useAliasesForRenames', true),
|
||||
includeCompletionsForImportStatements: this.getCompleteOptions(language).includeCompletionsForImportStatements,
|
||||
includeCompletionsWithSnippetText: this.getCompleteOptions(language).includeCompletionsWithSnippetText,
|
||||
}
|
||||
return preferences
|
||||
}
|
||||
|
@ -190,7 +198,7 @@ export default class FileConfigurationManager {
|
|||
|
||||
type ModuleImportType = 'relative' | 'non-relative' | 'auto'
|
||||
|
||||
function getImportModuleSpecifier(config): ModuleImportType {
|
||||
function getImportModuleSpecifier(config: WorkspaceConfiguration): ModuleImportType {
|
||||
let val = config.get('importModuleSpecifier')
|
||||
switch (val) {
|
||||
case 'relative':
|
||||
|
|
|
@ -39,7 +39,9 @@ export default class API {
|
|||
public static readonly v381 = API.fromSimpleString('3.8.1')
|
||||
public static readonly v390 = API.fromSimpleString('3.9.0')
|
||||
public static readonly v400 = API.fromSimpleString('4.0.0')
|
||||
public static readonly v401 = API.fromSimpleString('4.0.1');
|
||||
public static readonly v401 = API.fromSimpleString('4.0.1')
|
||||
public static readonly v420 = API.fromSimpleString('4.2.0')
|
||||
public static readonly v430 = API.fromSimpleString('4.3.0')
|
||||
|
||||
public static fromVersionString(versionString: string): API {
|
||||
let version = semver.valid(versionString)
|
||||
|
|
|
@ -37,7 +37,7 @@ export function convertCompletionEntry(
|
|||
if (tsEntry.isRecommended) {
|
||||
preselect = true
|
||||
}
|
||||
if (tsEntry.source) {
|
||||
if (tsEntry.source && tsEntry.hasAction) {
|
||||
// De-prioritze auto-imports https://github.com/Microsoft/vscode/issues/40311
|
||||
sortText = '\uffff' + sortText
|
||||
} else {
|
||||
|
@ -53,13 +53,18 @@ export function convertCompletionEntry(
|
|||
let insertText = tsEntry.insertText
|
||||
let commitCharacters = getCommitCharacters(tsEntry, context)
|
||||
|
||||
if (tsEntry.isImportStatementCompletion) {
|
||||
insertText = label
|
||||
insertTextFormat = InsertTextFormat.Snippet
|
||||
}
|
||||
|
||||
let textEdit: TextEdit | null = null
|
||||
if (tsEntry.replacementSpan) {
|
||||
let { start, end } = tsEntry.replacementSpan
|
||||
if (start.line == end.line) {
|
||||
textEdit = {
|
||||
range: Range.create(start.line - 1, start.offset - 1, end.line - 1, end.offset - 1),
|
||||
newText: insertText || label
|
||||
newText: tsEntry.insertText || label
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue