@@ -8,10 +8,10 @@ import { RequestOptionsTypes, RequestResponseTypes } from '../typescript/src/ipc
88import { sendCommand } from './sendCommand'
99
1010export default ( tsApi : { onCompletionAccepted } ) => {
11+ let inFlightMethodSnippetOperation : undefined | AbortController
1112 let justAcceptedReturnKeywordSuggestion = false
1213 let onCompletionAcceptedOverride : ( ( item : any ) => void ) | undefined
1314
14- // eslint-disable-next-line complexity
1515 tsApi . onCompletionAccepted ( async ( item : vscode . CompletionItem & { document : vscode . TextDocument } ) => {
1616 if ( onCompletionAcceptedOverride ) {
1717 onCompletionAcceptedOverride ( item )
@@ -41,55 +41,24 @@ export default (tsApi: { onCompletionAccepted }) => {
4141 const startPos = editor . selection . start
4242 const nextSymbol = editor . document . getText ( new vscode . Range ( startPos , startPos . translate ( 0 , 1 ) ) )
4343 if ( ! [ '(' , '.' ] . includes ( nextSymbol ) ) {
44- const insertMode = getExtensionSetting ( 'methodSnippets.insertText' )
45- const skipMode = getExtensionSetting ( 'methodSnippets.skip' )
46- const data : RequestResponseTypes [ 'getSignatureInfo' ] | undefined = await sendCommand ( 'getSignatureInfo' , {
47- inputOptions : {
48- includeInitializer : insertMode === 'always-declaration' ,
49- } satisfies RequestOptionsTypes [ 'getSignatureInfo' ] ,
50- } )
51- if ( data ) {
52- const parameters = data . parameters . filter ( ( { insertText, isOptional } ) => {
53- const isRest = insertText . startsWith ( '...' )
54- if ( skipMode === 'only-rest' && isRest ) return false
55- if ( skipMode === 'optional-and-rest' && isOptional ) return false
56- return true
57- } )
58-
44+ const controller = new AbortController ( )
45+ inFlightMethodSnippetOperation = controller
46+ const params : RequestResponseTypes [ 'getFullMethodSnippet' ] | undefined = await sendCommand ( 'getFullMethodSnippet' )
47+ if ( ! controller . signal . aborted && params ) {
5948 const snippet = new vscode . SnippetString ( '' )
6049 snippet . appendText ( '(' )
6150 // todo maybe when have skipped, add a way to leave trailing , (previous behavior)
62- for ( const [ i , { insertText, name } ] of parameters . entries ( ) ) {
63- const isRest = insertText . startsWith ( '...' )
64- let text : string
65- // eslint-disable-next-line default-case
66- switch ( insertMode ) {
67- case 'always-name' :
68- text = name
69- break
70- case 'prefer-name' :
71- // prefer name, but only if identifier and not binding pattern & rest
72- text = oneOf ( insertText [ 0 ] , '[' , '{' ) ? insertText : isRest ? insertText : name
73- break
74- case 'always-declaration' :
75- text = insertText
76- break
77- }
78-
79- snippet . appendPlaceholder ( text )
80- if ( i !== parameters . length - 1 ) snippet . appendText ( ', ' )
51+ for ( const [ i , param ] of params . entries ( ) ) {
52+ snippet . appendPlaceholder ( param )
53+ if ( i !== params . length - 1 ) snippet . appendText ( ', ' )
8154 }
8255
83- const allFiltered = data . parameters . length > parameters . length
84- // TODO when many, but at least one not empty
85- if ( allFiltered || data . hasManySignatures ) snippet . appendTabstop ( )
86-
8756 snippet . appendText ( ')' )
8857 void editor . insertSnippet ( snippet , undefined , {
8958 undoStopAfter : false ,
9059 undoStopBefore : false ,
9160 } )
92- if ( vscode . workspace . getConfiguration ( 'editor.parameterHints' ) . get ( 'enabled' ) ) {
61+ if ( vscode . workspace . getConfiguration ( 'editor.parameterHints' ) . get ( 'enabled' ) && params . length > 0 ) {
9362 void vscode . commands . executeCommand ( 'editor.action.triggerParameterHints' )
9463 }
9564 }
@@ -120,40 +89,42 @@ export default (tsApi: { onCompletionAccepted }) => {
12089 )
12190 } )
12291
123- conditionallyRegister (
124- 'suggestions.keywordsInsertText' ,
125- ( ) =>
126- vscode . workspace . onDidChangeTextDocument ( ( { document, contentChanges, reason } ) => {
127- if ( ! justAcceptedReturnKeywordSuggestion ) return
128- if ( document !== vscode . window . activeTextEditor ?. document ) return
129- try {
130- if ( oneOf ( reason , vscode . TextDocumentChangeReason . Redo , vscode . TextDocumentChangeReason . Undo ) ) {
131- return
132- }
92+ vscode . workspace . onDidChangeTextDocument ( ( { document, contentChanges, reason } ) => {
93+ if ( document !== vscode . window . activeTextEditor ?. document ) return
94+ // do the same for position change?
95+ if ( inFlightMethodSnippetOperation ) {
96+ inFlightMethodSnippetOperation . abort ( )
97+ inFlightMethodSnippetOperation = undefined
98+ }
13399
134- const char = contentChanges [ 0 ] ?. text
135- if ( char ?. length !== 1 || contentChanges . some ( ( { text } ) => text !== char ) ) {
136- return
137- }
100+ if ( ! justAcceptedReturnKeywordSuggestion ) return
138101
139- if ( char === ';' || char === '\n' ) {
140- void vscode . window . activeTextEditor . edit (
141- builder => {
142- for ( const { range } of contentChanges ) {
143- const pos = range . start
144- builder . delete ( expandPosition ( document , pos , - 1 ) )
145- }
146- } ,
147- {
148- undoStopAfter : false ,
149- undoStopBefore : false ,
150- } ,
151- )
152- }
153- } finally {
154- justAcceptedReturnKeywordSuggestion = false
155- }
156- } ) ,
157- ( ) => getExtensionSetting ( 'suggestions.keywordsInsertText' ) !== 'none' ,
158- )
102+ try {
103+ if ( oneOf ( reason , vscode . TextDocumentChangeReason . Redo , vscode . TextDocumentChangeReason . Undo ) ) {
104+ return
105+ }
106+
107+ const char = contentChanges [ 0 ] ?. text
108+ if ( char ?. length !== 1 || contentChanges . some ( ( { text } ) => text !== char ) ) {
109+ return
110+ }
111+
112+ if ( char === ';' || char === '\n' ) {
113+ void vscode . window . activeTextEditor . edit (
114+ builder => {
115+ for ( const { range } of contentChanges ) {
116+ const pos = range . start
117+ builder . delete ( expandPosition ( document , pos , - 1 ) )
118+ }
119+ } ,
120+ {
121+ undoStopAfter : false ,
122+ undoStopBefore : false ,
123+ } ,
124+ )
125+ }
126+ } finally {
127+ justAcceptedReturnKeywordSuggestion = false
128+ }
129+ } )
159130}
0 commit comments