Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private static getKind(refactor: Proto.RefactorActionInfo): string {
if (refactor.name.startsWith('function_')) {
return TypeScriptRefactorProvider.extractFunctionKind
} else if (refactor.name.startsWith('constant_')) {
return TypeScriptRefactorProvider.extractConstantKind
} else if (refactor.name.startsWith('Move')) {
return TypeScriptRefactorProvider.moveKind
}
return CodeActionKind.Refactor
}
}
private convertApplicableRefactors(
body: Proto.ApplicableRefactorInfo[],
document: TextDocument,
file: string,
rangeOrSelection: Range
): CodeAction[] {
const actions: CodeAction[] = []
for (const info of body) {
if (!info.inlineable) {
const codeAction: CodeAction = {
title: info.description,
kind: CodeActionKind.Refactor
}
codeAction.command = {
title: info.description,
command: SelectRefactorCommand.ID,
arguments: [document, file, info, rangeOrSelection]
}
actions.push(codeAction)
} else {
for (const action of info.actions) {
actions.push(
this.refactorActionToCodeAction(
action,
document,
file,
info,
rangeOrSelection
let label = info.actions[idx].name
if (!label) return false
return this.doRefactoring.execute(
document,
file,
info.name,
label,
range
)
}
}
export default class TypeScriptRefactorProvider implements CodeActionProvider {
private static readonly extractFunctionKind = CodeActionKind.RefactorExtract + '.function'
private static readonly extractConstantKind = CodeActionKind.RefactorExtract + '.constant'
private static readonly moveKind = CodeActionKind.Refactor + '.move'
constructor(
private readonly client: ITypeScriptServiceClient,
private readonly formattingOptionsManager: FormattingOptionsManager,
) {
const doRefactoringCommand = commands.register(
new ApplyRefactoringCommand(this.client)
)
commands.register(new SelectRefactorCommand(doRefactoringCommand))
}
public static readonly metadata: CodeActionProviderMetadata = {
providedCodeActionKinds: [CodeActionKind.Refactor]
}
public async provideCodeActions(
let label = info.actions[idx].name
if (!label) return false
return this.doRefactoring.execute(
document,
file,
info.name,
label,
range
)
}
}
export default class TypeScriptRefactorProvider implements CodeActionProvider {
private static readonly extractFunctionKind = CodeActionKind.RefactorExtract + '.function'
private static readonly extractConstantKind = CodeActionKind.RefactorExtract + '.constant'
private static readonly moveKind = CodeActionKind.Refactor + '.move'
constructor(
private readonly client: ITypeScriptServiceClient,
private readonly formattingOptionsManager: FormattingOptionsManager,
) {
const doRefactoringCommand = commandManager.register(
new ApplyRefactoringCommand(this.client)
)
commandManager.register(new SelectRefactorCommand(doRefactoringCommand))
}
public static readonly metadata: CodeActionProviderMetadata = {
providedCodeActionKinds: [CodeActionKind.Refactor]
}
public async provideCodeActions(
private static readonly extractFunctionKind = CodeActionKind.RefactorExtract + '.function'
private static readonly extractConstantKind = CodeActionKind.RefactorExtract + '.constant'
private static readonly moveKind = CodeActionKind.Refactor + '.move'
constructor(
private readonly client: ITypeScriptServiceClient,
private readonly formattingOptionsManager: FormattingOptionsManager,
) {
const doRefactoringCommand = commands.register(
new ApplyRefactoringCommand(this.client)
)
commands.register(new SelectRefactorCommand(doRefactoringCommand))
}
public static readonly metadata: CodeActionProviderMetadata = {
providedCodeActionKinds: [CodeActionKind.Refactor]
}
public async provideCodeActions(
document: TextDocument,
range: Range,
context: CodeActionContext,
token: CancellationToken
): Promise {
if (!this.shouldTrigger(context)) {
return undefined
}
const file = this.client.toPath(document.uri)
if (!file) return undefined
await this.formattingOptionsManager.ensureConfigurationForDocument(document)
const args: Proto.GetApplicableRefactorsRequestArgs = typeConverters.Range.toFileRangeRequestArgs(
file,
private static getKind(refactor: Proto.RefactorActionInfo): string {
if (refactor.name.startsWith('function_')) {
return TypeScriptRefactorProvider.extractFunctionKind
} else if (refactor.name.startsWith('constant_')) {
return TypeScriptRefactorProvider.extractConstantKind
} else if (refactor.name.startsWith('Move')) {
return TypeScriptRefactorProvider.moveKind
}
return CodeActionKind.Refactor
}
}
let conf = fileConfigurationManager.getLanguageConfiguration(this.id)
if (this.client.apiVersion.gte(API.v290)
&& conf.get('updateImportsOnFileMove.enable')) {
this.disposables.push(
new UpdateImportsOnFileRenameHandler(client, this.fileConfigurationManager, this.id)
)
}
if (this.client.apiVersion.gte(API.v240)) {
this.disposables.push(
languages.registerCodeActionProvider(
languageIds,
new RefactorProvider(client, this.fileConfigurationManager),
'tsserver',
[CodeActionKind.Refactor]))
}
this.disposables.push(
languages.registerCodeActionProvider(
languageIds,
new InstallModuleProvider(client),
'tsserver')
)
this.disposables.push(
languages.registerCodeActionProvider(
languageIds,
new QuickfixProvider(client),
'tsserver',
[CodeActionKind.QuickFix]))
private convertApplicableRefactors(
body: Proto.ApplicableRefactorInfo[],
document: TextDocument,
file: string,
rangeOrSelection: Range
): CodeAction[] {
const actions: CodeAction[] = []
for (const info of body) {
if (!info.inlineable) {
const codeAction: CodeAction = {
title: info.description,
kind: CodeActionKind.Refactor
}
codeAction.command = {
title: info.description,
command: SelectRefactorCommand.ID,
arguments: [document, file, info, rangeOrSelection]
}
actions.push(codeAction)
} else {
for (const action of info.actions) {
actions.push(
this.refactorActionToCodeAction(
action,
document,
file,
info,
rangeOrSelection
private shouldTrigger(context: CodeActionContext): boolean {
if (
context.only &&
context.only.indexOf(CodeActionKind.Refactor) == -1
) {
return false
}
return true
}