Ejemplo n.º 1
0
        private IRIntellisenseContext CreateContext(Position position)
        {
            var bufferPosition = EditorBuffer.ToStreamPosition(position);
            var session        = new EditorIntellisenseSession(new EditorView(EditorBuffer, position.ToStreamPosition(EditorBuffer.CurrentSnapshot)), _services);

            return(new RIntellisenseContext(session, EditorBuffer, Document.EditorTree, bufferPosition));
        }
Ejemplo n.º 2
0
        public async Task ProcessChangesAsync(TextDocumentContentChangedEvent[] contentChanges)
        {
            await _services.MainThread().SwitchToAsync();

            foreach (var change in contentChanges)
            {
                if (change.range == null)
                {
                    continue;
                }

                var position = EditorBuffer.ToStreamPosition(change.Range.Start);
                var range    = new TextRange(position, change.rangeLength);
                if (!string.IsNullOrEmpty(change.text))
                {
                    // Insert or replace
                    if (change.rangeLength == 0)
                    {
                        EditorBuffer.Insert(position, change.Text);
                    }
                    else
                    {
                        EditorBuffer.Replace(range, change.Text);
                    }
                }
                else
                {
                    EditorBuffer.Delete(range);
                }
            }
        }
Ejemplo n.º 3
0
        public void Construction(string content, int[] expectedStarts, int[] expectedLengths, int[] expectedLineBreakLengths)
        {
            var b = new EditorBuffer(content, "R");
            var s = new EditorBufferSnapshot(b, content, 2);

            s.Version.Should().Be(2);
            s.GetText().Should().Be(content);
            s.LineCount.Should().Be(expectedStarts.Length);

            for (var i = 0; i < s.LineCount; i++)
            {
                var lineFromNumber = s.GetLineFromLineNumber(i);
                lineFromNumber.LineNumber.Should().Be(i);

                var lineStart = expectedStarts[i];
                var lineEnd   = expectedStarts[i] + expectedLengths[i];

                lineFromNumber.Start.Should().Be(lineStart);
                lineFromNumber.End.Should().Be(lineEnd);
                lineFromNumber.Length.Should().Be(lineEnd - lineStart);
                lineFromNumber.LineBreak.Length.Should().Be(expectedLineBreakLengths[i]);

                for (var j = lineStart; j < lineEnd; j++)
                {
                    var lineFromPosition = s.GetLineFromPosition(j);
                    lineFromPosition.Should().Be(lineFromNumber);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Called multiple times as subject buffers get connected and disconnected in the buffer graph
        /// </summary>
        public void SubjectBuffersConnected(IWpfTextView textView, ConnectionReason reason, Collection <ITextBuffer> subjectBuffers)
        {
            EditorView.Create(textView);

            foreach (var textBuffer in subjectBuffers)
            {
                TextViewData viewData;
                var          newBuffer = !TextBufferToViewData.ContainsKey(textBuffer);
                if (newBuffer)
                {
                    viewData = new TextViewData();
                    TextBufferToViewData[textBuffer] = viewData;
                }
                else
                {
                    viewData = TextBufferToViewData[textBuffer];
                }

                viewData.AddView(textView);
                if (newBuffer)
                {
                    EditorBuffer.Create(textBuffer, _tdfs);
                    OnTextBufferCreated(textView, textBuffer);
                }

                OnTextViewConnected(textView, textBuffer);
            }
        }
Ejemplo n.º 5
0
        public TextBufferMock(string content, string contentTypeName)
        {
            ContentType = new ContentTypeMock(contentTypeName, new IContentType[] { ContentTypeMock.TextContentType });
            TextVersionMock initialVersion = new TextVersionMock(this, 0, content.Length);

            CurrentSnapshot = new TextSnapshotMock(content, this, initialVersion);
            EditorBuffer.Create(this, null);
        }
Ejemplo n.º 6
0
        public void InvalidLines(string content, int position)
        {
            var b = new EditorBuffer(content, "R");
            var s = new EditorBufferSnapshot(b, content, 1);

            Action a = () => s.GetLineFromPosition(position);

            a.ShouldThrow <ArgumentOutOfRangeException>();
        }
Ejemplo n.º 7
0
        public void Lines(string content, int position, int expectedLineNumber, int expectedCharIndex)
        {
            var b = new EditorBuffer(content, "R");
            var s = new EditorBufferSnapshot(b, content, 2);

            var line = s.GetLineFromPosition(position);

            line.LineNumber.Should().Be(expectedLineNumber);
            (position - line.Start).Should().Be(expectedCharIndex);
        }
Ejemplo n.º 8
0
        public DocumentEntry(string content, Uri uri, IServiceContainer services)
        {
            _services = services;

            EditorBuffer = new EditorBuffer(content, "R");
            Document     = new REditorDocument(EditorBuffer, services, false);

            _completionManager    = new CompletionManager(services);
            _signatureManager     = new SignatureManager(services);
            _diagnosticsPublisher = new DiagnosticsPublisher(Document, uri, services);
            _formatter            = new CodeFormatter(_services);
            _symbolsProvider      = new DocumentSymbolsProvider();
        }
Ejemplo n.º 9
0
        public void Insert(string content, int position, string insert)
        {
            var b = new EditorBuffer(content, "R");

            b.Insert(position, insert);
            var s = b.CurrentSnapshot;

            s.Version.Should().Be(1);

            var expected = content.Substring(0, position) + insert + content.Substring(position);

            s.GetText().Should().Be(expected);
        }
Ejemplo n.º 10
0
        protected override bool CanFormatLine(int position, int lineOffset)
        {
            // Do not format inside strings. At this point AST may be empty due to the nature
            // of [destructive] changes made to the document. We have to resort to tokenizer.
            // In order to keep performance good during typing we'll use token stream from the classifier.
            var snapshot   = EditorBuffer.CurrentSnapshot;
            var lineNumber = snapshot.GetLineNumberFromPosition(position);
            var line       = snapshot.GetLineFromLineNumber(lineNumber + lineOffset);

            var classifier = RClassifierProvider.GetRClassifier(EditorBuffer.As <ITextBuffer>());
            var tokenIndex = classifier.Tokens.GetItemContaining(line.Start);

            return(tokenIndex < 0 || classifier.Tokens[tokenIndex].TokenType != RTokenType.String);
        }
Ejemplo n.º 11
0
        public void DeleteLines(string content, int position, int delete)
        {
            var b = new EditorBuffer(content, "R");

            b.Delete(new TextRange(position, delete));
            var s = b.CurrentSnapshot;

            s.Version.Should().Be(1);

            var expected = content.Substring(0, position) + content.Substring(position + delete);
            var count    = expected.Count(ch => ch.IsLineBreak()) + 1;

            s.LineCount.Should().Be(count);
        }
Ejemplo n.º 12
0
        public void InsertLines(string content, int position, string insert)
        {
            var b = new EditorBuffer(content, "R");

            b.Insert(position, insert);
            var s = b.CurrentSnapshot;

            s.Version.Should().Be(1);

            var expected = content.Substring(0, position) + insert + content.Substring(position);
            var count    = expected.Count(ch => ch.IsLineBreak()) + 1;

            s.LineCount.Should().Be(count);
        }
Ejemplo n.º 13
0
        public ProjectionBufferManager(ITextBuffer diskBuffer, IServiceContainer services, string topLevelContentTypeName, string secondaryContentTypeName)
        {
            DiskBuffer = diskBuffer;

            var projectionBufferFactoryService = services.GetService <IProjectionBufferFactoryService>();
            var contentTypeRegistryService     = services.GetService <IContentTypeRegistryService>();

            var contentType = contentTypeRegistryService.GetContentType(topLevelContentTypeName);

            ViewBuffer = projectionBufferFactoryService.CreateProjectionBuffer(null, new List <object>(0), ProjectionBufferOptions.None, contentType);
            EditorBuffer.Create(ViewBuffer, services.GetService <ITextDocumentFactoryService>());

            contentType             = contentTypeRegistryService.GetContentType(secondaryContentTypeName);
            ContainedLanguageBuffer = projectionBufferFactoryService.CreateProjectionBuffer(null, new List <object>(0), ProjectionBufferOptions.WritableLiteralSpans, contentType);

            DiskBuffer.AddService(this);
            ViewBuffer.AddService(this);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Determines if contained language line can be formatted.
        /// </summary>
        /// <param name="position">Position in the contained language buffer</param>
        /// <param name="typedChar">Typed character</param>
        /// <remarks>In R Markdown lines with ```{r should not be formatted</remarks>
        protected override bool CanFormatContainedLanguageLine(int position, char typedChar)
        {
            // Make sure we are not formatting damaging the projected range in R Markdown
            // which looks like ```{r. 'r' should not separate from {.
            var textBuffer = EditorBuffer.As <ITextBuffer>();
            var host       = textBuffer.GetService <IContainedLanguageHost>();

            // If user typed enter, we should be asking for permission to format previous
            // line since automatic formatting is post-editing operation
            if (host != null)
            {
                var lineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(position);
                if (typedChar.IsLineBreak())
                {
                    lineNumber--;
                }
                return(host.CanFormatLine(EditorView, EditorBuffer, lineNumber));
            }
            return(true);
        }
Ejemplo n.º 15
0
 public void ProcessChanges(TextDocumentContentChangedEvent[] contentChanges)
 {
     foreach (var change in contentChanges.Where(c => c.range.HasValue))
     {
         var position = EditorBuffer.ToStreamPosition(change.range.Value.start);
         var range    = new TextRange(position, change.rangeLength ?? 0);
         if (!string.IsNullOrEmpty(change.text))
         {
             // Insert or replace
             if (change.rangeLength == 0)
             {
                 EditorBuffer.Insert(position, change.text);
             }
             else
             {
                 EditorBuffer.Replace(range, change.text);
             }
         }
         else
         {
             EditorBuffer.Delete(range);
         }
     }
 }
Ejemplo n.º 16
0
        private void HandleAddRemoveBuffers(ReadOnlyCollection <ITextBuffer> addedBuffers, ReadOnlyCollection <ITextBuffer> removedBuffers)
        {
            foreach (var tb in addedBuffers)
            {
                if (tb.ContentType.IsOfType(RContentTypeDefinition.ContentType))
                {
                    var doc = tb.GetEditorDocument <IREditorDocument>();
                    if (doc == null)
                    {
                        var eb = EditorBuffer.Create(tb, _shell.GetService <ITextDocumentFactoryService>());
                        new REditorDocument(eb, _shell.Services);
                    }
                }
            }

            foreach (var tb in removedBuffers)
            {
                if (tb.ContentType.IsOfType(RContentTypeDefinition.ContentType))
                {
                    var doc = tb.GetEditorDocument <IREditorDocument>();
                    doc?.Close();
                }
            }
        }
Ejemplo n.º 17
0
 public void Dispose()
 {
     EditorBuffer.RemoveService(this);
     _projectionBufferManager.Dispose();
 }