1
1
import fs from 'fs'
2
2
import { join } from 'path/posix'
3
- import ts_module from 'typescript/lib/tsserverlibrary'
3
+ import tslib , { SymbolDisplayPartKind , TypeFlags , TypeFormatFlags } from 'typescript/lib/tsserverlibrary'
4
4
5
- export = function ( { typescript } : { typescript : typeof ts_module } ) {
5
+ export = function ( { typescript } : { typescript : typeof tslib } ) {
6
6
return {
7
7
create ( info : ts . server . PluginCreateInfo ) {
8
8
// Set up decorator object
@@ -14,6 +14,7 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
14
14
proxy [ k ] = ( ...args : Array < Record < string , unknown > > ) => x . apply ( info . languageService , args )
15
15
}
16
16
17
+ let prevCompletions
17
18
proxy . getCompletionsAtPosition = ( fileName , position , options ) => {
18
19
const prior = info . languageService . getCompletionsAtPosition ( fileName , position , options )
19
20
if ( ! prior ) return
@@ -43,15 +44,63 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
43
44
}
44
45
// Feature: Force Suggestion Sorting
45
46
prior . entries = prior . entries . map ( ( entry , index ) => ( { ...entry , sortText : `${ entry . sortText ?? '' } ${ index } ` } ) )
47
+ // console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
46
48
// console.timeEnd('slow-down')
47
49
return prior
48
50
}
49
51
50
- proxy . getCodeFixesAtPosition = ( fileName , start , end , errorCodes , formatOptions , preferences ) => {
51
- const prior = info . languageService . getCodeFixesAtPosition ( fileName , start , end , errorCodes , formatOptions , preferences )
52
+ proxy . getCompletionEntryDetails = ( fileName , position , entryName , formatOptions , source , preferences , data ) => {
53
+ console . log ( 'source' , source )
54
+ const prior = info . languageService . getCompletionEntryDetails ( fileName , position , entryName , formatOptions , source , preferences , data )
55
+ if ( ! prior ) return
56
+ prior . codeActions = [ { description : '' , changes : [ { fileName, textChanges : [ { span : { start : position , length : 0 } , newText : '()' } ] } ] } ]
57
+ // formatOptions
58
+ // info.languageService.getDefinitionAtPosition(fileName, position)
59
+ return prior
60
+ }
61
+
62
+ proxy . getApplicableRefactors = ( fileName , positionOrRange , preferences ) => {
63
+ if ( typeof positionOrRange !== 'number' ) {
64
+ positionOrRange = positionOrRange . pos
65
+ }
66
+ const { textSpan } = proxy . getSmartSelectionRange ( fileName , positionOrRange )
67
+ console . log ( 'textSpan.start' , textSpan . start , textSpan . length )
68
+ const program = info . languageService . getProgram ( )
69
+ const sourceFile = program ?. getSourceFile ( fileName )
70
+
71
+ if ( ! program || ! sourceFile ) return [ ]
72
+ const originalSourceText = sourceFile . text
73
+ // sourceFile.update('test', { span: textSpan, newLength: sourceFile.text.length + 2 })
74
+ // sourceFile.text = patchText(sourceFile.text, textSpan.start, textSpan.start + textSpan.length, 'test')
75
+ // console.log('sourceFile.text', sourceFile.text)
76
+ const node = findChildContainingPosition ( sourceFile , positionOrRange )
77
+ if ( ! node ) {
78
+ console . log ( 'no node' )
79
+ return [ ]
80
+ }
81
+ // console.log(
82
+ // 'special 1',
83
+ // typescript.isJsxExpression(node),
84
+ // typescript.isJsxElement(node),
85
+ // typescript.isJsxText(node),
86
+ // typescript.isJsxExpression(node.parent),
87
+ // typescript.isJsxElement(node.parent),
88
+ // typescript.isJsxOpeningElement(node.parent),
89
+ // )
90
+ const typeChecker = program . getTypeChecker ( )
91
+ const type = typeChecker . getTypeAtLocation ( node )
92
+ const parentType = typeChecker . getTypeAtLocation ( node . parent )
93
+ // console.log(
94
+ // 'extracted.getCallSignatures()',
95
+ // type.getCallSignatures().map(item => item.getParameters().map(item => item.name)),
96
+ // )
97
+ sourceFile . text = originalSourceText
52
98
99
+ const prior = info . languageService . getApplicableRefactors ( fileName , positionOrRange , preferences )
100
+
101
+ return [ ]
53
102
// Feature: Remove useless code actions
54
- return prior . filter ( ( { fixName } ) => ! [ 'fixMissingFunctionDeclaration' ] . includes ( fixName ) )
103
+ // return prior.filter(({ fixName }) => !['fixMissingFunctionDeclaration'].includes(fixName))
55
104
}
56
105
57
106
return proxy
@@ -61,3 +110,17 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
61
110
} ,
62
111
}
63
112
}
113
+
114
+ const patchText = ( input : string , start : number , end : number , newText : string ) => {
115
+ return input . slice ( 0 , start ) + newText + input . slice ( end )
116
+ }
117
+
118
+ function findChildContainingPosition ( sourceFile : tslib . SourceFile , position : number ) : tslib . Node | undefined {
119
+ function find ( node : ts . Node ) : ts . Node | undefined {
120
+ if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) {
121
+ return tslib . forEachChild ( node , find ) || node
122
+ }
123
+ return
124
+ }
125
+ return find ( sourceFile )
126
+ }
0 commit comments