File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 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" : [
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ import { compact } from '@zardoy/utils'
77import { offsetPosition } from '@zardoy/vscode-utils/build/position'
88import { RequestInputTypes , RequestOutputTypes } from '../typescript/src/ipcTypes'
99import { sendCommand } from './sendCommand'
10- import { tsRangeToVscode , tsRangeToVscodeSelection } from './util'
10+ import { tsRangeToVscode , tsRangeToVscodeSelection , tsTextChangesToVscodeTextEdits } from './util'
1111import { onCompletionAcceptedOverride } from './onCompletionAccepted'
1212
1313export 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
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ export const triggerCharacterCommands = [
1717 'getLastResolvedCompletion' ,
1818 'getArgumentReferencesFromCurrentParameter' ,
1919 'performanceInfo' ,
20+ 'getMigrateToImportsEdits' ,
2021] as const
2122
2223export 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 = {
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments