public void UndoTransactionWithCaretPrimitive() { var textBuffer = CreateTextBuffer(""); var textView = TextEditorFactoryService.CreateTextView(textBuffer); var weakTextView = new WeakReference(textView); DoWork( () => { var undoManager = TextBufferUndoManagerProvider.GetTextBufferUndoManager(textBuffer); using (var transaction = undoManager.TextBufferUndoHistory.CreateTransaction("Test Edit")) { var operations = EditorOperationsFactoryService.GetEditorOperations(textView); operations.AddBeforeTextBufferChangePrimitive(); textBuffer.SetText("hello world"); transaction.Complete(); } }); textView.Close(); textView = null; // The AddBeforeTextBufferChangePrimitive put the ITextView into the undo stack // so we need to clear it out here ClearUndoHistory(textBuffer); RunGarbageCollector(); Assert.Null(weakTextView.Target); }
/// <inheritdoc/> /// <remarks> /// <para>When a text view is created, this method first checks if the content type of the underlying /// <see cref="ITextBuffer"/> matches a content type associated with any of the /// <see cref="CommenterProviders"/>. If so, <see cref="ICommenterProvider.TryCreateCommenter"/> is called to /// obtain the <see cref="ICommenter"/> to associate with the text buffer for the view. The commenter is then /// used to initialize a <see cref="CommenterFilter"/> that provides support for the comment and uncomment /// commands for the text view.</para> /// /// <para> /// If any of these operations fails, no changes are applied to the text view. /// </para> /// /// <note type="note"> /// <para>The current implementation does not support projection buffer scenarios involving multiple content /// types. However, the <see cref="ICommenterProvider"/> and <see cref="ICommenter"/> interfaces do not prevent /// such a feature from being implemented in a future release.</para> /// </note> /// </remarks> public void VsTextViewCreated(IVsTextView textViewAdapter) { ITextView textView = EditorAdaptersFactoryService.GetWpfTextView(textViewAdapter); if (textView == null) { return; } var provider = CommenterProviders.FirstOrDefault(providerInfo => providerInfo.Metadata.ContentTypes.Any(contentType => textView.TextBuffer.ContentType.IsOfType(contentType))); if (provider == null) { return; } var commenter = provider.Value.TryCreateCommenter(textView.TextBuffer); if (commenter == null) { return; } IEditorOperations editorOperations = EditorOperationsFactoryService.GetEditorOperations(textView); CommenterFilter filter = new CommenterFilter(textViewAdapter, textView, commenter, editorOperations, TextUndoHistoryRegistry); filter.Enabled = true; textView.Properties.AddProperty(typeof(CommenterFilter), filter); }
private void Create(bool insertMode, params string[] lines) { _factory = new MockRepository(MockBehavior.Strict); _factory.DefaultValue = DefaultValue.Mock; _textView = CreateTextView(lines); _textBuffer = _textView.TextBuffer; _vim = _factory.Create <IVim>(MockBehavior.Loose); _editorOptions = _factory.Create <IEditorOptions>(MockBehavior.Loose); _textChangeTracker = _factory.Create <ITextChangeTracker>(MockBehavior.Loose); _textChangeTracker.SetupGet(x => x.CurrentChange).Returns(FSharpOption <TextChange> .None); _undoRedoOperations = _factory.Create <IUndoRedoOperations>(); _wordCompletionSessionFactoryService = _factory.Create <IWordCompletionSessionFactoryService>(); var localSettings = new LocalSettings(Vim.GlobalSettings); _vimBuffer = MockObjectFactory.CreateVimBuffer( _textView, localSettings: localSettings, vim: _vim.Object, factory: _factory); _vimBuffer.SetupGet(x => x.ModeKind).Returns(ModeKind.Insert); _operations = _factory.Create <ICommonOperations>(); _operations.SetupGet(x => x.EditorOperations).Returns(EditorOperationsFactoryService.GetEditorOperations(_textView)); _broker = _factory.Create <IDisplayWindowBroker>(); _broker.SetupGet(x => x.IsCompletionActive).Returns(false); _broker.SetupGet(x => x.IsQuickInfoActive).Returns(false); _broker.SetupGet(x => x.IsSignatureHelpActive).Returns(false); _insertUtil = _factory.Create <IInsertUtil>(); // Setup the mouse. By default we say it has no buttons down as that's the normal state _mouseDevice = _factory.Create <IMouseDevice>(); _mouseDevice.SetupGet(x => x.IsLeftButtonPressed).Returns(false); // Setup the keyboard. By default we don't say that any button is pressed. Insert mode is usually // only concerned with arrow keys and we will set those up as appropriate for the typing tests _keyboardDevice = _factory.Create <IKeyboardDevice>(); _keyboardDevice.Setup(x => x.IsKeyDown(It.IsAny <VimKey>())).Returns(false); _modeRaw = new global::Vim.Modes.Insert.InsertMode( _vimBuffer.Object, _operations.Object, _broker.Object, _editorOptions.Object, _undoRedoOperations.Object, _textChangeTracker.Object, _insertUtil.Object, !insertMode, _keyboardDevice.Object, _mouseDevice.Object, WordUtilFactory.GetWordUtil(_textView.TextBuffer), _wordCompletionSessionFactoryService.Object); _mode = _modeRaw; _mode.CommandRan += (sender, e) => { _lastCommandRan = e.CommandRunData; }; }
/// <summary> /// Open up a new document window with the specified file /// </summary> public override bool LoadFileIntoNewWindow(string filePath, FSharpOption <int> line, FSharpOption <int> column) { try { // Open the document in a window. VsShellUtilities.OpenDocument(_vsAdapter.ServiceProvider, filePath, VSConstants.LOGVIEWID_Primary, out IVsUIHierarchy hierarchy, out uint itemID, out IVsWindowFrame windowFrame); if (line.IsSome()) { // Get the VS text view for the window. var vsTextView = VsShellUtilities.GetTextView(windowFrame); // Get the WPF text view for the VS text view. var wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(vsTextView); // Move the caret to its initial position. var snapshotLine = wpfTextView.TextSnapshot.GetLineFromLineNumber(line.Value); var point = snapshotLine.Start; if (column.IsSome()) { point = point.Add(column.Value); wpfTextView.Caret.MoveTo(point); } else { // Default column implies moving to the first non-blank. wpfTextView.Caret.MoveTo(point); var editorOperations = EditorOperationsFactoryService.GetEditorOperations(wpfTextView); editorOperations.MoveToStartOfLineAfterWhiteSpace(false); } } return(true); } catch (Exception e) { _vim.ActiveStatusUtil.OnError(e.Message); return(false); } }
public override bool LoadFileIntoNewWindow(string filePath, FSharpOption <int> line, FSharpOption <int> column) { try { var textDocument = TextDocumentFactoryService.CreateAndLoadTextDocument(filePath, TextBufferFactoryService.TextContentType); var wpfTextView = MainWindow.CreateTextView(textDocument.TextBuffer); MainWindow.AddNewTab(System.IO.Path.GetFileName(filePath), wpfTextView); if (line.IsSome()) { // Move the caret to its initial position. if (column.IsSome()) { wpfTextView.MoveCaretToLine(line.Value, column.Value); } else { // Default column implies moving to the first non-blank. wpfTextView.MoveCaretToLine(line.Value); var editorOperations = EditorOperationsFactoryService.GetEditorOperations(wpfTextView); editorOperations.MoveToStartOfLineAfterWhiteSpace(false); } } // Give the focus to the new buffer. var point = wpfTextView.Caret.Position.VirtualBufferPosition; NavigateTo(point); return(true); } catch (Exception ex) { _vim.ActiveStatusUtil.OnError(ex.Message); return(false); } }
public void CreateLines(params string[] lines) { _view = CreateTextView(lines); _buffer = _view.TextBuffer; _operations = EditorOperationsFactoryService.GetEditorOperations(_view); }
public void Execute(ITextView view, IEmacsCommandMetadata metadata, bool evaluateUniversalArgument = true) { try { changes = new StringBuilder(); view.TextBuffer.Changed += OnTextBufferChanged; EmacsCommand command = CreateCommand(metadata); EmacsCommand emacsCommand = null; IEmacsCommandMetadata metadata1 = null; var context = new EmacsCommandContext(this, TextStructureNavigatorSelectorService, EditorOperationsFactoryService.GetEditorOperations(view), view, CommandRouterProvider.GetCommandRouter(view)); if (ClipboardRing.Count == 0 || ClipboardRing.Last() != Clipboard.GetText()) { ClipboardRing.Add(Clipboard.GetText()); } if (command == null) { return; } ITextUndoHistory history = TextUndoHistoryRegistry.GetHistory(context.TextBuffer); using (ITextUndoTransaction transaction = CreateTransaction(metadata, history)) { int num = 1; bool flag = false; if (evaluateUniversalArgument) { flag = GetUniversalArgumentOrDefault(0) < 0; if (flag) { metadata1 = GetCommandMetadata(metadata.InverseCommand); emacsCommand = CreateCommand(metadata1); } if (metadata.CanBeRepeated) { num = Math.Abs(GetUniversalArgumentOrDefault(1)); } } for (; num > 0; --num) { if (flag) { if (emacsCommand != null) { emacsCommand.Execute(context); } else { command.ExecuteInverse(context); } } else { command.Execute(context); } } if (transaction != null) { transaction.Complete(); } if (context.Clipboard.Length > 0) { ClipboardRing.Add(context.Clipboard.ToString()); Clipboard.SetText(context.Clipboard.ToString()); } else if (changes.Length > 0 && metadata.CopyDeletedTextToTheClipboard) { ClipboardRing.Add(changes.ToString()); Clipboard.SetText(changes.ToString()); } LastExecutedCommand = flag ? metadata1 : metadata; } } catch (NoOperationException ex) { } finally { view.TextBuffer.Changed -= OnTextBufferChanged; } }
public void Create(params string[] lines) { _textView = CreateTextView(lines); _textView.Caret.MoveTo(new SnapshotPoint(_textView.TextSnapshot, 0)); _textBuffer = _textView.TextBuffer; _factory = new MockRepository(MockBehavior.Strict); // Create the Vim instance with our Mock'd services _vimData = new VimData(); var registerMap = VimUtil.CreateRegisterMap(MockObjectFactory.CreateClipboardDevice(_factory).Object); _vimHost = _factory.Create <IVimHost>(); _globalSettings = _factory.Create <IVimGlobalSettings>(); _globalSettings.SetupGet(x => x.Magic).Returns(true); _globalSettings.SetupGet(x => x.SmartCase).Returns(false); _globalSettings.SetupGet(x => x.IgnoreCase).Returns(true); _globalSettings.SetupGet(x => x.IsVirtualEditOneMore).Returns(false); _globalSettings.SetupGet(x => x.UseEditorIndent).Returns(false); _globalSettings.SetupGet(x => x.UseEditorSettings).Returns(false); _globalSettings.SetupGet(x => x.VirtualEdit).Returns(String.Empty); _globalSettings.SetupGet(x => x.WrapScan).Returns(true); _globalSettings.SetupGet(x => x.ShiftWidth).Returns(2); _searchService = VimUtil.CreateSearchService(_globalSettings.Object); var vim = MockObjectFactory.CreateVim( registerMap: registerMap, host: _vimHost.Object, settings: _globalSettings.Object, searchService: _searchService, factory: _factory); // Create the IVimTextBuffer instance with our Mock'd services _localSettings = MockObjectFactory.CreateLocalSettings(_globalSettings.Object, _factory); _localSettings.SetupGet(x => x.AutoIndent).Returns(false); _localSettings.SetupGet(x => x.GlobalSettings).Returns(_globalSettings.Object); _localSettings.SetupGet(x => x.ExpandTab).Returns(true); _localSettings.SetupGet(x => x.TabStop).Returns(4); var vimTextBuffer = MockObjectFactory.CreateVimTextBuffer( _textBuffer, localSettings: _localSettings.Object, vim: vim.Object, factory: _factory); // Create the VimBufferData instance with our Mock'd services _jumpList = _factory.Create <IJumpList>(); _statusUtil = _factory.Create <IStatusUtil>(); _undoRedoOperations = VimUtil.CreateUndoRedoOperations(_statusUtil.Object); var vimBufferData = CreateVimBufferData( vimTextBuffer.Object, _textView, statusUtil: _statusUtil.Object, jumpList: _jumpList.Object, undoRedoOperations: _undoRedoOperations); _smartIndentationService = _factory.Create <ISmartIndentationService>(); _outlining = _factory.Create <IOutliningManager>(); _outlining .Setup(x => x.ExpandAll(It.IsAny <SnapshotSpan>(), It.IsAny <Predicate <ICollapsed> >())) .Returns <IEnumerable <ICollapsible> >(null); _operationsRaw = new CommonOperations( vimBufferData, EditorOperationsFactoryService.GetEditorOperations(_textView), FSharpOption.Create(_outlining.Object), _smartIndentationService.Object); _operations = _operationsRaw; }