Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ internal static AstAnalysisContext ExtractAstContext(Ast inputAst, Token[] input
ast => IsCursorWithinOrJustAfterExtent(positionForAstSearch, ast.Extent),
searchNestedScriptBlocks: true).ToList();

// If the last ast is an unnamed block that starts with "param" the cursor is inside a param block.
// To avoid adding special handling to all the completers that look at the last ast, we remove it here because it's not useful for completion.
if (relatedAsts[^1].Extent.Text.StartsWith("param", StringComparison.OrdinalIgnoreCase)
&& relatedAsts[^1] is NamedBlockAst namedBlock && namedBlock.Unnamed)
{
relatedAsts.RemoveAt(relatedAsts.Count - 1);
}

Diagnostics.Assert(tokenAtCursor == null || tokenBeforeCursor == null, "Only one of these tokens can be non-null");

return new AstAnalysisContext(tokenAtCursor, tokenBeforeCursor, relatedAsts, replacementIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5527,7 +5527,7 @@ private static FunctionDefinitionAst GetCommentHelpFunctionTarget(CompletionCont
}

Ast lastAst = context.RelatedAsts[^1];
Ast firstAstAfterComment = lastAst.Find(ast => ast.Extent.StartOffset >= context.TokenAtCursor.Extent.EndOffset, searchNestedScriptBlocks: false);
Ast firstAstAfterComment = lastAst.Find(ast => ast.Extent.StartOffset >= context.TokenAtCursor.Extent.EndOffset && ast is not NamedBlockAst, searchNestedScriptBlocks: false);

// Comment-based help can apply to a following function definition if it starts within 2 lines
int commentEndLine = context.TokenAtCursor.Extent.EndLineNumber + 2;
Expand All @@ -5554,9 +5554,7 @@ private static FunctionDefinitionAst GetCommentHelpFunctionTarget(CompletionCont
// Helpblock before function
if (firstAstAfterComment is not null
&& firstAstAfterComment.Extent.StartLineNumber <= commentEndLine
&& firstAstAfterComment is NamedBlockAst block
&& block.Statements.Count > 0
&& block.Statements[0] is FunctionDefinitionAst statement)
&& firstAstAfterComment is FunctionDefinitionAst statement)
{
return statement;
}
Expand Down
37 changes: 37 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ switch ($x)
$actual | Should -BeExactly $expected
}

It 'Should complete parameter in param block' {
$res = TabExpansion2 -inputScript 'Param($Param1=(Get-ChildItem -))' -cursorColumn 30
$res.CompletionMatches[0].CompletionText | Should -BeExactly '-Path'
}
It 'Should complete member in param block' {
$res = TabExpansion2 -inputScript 'Param($Param1=($PSVersionTable.))' -cursorColumn 31
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'Count'
}
It 'Should complete attribute argument in param block' {
$res = TabExpansion2 -inputScript 'Param([Parameter()]$Param1)' -cursorColumn 17
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'Position'
}

Context "Format cmdlet's View paramter completion" {
BeforeAll {
$viewDefinition = @'
Expand Down Expand Up @@ -1970,6 +1983,30 @@ function MyFunction ($param1, $param2)
{
}
}
'@
}
@{
Intent = 'Complete help keyword PARAMETER argument for function inside advanced function'
Expected = 'param1','param2'
TestString = @'
function Verb-Noun
{
Param
(
[Parameter()]
[string[]]
$ParamA
)
Begin
{
<#
.Parameter ^
#>
function MyFunction ($param1, $param2)
{
}
}
}
'@
}
@{
Expand Down