@@ -13,7 +13,7 @@ export default (tsApi: { onCompletionAccepted }) => {
1313 let onCompletionAcceptedOverride : ( ( item : any ) => void ) | undefined
1414
1515 // eslint-disable-next-line complexity
16- tsApi . onCompletionAccepted ( async ( item : vscode . CompletionItem & { document : vscode . TextDocument } ) => {
16+ tsApi . onCompletionAccepted ( async ( item : vscode . CompletionItem & { document : vscode . TextDocument ; tsEntry } ) => {
1717 if ( onCompletionAcceptedOverride ) {
1818 onCompletionAcceptedOverride ( item )
1919 return
@@ -36,57 +36,56 @@ export default (tsApi: { onCompletionAccepted }) => {
3636 return
3737 }
3838
39- const enableMethodSnippets = vscode . workspace . getConfiguration ( process . env . IDS_PREFIX , item . document ) . get ( 'enableMethodSnippets' )
40-
41- if ( enableMethodSnippets && /* snippet by vscode or by us to ignore pos */ typeof insertText !== 'object' ) {
39+ if ( /* snippet is by vscode or by us to ignore pos */ typeof insertText !== 'object' ) {
4240 const editor = getActiveRegularEditor ( ) !
43- const startPos = editor . selection . start
44- const nextSymbol = editor . document . getText ( new vscode . Range ( startPos , startPos . translate ( 0 , 1 ) ) )
45- if ( ! [ '(' , '.' , '`' ] . includes ( nextSymbol ) ) {
46- // all-in handling
47- const controller = new AbortController ( )
48- inFlightMethodSnippetOperation = controller
49- const params : RequestResponseTypes [ 'getFullMethodSnippet' ] | undefined = await sendCommand ( 'getFullMethodSnippet' , {
50- inputOptions : {
51- acceptAmbiguous : lastAcceptedAmbiguousMethodSnippetSuggestion === suggestionName ,
52- } satisfies RequestOptionsTypes [ 'getFullMethodSnippet' ] ,
41+ if ( item . tsEntry . source ) {
42+ await new Promise < void > ( resolve => {
43+ vscode . workspace . onDidChangeTextDocument ( ( { document } ) => {
44+ if ( editor . document !== document ) return
45+ resolve ( )
46+ } )
5347 } )
48+ await new Promise ( resolve => {
49+ setTimeout ( resolve , 0 )
50+ } )
51+ }
5452
55- if ( controller . signal . aborted ) return
56- if ( params === 'ambiguous' ) {
57- lastAcceptedAmbiguousMethodSnippetSuggestion = suggestionName
58- return
59- }
60-
61- if ( ! params ) {
62- return
63- }
53+ const documentation = typeof item . documentation === 'object' ? item . documentation . value : item . documentation
54+ const dataMarker = '<!--tep '
55+ if ( ! documentation ?. startsWith ( dataMarker ) ) return
56+ const parsed = JSON . parse ( documentation . slice ( dataMarker . length , documentation . indexOf ( 'e-->' ) ) )
57+ const { methodSnippet : params , isAmbiguous } = parsed
58+ if ( ! params ) return
6459
65- const replaceArguments = getExtensionSetting ( 'methodSnippets.replaceArguments' )
66-
67- const snippet = new vscode . SnippetString ( '' )
68- snippet . appendText ( '(' )
69- // todo maybe when have optional (skipped), add a way to leave trailing , with tabstop (previous behavior)
70- for ( const [ i , param ] of params . entries ( ) ) {
71- const replacer = replaceArguments [ param . replace ( / \? $ / , '' ) ]
72- if ( replacer === null ) continue
73- if ( replacer ) {
74- useReplacer ( snippet , replacer )
75- } else {
76- snippet . appendPlaceholder ( param )
77- }
60+ if ( isAmbiguous && lastAcceptedAmbiguousMethodSnippetSuggestion !== suggestionName ) {
61+ lastAcceptedAmbiguousMethodSnippetSuggestion = suggestionName
62+ return
63+ }
7864
79- if ( i !== params . length - 1 ) snippet . appendText ( ', ' )
65+ const replaceArguments = getExtensionSetting ( 'methodSnippets.replaceArguments' )
66+
67+ const snippet = new vscode . SnippetString ( '' )
68+ snippet . appendText ( '(' )
69+ // todo maybe when have optional (skipped), add a way to leave trailing , with tabstop (previous behavior)
70+ for ( const [ i , param ] of params . entries ( ) ) {
71+ const replacer = replaceArguments [ param . replace ( / \? $ / , '' ) ]
72+ if ( replacer === null ) continue
73+ if ( replacer ) {
74+ useReplacer ( snippet , replacer )
75+ } else {
76+ snippet . appendPlaceholder ( param )
8077 }
8178
82- snippet . appendText ( ')' )
83- void editor . insertSnippet ( snippet , undefined , {
84- undoStopAfter : false ,
85- undoStopBefore : false ,
86- } )
87- if ( vscode . workspace . getConfiguration ( 'editor.parameterHints' ) . get ( 'enabled' ) && params . length > 0 ) {
88- void vscode . commands . executeCommand ( 'editor.action.triggerParameterHints' )
89- }
79+ if ( i !== params . length - 1 ) snippet . appendText ( ', ' )
80+ }
81+
82+ snippet . appendText ( ')' )
83+ void editor . insertSnippet ( snippet , undefined , {
84+ undoStopAfter : false ,
85+ undoStopBefore : false ,
86+ } )
87+ if ( vscode . workspace . getConfiguration ( 'editor.parameterHints' ) . get ( 'enabled' ) && params . length > 0 ) {
88+ void vscode . commands . executeCommand ( 'editor.action.triggerParameterHints' )
9089 }
9190 }
9291 } )
0 commit comments