Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
case TokenKind.Generic:
case TokenKind.MinusMinus: // for native commands '--'
case TokenKind.Identifier:
result = GetResultForIdentifier(completionContext, ref replacementIndex, ref replacementLength, isQuotedString);
if (tokenAtCursor.TokenFlags != TokenFlags.TypeName)
{
result = GetResultForIdentifier(completionContext, ref replacementIndex, ref replacementLength, isQuotedString);
}
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved

break;

case TokenKind.Parameter:
Expand Down Expand Up @@ -466,7 +470,8 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
case TokenKind.QuestionDot:
replacementIndex += tokenAtCursor.Text.Length;
replacementLength = 0;
result = CompletionCompleters.CompleteMember(completionContext, @static: tokenAtCursor.Kind == TokenKind.ColonColon);
result = CompletionCompleters.CompleteMember(completionContext, @static: tokenAtCursor.Kind == TokenKind.ColonColon, ref replacementLength);
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved

break;

case TokenKind.Comment:
Expand Down Expand Up @@ -805,6 +810,35 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
result = GetResultForIdentifierInConfiguration(completionContext, configAst, keywordAst, out matched);
}
}

// Handles following scenario where user is tab completing a member on an empty line:
// "Hello".
// <Tab>
if ((result is null || result.Count == 0) && tokenBeforeCursor is not null)
{
switch (completionContext.TokenBeforeCursor.Kind)
{

case TokenKind.Dot:
case TokenKind.ColonColon:
case TokenKind.QuestionDot:
replacementIndex = cursor.Offset;
replacementLength = 0;
result = CompletionCompleters.CompleteMember(completionContext, @static: completionContext.TokenBeforeCursor.Kind == TokenKind.ColonColon, ref replacementLength);
break;

case TokenKind.LParen:
case TokenKind.Comma:
if (lastAst is AttributeAst)
{
result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength);
}

break;
default:
break;
}
}
}
else if (completionContext.TokenAtCursor == null)
{
Expand Down Expand Up @@ -860,6 +894,18 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
}
break;
}
case TokenKind.Dot:
case TokenKind.ColonColon:
case TokenKind.QuestionDot:
// Handles following scenario with whitespace after member access token: "Hello". <Tab>
replacementIndex = cursor.Offset;
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
replacementLength = 0;
result = CompletionCompleters.CompleteMember(completionContext, @static: tokenBeforeCursor.Kind == TokenKind.ColonColon, ref replacementLength);
if (result is not null && result.Count > 0)
{
return result;
}
break;
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
default:
break;
}
Expand Down Expand Up @@ -913,6 +959,11 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
typeNameToComplete = FindTypeNameToComplete(typeConstraintAst.TypeName, _cursorPosition);
}
}

if (typeNameToComplete is null && tokenAtCursor?.TokenFlags == TokenFlags.TypeName)
{
typeNameToComplete = new TypeName(tokenAtCursor.Extent, tokenAtCursor.Text);
}

if (typeNameToComplete != null)
{
Expand Down Expand Up @@ -1989,8 +2040,24 @@ private List<CompletionResult> GetResultForIdentifier(CompletionContext completi
}

TokenKind memberOperator = TokenKind.Unknown;
bool isMemberCompletion = (lastAst.Parent is MemberExpressionAst);
bool isStatic = isMemberCompletion && ((MemberExpressionAst)lastAst.Parent).Static;
bool isMemberCompletion = lastAst.Parent is MemberExpressionAst;
bool isStatic = false;
if (isMemberCompletion)
{
var currentExpression = (MemberExpressionAst)lastAst.Parent;
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
// Handles following scenario with an incomplete member access token at the end of the statement:
// [System.IO.FileInfo]::new<Tab>().Directory.BaseName.Length.
// Traverses up the expressions until it finds one under at the cursor
while (currentExpression.Extent.EndOffset >= completionContext.CursorPosition.Offset
&& currentExpression.Expression is MemberExpressionAst memberExpression
&& memberExpression.Member.Extent.EndOffset >= completionContext.CursorPosition.Offset)
{
currentExpression = memberExpression;
}

isStatic = currentExpression.Static;
}

bool isWildcard = false;

if (!isMemberCompletion)
Expand Down Expand Up @@ -2041,7 +2108,7 @@ private List<CompletionResult> GetResultForIdentifier(CompletionContext completi

if (isMemberCompletion)
{
result = CompletionCompleters.CompleteMember(completionContext, @static: (isStatic || memberOperator == TokenKind.ColonColon));
result = CompletionCompleters.CompleteMember(completionContext, @static: (isStatic || memberOperator == TokenKind.ColonColon), ref replacementLength);

// If the last token was just a '.', we tried to complete members. That may
// have failed because it wasn't really an attempt to complete a member, in
Expand Down Expand Up @@ -2126,13 +2193,6 @@ private List<CompletionResult> GetResultForIdentifier(CompletionContext completi
replacementIndex = completionContext.ReplacementIndex;
replacementLength = completionContext.ReplacementLength;

if (result.Count == 0
&& completionContext.TokenAtCursor.TokenFlags.HasFlag(TokenFlags.TypeName)
&& lastAst?.Find(a => a is MemberExpressionAst, searchNestedScriptBlocks: false) is not null)
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
{
result = CompletionCompleters.CompleteType(completionContext.TokenAtCursor.Text).ToList();
}

return result;
}

Expand All @@ -2142,32 +2202,46 @@ private static List<CompletionResult> GetResultForAttributeArgument(CompletionCo
Type attributeType = null;
string argName = string.Empty;
Ast argAst = completionContext.RelatedAsts.Find(static ast => ast is NamedAttributeArgumentAst);
NamedAttributeArgumentAst namedArgAst = argAst as NamedAttributeArgumentAst;
if (argAst != null && namedArgAst != null)
AttributeAst attAst;
if (argAst is NamedAttributeArgumentAst namedArgAst)
{
attributeType = ((AttributeAst)namedArgAst.Parent).TypeName.GetReflectionAttributeType();
attAst = (AttributeAst)namedArgAst.Parent;
attributeType = attAst.TypeName.GetReflectionAttributeType();
argName = namedArgAst.ArgumentName;
replacementIndex = namedArgAst.Extent.StartOffset;
replacementLength = argName.Length;
}
else
{
Ast astAtt = completionContext.RelatedAsts.Find(static ast => ast is AttributeAst);
AttributeAst attAst = astAtt as AttributeAst;
if (astAtt != null && attAst != null)
attAst = astAtt as AttributeAst;
if (attAst is not null)
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
{
attributeType = attAst.TypeName.GetReflectionAttributeType();
}
}

if (attributeType != null)
if (attributeType is not null)
{
int cursorPosition = completionContext.CursorPosition.Offset;
var existingArguments = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var namedArgument in attAst.NamedArguments)
{
if (cursorPosition < namedArgument.Extent.StartOffset || cursorPosition > namedArgument.Extent.EndOffset)
{
existingArguments.Add(namedArgument.ArgumentName);
}
}
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved

PropertyInfo[] propertyInfos = attributeType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
List<CompletionResult> result = new List<CompletionResult>();
foreach (PropertyInfo property in propertyInfos)
{
// Ignore getter-only properties, including 'TypeId' (all attributes inherit it).
if (!property.CanWrite) { continue; }
// Ignore getter-only properties and properties that have already been set.
if (!property.CanWrite || existingArguments.Contains(property.Name))
{
continue;
}

if (property.Name.StartsWith(argName, StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5631,42 +5631,46 @@ private static List<CompletionResult> CompleteCommentParameterValue(CompletionCo
private static readonly HashSet<string> s_dscCollectionVariables =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "SelectedNodes", "AllNodes" };

internal static List<CompletionResult> CompleteMember(CompletionContext context, bool @static)
internal static List<CompletionResult> CompleteMember(CompletionContext context, bool @static, ref int replacementLength)
{
// If we get here, we know that either:
// * the cursor appeared immediately after a member access token ('.' or '::').
// * the cursor appeared after a member access token ('.' or '::').
// * the parent of the ast on the cursor was a member expression.
//
// In the first case, we have 2 possibilities:
// * the last ast is an error ast because no member name was entered and we were in expression context
// * the last ast is a string constant, with something like: echo $foo.

var results = new List<CompletionResult>();

var lastAst = context.RelatedAsts.Last();
var lastAstAsMemberExpr = lastAst as MemberExpressionAst;
var memberName = "*";
Ast memberNameCandidateAst = null;
ExpressionAst targetExpr = null;
if (lastAstAsMemberExpr != null)

if (lastAst is MemberExpressionAst LastAstAsMemberExpression)
{
// If the cursor is not inside the member name in the member expression, assume
// that the user had incomplete input, but the parser got lucky and succeeded parsing anyway.
if (context.TokenAtCursor.Extent.StartOffset >= lastAstAsMemberExpr.Member.Extent.StartOffset)
if (context.TokenAtCursor is not null && context.TokenAtCursor.Extent.StartOffset >= LastAstAsMemberExpression.Member.Extent.StartOffset)
{
memberNameCandidateAst = lastAstAsMemberExpr.Member;
memberNameCandidateAst = LastAstAsMemberExpression.Member;
}

targetExpr = lastAstAsMemberExpr.Expression;
targetExpr = LastAstAsMemberExpression.Expression;
// Handles scenario where the cursor is after the member access token but before the text
// like: "".<Tab>Le
// which completes the member using the partial text after the cursor.
if (LastAstAsMemberExpression.Member is StringConstantExpressionAst stringExpression && stringExpression.Extent.StartOffset <= context.CursorPosition.Offset)
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved
{
memberName = $"{stringExpression.Value}*";
}
}
else
{
memberNameCandidateAst = lastAst;
}

var memberNameAst = memberNameCandidateAst as StringConstantExpressionAst;

var memberName = "*";
if (memberNameAst != null)
if (memberNameCandidateAst is StringConstantExpressionAst memberNameAst)
{
// Make sure to correctly handle: echo $foo.
if (!memberNameAst.Value.Equals(".", StringComparison.OrdinalIgnoreCase) && !memberNameAst.Value.Equals("::", StringComparison.OrdinalIgnoreCase))
Expand All @@ -5680,8 +5684,7 @@ internal static List<CompletionResult> CompleteMember(CompletionContext context,
return results;
}

var commandAst = lastAst.Parent as CommandAst;
if (commandAst != null)
if (lastAst.Parent is CommandAst commandAst)
{
int i;
for (i = commandAst.CommandElements.Count - 1; i >= 0; --i)
Expand All @@ -5701,34 +5704,81 @@ internal static List<CompletionResult> CompleteMember(CompletionContext context,
targetExpr = nextToLastAst as ExpressionAst;
}
}
else if (lastAst.Parent is MemberExpressionAst)
else if (lastAst.Parent is MemberExpressionAst parentAsMemberExpression)
{
if (lastAst is ErrorExpressionAst)
{
// Handles scenarios like $PSVersionTable.PSVersi<tab>.Major.
// where the cursor is moved back to a previous member expression while
// there's an incomplete member expression at the end
targetExpr = parentAsMemberExpression;
do
{
if (targetExpr is MemberExpressionAst memberExpression)
Comment thread
iSazonov marked this conversation as resolved.
Resolved
{
targetExpr = memberExpression.Expression;
}
else
{
break;
}
} while (targetExpr.Extent.EndOffset >= context.CursorPosition.Offset);

if (targetExpr.Parent != parentAsMemberExpression
&& targetExpr.Parent is MemberExpressionAst memberAst
&& memberAst.Member is StringConstantExpressionAst stringExpression
&& stringExpression.Extent.StartOffset <= context.CursorPosition.Offset)
{
memberName = $"{stringExpression.Value}*";
}
}
// If 'targetExpr' has already been set, we should skip this step. This is for some member completion
// cases in ISE. In ISE, we may add a new statement in the middle of existing statements as follows:
// cases in VSCode, where we may add a new statement in the middle of existing statements as follows:
// $xml = New-Object Xml
// $xml.
// $xml.Save("C:\data.xml")
// In this example, we add $xml. between two existing statements, and the 'lastAst' in this case is
// a MemberExpressionAst '$xml.$xml', whose parent is still a MemberExpressionAst '$xml.$xml.Save'.
// But here we DO NOT want to re-assign 'targetExpr' to be '$xml.$xml'. 'targetExpr' in this case
// should be '$xml'.
if (targetExpr == null)
else if (targetExpr is null)
{
targetExpr = parentAsMemberExpression.Expression;
}
}
else if (lastAst.Parent is BinaryExpressionAst binaryExpression && context.TokenAtCursor.Kind.Equals(TokenKind.Multiply))
{
if (binaryExpression.Left is MemberExpressionAst memberExpression)
{
var memberExprAst = (MemberExpressionAst)lastAst.Parent;
targetExpr = memberExprAst.Expression;
targetExpr = memberExpression.Expression;
if (memberExpression.Member is StringConstantExpressionAst stringExpression)
{
memberName = $"{stringExpression.Value}*";
}
}
}
else if (lastAst.Parent is BinaryExpressionAst && context.TokenAtCursor.Kind.Equals(TokenKind.Multiply))
else if (lastAst.Parent is ErrorStatementAst errorStatement)
{
var memberExprAst = ((BinaryExpressionAst)lastAst.Parent).Left as MemberExpressionAst;
if (memberExprAst != null)
// Handles switches like:
// switch ($x)
// {
// 'RandomString'.<tab>
// { }
// }
Ast astBeforeMemberAccessToken = null;
for (int i = errorStatement.Bodies.Count - 1; i >= 0; i--)
{
targetExpr = memberExprAst.Expression;
if (memberExprAst.Member is StringConstantExpressionAst)
astBeforeMemberAccessToken = errorStatement.Bodies[i];
if (astBeforeMemberAccessToken.Extent.EndOffset < lastAst.Extent.EndOffset)
{
memberName = ((StringConstantExpressionAst)memberExprAst.Member).Value + "*";
break;
}
}

if (astBeforeMemberAccessToken is ExpressionAst expression)
{
targetExpr = expression;
}
}

if (targetExpr == null)
Expand Down Expand Up @@ -5818,6 +5868,12 @@ internal static List<CompletionResult> CompleteMember(CompletionContext context,
}
}

if (memberName != "*" && results.Count > 0)
{
// -1 because membername always has a trailing wildcard *
replacementLength = memberName.Length - 1;
}
Comment thread
MartinGC94 marked this conversation as resolved.
Resolved

return results;
}

Expand Down
Loading