コード例 #1
0
 public void Substitute2()
 {
     Create("cat bat", "dag");
     RunCommand("s/a/o/g 2");
     Assert.AreEqual("cot bot", _textView.GetLine(0).GetText());
     Assert.AreEqual("dog", _textView.GetLine(1).GetText());
 }
コード例 #2
0
ファイル: VisualModeTest.cs プロジェクト: bentayloruk/VsVim
 public void ChangeModeToNormalShouldClearSelection()
 {
     Create(lines: "foo");
     _selection.Select(_textView.GetLine(0).Extent);
     _mode.Process(KeyInputUtil.EscapeKey);
     Assert.IsTrue(_selection.GetSpan().IsEmpty);
 }
コード例 #3
0
 public void SelectionChange1()
 {
     Create("foo", "bar");
     _buffer.SwitchMode(ModeKind.Insert, ModeArgument.None);
     _textView.SelectAndUpdateCaret(new SnapshotSpan(_textView.GetLine(0).Start, 0));
     Assert.AreEqual(ModeKind.Insert, _buffer.ModeKind);
 }
コード例 #4
0
 public void TypeForwardShouldReplace()
 {
     Create("hello world");
     _buffer.SwitchMode(ModeKind.Replace, ModeArgument.None);
     _buffer.Process("again");
     Assert.AreEqual("again world", _textView.GetLine(0).GetText());
     Assert.AreEqual(5, _textView.GetCaretPoint().Position);
 }
コード例 #5
0
        public void Execute()
        {
            IWpfTextView textView = txtMgr.GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            int selectionLastLineNumber  = 0;
            int selectionFirstLineNumber = 0;

            if (!textView.Selection.IsEmpty)
            {
                selectionLastLineNumber  = textView.Selection.End.Position.GetContainingLine().LineNumber;
                selectionFirstLineNumber = textView.Selection.Start.Position.GetContainingLine().LineNumber;
            }
            else
            {
                selectionFirstLineNumber    =
                    selectionLastLineNumber = textView.GetCaretLine().End.GetContainingLine().LineNumber;
            }

            textView.Selection.Clear();

            try
            {
                using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                {
                    for (int i = selectionLastLineNumber; i >= selectionFirstLineNumber; i--)
                    {
                        var line = textView.GetLine(i);
                        edit.Delete(new Span(line.Start.Position, line.LengthIncludingLineBreak));
                    }
                    edit.Apply();
                }
            }
            catch
            {
            }

            ////ITextSnapshotLine currentLineContent = snapshot.GetLineFromPosition(textView.Caret.Position.BufferPosition);
            //ITextViewLine currentLineContent = textView.Caret.ContainingTextViewLine;

            //double initialStartPosition =  textView.Caret.Left;

            //ITextEdit edit = snapshot.TextBuffer.CreateEdit();
            //edit.Delete(currentLineContent.Start.Position, currentLineContent.LengthIncludingLineBreak);
            //edit.Apply();

            //ITextSnapshotLine newCurrentLineContent = snapshot.GetLineFromPosition(textView.Caret.Position.BufferPosition);

            ////make a new selection
            //ITextViewLine line = textView.Caret.ContainingTextViewLine;
            //textView.Caret.MoveTo(line, initialStartPosition - 1);
        }
コード例 #6
0
        public static void MoveCaretToLine(this IWpfTextView obj, int line)
        {
            var snapshotLine = obj.GetLine(line);
            var pos          = snapshotLine.Start.Position;

            obj.MoveCaretTo(pos);
            obj.ViewScroller.EnsureSpanVisible(new SnapshotSpan(snapshotLine.Start, 1));
            obj.VisualElement.Focus();
        }
コード例 #7
0
 public static ITextViewLine GetLine(this IWpfTextView wpfTextView, Position position)
 {
     return(wpfTextView.GetLine(position.Line));
 }
コード例 #8
0
        public void Execute()
        {
            IWpfTextView textView = txtMgr.GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            Extensions.RefreshCommentForCurrentDocument();

            if (!textView.Selection.IsEmpty)
            {
                int areaStart = textView.Selection.Start.Position.GetContainingLine().Start.Position;
                int areaEnd   = textView.Selection.End.Position.GetContainingLine().End.Position;

                if (textView.Selection.Start > textView.Selection.End)
                {
                    Extensions.Swap(ref areaStart, ref areaEnd);
                }

                string text = textView.TextBuffer.CurrentSnapshot.GetText(areaStart, areaEnd - areaStart);

                var textLines = text.Replace(Environment.NewLine, "\n").Split('\n')
                                .Select(x => new
                {
                    Text        = x,
                    IsCommented = x.TrimStart().StartsWith(Extensions.commentPreffix),
                    TextStart   = x.IndexOfNonWhitespace(),
                    IsEmpty     = (x == "")
                });

                bool doComment = textLines.Any(x => !x.IsCommented && !x.IsEmpty);
                int  indent    = textLines.Where(x => !x.IsEmpty).Min(x => x.TextStart);

                string[] replacementLines = textLines.Select(x =>
                {
                    if (x.IsEmpty)
                    {
                        return(x.Text);
                    }
                    else
                    {
                        return(x.Text.CommentText(indent, doComment));
                    }
                }).ToArray();

                string replacementText = string.Join(Environment.NewLine, replacementLines);

                using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                {
                    edit.Replace(new Span(areaStart, areaEnd - areaStart), replacementText);
                    edit.Apply();
                }

                textView.Selection.Clear();
                textView.SetSelection(areaStart, replacementText.Length);
            }
            else
            {
                int lineNum = textView.Caret.ContainingTextViewLine.End.GetContainingLine().LineNumber;
                var line    = textView.GetLine(lineNum);

                int caretLineOffset = textView.GetCaretPosition() - textView.Caret.ContainingTextViewLine.Start.Position;

                using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                {
                    string lineText  = line.GetText();
                    bool   doComment = !lineText.IsCommented();
                    int    indent    = lineText.IndexOfNonWhitespace();

                    if (caretLineOffset > indent)
                    {
                        if (doComment)
                        {
                            caretLineOffset += Extensions.commentPreffix.Length;
                        }
                        else
                        {
                            caretLineOffset -= Extensions.commentPreffix.Length;
                        }
                    }

                    string text = line.GetTextIncludingLineBreak().CommentText(indent, doComment);
                    edit.Replace(new Span(line.Start.Position, line.LengthIncludingLineBreak), text);
                    edit.Apply();
                }

                textView.MoveCaretTo(line.Start.Position + caretLineOffset);
            }
        }
コード例 #9
0
        public void Execute_Old(bool up) //non-transactional
        {
            IWpfTextView textView = GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            string sourceText = "";
            int    selectionLastLineNumber  = 0;
            int    selectionFirstLineNumber = 0;

            int selectionStartLineOffset = 0;
            int selectionLength          = 0;

            int caretLineOffset = 0;

            bool noInitialSelection = false;

            caretLineOffset = textView.GetCaretPosition() - textView.GetCaretLine().Start.Position;

            if (!textView.Selection.IsEmpty)
            {
                if (textView.Selection.Start.Position < textView.Selection.End.Position)
                {
                    selectionStartLineOffset = textView.Selection.Start.Position - textView.Selection.Start.Position.GetContainingLine().Start.Position;
                    selectionLength          = textView.Selection.End.Position - textView.Selection.Start.Position;
                }
                else
                {
                    selectionStartLineOffset = textView.Selection.End.Position - textView.Selection.Start.Position.GetContainingLine().End.Position;
                    selectionLength          = textView.Selection.Start.Position - textView.Selection.End.Position;
                }

                selectionLastLineNumber  = textView.Selection.End.Position.GetContainingLine().LineNumber;
                selectionFirstLineNumber = textView.Selection.Start.Position.GetContainingLine().LineNumber;

                var builder = new StringBuilder();

                for (int i = selectionFirstLineNumber; i <= selectionLastLineNumber; i++)
                {
                    builder.AppendLine(snapshot.GetLineFromLineNumber(i).GetText());
                }

                sourceText = builder.ToString();

                textView.Selection.Clear();
            }
            else
            {
                noInitialSelection = true;

                selectionLength =
                    selectionStartLineOffset = 0;
                selectionFirstLineNumber     =
                    selectionLastLineNumber  = textView.GetCaretLine().End.GetContainingLine().LineNumber;

                sourceText = textView.Caret.ContainingTextViewLine.ExtentIncludingLineBreak.GetText();
            }

            {
                int lineCount = selectionLastLineNumber - selectionFirstLineNumber + 1;
                try
                {
                    using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                    {
                        for (int i = selectionLastLineNumber; i >= selectionFirstLineNumber; i--)
                        {
                            var line = textView.GetLine(i);
                            edit.Delete(new Span(line.Start.Position, line.LengthIncludingLineBreak));
                        }
                        edit.Apply();
                    }
                }
                catch
                {
                }

                int insertionPosition;

                if (up)
                {
                    if (selectionFirstLineNumber == 0)
                    {
                        insertionPosition = 0;
                    }
                    else
                    {
                        insertionPosition = textView.GetLine(selectionFirstLineNumber - 1).Start.Position;
                    }
                }
                else
                {
                    insertionPosition = textView.GetLine(selectionFirstLineNumber).ExtentIncludingLineBreak.End.Position;
                }

                textView.Insert(insertionPosition, sourceText);

                if (!noInitialSelection)
                {
                    textView.SetSelection(insertionPosition + selectionStartLineOffset, selectionLength);
                }

                textView.MoveCaretTo(insertionPosition + caretLineOffset);
            }

            int caretLineNumber = textView.GetLineFromPosition(textView.GetCaretPosition()).LineNumber;

            if (up)
            {
                if ((caretLineNumber - textView.VisualSnapshot.Lines.First().LineNumber) < 5)
                {
                    textView.ViewScroller.ScrollViewportVerticallyByLine(ScrollDirection.Up);
                }
            }
            else
            {
                if ((textView.VisualSnapshot.Lines.Last().LineNumber - caretLineNumber) < 5)
                {
                    textView.ViewScroller.ScrollViewportVerticallyByLine(ScrollDirection.Down);
                }
            }

            if (textView.IsCaretClosToHorizontalEdge(up))
            {
                textView.ViewScroller.ScrollViewportVerticallyByLines(up ? ScrollDirection.Up : ScrollDirection.Down, 1);
            }
        }
コード例 #10
0
        public void CustomProcess_DirectInsert()
        {
            Create("world", "");
            _vimBuffer.InsertMode.CustomProcess(
                KeyInputUtil.CharToKeyInput('a'),
                () =>
            {
                _textBuffer.Insert(0, "hello ");
                return(true);
            });
            Assert.AreEqual("hello world", _textView.GetLine(0).GetText());
            _vimBuffer.Process(VimKey.Escape);

            // Now ensure it repeats properly
            _textView.MoveCaretToLine(1);
            _vimBuffer.Process(".");
            Assert.AreEqual("hello ", _textView.GetLine(1).GetText());
        }
コード例 #11
0
 public void Repeat1()
 {
     Create("dog again", "cat again", "chicken");
     EnterMode(ModeKind.VisualLine, _textView.GetLineRange(0, 1).ExtentIncludingLineBreak);
     _buffer.Settings.GlobalSettings.ShiftWidth = 2;
     _buffer.Process(">.");
     Assert.AreEqual("    dog again", _textView.GetLine(0).GetText());
 }
コード例 #12
0
 public void Change_LineWise()
 {
     Create("cat", "  dog", "  bear", "tree");
     EnterMode(ModeKind.VisualLine, _textView.GetLineRange(1, 2).ExtentIncludingLineBreak);
     _buffer.LocalSettings.AutoIndent = true;
     _buffer.Process("c");
     Assert.AreEqual("cat", _textView.GetLine(0).GetText());
     Assert.AreEqual("", _textView.GetLine(1).GetText());
     Assert.AreEqual("tree", _textView.GetLine(2).GetText());
     Assert.AreEqual(2, _textView.Caret.Position.VirtualBufferPosition.VirtualSpaces);
     Assert.AreEqual(_textView.GetLine(1).Start, _textView.GetCaretPoint());
     Assert.AreEqual(ModeKind.Insert, _buffer.ModeKind);
 }
コード例 #13
0
        public void Execute(bool commentOriginal = false)
        {
            IWpfTextView textView = txtMgr.GetTextView();

            ITextSnapshot snapshot = textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
            {
                return;
            }

            Extensions.RefreshCommentForCurrentDocument();

            if (!textView.Selection.IsEmpty)
            {
                var selectionStart = textView.Selection.Start;
                var selectionEnd   = textView.Selection.End;

                if (textView.Selection.Start > textView.Selection.End)
                {
                    Extensions.Swap(ref selectionStart, ref selectionEnd);
                }

                var selectionStartLine = selectionStart.Position.GetContainingLine();
                var selectionEndLine   = selectionEnd.Position.GetContainingLine();

                var    blockTextStart = selectionStartLine.Start.Position;
                string blockText      = textView.TextBuffer.CurrentSnapshot.GetText(selectionStartLine.Start.Position,
                                                                                    selectionEndLine.End.Position - selectionStartLine.Start.Position);

                string selectedText = textView.TextBuffer.CurrentSnapshot.GetText(selectionStart.Position,
                                                                                  selectionEnd.Position - selectionStart.Position);

                var caretPositionWithinBlock  = textView.GetCaretPosition() - selectionStartLine.Start.Position;
                var nonSelectedTextLeftOffset = selectionStart.Position - selectionStartLine.Start.Position;

                string textOffset     = new string(' ', nonSelectedTextLeftOffset);
                string duplicatedText = textOffset + selectedText;

                string replacementText;

                if (commentOriginal)
                {
                    var commentedText = blockText.Comment();

                    replacementText = commentedText + Environment.NewLine + duplicatedText;
                }
                else
                {
                    replacementText = blockText + Environment.NewLine + duplicatedText;
                }

                using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                {
                    edit.Replace(new Span(blockTextStart, blockText.Length), replacementText);
                    edit.Apply();
                }

                var firstDuplicatedTextLine = textView.GetLine(selectionEndLine.LineNumber + 1);

                int newSelectionStart  = firstDuplicatedTextLine.Start.Position + nonSelectedTextLeftOffset;
                int newSelectionLength = Math.Min(selectedText.Length, textView.GetText().Length - newSelectionStart);

                textView.Selection.Clear();
                textView.SetSelection(newSelectionStart, newSelectionLength);

                textView.MoveCaretTo(firstDuplicatedTextLine.Start.Position + caretPositionWithinBlock);
            }
            else
            {
                int selectionLastLineNumber = textView.Caret.ContainingTextViewLine.End.GetContainingLine().LineNumber;
                int caretLineOffset         = textView.GetCaretPosition() - textView.Caret.ContainingTextViewLine.Start.Position;

                int areaStart = textView.Caret.ContainingTextViewLine.Start.Position;
                int areaEnd   = textView.Caret.ContainingTextViewLine.End.Position;

                string text = textView.TextBuffer.CurrentSnapshot.GetText(areaStart, areaEnd - areaStart);
                string replacementText;

                if (commentOriginal)
                {
                    replacementText = text.CommentText(text.IndexOfNonWhitespace(), true) + Environment.NewLine + text;
                }
                else
                {
                    replacementText = text + Environment.NewLine + text;
                }

                using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                {
                    edit.Replace(new Span(areaStart, text.Length), replacementText);
                    edit.Apply();
                }

                var line = textView.GetLine(selectionLastLineNumber + 1);

                textView.MoveCaretTo(line.Start.Position + caretLineOffset);
                return;
            }
        }
コード例 #14
0
 public void RunMacro_InsertText()
 {
     Create("world");
     TestRegister.UpdateValue("ihello ");
     _buffer.Process("@c");
     Assert.AreEqual(ModeKind.Insert, _buffer.ModeKind);
     Assert.AreEqual("hello world", _textView.GetLine(0).GetText());
 }