microsoft / microsoft/TypeScript

Use context to prioritize or only show `case` and `default` in a `switch` statement

Ouverte
#52,234 0 commentaires 1 réaction 1 personne assignée Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: LS: Completion Lists In Discussion Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

While playing around with switch/case snippets, I ran into the following:

switch (someValue) {
    /**/
}

Request completions at /**/

The only completions that should be offered here are case and default (along with corresponding snippet completions). It's bad that we don't do that because these keywords are always sorted to come after any other invalid completions.

Fixing just this is one small case. If we wanted to go deeper, I would propose that we use the line and column of the current case or default clause to inform what completions we offer.

Some test cases I would want to remain the same or change:

// same - show all completions
switch (thing) {
  case 42:
  /*$*/
}
// same - show all completions
switch (thing) {
  case 42:
  break;
  /*$*/
}
// same - show all completions
switch (thing) {
  case 42:
  if (Math.random()) {
  }
  else {
  }
  /*$*/
}
// NEW - prioritize `case`/`default`
switch (thing) {
  case 42:
    break;
  /*$*/
}
// NEW - prioritize `case`/`default`
switch (thing) {
  case 42: break;
  /*$*/
}
// NEW - prioritize `case`/`default`
switch (thing) {
  case 42: {
  }
  break;
  /*$*/
}

The current closest approximation I can think of is:

  • the cursor is at the same column as the containing clause's case/default keyword, but on a different line, and
  • there is at least one statement in the clause, and any of the following are true:
    • the last statement is one that would jump out of the current control flow (e.g. break/return/throw/continue)
    • the start of the last statement's token is on a different line and column as the containing clause's case/default keyword

then we should actually ensure case and default appear at the top.

This case:

// NEW - prioritize `case`/`default`/`break`
switch (thing) {
  case 42: {
    // ...
  }
  /*$*/
}

is one that might get another special-case where we'd possibly want to provide break if we don't see it as the last statement of the block. But I'm curious to hear others' thoughts.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.