Skip to content

Commit 24e105e

Browse files
committed
feat(special-command): migrateRequireToImports (just a shortcut for existing quickfix code action)
1 parent 0b1c13f commit 24e105e

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
{
7171
"command": "printPerformanceMemoryInfo",
7272
"title": "Print Performance & Memory Info"
73+
},
74+
{
75+
"command": "migrateRequireToImports",
76+
"title": "Migrate Require to Imports"
7377
}
7478
],
7579
"keybindings": [

src/specialCommands.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { compact } from '@zardoy/utils'
77
import { offsetPosition } from '@zardoy/vscode-utils/build/position'
88
import { RequestInputTypes, RequestOutputTypes } from '../typescript/src/ipcTypes'
99
import { sendCommand } from './sendCommand'
10-
import { tsRangeToVscode, tsRangeToVscodeSelection } from './util'
10+
import { tsRangeToVscode, tsRangeToVscodeSelection, tsTextChangesToVscodeTextEdits } from './util'
1111
import { onCompletionAcceptedOverride } from './onCompletionAccepted'
1212

1313
export default () => {
@@ -309,6 +309,16 @@ export default () => {
309309
console.show(true)
310310
})
311311

312+
registerExtensionCommand('migrateRequireToImports', async () => {
313+
const data = await sendCommand('getMigrateToImportsEdits', {})
314+
if (!data) return
315+
const { document } = vscode.window.activeTextEditor!
316+
const edits = tsTextChangesToVscodeTextEdits(document, data)
317+
const edit = new vscode.WorkspaceEdit()
318+
edit.set(document.uri, edits)
319+
await vscode.workspace.applyEdit(edit)
320+
})
321+
312322
// registerExtensionCommand('insertImportFlatten', () => {
313323
// // got -> default, got
314324
// type A = ts.Type

typescript/src/ipcTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const triggerCharacterCommands = [
1717
'getLastResolvedCompletion',
1818
'getArgumentReferencesFromCurrentParameter',
1919
'performanceInfo',
20+
'getMigrateToImportsEdits',
2021
] as const
2122

2223
export type TriggerCharacterCommand = (typeof triggerCharacterCommands)[number]
@@ -115,6 +116,7 @@ export type RequestOutputTypes = {
115116
}
116117
getArgumentReferencesFromCurrentParameter: Array<{ line: number; character: number; filename: string }>
117118
'emmet-completions': EmmetResult
119+
getMigrateToImportsEdits: ts.TextChange[]
118120
}
119121

120122
// export type EmmetResult = {

typescript/src/specialCommands/handle.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ export default (
304304
memoryUsedMb: toMb(process.memoryUsage().heapUsed),
305305
}
306306
}
307+
if (specialCommand === 'getMigrateToImportsEdits') {
308+
const combinedCodeFix = languageService.getCombinedCodeFix(
309+
{ type: 'file', fileName: sourceFile.fileName },
310+
'requireInTs',
311+
ts.getDefaultFormatCodeSettings(),
312+
preferences,
313+
)
314+
return combinedCodeFix.changes[0]?.textChanges
315+
}
307316

308317
return null
309318
}

0 commit comments

Comments
 (0)