本文整理汇总了C#中TextSpan.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# TextSpan.Contains方法的具体用法?C# TextSpan.Contains怎么用?C# TextSpan.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextSpan
的用法示例。
在下文中一共展示了TextSpan.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveTriviaAroundSelection
public ITriviaSavedResult SaveTriviaAroundSelection(SyntaxNode root, TextSpan textSpan)
{
Contract.ThrowIfNull(root);
Contract.ThrowIfTrue(textSpan.IsEmpty);
Contract.Requires(Enum.GetNames(typeof(TriviaLocation)).Length == TriviaLocationsCount);
var tokens = GetTokensAtEdges(root, textSpan);
// span must contain after and before spans at the both edges
Contract.ThrowIfFalse(textSpan.Contains(tokens[TriviaLocation.AfterBeginningOfSpan].Span) && textSpan.Contains(tokens[TriviaLocation.BeforeEndOfSpan].Span));
var triviaList = GetTriviaAtEdges(tokens, textSpan);
var annotations = Enumerable.Range((int)TriviaLocation.BeforeBeginningOfSpan, TriviaLocationsCount)
.Cast<TriviaLocation>()
.ToDictionary(location => location, _ => new SyntaxAnnotation());
var map = CreateOldToNewTokensMap(tokens, annotations);
var rootWithAnnotation = ReplaceTokens(root, map.Keys, (o, n) => map[o]);
return CreateResult(rootWithAnnotation, annotations, triviaList);
}
示例2: GetOuterMostParenthesizedExpressionInSpan
private bool GetOuterMostParenthesizedExpressionInSpan(SyntaxNode root, int position,
ISyntaxFactsService syntaxFacts, TextSpan currentSpan, CancellationToken cancellationToken, out ParenthesizedExpressionSyntax result)
{
result = null;
while (TryGetParenthesizedExpression(SignatureHelpTriggerReason.InvokeSignatureHelpCommand,
root, position, syntaxFacts, cancellationToken, out var expression))
{
if (!currentSpan.Contains(expression.Span))
{
break;
}
result = expression;
position = expression.SpanStart;
}
return result != null;
}
示例3: GetMembersInSpan
private static List<MemberDeclarationSyntax> GetMembersInSpan(
TextSpan textSpan,
TypeDeclarationSyntax containingType,
MemberDeclarationSyntax firstMember)
{
List<MemberDeclarationSyntax> selectedMembers = null;
var members = containingType.Members;
var fieldIndex = members.IndexOf(firstMember);
if (fieldIndex < 0)
{
return null;
}
for (var i = fieldIndex; i < members.Count; i++)
{
var member = members[i];
if (textSpan.Contains(member.Span))
{
selectedMembers = selectedMembers ?? new List<MemberDeclarationSyntax>();
selectedMembers.Add(member);
}
else if (textSpan.OverlapsWith(member.Span))
{
return null;
}
else
{
break;
}
}
return selectedMembers;
}
示例4: GetDeclaredMember
/// <summary>
/// Finds the member in the containing symbol which is inside the given declaration span.
/// </summary>
private Symbol GetDeclaredMember(NamespaceOrTypeSymbol container, TextSpan declarationSpan, string name = null)
{
if ((object)container == null)
{
return null;
}
// look for any member with same declaration location
var collection = name != null ? container.GetMembers(name) : container.GetMembersUnordered();
Symbol zeroWidthMatch = null;
foreach (var symbol in collection)
{
var namedType = symbol as ImplicitNamedTypeSymbol;
if ((object)namedType != null && namedType.IsImplicitClass)
{
// look inside wrapper around illegally placed members in namespaces
var result = GetDeclaredMember(namedType, declarationSpan, name);
if ((object)result != null)
{
return result;
}
}
foreach (var loc in symbol.Locations)
{
if (loc.IsInSource && loc.SourceTree == this.SyntaxTree && declarationSpan.Contains(loc.SourceSpan))
{
if (loc.SourceSpan.IsEmpty && loc.SourceSpan.End == declarationSpan.Start)
{
// exclude decls created via syntax recovery
zeroWidthMatch = symbol;
}
else
{
return symbol;
}
}
}
// Handle the case of the implementation of a partial method.
var partial = symbol.Kind == SymbolKind.Method
? ((MethodSymbol)symbol).PartialImplementationPart
: null;
if ((object)partial != null)
{
var loc = partial.Locations[0];
if (loc.IsInSource && loc.SourceTree == this.SyntaxTree && declarationSpan.Contains(loc.SourceSpan))
{
return partial;
}
}
}
// If we didn't find anything better than the symbol that matched because of syntax error recovery, then return that.
// Otherwise, if there's a name, try again without a name.
// Otherwise, give up.
return zeroWidthMatch ??
(name != null ? GetDeclaredMember(container, declarationSpan) : null);
}
示例5: GetSuppressionTargetInfoAsync
private async Task<SuppressionTargetInfo> GetSuppressionTargetInfoAsync(Document document, TextSpan span, CancellationToken cancellationToken)
{
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (syntaxTree.GetLineVisibility(span.Start, cancellationToken) == LineVisibility.Hidden)
{
return null;
}
// Find the start token to attach leading pragma disable warning directive.
var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var lines = syntaxTree.GetText(cancellationToken).Lines;
var indexOfLine = lines.IndexOf(span.Start);
var lineAtPos = lines[indexOfLine];
var startToken = root.FindToken(lineAtPos.Start);
startToken = GetAdjustedTokenForPragmaDisable(startToken, root, lines, indexOfLine);
// Find the end token to attach pragma restore warning directive.
var spanEnd = Math.Max(startToken.Span.End, span.End);
indexOfLine = lines.IndexOf(spanEnd);
lineAtPos = lines[indexOfLine];
var endToken = root.FindToken(lineAtPos.End);
endToken = GetAdjustedTokenForPragmaRestore(endToken, root, lines, indexOfLine);
var nodeWithTokens = GetNodeWithTokens(startToken, endToken, root);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
ISymbol targetSymbol = null;
var targetMemberNode = syntaxFacts.GetContainingMemberDeclaration(root, startToken.SpanStart);
if (targetMemberNode != null)
{
targetSymbol = semanticModel.GetDeclaredSymbol(targetMemberNode, cancellationToken);
if (targetSymbol == null)
{
var analyzerDriverService = document.GetLanguageService<IAnalyzerDriverService>();
// targetMemberNode could be a declaration node with multiple decls (e.g. field declaration defining multiple variables).
// Let us compute all the declarations intersecting the span.
var decls = new List<DeclarationInfo>();
analyzerDriverService.ComputeDeclarationsInSpan(semanticModel, span, true, decls, cancellationToken);
if (decls.Any())
{
var containedDecls = decls.Where(d => span.Contains(d.DeclaredNode.Span));
if (containedDecls.Count() == 1)
{
// Single containing declaration, use this symbol.
var decl = containedDecls.Single();
targetSymbol = decl.DeclaredSymbol;
}
else
{
// Otherwise, use the most enclosing declaration.
TextSpan? minContainingSpan = null;
foreach (var decl in decls)
{
var declSpan = decl.DeclaredNode.Span;
if (declSpan.Contains(span) &&
(!minContainingSpan.HasValue || minContainingSpan.Value.Contains(declSpan)))
{
minContainingSpan = declSpan;
targetSymbol = decl.DeclaredSymbol;
}
}
}
}
}
}
if (targetSymbol == null)
{
// Outside of a member declaration, suppress diagnostic for the entire assembly.
targetSymbol = semanticModel.Compilation.Assembly;
}
return new SuppressionTargetInfo() { TargetSymbol = targetSymbol, NodeWithTokens = nodeWithTokens, StartToken = startToken, EndToken = endToken };
}
示例6: TextSpanContains01
public void TextSpanContains01()
{
TextSpan span_05_15 = new TextSpan(5, 10);
TextSpan span_03_10 = new TextSpan(3, 7);
TextSpan span_10_11 = new TextSpan(10, 1);
TextSpan span_00_03 = new TextSpan(0, 3);
// nonoverlapping
Assert.False(span_05_15.Contains(span_00_03));
Assert.False(span_00_03.Contains(span_05_15));
// overlap with slop
Assert.True(span_05_15.Contains(span_10_11));
// same span
Assert.True(span_05_15.Contains(span_05_15));
// partial overlap
Assert.False(span_05_15.Contains(span_03_10));
Assert.False(span_03_10.Contains(span_05_15));
}
示例7: TextSpanContains00
public void TextSpanContains00()
{
TextSpan span = new TextSpan(0, 10);
Assert.True(span.Contains(3));
Assert.False(span.Contains(30));
Assert.False(span.Contains(11));
Assert.False(span.Contains(-1));
}
示例8: GetSuppressionsAsync
public async Task<IEnumerable<CodeFix>> GetSuppressionsAsync(Document document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var suppressableDiagnostics = diagnostics.Where(CanBeSuppressed);
if (suppressableDiagnostics.IsEmpty())
{
return null;
}
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (syntaxTree.GetLineVisibility(span.Start, cancellationToken) == LineVisibility.Hidden)
{
return null;
}
// Find the start token to attach leading pragma disable warning directive.
var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxTrivia containingTrivia = root.FindTrivia(span.Start);
var lines = syntaxTree.GetText(cancellationToken).Lines;
int indexOfLine;
if (containingTrivia == default(SyntaxTrivia))
{
indexOfLine = lines.IndexOf(span.Start);
}
else
{
indexOfLine = lines.IndexOf(containingTrivia.Token.SpanStart);
}
var lineAtPos = lines[indexOfLine];
var startToken = root.FindToken(lineAtPos.Start);
startToken = GetAdjustedTokenForPragmaDisable(startToken, root, lines, indexOfLine);
// Find the end token to attach pragma restore warning directive.
// This should be the last token on the line that contains the start token.
indexOfLine = lines.IndexOf(startToken.Span.End);
lineAtPos = lines[indexOfLine];
var endToken = root.FindToken(lineAtPos.End);
endToken = GetAdjustedTokenForPragmaRestore(endToken, root, lines, indexOfLine);
SyntaxNode nodeWithTokens = null;
if (IsEndOfFileToken(endToken))
{
nodeWithTokens = root;
}
else
{
nodeWithTokens = startToken.GetCommonRoot(endToken);
}
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
ISymbol targetSymbol = null;
SyntaxNode targetMemberNode = null;
var suppressMessageAttribute = semanticModel.Compilation.SuppressMessageAttributeType();
bool skipSuppressMessage = suppressMessageAttribute == null || !suppressMessageAttribute.IsAttribute();
if (!skipSuppressMessage)
{
targetMemberNode = syntaxFacts.GetContainingMemberDeclaration(root, startToken.SpanStart);
if (targetMemberNode != null)
{
targetSymbol = semanticModel.GetDeclaredSymbol(targetMemberNode, cancellationToken);
if (targetSymbol == null)
{
var analyzerDriverService = document.GetLanguageService<IAnalyzerDriverService>();
// targetMemberNode could be a declaration node with multiple decls (e.g. field declaration defining multiple variables).
// Let us compute all the declarations intersecting the span.
var decls = analyzerDriverService.GetDeclarationsInSpan(semanticModel, span, true, cancellationToken);
if (decls.Any())
{
var containedDecls = decls.Where(d => span.Contains(d.DeclaredNode.Span));
if (containedDecls.Count() == 1)
{
// Single containing declaration, use this symbol.
var decl = containedDecls.Single();
targetSymbol = decl.DeclaredSymbol;
}
else
{
// Otherwise, use the most enclosing declaration.
TextSpan? minContainingSpan = null;
foreach (var decl in decls)
{
var declSpan = decl.DeclaredNode.Span;
if (declSpan.Contains(span) &&
(!minContainingSpan.HasValue || minContainingSpan.Value.Contains(declSpan)))
{
minContainingSpan = declSpan;
targetSymbol = decl.DeclaredSymbol;
}
}
}
}
}
}
if (targetSymbol == null)
{
//.........这里部分代码省略.........
示例9: TextSpan_DoesNotContainEnd
public void TextSpan_DoesNotContainEnd()
{
var textSpan = new TextSpan(SourceText.From(""), 1, 2);
Assert.False(textSpan.Contains(textSpan.End));
}
示例10: TextSpan_ContainsItself
public void TextSpan_ContainsItself()
{
var textSpan = new TextSpan(SourceText.From(""), 1, 2);
Assert.True(textSpan.Contains(textSpan));
}
示例11: AdjustIndentationForSpan
private void AdjustIndentationForSpan(
Document document, ITextEdit edit, TextSpan visibleSpan, IFormattingRule baseIndentationRule, OptionSet options)
{
var root = document.GetSyntaxRootAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
using (var rulePool = SharedPools.Default<List<IFormattingRule>>().GetPooledObject())
using (var spanPool = SharedPools.Default<List<TextSpan>>().GetPooledObject())
{
var venusFormattingRules = rulePool.Object;
var visibleSpans = spanPool.Object;
venusFormattingRules.Add(baseIndentationRule);
venusFormattingRules.Add(ContainedDocumentPreserveFormattingRule.Instance);
var formattingRules = venusFormattingRules.Concat(Formatter.GetDefaultFormattingRules(document));
var workspace = document.Project.Solution.Workspace;
var changes = Formatter.GetFormattedTextChanges(
root, new TextSpan[] { CommonFormattingHelpers.GetFormattingSpan(root, visibleSpan) },
workspace, options, formattingRules, CancellationToken.None);
visibleSpans.Add(visibleSpan);
var newChanges = FilterTextChanges(document.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None), visibleSpans, changes.ToReadOnlyCollection()).Where(t => visibleSpan.Contains(t.Span));
foreach (var change in newChanges)
{
edit.Replace(change.Span.ToSpan(), change.NewText);
}
}
}
示例12: TryGetSubTextChange
private bool TryGetSubTextChange(
SourceText originalText, TextSpan visibleSpanInOriginalText,
string rightText, TextSpan spanInOriginalText, TextSpan spanInRightText, out TextChange textChange)
{
textChange = default(TextChange);
var visibleFirstLineInOriginalText = originalText.Lines.GetLineFromPosition(visibleSpanInOriginalText.Start);
var visibleLastLineInOriginalText = originalText.Lines.GetLineFromPosition(visibleSpanInOriginalText.End);
// skip easy case
// 1. things are out of visible span
if (!visibleSpanInOriginalText.IntersectsWith(spanInOriginalText))
{
return false;
}
// 2. there are no intersects
var snippetInRightText = rightText.Substring(spanInRightText.Start, spanInRightText.Length);
if (visibleSpanInOriginalText.Contains(spanInOriginalText) && visibleSpanInOriginalText.End != spanInOriginalText.End)
{
textChange = new TextChange(spanInOriginalText, snippetInRightText);
return true;
}
// okay, more complex case. things are intersecting boundaries.
var firstLineOfRightTextSnippet = snippetInRightText.GetFirstLineText();
var lastLineOfRightTextSnippet = snippetInRightText.GetLastLineText();
// there are 4 complex cases - these are all heuristic. not sure what better way I have. and the heristic is heavily based on
// text differ's behavior.
// 1. it is a single line
if (visibleFirstLineInOriginalText.LineNumber == visibleLastLineInOriginalText.LineNumber)
{
// don't do anything
return false;
}
// 2. replacement contains visible spans
if (spanInOriginalText.Contains(visibleSpanInOriginalText))
{
// header
// don't do anything
// body
textChange = new TextChange(
TextSpan.FromBounds(visibleFirstLineInOriginalText.EndIncludingLineBreak, visibleLastLineInOriginalText.Start),
snippetInRightText.Substring(firstLineOfRightTextSnippet.Length, snippetInRightText.Length - firstLineOfRightTextSnippet.Length - lastLineOfRightTextSnippet.Length));
// footer
// don't do anything
return true;
}
// 3. replacement intersects with start
if (spanInOriginalText.Start < visibleSpanInOriginalText.Start &&
visibleSpanInOriginalText.Start <= spanInOriginalText.End &&
spanInOriginalText.End < visibleSpanInOriginalText.End)
{
// header
// don't do anything
// body
if (visibleFirstLineInOriginalText.EndIncludingLineBreak <= spanInOriginalText.End)
{
textChange = new TextChange(
TextSpan.FromBounds(visibleFirstLineInOriginalText.EndIncludingLineBreak, spanInOriginalText.End),
snippetInRightText.Substring(firstLineOfRightTextSnippet.Length));
return true;
}
return false;
}
// 4. replacement intersects with end
if (visibleSpanInOriginalText.Start < spanInOriginalText.Start &&
spanInOriginalText.Start <= visibleSpanInOriginalText.End &&
visibleSpanInOriginalText.End <= spanInOriginalText.End)
{
// body
if (spanInOriginalText.Start <= visibleLastLineInOriginalText.Start)
{
textChange = new TextChange(
TextSpan.FromBounds(spanInOriginalText.Start, visibleLastLineInOriginalText.Start),
snippetInRightText.Substring(0, snippetInRightText.Length - lastLineOfRightTextSnippet.Length));
return true;
}
// footer
// don't do anything
return false;
}
// if it got hit, then it means there is a missing case
throw ExceptionUtilities.Unreachable;
}
示例13: GetNodes
protected virtual IEnumerable<SyntaxNode> GetNodes(SyntaxNode root, TextSpan span)
{
IEnumerable<SyntaxNode> nodes;
nodes = root.FindToken(span.Start, findInsideTrivia: true).GetAncestors<SyntaxNode>().Where(a => span.Contains(a.Span)).Reverse();
return nodes;
}
示例14: TextSpan_DoesNotContainEnd
public void TextSpan_DoesNotContainEnd()
{
var textSpan = new TextSpan(null, 1, 2);
Assert.False(textSpan.Contains(textSpan.End));
}
示例15: TextSpan_ContainsStart
public void TextSpan_ContainsStart()
{
var textSpan = new TextSpan(null, 1, 2);
Assert.True(textSpan.Contains(textSpan.Start));
}