11import * as vscode from 'vscode'
2- import { relative , join } from 'path-browserify'
32import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'
4- import { partition } from 'lodash'
53import { registerExtensionCommand , showQuickPick , getExtensionSetting , getExtensionCommandId } from 'vscode-framework'
64import { compact } from '@zardoy/utils'
75import { RequestResponseTypes , RequestOptionsTypes } from '../typescript/src/ipcTypes'
86import { sendCommand } from './sendCommand'
9- import {
10- pickFileWithQuickPick ,
11- getTsLikePath ,
12- tsRangeToVscode ,
13- tsTextChangesToVscodeTextEdits ,
14- vscodeRangeToTs ,
15- tsTextChangesToVscodeSnippetTextEdits ,
16- } from './util'
7+ import { tsTextChangesToVscodeTextEdits , vscodeRangeToTs , tsTextChangesToVscodeSnippetTextEdits } from './util'
178
189// extended and interactive code actions
1910export default ( ) => {
@@ -53,7 +44,7 @@ export default () => {
5344 if ( context . triggerKind !== vscode . CodeActionTriggerKind . Invoke ) return
5445 const result = await getPossibleTwoStepRefactorings ( range )
5546 if ( ! result ) return
56- const { turnArrayIntoObject, moveToExistingFile , extendedCodeActions } = result
47+ const { turnArrayIntoObject, extendedCodeActions } = result
5748 const codeActions : vscode . CodeAction [ ] = [ ]
5849 const getCommand = ( arg ) : vscode . Command | undefined => ( {
5950 title : '' ,
@@ -69,14 +60,6 @@ export default () => {
6960 } )
7061 }
7162
72- if ( moveToExistingFile ) {
73- // codeActions.push({
74- // title: `Move to existing file`,
75- // command: getCommand({ moveToExistingFile }),
76- // kind: vscode.CodeActionKind.Refactor.append('move'),
77- // })
78- }
79-
8063 codeActions . push (
8164 ...compact (
8265 extendedCodeActions . map ( ( { title, kind, codes } ) : ExtendedCodeAction | undefined => {
@@ -131,7 +114,7 @@ export default () => {
131114 registerExtensionCommand ( 'applyRefactor' as any , async ( _ , arg ?: RequestResponseTypes [ 'getTwoStepCodeActions' ] ) => {
132115 if ( ! arg ) return
133116 let sendNextData : RequestOptionsTypes [ 'twoStepCodeActionSecondStep' ] [ 'data' ] | undefined
134- const { turnArrayIntoObject, moveToExistingFile } = arg
117+ const { turnArrayIntoObject } = arg
135118 if ( turnArrayIntoObject ) {
136119 const { keysCount, totalCount, totalObjectCount } = turnArrayIntoObject
137120 const selectedKey = await showQuickPick (
@@ -155,54 +138,12 @@ export default () => {
155138 }
156139 }
157140
158- if ( moveToExistingFile ) {
159- sendNextData = {
160- name : 'moveToExistingFile' ,
161- }
162- }
163-
164141 if ( ! sendNextData ) return
165142 const editor = vscode . window . activeTextEditor !
166143 const nextResponse = await getSecondStepRefactoringData ( editor . selection , sendNextData )
167144 if ( ! nextResponse ) throw new Error ( 'No code action data. Try debug.' )
168145 const edit = new vscode . WorkspaceEdit ( )
169- let mainChanges = 'edits' in nextResponse && nextResponse . edits
170- if ( moveToExistingFile && 'fileNames' in nextResponse ) {
171- const { fileNames, fileEdits } = nextResponse
172- const selectedFilePath = await pickFileWithQuickPick ( fileNames )
173- if ( ! selectedFilePath ) return
174- const document = await vscode . workspace . openTextDocument ( vscode . Uri . file ( selectedFilePath ) )
175- // const outline = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri)
176-
177- const currentEditorPath = getTsLikePath ( vscode . window . activeTextEditor ! . document . uri )
178- const currentFileEdits = [ ...fileEdits . find ( fileEdit => fileEdit . fileName === currentEditorPath ) ! . textChanges ]
179- const textChangeIndexToPatch = currentFileEdits . findIndex ( currentFileEdit => currentFileEdit . newText . trim ( ) )
180- const { newText : updateImportText } = currentFileEdits [ textChangeIndexToPatch ] !
181- // TODO-mid use native path resolver (ext, index, alias)
182- let newRelativePath = relative ( join ( currentEditorPath , '..' ) , selectedFilePath )
183- if ( ! newRelativePath . startsWith ( './' ) && ! newRelativePath . startsWith ( '../' ) ) newRelativePath = `./${ newRelativePath } `
184- currentFileEdits [ textChangeIndexToPatch ] ! . newText = updateImportText . replace ( / ( [ ' " ] ) .+ ( [ ' " ] ) / , ( _m , g1 ) => `${ g1 } ${ newRelativePath } ${ g1 } ` )
185- mainChanges = currentFileEdits
186- const newFileText = fileEdits . find ( fileEdit => fileEdit . isNewFile ) ! . textChanges [ 0 ] ! . newText
187- const [ importLines , otherLines ] = partition ( newFileText . split ( '\n' ) , line => line . startsWith ( 'import ' ) )
188- const startPos = new vscode . Position ( 0 , 0 )
189- const newFileNodes = await sendCommand < RequestResponseTypes [ 'filterBySyntaxKind' ] > ( 'filterBySyntaxKind' , {
190- position : startPos ,
191- document,
192- } )
193- const lastImportDeclaration = newFileNodes ?. nodesByKind . ImportDeclaration ?. at ( - 1 )
194- const lastImportEnd = lastImportDeclaration ? tsRangeToVscode ( document , lastImportDeclaration . range ) . end : startPos
195- edit . set ( vscode . Uri . file ( selectedFilePath ) , [
196- {
197- range : new vscode . Range ( startPos , startPos ) ,
198- newText : [ ...importLines , '\n' ] . join ( '\n' ) ,
199- } ,
200- {
201- range : new vscode . Range ( lastImportEnd , lastImportEnd ) ,
202- newText : [ '\n' , ...otherLines ] . join ( '\n' ) ,
203- } ,
204- ] )
205- }
146+ const mainChanges = 'edits' in nextResponse && nextResponse . edits
206147
207148 if ( ! mainChanges ) return
208149 edit . set ( editor . document . uri , tsTextChangesToVscodeTextEdits ( editor . document , mainChanges ) )
0 commit comments