@@ -2,10 +2,11 @@ import get from 'lodash.get'
2
2
import type tslib from 'typescript/lib/tsserverlibrary'
3
3
import * as emmet from '@vscode/emmet-helper'
4
4
5
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
6
//@ts -ignore
6
7
import type { Configuration } from '../../src/configurationType'
7
8
8
- export = function ( { typescript } : { typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) } ) {
9
+ export = function ( { typescript } : { typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) } ) {
9
10
const ts = typescript
10
11
let _configuration : Configuration
11
12
const c = < T extends keyof Configuration > ( key : T ) : Configuration [ T ] => get ( _configuration , key )
@@ -42,11 +43,11 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
42
43
}
43
44
44
45
let prevCompletionsMap : Record < string , { originalName : string } >
46
+ // eslint-disable-next-line complexity
45
47
proxy . getCompletionsAtPosition = ( fileName , position , options ) => {
46
48
prevCompletionsMap = { }
47
- if ( ! _configuration ) {
48
- console . log ( 'no received configuration!' )
49
- }
49
+ if ( ! _configuration ) console . log ( 'no received configuration!' )
50
+
50
51
// console.time('slow-down')
51
52
const program = info . languageService . getProgram ( )
52
53
const sourceFile = program ?. getSourceFile ( fileName )
@@ -79,13 +80,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
79
80
if ( lastPart . startsWith ( '<' ) ) lastPart = lastPart . slice ( 1 )
80
81
const isStartingWithUpperCase = ( str : string ) => str [ 0 ] === str [ 0 ] ?. toUpperCase ( )
81
82
// check if starts with lowercase
82
- if ( isStartingWithUpperCase ( lastPart ) ) {
83
+ if ( isStartingWithUpperCase ( lastPart ) )
83
84
// TODO! compare with suggestions from lib.dom
84
85
prior . entries = prior . entries . filter (
85
86
entry => isStartingWithUpperCase ( entry . name ) && ! [ typescript . ScriptElementKind . enumElement ] . includes ( entry . kind ) ,
86
87
)
87
- }
88
88
}
89
+
89
90
if (
90
91
c ( 'jsxEmmet.type' ) !== 'disabled' &&
91
92
( emmetSyntaxKinds . includes ( node . kind ) ||
@@ -121,7 +122,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
121
122
'html' ,
122
123
{ } ,
123
124
) ?? { items : [ ] }
124
- for ( const completion of emmetCompletions . items ) {
125
+ for ( const completion of emmetCompletions . items )
125
126
prior . entries . push ( {
126
127
kind : typescript . ScriptElementKind . label ,
127
128
name : completion . label . slice ( 1 ) ,
@@ -132,7 +133,6 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
132
133
sourceDisplay : completion . detail !== undefined ? [ { kind : 'text' , text : completion . detail } ] : undefined ,
133
134
// replacementSpan: { start: position - 5, length: 5 },
134
135
} )
135
- }
136
136
} else {
137
137
const tags = c ( 'jsxPseudoEmmet.tags' )
138
138
for ( let [ tag , value ] of Object . entries ( tags ) ) {
@@ -149,9 +149,9 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
149
149
}
150
150
}
151
151
}
152
- if ( ! prior ) {
153
- return
154
- }
152
+
153
+ if ( ! prior ) return
154
+
155
155
// const fullText = scriptSnapshot.getText(0, scriptSnapshot.getLength())
156
156
// const matchImport = /(import (.*)from )['"].*['"]/.exec(fullText.split('\n')[line]!)?.[1]
157
157
// if (matchImport && character <= `import${matchImport}`.length) {
@@ -160,14 +160,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
160
160
// }
161
161
// prior.isGlobalCompletion
162
162
// prior.entries[0]
163
- const entryNames = prior . entries . map ( ( { name } ) => name )
163
+ const entryNames = new Set ( prior . entries . map ( ( { name } ) => name ) )
164
164
if ( c ( 'removeUselessFunctionProps.enable' ) ) prior . entries = prior . entries . filter ( e => ! [ 'Symbol' , 'caller' , 'prototype' ] . includes ( e . name ) )
165
- if ( [ 'bind' , 'call' , 'caller' ] . every ( name => entryNames . includes ( name ) ) ) {
166
- if ( c ( 'highlightNonFunctionMethods.enable' ) ) {
167
- const standardProps = [ 'Symbol' , 'apply' , 'arguments' , 'bind' , 'call' , 'caller' , 'length' , 'name' , 'prototype' , 'toString' ]
165
+ if ( [ 'bind' , 'call' , 'caller' ] . every ( name => entryNames . has ( name ) ) && c ( 'highlightNonFunctionMethods.enable' ) ) {
166
+ const standardProps = new Set ( [ 'Symbol' , 'apply' , 'arguments' , 'bind' , 'call' , 'caller' , 'length' , 'name' , 'prototype' , 'toString' ] )
168
167
// TODO lift up!
169
168
prior . entries = prior . entries . map ( entry => {
170
- if ( ! standardProps . includes ( entry . name ) ) {
169
+ if ( ! standardProps . has ( entry . name ) && entry . kind !== ts . ScriptElementKind . warning ) {
171
170
const newName = `☆${ entry . name } `
172
171
prevCompletionsMap [ newName ] = {
173
172
originalName : entry . name ,
@@ -178,28 +177,30 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
178
177
name : newName ,
179
178
}
180
179
}
180
+
181
181
return entry
182
182
} )
183
183
}
184
- }
184
+
185
185
if ( c ( 'patchToString.enable' ) ) {
186
186
// const indexToPatch = arrayMoveItemToFrom(
187
187
// prior.entries,
188
188
// ({ name }) => name === 'toExponential',
189
189
// ({ name }) => name === 'toString',
190
190
// )
191
- let indexToPatch = prior . entries . findIndex ( ( { name } ) => name === 'toString' )
191
+ const indexToPatch = prior . entries . findIndex ( ( { name } ) => name === 'toString' )
192
192
if ( indexToPatch !== - 1 ) {
193
193
prior . entries [ indexToPatch ] ! . insertText = `${ prior . entries [ indexToPatch ] ! . insertText ?? prior . entries [ indexToPatch ] ! . name } ()`
194
194
prior . entries [ indexToPatch ] ! . kind = typescript . ScriptElementKind . constElement
195
195
// prior.entries[indexToPatch]!.isSnippet = true
196
196
}
197
197
}
198
+
198
199
const banAutoImportPackages = c ( 'suggestions.banAutoImportPackages' )
199
200
if ( banAutoImportPackages ?. length )
200
201
prior . entries = prior . entries . filter ( entry => {
201
202
if ( ! entry . sourceDisplay ) return true
202
- const text = entry . sourceDisplay . map ( item => item . text ) . join ( )
203
+ const text = entry . sourceDisplay . map ( item => item . text ) . join ( '' )
203
204
if ( text . startsWith ( '.' ) ) return true
204
205
// TODO change to startsWith?
205
206
return ! banAutoImportPackages . includes ( text )
@@ -214,20 +215,20 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
214
215
} )
215
216
if ( ! suggestion ) continue
216
217
217
- if ( rule . delete ) {
218
+ if ( rule . delete )
218
219
prior . entries . splice ( foundIndex ! , 1 )
219
- }
220
220
221
- if ( rule . duplicateOriginal ) {
221
+
222
+ if ( rule . duplicateOriginal )
222
223
prior . entries . splice ( rule . duplicateOriginal === 'above' ? foundIndex ! : foundIndex ! + 1 , 0 , { ...suggestion } )
223
- }
224
224
225
225
Object . assign ( suggestion , rule . patch ?? { } )
226
226
if ( rule . patch ?. insertText ) suggestion . isSnippet = true
227
227
}
228
- if ( c ( 'correctSorting.enable' ) ) {
228
+
229
+ if ( c ( 'correctSorting.enable' ) )
229
230
prior . entries = prior . entries . map ( ( entry , index ) => ( { ...entry , sortText : `${ entry . sortText ?? '' } ${ index } ` } ) )
230
- }
231
+
231
232
// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
232
233
// console.timeEnd('slow-down')
233
234
return prior
@@ -265,6 +266,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
265
266
266
267
return prior
267
268
}
269
+
268
270
proxy . getCodeFixesAtPosition = ( fileName , start , end , errorCodes , formatOptions , preferences ) => {
269
271
let prior = info . languageService . getCodeFixesAtPosition ( fileName , start , end , errorCodes , formatOptions , preferences )
270
272
@@ -278,11 +280,12 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
278
280
279
281
return prior
280
282
}
281
- // @ts -expect-error
283
+
284
+ // @ts -expect-error some experiments
282
285
proxy . ignored = ( fileName : string , positionOrRange : number , preferences : any ) => {
283
- if ( typeof positionOrRange !== 'number' ) {
286
+ if ( typeof positionOrRange !== 'number' )
284
287
positionOrRange = positionOrRange
285
- }
288
+
286
289
// ts.createSourceFile(fileName, sourceText, languageVersion)
287
290
const { textSpan } = proxy . getSmartSelectionRange ( fileName , positionOrRange )
288
291
console . log ( 'textSpan.start' , textSpan . start , textSpan . length )
@@ -298,6 +301,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
298
301
console . log ( 'no node' )
299
302
return [ ]
300
303
}
304
+
301
305
// console.log(
302
306
// 'special 1',
303
307
// typescript.isJsxExpression(node),
@@ -339,19 +343,17 @@ const arrayMoveItemToFrom = <T>(array: T[], originalItem: ArrayPredicate<T>, ite
339
343
return originalItemIndex
340
344
}
341
345
342
- const patchText = ( input : string , start : number , end : number , newText : string ) => {
343
- return input . slice ( 0 , start ) + newText + input . slice ( end )
344
- }
346
+ const patchText = ( input : string , start : number , end : number , newText : string ) => input . slice ( 0 , start ) + newText + input . slice ( end )
345
347
346
348
function findChildContainingPosition (
347
349
typescript : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
348
350
sourceFile : tslib . SourceFile ,
349
351
position : number ,
350
352
) : tslib . Node | undefined {
351
353
function find ( node : ts . Node ) : ts . Node | undefined {
352
- if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) {
354
+ if ( position >= node . getStart ( ) && position < node . getEnd ( ) )
353
355
return typescript . forEachChild ( node , find ) || node
354
- }
356
+
355
357
return
356
358
}
357
359
return find ( sourceFile )
0 commit comments