-
Notifications
You must be signed in to change notification settings - Fork 24
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
sorting parameters by position then by required and then by name in syntax generation #19
Conversation
Thank you for taking the time to check out the code and submit a PR. However, I don't really understand why the order that the parameters are defined in the
Are you referring to the the C# source editing feature of ReSharper, whereby it will order the fields and properties in its auto-completion lists, for example? If so, I don't see how controlling the ordering of the documentation in the MAML help file has any influence over this. Are you referring to a PowerShell editing feature of ReSharper? If so, it's something I'm unaware of.
Yes, this PR would sort the parameters irrespective of them being defined in the cmdlet class or a base class, but why does this matter? This would simply control the declaration order of the parameter elements in the MAML help file, but I'm not aware of any consumer of the file (e.g. the Get-Help cmdlet) that is sensitive to that ordering. Could you please clarify in more detail exactly why the ordering of the parameters in the MAML help file is important, with a specific example? |
I propose you an experiment. I'm sure that you have some MAML help file. Go to the syntaxItem element and change the order of positioned parameters e.g. move parameter with position 0 after a parameter with position 1, save it. Now, check how Get-Help renders syntax for changed cmdlet. Order of parameters in syntaxItem element has direct influence on generated syntax by Get-Help. It can mislead a user of the cmdlet. This feature of R# is called "File Layout". If you have it configured that way that during code clean up you sort members of the class it can interchange properties e.g. property ParA (position 1) will be above property ParB (position 0). Similar effect will be if you have class hierarchy: "B" inherits from "A" and "A" inherits from "Cmdlet" and "A" has parameter ParA (position 0, type string) and "B" has parameter ParB (position 1, type string) then syntax for cmdlet will be: [-ParB] [-ParA] but should be exactly opposite. I suppose that System.Type.GetMembers(BindingFlags) looks for members in the order they are declared and when we have deeper class hierarchy it first looks for members declared in the type and after it his parent and so on. Nevertheless MSDN documentation doesn't guarantee any order neither alphabetical nor declaration so we shouldn't depend on it. I hope this clarifies my PR. |
Ah, I think I get it now. Okay, will have another look at the code as there's one minor issue I wanted to dig into, and then hopefully merge it soon. Thanks again for this PR. 😄 |
@@ -324,7 +324,9 @@ private XElement GenerateSyntaxItemElement(ICommentReader commentReader, Command | |||
{ | |||
var syntaxItemElement = new XElement(CommandNs + "syntaxItem", | |||
new XElement(MamlNs + "name", command.Name)); | |||
foreach (var parameter in command.GetParameters(parameterSetName)) | |||
foreach (var parameter in command.GetParameters(parameterSetName).OrderBy(p => p.GetPosition(parameterSetName)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p.GetPosition
returns a string, which may be any of the following:
"n"
, wheren
is an integer, taken from thePosition
property of theParameter
attribute, if it is specified."named"
if thePosition
property is equal toint.MinValue
, which is the default value if thePosition
hasn't actually been set.. I cannot remember under what circumstances this might happen.null
if XmlDoc2CmdletDoc is unable to retrieve a value for the current parameter set being documented.
This rather relies on parameter positions being single digits, and that "1" and "2", for example, are lexicographically lower than "named" and null
. This is probably good enough in most circumstances, but I'll probably revisit this area of the code at some point to make things a bit more explicit.
Cheers! This has now been published to NuGet, and I've updated the contributors list in the project readme. 😸 |
Now, I can show off to my wife 😃 |
Two cases when it's needed:
Additionaly MSDN doesn't guarantee that Type.GetMembers will return array of members in order of declaration
https://msdn.microsoft.com/en-us/library/k2w5ey1e(v=vs.110).aspx