public bool CanNavigateToSpan(Workspace workspace, DocumentId documentId, SourceFileSpan sourceFileSpan) { if (!IsSecondaryBuffer(workspace, documentId)) { return(true); } if (!sourceFileSpan.IsInRootFile) { return(true); } var document = workspace.CurrentDocuments.GetDocument(documentId); var text = document.SourceText; var textSpan = sourceFileSpan.Span; var boundedTextSpan = GetSpanWithinDocumentBounds(textSpan, text.Length); if (boundedTextSpan != textSpan) { try { throw new ArgumentOutOfRangeException(); } catch (ArgumentOutOfRangeException e) when(FatalError.ReportWithoutCrash(e)) { } return(false); } var vsTextSpan = text.GetVsTextSpanForSpan(textSpan); return(CanMapFromSecondaryBufferToPrimaryBuffer(workspace, documentId, vsTextSpan)); }
private SyntaxToken(SyntaxKind kind, SyntaxKind contextualKind, SyntaxTokenSource source, SourceRange sourceRange, SourceFileSpan fileSpan, string text, object value, IEnumerable <SyntaxNode> leadingTrivia, IEnumerable <SyntaxNode> trailingTrivia, IEnumerable <Diagnostic> diagnostics) : base(kind, diagnostics) { ContextualKind = contextualKind; Source = source; Text = text; FileSpan = fileSpan; SourceRange = sourceRange; Value = value; LeadingTrivia = leadingTrivia.ToImmutableArray(); foreach (var triviaNode in LeadingTrivia) { ((SyntaxNodeBase)triviaNode).Parent = this; } TrailingTrivia = trailingTrivia.ToImmutableArray(); foreach (var triviaNode in TrailingTrivia) { ((SyntaxNodeBase)triviaNode).Parent = this; } FullSourceRange = ComputeFullSpan(sourceRange, LeadingTrivia, TrailingTrivia); ContainsDiagnostics = Diagnostics.Any() || LeadingTrivia.Any(x => x.ContainsDiagnostics) || TrailingTrivia.Any(x => x.ContainsDiagnostics); }
public MappedDiagnostic(Diagnostic diagnostic, DiagnosticSource source, SourceFileSpan fileSpan, LogicalDocument logicalDocument) { Diagnostic = diagnostic; FileSpan = fileSpan; Source = source; LogicalDocument = logicalDocument; }
private static BraceMatchingResult?MapResultToFile(SourceFileSpan left, SourceFileSpan right) { if (!left.File.IsRootFile || !right.File.IsRootFile) { return(null); } return(new BraceMatchingResult(left.Span, right.Span)); }
public SymbolSpan(SymbolSpanKind kind, ISymbol symbol, SourceRange sourceRange, SourceFileSpan span) { if (symbol == null) { throw new ArgumentNullException(nameof(symbol)); } Kind = kind; Symbol = symbol; SourceRange = sourceRange; Span = span; }
protected static SignatureHelpItems CreateSignatureHelpItems( IList <SignatureHelpItem> items, SourceFileSpan applicableSpan, SignatureHelpState state, int?selectedItem) { if (items == null || !items.Any() || state == null) { return(null); } if (!applicableSpan.IsInRootFile) { return(null); } items = Filter(items, state.ArgumentNames); return(new SignatureHelpItems(items, applicableSpan.Span, state.ArgumentIndex, state.ArgumentCount, state.ArgumentName, selectedItem)); }
protected SyntaxToken InsertMissingToken(SyntaxKind kind) { var missingTokenSourceRange = new SourceRange(Current.FullSourceRange.Start, 0); var missingTokenSpan = new SourceFileSpan(Current.FileSpan.File, new TextSpan(Current.FileSpan.Span.Start, 0)); var leadingLocatedTrivia = Current.LeadingTrivia.OfType <LocatedNode>().FirstOrDefault(); if (leadingLocatedTrivia != null) { missingTokenSpan = new SourceFileSpan(leadingLocatedTrivia.FileSpan.File, new TextSpan(leadingLocatedTrivia.FileSpan.Span.Start, 0)); } var diagnosticSpan = GetDiagnosticSourceRangeForMissingToken(); var diagnostics = new List <Diagnostic>(1); diagnostics.ReportTokenExpected(diagnosticSpan, Current, kind); return(new SyntaxToken(kind, true, missingTokenSourceRange, missingTokenSpan).WithDiagnostics(diagnostics)); }
internal SyntaxToken(SyntaxKind kind, SyntaxKind contextualKind, bool isMissing, SourceRange sourceRange, SourceFileSpan span, string text, object value, IEnumerable <SyntaxNode> leadingTrivia, IEnumerable <SyntaxNode> trailingTrivia, IEnumerable <Diagnostic> diagnostics, MacroReference macroReference, bool isFirstTokenInMacroExpansion) : base(kind, text, span, diagnostics) { ContextualKind = contextualKind; IsMissing = isMissing; SourceRange = sourceRange; Value = value; LeadingTrivia = leadingTrivia.ToImmutableArray(); foreach (var triviaNode in LeadingTrivia) { ((SyntaxNodeBase)triviaNode).Parent = this; } TrailingTrivia = trailingTrivia.ToImmutableArray(); foreach (var triviaNode in TrailingTrivia) { ((SyntaxNodeBase)triviaNode).Parent = this; } FullSourceRange = ComputeFullSpan(sourceRange, LeadingTrivia, TrailingTrivia); ContainsDiagnostics = Diagnostics.Any() || LeadingTrivia.Any(x => x.ContainsDiagnostics) || TrailingTrivia.Any(x => x.ContainsDiagnostics); ContainsDirectives = LeadingTrivia.OfType <DirectiveTriviaSyntax>().Any() || TrailingTrivia.OfType <DirectiveTriviaSyntax>().Any(); MacroReference = macroReference; IsFirstTokenInMacroExpansion = isFirstTokenInMacroExpansion; }
/// <summary> /// Computes a span for a given field symbol, expanding to the outer /// </summary> private static TextSpan GetVariableDeclarationSpan(SyntaxTreeBase tree, SyntaxNode node, SourceFileSpan nodeFileSpan) { int spanStart = nodeFileSpan.Span.Start; int spanEnd = nodeFileSpan.Span.End; var fieldDeclaration = node.GetAncestor <VariableDeclarationStatementSyntax>(); if (fieldDeclaration != null) { var variables = fieldDeclaration.Declaration.Variables; var fieldDeclarationFileSpan = tree.GetSourceFileSpan(fieldDeclaration.SourceRange); if (fieldDeclarationFileSpan.IsInRootFile) { if (variables.FirstOrDefault() == node) { spanStart = fieldDeclarationFileSpan.Span.Start; } if (variables.LastOrDefault() == node) { spanEnd = fieldDeclarationFileSpan.Span.End; } } } return(TextSpan.FromBounds(spanStart, spanEnd)); }
private static bool FindMatchingBrace(SourceLocation position, int direction, SyntaxNode parent, SyntaxKind syntaxKind, out SourceFileSpan right) { var tokens = parent.ChildNodes.Where(t => t.IsKind(syntaxKind)); var relevantTokens = (direction < 0) ? from t in tokens where t.SourceRange.End <= position select t : from t in tokens where position < t.SourceRange.Start select t; right = default(SourceFileSpan); var found = false; foreach (var token in relevantTokens.Cast <SyntaxToken>()) { if (!found) { right = token.FileSpan; found = true; } else { return(false); } } return(found); }
public static INavigableItem GetItemFromDeclaredSymbol(ISymbol declaredSymbol, LogicalDocument document, SourceFileSpan sourceFileSpan) { return(new DeclaredSymbolNavigableItem(document, declaredSymbol, sourceFileSpan)); }
public DeclaredSymbolNavigableItem(Document document, ISymbol symbol, SourceFileSpan sourceSpan) { Document = document; _declaredSymbol = symbol; SourceSpan = sourceSpan; }
internal SyntaxToken(SyntaxKind kind, bool isMissing, SourceRange sourceRange, SourceFileSpan span) : this(kind, SyntaxKind.BadToken, isMissing, sourceRange, span, string.Empty, null, Enumerable.Empty <SyntaxNode>(), Enumerable.Empty <SyntaxNode>(), Enumerable.Empty <Diagnostic>(), null, false) { }
public SyntaxToken WithSpan(SourceRange sourceRange, SourceFileSpan span) { return(new SyntaxToken(Kind, ContextualKind, IsMissing, sourceRange, span, Text, Value, LeadingTrivia, TrailingTrivia, Diagnostics, MacroReference, IsFirstTokenInMacroExpansion)); }
protected LocatedNode(SyntaxKind kind, string text, SourceFileSpan span, IEnumerable <Diagnostic> diagnostics) : base(kind, diagnostics) { Text = text; FileSpan = span; }
private SyntaxToken LexDirectiveToken() { _kind = SyntaxKind.BadToken; _contextualKind = SyntaxKind.BadToken; _diagnostics.Clear(); _start = _charReader.Position; var trailingTrivia = new List <SyntaxNode>(); var isEndOfLine = false; switch (_charReader.Current) { case '#': NextChar(); if (_charReader.Current == '#') { NextChar(); _kind = SyntaxKind.HashHashToken; } else { _kind = SyntaxKind.HashToken; _currentDirectiveKind = null; } break; case '\r': case '\n': _kind = SyntaxKind.EndOfDirectiveToken; _currentDirectiveKind = null; isEndOfLine = true; break; case '\0': _kind = SyntaxKind.EndOfDirectiveToken; _currentDirectiveKind = null; break; case '<': if (_currentDirectiveKind != SyntaxKind.IncludeKeyword) { goto default; } ReadBracketedString(); break; default: ReadToken(); if (_contextualKind.IsPreprocessorDirective()) { _currentDirectiveKind = _contextualKind; } break; } var end = _charReader.Position; var kind = _kind; var span = TextSpan.FromBounds(_start, end); var text = File.Text.GetSubText(span).ToString(); var fileSpan = new SourceFileSpan(File, span); var diagnostics = _diagnostics.ToImmutableArray(); LexDirectiveTrailingTrivia(trailingTrivia, kind, isEndOfLine); var token = new SyntaxToken(kind, _contextualKind, false, MakeAbsolute(span), fileSpan, text, _value, ImmutableArray <SyntaxNode> .Empty, trailingTrivia.ToImmutableArray(), diagnostics, null, false); return(token); }
internal static SyntaxToken CreateMissing(SyntaxKind kind, SourceRange sourceRange, SourceFileSpan fileSpan) { return(new SyntaxToken( kind, SyntaxKind.BadToken, SyntaxTokenSource.MissingToken, sourceRange, fileSpan, string.Empty, null, Enumerable.Empty <SyntaxNode>(), Enumerable.Empty <SyntaxNode>(), Enumerable.Empty <Diagnostic>())); }
public MappedDiagnostic(Diagnostic diagnostic, DiagnosticSource source, SourceFileSpan fileSpan) { Diagnostic = diagnostic; FileSpan = fileSpan; Source = source; }
public DocumentSpan(Document document, SourceFileSpan sourceSpan) { Document = document; SourceSpan = sourceSpan; }
public static SymbolSpan CreateDefinition(ISymbol symbol, SourceRange sourceRange, SourceFileSpan span) { return(new SymbolSpan(SymbolSpanKind.Definition, symbol, sourceRange, span)); }
public static SymbolSpan CreateReference(ISymbol symbol, SourceRange sourceRange, SourceFileSpan span) { return(new SymbolSpan(SymbolSpanKind.Reference, symbol, sourceRange, span)); }
public bool TryNavigateToSpan(Workspace workspace, DocumentId documentId, SourceFileSpan sourceFileSpan, OptionSet options) { if (!IsForeground()) { throw new InvalidOperationException(LanguageServicesResources.Navigation_must_be_performed_on_the_foreground_thread); } var document = OpenDocument(workspace, documentId, options); if (document == null) { return(false); } var text = sourceFileSpan.File.Text; ITextBuffer textBuffer; if (document.SourceText != text) { uint itemID; IVsUIHierarchy hierarchy; IVsWindowFrame docFrame; IVsTextView textView; try { VsShellUtilities.OpenDocument( _serviceProvider, text.FilePath, VSConstants.LOGVIEWID_Code, out hierarchy, out itemID, out docFrame, out textView); } catch { // File might not exist, etc. return(false); } if (ErrorHandler.Failed(docFrame.Show())) { return(false); } if (ErrorHandler.Failed(textView.GetBuffer(out var vsTextBuffer))) { return(false); } textBuffer = _editorAdaptersFactoryService.GetDocumentBuffer(vsTextBuffer); } else { textBuffer = text.Container.GetTextBuffer(); } var textSpan = sourceFileSpan.Span; var boundedTextSpan = GetSpanWithinDocumentBounds(textSpan, text.Length); if (boundedTextSpan != textSpan) { try { throw new ArgumentOutOfRangeException(); } catch (ArgumentOutOfRangeException e) when(FatalError.ReportWithoutCrash(e)) { } } var vsTextSpan = text.GetVsTextSpanForSpan(boundedTextSpan); if (IsSecondaryBuffer(workspace, documentId) && !vsTextSpan.TryMapSpanFromSecondaryBufferToPrimaryBuffer(workspace, documentId, out vsTextSpan)) { return(false); } return(NavigateTo(textBuffer, vsTextSpan)); }
internal SyntaxTrivia(SyntaxKind kind, string text, SourceRange sourceRange, SourceFileSpan span, ImmutableArray <Diagnostic> diagnostics) : base(kind, text, span, diagnostics) { SourceRange = sourceRange; FullSourceRange = sourceRange; }