1
1
import * as vscode from 'vscode'
2
- import { relative , join } from 'path-browserify'
3
2
import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'
4
- import { partition } from 'lodash'
5
3
import { registerExtensionCommand , showQuickPick , getExtensionSetting , getExtensionCommandId } from 'vscode-framework'
6
4
import { compact } from '@zardoy/utils'
7
5
import { RequestResponseTypes , RequestOptionsTypes } from '../typescript/src/ipcTypes'
8
6
import { 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'
17
8
18
9
// extended and interactive code actions
19
10
export default ( ) => {
@@ -53,7 +44,7 @@ export default () => {
53
44
if ( context . triggerKind !== vscode . CodeActionTriggerKind . Invoke ) return
54
45
const result = await getPossibleTwoStepRefactorings ( range )
55
46
if ( ! result ) return
56
- const { turnArrayIntoObject, moveToExistingFile , extendedCodeActions } = result
47
+ const { turnArrayIntoObject, extendedCodeActions } = result
57
48
const codeActions : vscode . CodeAction [ ] = [ ]
58
49
const getCommand = ( arg ) : vscode . Command | undefined => ( {
59
50
title : '' ,
@@ -69,14 +60,6 @@ export default () => {
69
60
} )
70
61
}
71
62
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
-
80
63
codeActions . push (
81
64
...compact (
82
65
extendedCodeActions . map ( ( { title, kind, codes } ) : ExtendedCodeAction | undefined => {
@@ -131,7 +114,7 @@ export default () => {
131
114
registerExtensionCommand ( 'applyRefactor' as any , async ( _ , arg ?: RequestResponseTypes [ 'getTwoStepCodeActions' ] ) => {
132
115
if ( ! arg ) return
133
116
let sendNextData : RequestOptionsTypes [ 'twoStepCodeActionSecondStep' ] [ 'data' ] | undefined
134
- const { turnArrayIntoObject, moveToExistingFile } = arg
117
+ const { turnArrayIntoObject } = arg
135
118
if ( turnArrayIntoObject ) {
136
119
const { keysCount, totalCount, totalObjectCount } = turnArrayIntoObject
137
120
const selectedKey = await showQuickPick (
@@ -155,54 +138,12 @@ export default () => {
155
138
}
156
139
}
157
140
158
- if ( moveToExistingFile ) {
159
- sendNextData = {
160
- name : 'moveToExistingFile' ,
161
- }
162
- }
163
-
164
141
if ( ! sendNextData ) return
165
142
const editor = vscode . window . activeTextEditor !
166
143
const nextResponse = await getSecondStepRefactoringData ( editor . selection , sendNextData )
167
144
if ( ! nextResponse ) throw new Error ( 'No code action data. Try debug.' )
168
145
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
206
147
207
148
if ( ! mainChanges ) return
208
149
edit . set ( editor . document . uri , tsTextChangesToVscodeTextEdits ( editor . document , mainChanges ) )
0 commit comments