Fix tab completions for a hash table#6839
Conversation
| replacementLength = 0; | ||
| break; | ||
|
|
||
| case TokenKind.Semi: |
There was a problem hiding this comment.
Does adding this case affect get-something; e<TAB> - in other words, no hashtable, just simple command completion?
There was a problem hiding this comment.
No, I don't see the side affect.
Maybe add the test?
There was a problem hiding this comment.
I re-check get-something; e<Tab> - in Windows PowerShell 5.1 and in PowerShell Core 6.0 simple command completion works - nothing new added in the PR.
|
@SteveL-MSFT @daxian-dbw Could you please review too? |
|
|
||
| case TokenKind.Semi: | ||
| // Handle scenarios such as 'gci | Format-Table @{Label=...;<tab>' | ||
| result = GetResultForHashtable(completionContext); |
There was a problem hiding this comment.
get-something;<tab> does go through this path. It's better to check if (lastAst is HashtableAst) before calling GetResultForHashtable.
| } | ||
| // Handle scenarios such as 'gci | Format-Table @{Label=<tab>' | ||
| return null; | ||
| } |
There was a problem hiding this comment.
This duplicates the call to GetResultForEnumPropertyValueOfDSCResource.
How about changing to this:
case TokenKind.LParen:
{
if (lastAst is AttributeAst)
{
...
}
else if (lastAst.Parent is DynamicKeywordStatementAst) // <-- change here
{
...
}
else if (lastAst is HashtableAst hashTableAst && CheckForPendingAssignment(hashTableAst)) // <-- change here
{
return result;
}
break;
}
There was a problem hiding this comment.
Second condition doesn't work for configuration foo { File ab { Attributes =( test - really lastAst.Parent is CommandExpressionAst.
Fixed.
| $result.CompletionMatches.Count | Should -Be 3 | ||
| $result.CompletionMatches[0].CompletionText | Should -Be 'Expression' | ||
| $result.CompletionMatches[1].CompletionText | Should -Be 'Ascending' | ||
| $result.CompletionMatches[2].CompletionText | Should -Be 'Descending' |
There was a problem hiding this comment.
This test succeeds even without changes in this PR. You need to check ReplacementIndex and ReplacementLength, as they were -1 previously and changes in this PR should fix them.
| { | ||
| // | ||
| // Handle auto completion for enum/dependson property of DSC resource, | ||
| // 1. Handle scenarios such as 'configuration foo { File ab { Attributes =' |
There was a problem hiding this comment.
Handle auto completion for enum/dependson property of DSC resource
The example configuration foo { File ab { Attributes = is a completion for enum property, so it should not be marked as 1..
| result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength); | ||
| } | ||
| else | ||
| else if (lastAst.Parent is DynamicKeywordStatementAst || lastAst.Parent is CommandExpressionAst) |
There was a problem hiding this comment.
We are making two special checks here. Maybe we should only have one special check, like the following. What do you think?
if (lastAst is AttributeAst)
{
// the original attribute handling
}
else if (lastAst is HashtableAst hashTableAst &&
!(lastAst.Parent is DynamicKeywordStatementAst) &&
CheckForPendingAssignment(hashTableAst))
{
return null;
}
else
{
// the original else block
}There was a problem hiding this comment.
Looks good. Thanks!
Fixed.
| $cmd = "Get-Date | Sort-Object @{Expression=...;" | ||
| $result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length | ||
| $result.CurrentMatchIndex | Should -Be -1 | ||
| $result.ReplacementIndex | Should -Be 40 |
| $result.CompletionMatches[2].CompletionText | Should -Be 'Descending' | ||
| $result.CompletionMatches[0].CompletionText | Should -Be 'Expression' | ||
| $result.CompletionMatches[1].CompletionText | Should -Be 'Ascending' | ||
| $result.CompletionMatches[2].CompletionText | Should -Be 'Descending' |
There was a problem hiding this comment.
$result.CompletionMatches[2].CompletionText is checked twice.
There was a problem hiding this comment.
It was for ReplacementLength :-)
Fixed.
|
The while code is very tricky and sensetive 😕 and crush PowerShell if something wrong. Maybe we should make it more robust. |
daxian-dbw
left a comment
There was a problem hiding this comment.
LGTM.
@iSazonov What while code were you referring to?
|
I means our Completion code - if I return wrong index PowerShell is crashed. Asts can be combined in many ways. Can we be sure that we tested all their? The code is complex and tricky - can we create simple algorithm? Using visitors? |
|
@lzybkr @SteveL-MSFT Do you have any comments? |
|
@iSazonov Sorry for the late reply. I agree the tab completion code is a bit hard to organize now. Many changes have been made that are very specific to the scenarios they try to solve, so you can see checks in However, the crash is possibly not related to tab completion, but the code rending results to console. If you mess up the index, the rendering code might do weird bad things. I don't think a visitor can help much here, as many checks need to inspect ancestors of an ast. |
|
@daxian-dbw Thanks for great comment! |
PR Summary
Fix #5322.
Now working:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests