本文整理汇总了C#中SyntaxTrivia.GetLocation方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTrivia.GetLocation方法的具体用法?C# SyntaxTrivia.GetLocation怎么用?C# SyntaxTrivia.GetLocation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxTrivia
的用法示例。
在下文中一共展示了SyntaxTrivia.GetLocation方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Evaluate
public EvaluationResult Evaluate(SyntaxTrivia node)
{
var result = EvaluateImpl(node);
if (result != null)
{
var sourceTree = node.GetLocation().SourceTree;
var filePath = sourceTree.FilePath;
var typeDefinition = GetNodeType(node.Token.Parent);
var unitNamespace = GetNamespace(node.Token.Parent);
if (result.ErrorCount == 0)
{
result.ErrorCount = 1;
}
result.LinesOfCodeAffected = 0;
result.Namespace = unitNamespace;
result.TypeKind = typeDefinition.Item1;
result.TypeName = typeDefinition.Item2;
result.Title = Title;
result.Suggestion = Suggestion;
result.Quality = Quality;
result.QualityAttribute = QualityAttribute;
result.ImpactLevel = ImpactLevel;
result.FilePath = filePath;
}
return result;
}
示例2: HandleWhitespaceTrivia
private void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
{
if (trivia.ToFullString().IndexOf('\t') < 0)
{
return;
}
// Tabs must not be used.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
}
示例3: HandleMultiLineComment
private static void HandleMultiLineComment(SyntaxTreeAnalysisContext context, SyntaxTrivia multiLineComment)
{
var nodeText = multiLineComment.ToString();
// We remove the /* and the */ and determine if the comment has any content.
var commentText = nodeText.Substring(2, nodeText.Length - 4);
if (string.IsNullOrWhiteSpace(commentText))
{
var diagnostic = Diagnostic.Create(Descriptor, multiLineComment.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
示例4: HandleSingleLineCommentTrivia
private static void HandleSingleLineCommentTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia, bool isFirstSingleLineTrivia)
{
string text = trivia.ToFullString();
if (text.Equals(@"//"))
{
return;
}
// special case: commented code or documentation if the parsers documentation mode is DocumentationMode.None
if (text.StartsWith(@"///", StringComparison.Ordinal))
{
return;
}
// Special case: multiple dashes at start of comment
if (text.StartsWith(@"//--", StringComparison.Ordinal))
{
return;
}
// Special case: //\ negates spacing requirements
if (text.StartsWith(@"//\", StringComparison.Ordinal))
{
return;
}
// No need to handle documentation comments ("///") because they're not
// reported as SingleLineCommentTrivia.
int spaceCount = 0;
for (int i = 2; (i < text.Length) && (text[i] == ' '); i++)
{
spaceCount++;
}
if (spaceCount == 1)
{
return;
}
// Special case: follow-on comment lines may be indented with more than
// one space.
if (spaceCount > 1 && !isFirstSingleLineTrivia)
{
return;
}
// Single line comment must begin with a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
}
开发者ID:neugenes,项目名称:StyleCopAnalyzers,代码行数:49,代码来源:SA1005SingleLineCommentsMustBeginWithSingleSpace.cs
示例5: Mark
void Mark(SyntaxTrivia trivia, string type, JObject props = null)
{
var location = trivia.GetLocation().GetLineSpan();
var startLine = location.StartLinePosition.Line;
var endLine = location.EndLinePosition.Line;
for (var i = startLine; i <= endLine; i++)
{
var start = i == startLine ? location.StartLinePosition.Character : 0;
var end = i == endLine ? location.EndLinePosition.Character : int.MaxValue;
_regions.Add(new Region
{
Line = i,
Start = start,
End = end,
Style = type,
Props = props
});
}
}
示例6: HandleWhitespaceTrivia
private static void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
{
string fullString = trivia.ToFullString();
if (fullString.StartsWith("////"))
{
// This is a line of commented code.
return;
}
if (fullString.IndexOf('\t') < 0)
{
// No hard tabs were found.
return;
}
// Tabs must not be used.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
}
示例7: HandleWhitespaceTrivia
private static void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
{
if (trivia.Span.Length <= 1)
{
return;
}
if (trivia.SyntaxTree.GetMappedLineSpan(trivia.Span).StartLinePosition.Character == 0)
{
return;
}
SyntaxToken token = trivia.Token;
SyntaxToken precedingToken;
SyntaxToken followingToken;
int index;
SyntaxTriviaList list;
if ((index = token.LeadingTrivia.IndexOf(trivia)) >= 0)
{
precedingToken = token.GetPreviousToken();
followingToken = token;
list = token.LeadingTrivia;
}
else if ((index = token.TrailingTrivia.IndexOf(trivia)) >= 0)
{
precedingToken = token;
followingToken = precedingToken.GetNextToken();
list = token.TrailingTrivia;
}
else
{
// shouldn't be reachable, but either way can't proceed
return;
}
var followingTrivia = index + 1 < list.Count ? list[index + 1] : default(SyntaxTrivia);
if (precedingToken.IsKind(SyntaxKind.CommaToken)
|| precedingToken.IsKind(SyntaxKind.SemicolonToken)
|| followingTrivia.IsKind(SyntaxKind.EndOfLineTrivia)
|| followingToken.IsKind(SyntaxKind.EndOfFileToken))
{
return;
}
// Code must not contain multiple whitespace characters in a row.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
}
开发者ID:endjin,项目名称:StyleCopAnalyzers,代码行数:50,代码来源:SA1025CodeMustNotContainMultipleWhitespaceInARow.cs
示例8: HandleWhitespaceTrivia
private void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
{
if (trivia.Span.Length <= 1)
{
return;
}
if (trivia.SyntaxTree.GetMappedLineSpan(trivia.Span).StartLinePosition.Character == 0)
{
return;
}
SyntaxToken token = trivia.Token;
SyntaxToken precedingToken;
SyntaxToken followingToken;
if (token.LeadingTrivia.Contains(trivia))
{
precedingToken = token.GetPreviousToken();
followingToken = token;
}
else if (token.TrailingTrivia.Contains(trivia))
{
precedingToken = token;
followingToken = precedingToken.GetNextToken();
}
else
{
// shouldn't be reachable, but either way can't proceed
return;
}
if (precedingToken.IsKind(SyntaxKind.CommaToken) || precedingToken.IsKind(SyntaxKind.SemicolonToken))
{
return;
}
// Code must not contain multiple whitespace characters in a row.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, trivia.GetLocation()));
}
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:39,代码来源:SA1025CodeMustNotContainMultipleWhitespaceInARow.cs
示例9: HandleSingleLineComment
private static void HandleSingleLineComment(SyntaxTreeAnalysisContext context, SyntaxTrivia singleLineComment)
{
int index = 0;
// PERF: Explicitly cast to IReadOnlyList so we only box once.
IReadOnlyList<SyntaxTrivia> list = TriviaHelper.GetContainingTriviaList(singleLineComment, out index);
var firstNonWhiteSpace = TriviaHelper.IndexOfFirstNonWhitespaceTrivia(list);
// When we encounter a block of single line comments, we only want to raise this diagnostic
// on the first or last line. This ensures that whitespace in code commented out using
// the Comment Selection option in Visual Studio will not raise the diagnostic for every
// blank line in the code which is commented out.
bool isFirst = index == firstNonWhiteSpace;
if (!isFirst)
{
// This is -2 because we need to go back past the end of line trivia as well.
var lastNonWhiteSpace = TriviaHelper.IndexOfTrailingWhitespace(list) - 2;
if (index != lastNonWhiteSpace)
{
return;
}
}
if (IsNullOrWhiteSpace(singleLineComment.ToString(), 2))
{
var diagnostic = Diagnostic.Create(Descriptor, singleLineComment.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
示例10: VisitTrivia
public override void VisitTrivia (SyntaxTrivia trivia)
{
base.VisitTrivia (trivia);
if (trivia.IsKind (SyntaxKind.RegionDirectiveTrivia)) {
regionStack.Push (trivia);
} else if (trivia.IsKind (SyntaxKind.EndRegionDirectiveTrivia)) {
if (regionStack.Count == 0)
return;
var regionStart = regionStack.Pop ();
try {
var first = regionStart.GetLocation ().GetLineSpan ();
var last = trivia.GetLocation ().GetLineSpan ();
var v = regionStart.ToString ();
v = v.Substring ("#region".Length).Trim ();
if (v.Length == 0)
v = "...";
Foldings.Add (new FoldingRegion(v, new DocumentRegion(first.StartLinePosition, last.EndLinePosition), FoldType.UserRegion, true));
} catch (ArgumentOutOfRangeException) { }
}
}
示例11: GetFullCommentedCode
internal static GetFullCommentedCodeResult GetFullCommentedCode(SyntaxNode root, SyntaxTrivia firstComment)
{
var result = new StringBuilder();
var current = firstComment;
var numberOfComments = 1;
var start = firstComment.GetLocation().SourceSpan.Start;
int end;
do
{
end = current.GetLocation().SourceSpan.End;
result.Append(current.ToString().Substring(2));
var eol = root.FindTrivia(current.GetLocation().SourceSpan.End + 1);
if (!eol.IsKind(SyntaxKind.EndOfLineTrivia)) break;
var whitespace = root.FindTrivia(eol.GetLocation().SourceSpan.End + 1);
if (!whitespace.IsKind(SyntaxKind.WhitespaceTrivia)) break;
current = root.FindTrivia(whitespace.GetLocation().SourceSpan.End + 1);
if (!current.IsKind(SyntaxKind.SingleLineCommentTrivia)) break;
numberOfComments ++;
} while (true);
return new GetFullCommentedCodeResult(result.ToString(), numberOfComments, start, end);
}
示例12: CheckMultilineComment
private static void CheckMultilineComment(SyntaxTreeAnalysisContext c, SyntaxTrivia comment)
{
var triviaContent = GetTriviaContent(comment);
var triviaLines = triviaContent.Split(MetricsBase.LineTerminators, StringSplitOptions.None);
for (var triviaLineNumber = 0; triviaLineNumber < triviaLines.Length; triviaLineNumber++)
{
if (!IsCode(triviaLines[triviaLineNumber]))
{
continue;
}
var triviaStartingLineNumber = comment.GetLocation().GetLineSpan().StartLinePosition.Line;
var lineNumber = triviaStartingLineNumber + triviaLineNumber;
var lineSpan = c.Tree.GetText().Lines[lineNumber].Span;
var commentLineSpan = lineSpan.Intersection(comment.GetLocation().SourceSpan);
var location = Location.Create(c.Tree, commentLineSpan ?? lineSpan);
c.ReportDiagnostic(Diagnostic.Create(Rule, location));
return;
}
}