@@ -269,7 +269,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
269269 if ( ! context . only ) {
270270 return actions ;
271271 }
272- return this . appendInvalidActions ( actions ) ;
272+ return this . pruneInvalidActions ( this . appendInvalidActions ( actions ) , context . only , /* numberOfInvalid = */ 5 ) ;
273273 }
274274
275275 private toTsTriggerReason ( context : vscode . CodeActionContext ) : Experimental . RefactorTriggerReason | undefined {
@@ -369,6 +369,11 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
369369 }
370370
371371 private appendInvalidActions ( actions : vscode . CodeAction [ ] ) : vscode . CodeAction [ ] {
372+ if ( this . client . apiVersion . gte ( API . v400 ) ) {
373+ // Invalid actions come from TS server instead
374+ return actions ;
375+ }
376+
372377 if ( ! actions . some ( action => action . kind && Extract_Constant . kind . contains ( action . kind ) ) ) {
373378 const disabledAction = new vscode . CodeAction (
374379 localize ( 'extractConstant.disabled.title' , "Extract to constant" ) ,
@@ -394,6 +399,39 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
394399 }
395400 return actions ;
396401 }
402+
403+ private pruneInvalidActions ( actions : vscode . CodeAction [ ] , only ?: vscode . CodeActionKind , numberOfInvalid ?: number ) : vscode . CodeAction [ ] {
404+ if ( this . client . apiVersion . lt ( API . v400 ) ) {
405+ // Older TS version don't return extra actions
406+ return actions ;
407+ }
408+
409+ const availableActions : vscode . CodeAction [ ] = [ ] ;
410+ const invalidCommonActions : vscode . CodeAction [ ] = [ ] ;
411+ const invalidUncommonActions : vscode . CodeAction [ ] = [ ] ;
412+ for ( const action of actions ) {
413+ if ( ! action . disabled ) {
414+ availableActions . push ( action ) ;
415+ continue ;
416+ }
417+
418+ // These are the common refactors that we should always show if applicable.
419+ if ( action . kind && ( Extract_Constant . kind . contains ( action . kind ) || Extract_Function . kind . contains ( action . kind ) ) ) {
420+ invalidCommonActions . push ( action ) ;
421+ continue ;
422+ }
423+
424+ // These are the remaining refactors that we can show if we haven't reached the max limit with just common refactors.
425+ invalidUncommonActions . push ( action ) ;
426+ }
427+
428+ const prioritizedActions : vscode . CodeAction [ ] = [ ] ;
429+ prioritizedActions . push ( ...invalidCommonActions ) ;
430+ prioritizedActions . push ( ...invalidUncommonActions ) ;
431+ const topNInvalid = prioritizedActions . filter ( action => ! only || ( action . kind && only . contains ( action . kind ) ) ) . slice ( 0 , numberOfInvalid ) ;
432+ availableActions . push ( ...topNInvalid ) ;
433+ return availableActions ;
434+ }
397435}
398436
399437export function register (
0 commit comments