@@ -8,6 +8,8 @@ import { GetConfig } from './types'
88import { getCompletionsAtPosition , PrevCompletionMap } from './completionsAtPosition'
99import { oneOf } from '@zardoy/utils'
1010import { isGoodPositionMethodCompletion } from './isGoodPositionMethodCompletion'
11+ import { inspect } from 'util'
12+ import { getIndentFromPos } from './utils'
1113
1214const thisPluginMarker = Symbol ( '__essentialPluginsMarker__' )
1315
@@ -132,12 +134,25 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
132134
133135 proxy . getCodeFixesAtPosition = ( fileName , start , end , errorCodes , formatOptions , preferences ) => {
134136 let prior = info . languageService . getCodeFixesAtPosition ( fileName , start , end , errorCodes , formatOptions , preferences )
137+ // fix builtin codefixes/refactorings
138+ prior . forEach ( fix => {
139+ if ( fix . fixName === 'fixConvertConstToLet' ) {
140+ const { start, length } = fix . changes [ 0 ] ! . textChanges [ 0 ] ! . span
141+ const fixedLength = 'const' . length as 5
142+ fix . changes [ 0 ] ! . textChanges [ 0 ] ! . span . start = start + length - fixedLength
143+ fix . changes [ 0 ] ! . textChanges [ 0 ] ! . span . length = fixedLength
144+ }
145+ return fix
146+ } )
135147 // const scriptSnapshot = info.project.getScriptSnapshot(fileName)
136148 const diagnostics = proxy . getSemanticDiagnostics ( fileName )
137149
138150 // https://github.com/Microsoft/TypeScript/blob/v4.5.5/src/compiler/diagnosticMessages.json#L458
139151 const appliableErrorCode = [ 1156 , 1157 ] . find ( code => errorCodes . includes ( code ) )
140152 if ( appliableErrorCode ) {
153+ const program = info . languageService . getProgram ( )
154+ const sourceFile = program ! . getSourceFile ( fileName ) !
155+ const startIndent = getIndentFromPos ( typescript , sourceFile , end )
141156 const diagnostic = diagnostics . find ( ( { code } ) => code === appliableErrorCode ) !
142157 prior = [
143158 ...prior ,
@@ -148,8 +163,8 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
148163 {
149164 fileName,
150165 textChanges : [
151- { span : { start : diagnostic . start ! , length : 0 } , newText : '{' } ,
152- { span : { start : diagnostic . start ! + diagnostic . length ! , length : 0 } , newText : '}' } ,
166+ { span : { start : diagnostic . start ! , length : 0 } , newText : `{\n ${ startIndent } \t` } ,
167+ { span : { start : diagnostic . start ! + diagnostic . length ! , length : 0 } , newText : `\n ${ startIndent } }` } ,
153168 ] ,
154169 } ,
155170 ] ,
@@ -197,7 +212,10 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
197212 let prior = info . languageService . findReferences ( fileName , position )
198213 if ( ! prior ) return
199214 if ( c ( 'removeDefinitionFromReferences' ) ) {
200- prior = prior . map ( ( { references, ...other } ) => ( { ...other , references : references . filter ( ( { isDefinition } ) => ! isDefinition ) } ) )
215+ prior = prior . map ( ( { references, ...other } ) => ( {
216+ ...other ,
217+ references : references . filter ( ( { isDefinition } ) => ! isDefinition ) ,
218+ } ) )
201219 }
202220 return prior
203221 }
0 commit comments