提交 7f0aff0a 编写于 作者: C Cyrus Najmabadi

Small tweaks

上级 1d03590f
......@@ -69,7 +69,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
{
// If the caller wants no indent, then we'll return an effective '0' indent.
if (indentStyle == FormattingOptions.IndentStyle.None)
return new IndentationResult(basePosition: 0, offset: 0);
return default;
// If the user has explicitly set 'block' indentation, or they're in an inactive preprocessor region,
// then just do simple block indentation.
......@@ -80,7 +80,11 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
}
Debug.Assert(indentStyle == FormattingOptions.IndentStyle.Smart);
return GetDesiredSmartIndentation();
}
private readonly IndentationResult GetDesiredSmartIndentation()
{
// For smart indent, we want the previous to compute indentation from.
var token = Root.FindToken(LineToBeIndented.Start);
......@@ -88,14 +92,20 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
// based on the preceding token. So if we're before a token, look back to the previous token to
// determine what our indentation is based off of.
if (token.SpanStart >= LineToBeIndented.Start)
{
token = token.GetPreviousToken();
if (token == default)
{
// we're at the start of the file. No indentation here.
return new IndentationResult(basePosition: 0, offset: 0);
// Skip past preceding blank tokens. This can happen in VB for example where there can be
// whitespace tokens in things like xml literals. We want to get the first visible token that we
// would actually anch would anchor indentation off of.
while (token != default && string.IsNullOrWhiteSpace(token.ToString()))
token = token.GetPreviousToken();
}
// if we're at the start of the file then there's no indentation here.
if (token == default)
return default;
return _service.GetDesiredIndentationWorker(
this, token, default, default/*previousNonWhitespaceOrPreprocessorLine, lastNonWhitespacePosition*/);
}
......@@ -116,7 +126,7 @@ private IndentationResult GetDesiredBlockIndentation()
}
// Couldn't find a previous non-blank line. Don't indent at all.
return new IndentationResult(basePosition: 0, offset: 0);
return default;
}
public bool TryGetSmartTokenIndentation(out IndentationResult indentationResult)
......
......@@ -27,56 +27,64 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation
Protected Overrides Function GetDesiredIndentationWorker(
indenter As Indenter,
token As SyntaxToken,
previousLine As TextLine,
lastNonWhitespacePosition As Integer) As IndentationResult
'If token.Span.End = lastNonWhitespacePosition + 1 Then
Return GetIndentationBasedOnToken(indenter, token)
'Else
' Debug.Assert(token.FullSpan.Contains(lastNonWhitespacePosition))
' Dim trivia = indenter.Root.FindTrivia(lastNonWhitespacePosition)
' ' preserve the indentation of the comment trivia before a case statement
' If trivia.Kind = SyntaxKind.CommentTrivia AndAlso trivia.Token.IsKind(SyntaxKind.CaseKeyword) AndAlso trivia.Token.Parent.IsKind(SyntaxKind.CaseStatement) Then
' Return indenter.GetIndentationOfLine(previousLine)
' End If
' If trivia.Kind = SyntaxKind.LineContinuationTrivia Then
' Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
' End If
' ' Line ends in comment
' If trivia.Kind = SyntaxKind.CommentTrivia Then ' Two cases a line ending comment or _ comment
' Dim firstTrivia As SyntaxTrivia = indenter.Tree.GetRoot(indenter.CancellationToken).FindTrivia(token.Span.End + 1)
' ' firstTrivia contains either an _ or a comment, this is the First trivia after the last Token on the line
' If firstTrivia.Kind = SyntaxKind.LineContinuationTrivia Then
' Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(firstTrivia), firstTrivia)
' Else
' ' This is we have just a comment
' Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
' End If
' End If
' ' if we are at invalid token (skipped token) at the end of statement, treat it like we are after line continuation
' If trivia.Kind = SyntaxKind.SkippedTokensTrivia AndAlso trivia.Token.IsLastTokenOfStatement() Then
' Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
' End If
' ' okay, now check whether the trivia is at the beginning of the line
' Dim firstNonWhitespacePosition = previousLine.GetFirstNonWhitespacePosition()
' If Not firstNonWhitespacePosition.HasValue Then
' Return indenter.IndentFromStartOfLine(0)
' End If
' Dim firstTokenOnLine = indenter.Root.FindToken(firstNonWhitespacePosition.Value, findInsideTrivia:=True)
' If firstTokenOnLine.Kind <> SyntaxKind.None AndAlso firstTokenOnLine.Span.Contains(firstNonWhitespacePosition.Value) Then
' 'okay, beginning of the line is not trivia, use this token as the base token
' Return GetIndentationBasedOnToken(indenter, firstTokenOnLine)
' End If
previousLine1 As TextLine,
lastNonWhitespacePosition1 As Integer) As IndentationResult
' in VB the trivia following the preceding token is relevant for determining what indentation we should be
' at. For example, it's very different to get indentation after
'
' dim x _
'
' versus
'
' dim x
Dim trivia = token.TrailingTrivia().LastOrDefault(Function(t) Not t.IsWhitespaceOrEndOfLine())
If trivia = Nothing Then
Return GetIndentationBasedOnToken(indenter, token)
End If
' preserve the indentation of the comment trivia before a case statement
'If trivia.Kind = SyntaxKind.CommentTrivia AndAlso
' trivia.Token.IsKind(SyntaxKind.CaseKeyword) AndAlso
' trivia.Token.Parent.IsKind(SyntaxKind.CaseStatement) Then
' Return indenter.GetIndentationOfLine(previousLine)
'End If
If trivia.Kind = SyntaxKind.LineContinuationTrivia Then
Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
End If
' Line ends in comment
If trivia.Kind = SyntaxKind.CommentTrivia Then ' Two cases a line ending comment or _ comment
Dim firstTrivia As SyntaxTrivia = indenter.Tree.GetRoot(indenter.CancellationToken).FindTrivia(token.Span.End + 1)
' firstTrivia contains either an _ or a comment, this is the First trivia after the last Token on the line
If firstTrivia.Kind = SyntaxKind.LineContinuationTrivia Then
Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(firstTrivia), firstTrivia)
Else
' This is we have just a comment
Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
End If
End If
' if we are at invalid token (skipped token) at the end of statement, treat it like we are after line continuation
If trivia.Kind = SyntaxKind.SkippedTokensTrivia AndAlso trivia.Token.IsLastTokenOfStatement() Then
Return GetIndentationBasedOnToken(indenter, GetTokenOnLeft(trivia), trivia)
End If
' okay, now check whether the trivia is at the beginning of the line
'Dim firstNonWhitespacePosition = previousLine.GetFirstNonWhitespacePosition()
'If Not firstNonWhitespacePosition.HasValue Then
' Return indenter.IndentFromStartOfLine(0)
'End If
'Dim firstTokenOnLine = indenter.Root.FindToken(firstNonWhitespacePosition.Value, findInsideTrivia:=True)
'If firstTokenOnLine.Kind <> SyntaxKind.None AndAlso firstTokenOnLine.Span.Contains(firstNonWhitespacePosition.Value) Then
' 'okay, beginning of the line is not trivia, use this token as the base token
' Return GetIndentationBasedOnToken(indenter, firstTokenOnLine)
'End If
Return GetIndentationBasedOnToken(indenter, token)
End Function
Private Function GetTokenOnLeft(trivia As SyntaxTrivia) As SyntaxToken
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册