Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd snippet 'foreach-indexed' #2070
Conversation
|
Thanks for your contribution @travis-c-lagrone. Although this is generally applicable, I think this one is more advanced and specific than other snippets we enable by default and would be better in the community snippets doc. |
|
@rjmholt Would you plesae clarify what the criteria (even if open-ended) is for whether a snippet is appropriate to be included in the extension? I ask because while I do not understand how this proposed snippet is either more "advanced" or "specific" than many of the other snippets in the extension (e.g. "IfShouldProcess", "CalculatedProperty", "IArgumentCompleter Class", etc.), although I could see how it might be rejected on the basis of less frequent use. |
|
It's certainly case-by-case, but here the feeling was that an indexed foreach loop is an uncommon use-case given a standard for-loop and the usage of the private Because the JSON snippets represent part of the completions the extension offers, we really want to make sure that those snippets represent PowerShellisms most users would use; so essentially standard structures (like a loop or an advanced function or a switch) that you would find yourself using on a weekly basis and that would see in examples and tutorials in the wild but for which there's no completion. |
|
Ok, I can grok the idea of "PowerShellisms". I'll modify this PR to add the Also, before I make a PR for it, how would you see a snippet like the following applying to the idea of regularly used PowerShellisms for which there's no completion (much less dedicated command)? "Test-Any loop": {
"prefix": "any",
"body": [
"\\$any = \\$false",
"foreach (\\$${1:object} in \\$${2:objects}) {",
"\tif (${3:\\$$1}) {",
"\t\t\\$any = \\$true",
"\t\tbreak",
"\t}",
"}",
],
},
"Test-All loop": {
"prefix": "all",
"body": [
"\\$all = \\$true",
"foreach (\\$${1:object} in \\$${2:objects}) {",
"\tif (-not ${3:\\$$1}) {",
"\t\t\\$all = \\$false",
"\t\tbreak",
"\t}",
"}",
],
}, |
PR Summary
Adds a snippet named
foreach-indexed.The
foreach-indexedsnippet enumerates a collection using aforeachloop, but also tracks the index of the current item in the process. Regarding motivation, the use cases of theforeach-indexedconstruct (which enumerates element-to-index) are somewhat the opposite of theforconstruct (which enumerates index-to-element): synthetically indexing an unordered collection (e.g. aHashSet), relatively indexing a live enumerator (vs. an absolutely-indexed enumerable), etc. Regarding justification, the logic sequencing required by theforeach-indexedconstruct to defend against naive uses of thecontinuekeyword in the middle of the loop body (i.e. continuing without also incrementing the index) is both unconventional as well as more verbose than the conventional pattern (i.e. of incrementing the index at the end of the loop body).PR Checklist
NAPR has tests