// Internal for testing
        internal void OnDocumentStructureChanged(object state)
        {
            _dispatcher.AssertForegroundThread();

            if (_disposed)
            {
                return;
            }

            var args = (DocumentStructureChangedEventArgs)state;

            if (_latestChangeReference == null || // extra hardening
                !_latestChangeReference.IsAssociatedWith(args) ||
                args.Snapshot != TextBuffer.CurrentSnapshot)
            {
                // In the middle of parsing a newer change or about to parse a newer change.
                return;
            }

            _latestChangeReference = null;
            _codeDocument          = args.CodeDocument;
            _snapshot      = args.Snapshot;
            _partialParser = new RazorSyntaxTreePartialParser(CodeDocument.GetSyntaxTree());

            DocumentStructureChanged?.Invoke(this, args);
        }
        // Internal for testing
        internal void OnDocumentStructureChanged(object state)
        {
            _dispatcher.AssertForegroundThread();

            if (_disposed)
            {
                return;
            }

            var backgroundParserArgs = (BackgroundParserResultsReadyEventArgs)state;

            if (_latestChangeReference == null || // extra hardening
                _latestChangeReference != backgroundParserArgs.ChangeReference ||
                backgroundParserArgs.ChangeReference.Snapshot != TextBuffer.CurrentSnapshot)
            {
                // In the middle of parsing a newer change or about to parse a newer change.
                return;
            }

            _latestChangeReference = null;
            _codeDocument          = backgroundParserArgs.CodeDocument;
            _snapshot      = backgroundParserArgs.ChangeReference.Snapshot;
            _partialParser = new RazorSyntaxTreePartialParser(CodeDocument.GetSyntaxTree());

            var documentStructureChangedArgs = new DocumentStructureChangedEventArgs(
                backgroundParserArgs.ChangeReference.Change,
                backgroundParserArgs.ChangeReference.Snapshot,
                backgroundParserArgs.CodeDocument);

            DocumentStructureChanged?.Invoke(this, documentStructureChangedArgs);
        }
        public override Task <RazorSyntaxTree> GetLatestSyntaxTreeAsync(ITextSnapshot atOrNewerSnapshot, CancellationToken cancellationToken = default)
        {
            if (atOrNewerSnapshot == null)
            {
                throw new ArgumentNullException(nameof(atOrNewerSnapshot));
            }

            lock (UpdateStateLock)
            {
                if (_disposed ||
                    (_latestParsedSnapshot != null && atOrNewerSnapshot.Version.VersionNumber <= _latestParsedSnapshot.Version.VersionNumber))
                {
                    return(Task.FromResult(CodeDocument?.GetSyntaxTree()));
                }

                SyntaxTreeRequest request = null;
                for (var i = _syntaxTreeRequests.Count - 1; i >= 0; i--)
                {
                    if (_syntaxTreeRequests[i].Snapshot == atOrNewerSnapshot)
                    {
                        request = _syntaxTreeRequests[i];
                        break;
                    }
                }

                if (request == null)
                {
                    request = new SyntaxTreeRequest(atOrNewerSnapshot, cancellationToken);
                    _syntaxTreeRequests.Add(request);
                }

                return(request.Task);
            }
        }
Beispiel #4
0
        private IReadOnlyList <FormattingSpan> GetFormattingSpans()
        {
            if (_formattingSpans is null)
            {
                var syntaxTree = CodeDocument.GetSyntaxTree();
                _formattingSpans = syntaxTree.GetFormattingSpans();
            }

            return(_formattingSpans);
        }
        public bool TryGetIndentationLevel(int position, out int indentationLevel)
        {
            var syntaxTree      = CodeDocument.GetSyntaxTree();
            var formattingSpans = syntaxTree.GetFormattingSpans();

            if (TryGetFormattingSpan(position, formattingSpans, out var span))
            {
                indentationLevel = span.IndentationLevel;
                return(true);
            }

            indentationLevel = 0;
            return(false);
        }
        private void OnResultsReady(object sender, DocumentStructureChangedEventArgs args)
        {
            _dispatcher.AssertBackgroundThread();

            if (DocumentStructureChanged != null)
            {
                if (args.Snapshot != _textBuffer.CurrentSnapshot)
                {
                    // A different text change is being parsed.
                    return;
                }

                CodeDocument   = args.CodeDocument;
                Snapshot       = args.Snapshot;
                _partialParser = new RazorSyntaxTreePartialParser(CodeDocument.GetSyntaxTree());
                DocumentStructureChanged(this, args);
            }
        }
        // Internal for testing
        internal void OnDocumentStructureChanged(object state)
        {
            _dispatcher.AssertForegroundThread();

            if (_disposed)
            {
                return;
            }

            var backgroundParserArgs = (BackgroundParserResultsReadyEventArgs)state;

            if (_latestChangeReference == null || // extra hardening
                _latestChangeReference != backgroundParserArgs.ChangeReference)
            {
                // In the middle of parsing a newer change or about to parse a newer change.
                return;
            }

            if (backgroundParserArgs.ChangeReference.Snapshot != TextBuffer.CurrentSnapshot)
            {
                // Changes have impacted the snapshot after our we recorded our last change reference.
                // This can happen for a multitude of reasons, usually because of a user auto-completing
                // C# statements (causes multiple edits in quick succession). This ensures that our latest
                // parse corresponds to the current snapshot.
                QueueReparse();
                return;
            }

            _latestChangeReference = null;
            _codeDocument          = backgroundParserArgs.CodeDocument;
            _snapshot      = backgroundParserArgs.ChangeReference.Snapshot;
            _partialParser = new RazorSyntaxTreePartialParser(CodeDocument.GetSyntaxTree());

            var documentStructureChangedArgs = new DocumentStructureChangedEventArgs(
                backgroundParserArgs.ChangeReference.Change,
                backgroundParserArgs.ChangeReference.Snapshot,
                backgroundParserArgs.CodeDocument);

            DocumentStructureChanged?.Invoke(this, documentStructureChangedArgs);
        }