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);
        }
예제 #3
0
 public MappedDiagnostic(Diagnostic diagnostic, DiagnosticSource source, SourceFileSpan fileSpan, LogicalDocument logicalDocument)
 {
     Diagnostic      = diagnostic;
     FileSpan        = fileSpan;
     Source          = source;
     LogicalDocument = logicalDocument;
 }
예제 #4
0
        private static BraceMatchingResult?MapResultToFile(SourceFileSpan left, SourceFileSpan right)
        {
            if (!left.File.IsRootFile || !right.File.IsRootFile)
            {
                return(null);
            }

            return(new BraceMatchingResult(left.Span, right.Span));
        }
예제 #5
0
        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));
        }
예제 #7
0
        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));
        }
예제 #8
0
        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;
        }
예제 #9
0
        /// <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));
        }
예제 #10
0
        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));
 }
예제 #12
0
 public DeclaredSymbolNavigableItem(Document document, ISymbol symbol, SourceFileSpan sourceSpan)
 {
     Document        = document;
     _declaredSymbol = symbol;
     SourceSpan      = sourceSpan;
 }
예제 #13
0
 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)
 {
 }
예제 #14
0
 public SyntaxToken WithSpan(SourceRange sourceRange, SourceFileSpan span)
 {
     return(new SyntaxToken(Kind, ContextualKind, IsMissing, sourceRange, span, Text, Value, LeadingTrivia, TrailingTrivia, Diagnostics, MacroReference, IsFirstTokenInMacroExpansion));
 }
예제 #15
0
 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);
        }
예제 #17
0
 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>()));
 }
예제 #18
0
 public MappedDiagnostic(Diagnostic diagnostic, DiagnosticSource source, SourceFileSpan fileSpan)
 {
     Diagnostic = diagnostic;
     FileSpan   = fileSpan;
     Source     = source;
 }
예제 #19
0
 public DocumentSpan(Document document, SourceFileSpan sourceSpan)
 {
     Document   = document;
     SourceSpan = sourceSpan;
 }
예제 #20
0
 public static SymbolSpan CreateDefinition(ISymbol symbol, SourceRange sourceRange, SourceFileSpan span)
 {
     return(new SymbolSpan(SymbolSpanKind.Definition, symbol, sourceRange, span));
 }
예제 #21
0
 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;
 }