Ejemplo n.º 1
0
        public ISnapshotPoint GetCaretPosition(IEditorBuffer editorBuffer = null)
        {
            var textBuffer = editorBuffer?.As <ITextBuffer>() ?? _textView.TextBuffer;
            var point      = _textView.GetCaretPosition(editorBuffer ?? _textView.TextBuffer.ToEditorBuffer());

            return(point.HasValue ? new EditorSnapshotPoint(textBuffer.CurrentSnapshot, point.Value) : null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates contained language host with default settings.
        /// </summary>
        /// <param name="document">Markdown editor document</param>
        /// <param name="editorBuffer">Contained language text buffer</param>
        public MdContainedLanguageHost(IEditorDocument document, IEditorBuffer editorBuffer)
        {
            _textBuffer = editorBuffer.As <ITextBuffer>();

            _document          = document;
            _document.Closing += OnDocumentClosing;

            editorBuffer.AddService(this);
        }
Ejemplo n.º 3
0
        public MdEditorDocument(IEditorBuffer editorBuffer, IServiceContainer services)
        {
            _services = services;

            EditorBuffer = editorBuffer;
            EditorBuffer.AddService(this);

            var textBuffer = editorBuffer.As <ITextBuffer>();

            _projectionBufferManager = new ProjectionBufferManager(textBuffer, services, MdProjectionContentTypeDefinition.ContentType, RContentTypeDefinition.ContentType);
            ContainedLanguageHandler = _rLanguageHandler = new RLanguageHandler(textBuffer, _projectionBufferManager, services);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Determines if secondary language can format given line.
        /// </summary>
        /// <param name="editorView">Text view</param>
        /// <param name="containedLanguageBuffer">Contained language buffer</param>
        /// <param name="lineNumber">Line number in the contained language buffer</param>
        public bool CanFormatLine(IEditorView editorView, IEditorBuffer containedLanguageBuffer, int lineNumber)
        {
            var textView   = editorView.As <ITextView>();
            var textBuffer = containedLanguageBuffer.As <ITextBuffer>();
            var line       = textBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber);
            var viewPoint  = textView.MapUpToView(line.Start);

            if (viewPoint.HasValue)
            {
                var lineText = textView.TextBuffer.CurrentSnapshot.GetLineFromPosition(viewPoint.Value).GetText();
                return(lineText.IndexOfOrdinal("```") < 0 && !lineText.TrimStart().StartsWithIgnoreCase("```{r"));
            }
            return(false);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Incrementally applies whitespace change to the buffer
 /// having old and new tokens produced from the 'before formatting'
 /// and 'after formatting' versions of the same text.
 /// </summary>
 /// <param name="editorBuffer">Text buffer to apply changes to</param>
 /// <param name="oldTextProvider">Text provider of the text fragment before formatting</param>
 /// <param name="newTextProvider">Text provider of the formatted text</param>
 /// <param name="oldTokens">Tokens from the 'before' text fragment</param>
 /// <param name="newTokens">Tokens from the 'after' text fragment</param>
 /// <param name="formatRange">Range that is being formatted in the text buffer</param>
 /// <param name="transactionName">Name of the undo transaction to open</param>
 /// <param name="selectionTracker">
 /// Selection tracker object that will save,
 /// track and restore selection after changes have been applied.</param>
 /// <param name="additionalAction">
 /// Action to perform after changes are applies by undo unit is not yet closed.
 /// </param>
 public void ApplyChange(
     IEditorBuffer editorBuffer,
     ITextProvider oldTextProvider,
     ITextProvider newTextProvider,
     IReadOnlyList <ITextRange> oldTokens,
     IReadOnlyList <ITextRange> newTokens,
     ITextRange formatRange,
     string transactionName,
     ISelectionTracker selectionTracker,
     Action additionalAction = null)
 {
     Debug.Assert(oldTokens.Count == newTokens.Count);
     if (oldTokens.Count == newTokens.Count)
     {
         using (CreateSelectionUndo(selectionTracker, _services, transactionName)) {
             var textBuffer = editorBuffer.As <ITextBuffer>();
             using (var edit = textBuffer.CreateEdit()) {
                 if (oldTokens.Count > 0)
                 {
                     // Replace whitespace between tokens in reverse so relative positions match
                     var    oldEnd = oldTextProvider.Length;
                     var    newEnd = newTextProvider.Length;
                     string newText;
                     for (var i = newTokens.Count - 1; i >= 0; i--)
                     {
                         var oldText = oldTextProvider.GetText(TextRange.FromBounds(oldTokens[i].End, oldEnd));
                         newText = newTextProvider.GetText(TextRange.FromBounds(newTokens[i].End, newEnd));
                         if (oldText != newText)
                         {
                             edit.Replace(formatRange.Start + oldTokens[i].End, oldEnd - oldTokens[i].End, newText);
                         }
                         oldEnd = oldTokens[i].Start;
                         newEnd = newTokens[i].Start;
                     }
                     newText = newTextProvider.GetText(TextRange.FromBounds(0, newEnd));
                     edit.Replace(formatRange.Start, oldEnd, newText);
                 }
                 else
                 {
                     var newText = newTextProvider.GetText(TextRange.FromBounds(0, newTextProvider.Length));
                     edit.Replace(formatRange.Start, formatRange.Length, newText);
                 }
                 edit.Apply();
                 additionalAction?.Invoke();
             }
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Determines if contained language line can be formatted.
        /// </summary>
        /// <param name="editorView">Editor view</param>
        /// <param name="editorBuffer">Contained language buffer</param>
        /// <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>
        private bool CanFormatContainedLanguageLine(IEditorView editorView, IEditorBuffer editorBuffer, 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, textBuffer.ToEditorBuffer(), lineNumber));
            }
            return(true);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Incrementally applies whitespace change to the buffer
 /// having old and new tokens produced from the 'before formatting'
 /// and 'after formatting' versions of the same text.
 /// </summary>
 /// <param name="editorBuffer">Text buffer to apply changes to</param>
 /// <param name="oldTextProvider">Text provider of the text fragment before formatting</param>
 /// <param name="newTextProvider">Text provider of the formatted text</param>
 /// <param name="oldTokens">Tokens from the 'before' text fragment</param>
 /// <param name="newTokens">Tokens from the 'after' text fragment</param>
 /// <param name="formatRange">Range that is being formatted in the text buffer</param>
 /// <param name="transactionName">Name of the undo transaction to open</param>
 /// <param name="selectionTracker">
 /// Selection tracker object that will save,
 /// track and restore selection after changes have been applied.</param>
 /// <param name="additionalAction">
 /// Action to perform after changes are applies by undo unit is not yet closed.
 /// </param>
 public void ApplyChange(
     IEditorBuffer editorBuffer,
     ITextProvider oldTextProvider,
     ITextProvider newTextProvider,
     IReadOnlyList <ITextRange> oldTokens,
     IReadOnlyList <ITextRange> newTokens,
     ITextRange formatRange,
     string transactionName,
     ISelectionTracker selectionTracker,
     Action additionalAction = null)
 {
     Debug.Assert(oldTokens.Count == newTokens.Count);
     if (oldTokens.Count == newTokens.Count)
     {
         using (CreateSelectionUndo(selectionTracker, _services, transactionName)) {
             var textBuffer = editorBuffer.As <ITextBuffer>();
             using (var edit = textBuffer.CreateEdit()) {
                 var edits = CalculateChanges(oldTextProvider, newTextProvider, oldTokens, newTokens, formatRange);
                 foreach (var e in edits)
                 {
                     if (string.IsNullOrEmpty(e.NewText))
                     {
                         edit.Delete(e.Range.ToSpan());
                     }
                     else if (e.Range.Length > 0)
                     {
                         edit.Replace(e.Range.ToSpan(), e.NewText);
                     }
                     else
                     {
                         edit.Insert(e.Range.Start, e.NewText);
                     }
                 }
                 edit.Apply();
                 additionalAction?.Invoke();
             }
         }
     }
 }
Ejemplo n.º 8
0
 public static SnapshotPoint?GetCaretPosition(this ITextView textView, IEditorBuffer editorBuffer = null)
 => textView.GetCaretPosition(editorBuffer?.As <ITextBuffer>());
Ejemplo n.º 9
0
 public IEnumerable <IEditorView> GetAllViews(IEditorBuffer editorBuffer)
 => TextViewConnectionListener.GetViewsForBuffer(editorBuffer.As <ITextBuffer>()).Select(v => v.ToEditorView()).Where(v => v != null);
Ejemplo n.º 10
0
 public ISelectionTracker CreateSelectionTracker(IEditorView editorView, IEditorBuffer editorBuffer, ITextRange selectedRange)
 => new RSelectionTracker(editorView.As <ITextView>(), editorBuffer.As <ITextBuffer>(), selectedRange);