Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize validateset completions over Enums for parameters. #15257

Merged
merged 2 commits into from
Mar 23, 2022

Conversation

MartinGC94
Copy link
Contributor

PR Summary

Makes tab completion respect validateset values for parameters with an enum type.

function foo
{
    param
    (
        [ValidateSet('DarkBlue','DarkCyan')]
        [ConsoleColor]$Color
    )
}
#Tab completion only cycles through DarkBlue and DarkCyan
foo <Tab>

PR Context

The completion for variables already work this way and Powershell does allow you to use ValidateSet to limit valid enums so this is just a fix for the parameter completion.

PR Checklist

@ghost ghost assigned TravisEz13 Apr 17, 2021
@iSazonov
Copy link
Collaborator

What if implement this as filter over enum values? I mean get enum values and filter out them with values from ValidateSet?
This also protects from typos in ValidateSet values.

@mklement0 Are there such scenarios where we could use ValidateSet as follow filter after another source of values?

@mklement0
Copy link
Contributor

I'm not sure we need more sophistication here, as it probably isn't that common a scenario.

No other value sources where filtering based on explicitly enumerated constants - which is the prerequisite for taking effect for tab-completion - come to mind.

The typo problem can be avoided if you use the enum type explicitly, but it's obviously more effort to type:

# A typo here would be flagged in Visual Studio Code and would cause a *parse*-time error when invoked.
[ValidateSet([ConsoleColor]::DarkBlue, [ConsoleColor]::DarkCyan)]

However, if you use strings, you won't see the problem until runtime, situationally, namely only if you happen to pass as value with a typo.

Again, given the use case, I personally think it's fine to make this the command author's responsibility.

On a related note, the ability to rule out specifying multiple values for a flag-based enumeration via a dedicated attribute would be handy - see @SeeminglyScience's suggestions here.

@iSazonov
Copy link
Collaborator

What if we use ValidateScript in the scenario?

@vexx32
Copy link
Collaborator

vexx32 commented Apr 18, 2021

ValidateScript doesn't provide any tab completion, so I'm not really sure how it's relevant here.

@iSazonov
Copy link
Collaborator

ValidateScript doesn't provide any tab completion, so I'm not really sure how it's relevant here.

My question is about using it as a filter.

@vexx32
Copy link
Collaborator

vexx32 commented Apr 18, 2021

I mean yeah, you can use it as a filter for the enum values as they're passed into the function, but it doesn't have any real advantages over using ValidateSet. You also still can't use it to filter out autocompletion options, which seems to be the point of the PR here.

@iSazonov
Copy link
Collaborator

but it doesn't have any real advantages over using ValidateSet.

What if a parameter gets a list of VMs from Cloud but want only stopped ones?

@mklement0
Copy link
Contributor

@iSazonov, in that case you'd use a System.Management.Automation.ArgumentCompleterAttribute attribute.

@iSazonov
Copy link
Collaborator

@iSazonov, in that case you'd use a System.Management.Automation.ArgumentCompleterAttribute attribute.

Then it is a binary attribute and you write a script? :-)

@mklement0
Copy link
Contributor

mklement0 commented Apr 18, 2021

It's usable in PowerShell code too; e.g.:

function foo {
  [cmdletbinding()]
  param(
    [ArgumentCompleter({ param($cmd, $param, $wordToComplete) 'Unix', 'Win32NT' -like "$wordToComplete*" })]
    $Platform
  )
  [System.PlatformID] $Platform
}

Incidentally, this highlights that the [ArgumentCompleter()] attribute too should probably take precedence over the enum values: if you define the $Platform parameter as [System.PlatformID] $Platform, the enum values currently take precedence.

@ghost ghost added the Review - Needed The PR is being reviewed label Apr 26, 2021
@ghost
Copy link

ghost commented Apr 26, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@TravisEz13 TravisEz13 added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Jul 12, 2021
@ghost ghost added the Stale label Jul 27, 2021
@ghost
Copy link

ghost commented Jul 27, 2021

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment.

@MartinGC94
Copy link
Contributor Author

Why has this been marked as "Waiting on Author"? What do I need to do?

@ghost ghost removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept Stale labels Jul 28, 2021
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 28, 2021
Co-authored-by: Ilya <darpa@yandex.ru>
@ghost ghost added the Review - Needed The PR is being reviewed label Aug 4, 2021
@ghost
Copy link

ghost commented Aug 4, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@TravisEz13
Copy link
Member

I would like to get @daxian-dbw to review this before merging.

@ghost ghost removed the Review - Needed The PR is being reviewed label Aug 5, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Aug 13, 2021
@ghost
Copy link

ghost commented Aug 13, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw daxian-dbw closed this Mar 23, 2022
@ghost ghost removed the Review - Needed The PR is being reviewed label Mar 23, 2022
@daxian-dbw daxian-dbw reopened this Mar 23, 2022
@pull-request-quantifier-deprecated

This PR has 72 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +40 -32
Percentile : 28.8%

Total files changed: 2

Change summary by file extension:
.cs : +33 -32
.ps1 : +7 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@daxian-dbw daxian-dbw merged commit 123373e into PowerShell:master Mar 23, 2022
@ghost
Copy link

ghost commented May 23, 2022

🎉v7.3.0-preview.4 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants