private async void OnCodeEditorTextChanging(RichEditBox sender, RichEditBoxTextChangingEventArgs e)
        {
            if (e.IsContentChanging && previousSelection != currentSelection)
            {
                HighlightSearchBoxMatches();

                CodeEditor.TextDocument.GetText(TextGetOptionsFromNewLineMode(ViewModel.NewLineMode), out string wholeText);

                string?previousText = ViewModel.CurrentText;

                if (wholeText != previousText)
                {
                    ViewModel.CurrentText = wholeText;

                    int start = Math.Min(previousSelection.Start, currentSelection.Start);

                    ITextRange range = CodeEditor.Document.GetRange(start, currentSelection.End);
                    range.GetText(TextGetOptionsFromNewLineMode(ViewModel.NewLineMode), out string text);

                    TextSpan   span       = GetAdjustedTextSpan(TextSpan.FromBounds(start, previousSelection.End), previousText, ViewModel.NewLineMode, true);
                    TextChange textChange = new TextChange(span, text);

                    await ViewModel.ApplyChangesAsync(textChange, null);
                }
            }
        }
Exemplo n.º 2
0
        private bool TryCommitSuggestionIntoDocument(ITextRange range, string displayText, Guid id, ITextCharacterFormat format, bool addTrailingSpace = true)
        {
            // We don't want to set text when the display text doesn't change since it may lead to unexpected caret move.
            range.GetText(TextGetOptions.NoHidden, out var existingText);
            if (existingText != displayText)
            {
                range.SetText(TextSetOptions.Unhide, displayText);
            }

            var formatBefore = range.CharacterFormat.GetClone();

            range.CharacterFormat.SetClone(format);
            PadRange(range, formatBefore);
            range.Link = $"\"{id}\"";

            // In some rare case, setting Link can fail. Only observed when interacting with Undo/Redo feature.
            if (range.Link != $"\"{id}\"")
            {
                range.Delete(TextRangeUnit.Story, -1);
                return(false);
            }

            if (addTrailingSpace)
            {
                var clone = range.GetClone();
                clone.Collapse(false);
                clone.SetText(TextSetOptions.Unhide, " ");
                clone.Collapse(false);
                TextDocument.Selection.SetRange(clone.EndPosition, clone.EndPosition);
            }

            return(true);
        }
Exemplo n.º 3
0
        private void ClearStyle(ITextRange range)
        {
            var start = Math.Min(range.StartPosition, range.EndPosition);
            var end   = Math.Max(range.StartPosition, range.EndPosition);

            range.SetRange(start, end);
            range.CharacterFormat = Document.GetDefaultCharacterFormat();

            range.GetText(TextGetOptions.NoHidden, out string text);
            range.SetText(TextSetOptions.Unlink, text);
            range.SetRange(start, start + text.Length);
        }
Exemplo n.º 4
0
        private bool TryExtractQueryFromRange(ITextRange range, out string prefix, out string query)
        {
            prefix = string.Empty;
            query  = string.Empty;
            range.GetText(TextGetOptions.NoHidden, out var possibleQuery);
            if (possibleQuery.Length > 0 && Prefixes.Contains(possibleQuery[0]) &&
                !possibleQuery.Any(char.IsWhiteSpace) && string.IsNullOrEmpty(range.Link))
            {
                if (possibleQuery.Length == 1)
                {
                    prefix = possibleQuery;
                    return(true);
                }

                prefix = possibleQuery[0].ToString();
                query  = possibleQuery.Substring(1);
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
 public static string GetText(this ITextRange range, ITextBuffer textBuffer) => range.GetText(textBuffer.CurrentSnapshot);
Exemplo n.º 6
0
 public static string GetText(this ITextRange range, ITextBuffer textBuffer)
 {
     return(range.GetText(textBuffer.CurrentSnapshot));
 }
        public static string GetText(this ITextRange range)
        {
            range.GetText(TextGetOptions.None, out string text);

            return(text);
        }