11import fs from 'fs'
22import { join } from 'path/posix'
3- import ts_module from 'typescript/lib/tsserverlibrary'
3+ import tslib , { SymbolDisplayPartKind , TypeFlags , TypeFormatFlags } from 'typescript/lib/tsserverlibrary'
44
5- export = function ( { typescript } : { typescript : typeof ts_module } ) {
5+ export = function ( { typescript } : { typescript : typeof tslib } ) {
66 return {
77 create ( info : ts . server . PluginCreateInfo ) {
88 // Set up decorator object
@@ -14,6 +14,7 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
1414 proxy [ k ] = ( ...args : Array < Record < string , unknown > > ) => x . apply ( info . languageService , args )
1515 }
1616
17+ let prevCompletions
1718 proxy . getCompletionsAtPosition = ( fileName , position , options ) => {
1819 const prior = info . languageService . getCompletionsAtPosition ( fileName , position , options )
1920 if ( ! prior ) return
@@ -43,15 +44,63 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
4344 }
4445 // Feature: Force Suggestion Sorting
4546 prior . entries = prior . entries . map ( ( entry , index ) => ( { ...entry , sortText : `${ entry . sortText ?? '' } ${ index } ` } ) )
47+ // console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
4648 // console.timeEnd('slow-down')
4749 return prior
4850 }
4951
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
5298
99+ const prior = info . languageService . getApplicableRefactors ( fileName , positionOrRange , preferences )
100+
101+ return [ ]
53102 // Feature: Remove useless code actions
54- return prior . filter ( ( { fixName } ) => ! [ 'fixMissingFunctionDeclaration' ] . includes ( fixName ) )
103+ // return prior.filter(({ fixName }) => !['fixMissingFunctionDeclaration'].includes(fixName))
55104 }
56105
57106 return proxy
@@ -61,3 +110,17 @@ export = function ({ typescript }: { typescript: typeof ts_module }) {
61110 } ,
62111 }
63112}
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