Skip to content

Commit

Permalink
miscellaneous small feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Dec 2, 2020
1 parent d9a327c commit 9d9b066
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ protected override bool IsValidClosingBraceToken(SyntaxToken rightToken)
=> rightToken.IsKind(SyntaxKind.InterpolatedStringEndToken);

protected override Task<bool> IsValidOpenBraceTokenAtPositionAsync(SyntaxToken token, int position, Document document, CancellationToken cancellationToken)
{
return Task.FromResult(IsValidOpeningBraceToken(token)
&& token.Span.End - 1 == position);
}
=> Task.FromResult(IsValidOpeningBraceToken(token) && token.Span.End - 1 == position);

/// <summary>
/// Returns true when the input position could be starting an interpolated string if opening quotes were typed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,28 @@ public override Task<bool> AllowOverTypeAsync(BraceCompletionContext context, Ca
protected override async Task<bool> IsValidOpenBraceTokenAtPositionAsync(SyntaxToken token, int position, Document document, CancellationToken cancellationToken)
{
var syntaxFactsService = document.GetRequiredLanguageService<ISyntaxFactsService>();
if (ParentIsSkippedTokensTrivia(syntaxFactsService, token) ||
!IsValidOpeningBraceToken(token) ||
token.SpanStart != position || token.Parent == null)
if (ParentIsSkippedTokensTrivia(syntaxFactsService, token)
|| !IsValidOpeningBraceToken(token)
|| token.SpanStart != position
|| token.Parent == null)
{
return false;
}

// now check whether parser think whether there is already counterpart closing parenthesis
var (openBrace, closeBrace) = token.Parent.GetParentheses();

// If the completed pair is on the same line, then the closing parenthesis must belong to a different
// brace completion session higher up on the stack. If that's the case then we can
// complete the opening brace here, so return this as valid for completion.
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
if (text.Lines.GetLineFromPosition(openBrace.SpanStart) == text.Lines.GetLineFromPosition(closeBrace.Span.End))
// We can complete the brace if the closing brace is missing or the incorrect kind.
if (closeBrace.Kind() != SyntaxKind.CloseParenToken || closeBrace.Span.Length == 0)
{
return true;
}

// We can complete the brace if the closing brace is missing or the incorrect kind.
return closeBrace.Kind() != SyntaxKind.CloseParenToken || closeBrace.Span.Length == 0;
// If the completed pair is on the same line, then the closing parenthesis must belong to a different
// brace completion session higher up on the stack. If that's the case then we can
// complete the opening brace here, so return this as valid for completion.
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
return text.Lines.GetLineFromPosition(openBrace.SpanStart) == text.Lines.GetLineFromPosition(closeBrace.Span.End);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ internal abstract class AbstractBraceCompletionService : IBraceCompletionService

public virtual async Task<bool> CanProvideBraceCompletionAsync(char brace, int openingPosition, Document document, CancellationToken cancellationToken)
{
if (OpeningBrace != brace)
{
return false;
}

// check that the user is not typing in a string literal or comment
var tree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var syntaxFactsService = document.GetRequiredLanguageService<ISyntaxFactsService>();

return OpeningBrace == brace && !syntaxFactsService.IsInNonUserCode(tree, openingPosition, cancellationToken);
return !syntaxFactsService.IsInNonUserCode(tree, openingPosition, cancellationToken);
}

public async Task<BraceCompletionContext?> GetCompletedBraceContextAsync(Document document, int caretLocation, CancellationToken cancellationToken)
Expand All @@ -97,15 +102,13 @@ public virtual async Task<bool> CanProvideBraceCompletionAsync(char brace, int o
/// </summary>
protected virtual Task<bool> IsValidOpenBraceTokenAtPositionAsync(SyntaxToken token, int position, Document document, CancellationToken cancellationToken)
{
var syntaxFactsService = document.GetRequiredLanguageService<ISyntaxFactsService>();

// The open token is typed in skipped token trivia, we should not attempt to complete it.
if (ParentIsSkippedTokensTrivia(syntaxFactsService, token))
if (token.SpanStart != position)
{
return SpecializedTasks.False;
}

return Task.FromResult(token.SpanStart == position && IsValidOpeningBraceToken(token));
var syntaxFactsService = document.GetRequiredLanguageService<ISyntaxFactsService>();
return Task.FromResult(IsValidOpeningBraceToken(token) && !ParentIsSkippedTokensTrivia(syntaxFactsService, token));
}

/// <summary>
Expand Down Expand Up @@ -181,6 +184,11 @@ public static class SingleQuote
public const char CloseCharacter = '\'';
}

/// <summary>
/// Determines if inserting the opening brace at the location could be an attempt to
/// escape a previously inserted opening brace.
/// E.g. they are trying to type $"{{"
/// </summary>
protected static async Task<bool> CouldEscapePreviousOpenBraceAsync(char openingBrace, int position, Document document, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
Expand Down

0 comments on commit 9d9b066

Please sign in to comment.