/// <summary> /// Called by the editor when return is pressed while both braces are on the same line and no typing has occurred in the session. /// </summary> /// <param name="session">Current brace completion session.</param> public void OnReturn(IBraceCompletionSession session) { // Return in Repl window would just execute the current command if (session.SubjectBuffer.ContentType.TypeName.Equals(ReplConstants.ReplContentTypeName, StringComparison.OrdinalIgnoreCase)) { return; } var closingPointPosition = session.ClosingPoint.GetPosition(session.SubjectBuffer.CurrentSnapshot); Debug.Assert( condition: closingPointPosition > 0, message: "The closing point position should always be greater than zero", detailMessage: "The closing point position should always be greater than zero, " + "since there is also an opening point for this brace completion session"); // reshape code from // { // |} // // to // { // | // } // where | indicates caret position. using (var undo = _undoHistory.CreateTransaction("Insert new line.")) { _editorOperations.AddBeforeTextBufferChangePrimitive(); _editorOperations.MoveLineUp(false); _editorOperations.MoveToEndOfLine(false); _editorOperations.InsertNewLine(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } }
protected override void NextAction(IEditorOperations editorOperation, Action nextAction) { editorOperation.InsertNewLine(); }
public CommandResult Invoke(Guid group, int id, object args, ref object outargs) { CommandResult result = _braceCompletionTarget.Invoke(group, id, args, ref outargs); if (result.WasExecuted) { return(result); } if (group == VSConstants.VSStd2K) { switch (id) { case (int)VSConstants.VSStd2KCmdID.TYPECHAR: string text; if (args is char) { text = args.ToString(); } else { text = Char.ConvertFromUtf32((System.UInt16)args); } result = this.PerformEditAction(() => _editorOperations.InsertText(text)); break; case (int)VSConstants.VSStd2KCmdID.BACKSPACE: result = this.PerformEditAction(() => _editorOperations.Backspace()); break; case (int)VSConstants.VSStd2KCmdID.DELETE: result = this.PerformEditAction(() => _editorOperations.Delete()); break; case (int)VSConstants.VSStd2KCmdID.CANCEL: _editorOperations.ResetSelection(); break; case (int)VSConstants.VSStd2KCmdID.DOWN_EXT: _editorOperations.MoveLineDown(true); break; case (int)VSConstants.VSStd2KCmdID.DOWN: _editorOperations.MoveLineDown(false); break; case (int)VSConstants.VSStd2KCmdID.RIGHT_EXT: _editorOperations.MoveToNextCharacter(true); break; case (int)VSConstants.VSStd2KCmdID.RIGHT: _editorOperations.MoveToNextCharacter(false); break; case (int)VSConstants.VSStd2KCmdID.LEFT_EXT: _editorOperations.MoveToPreviousCharacter(true); break; case (int)VSConstants.VSStd2KCmdID.LEFT: _editorOperations.MoveToPreviousCharacter(false); break; case (int)VSConstants.VSStd2KCmdID.UP_EXT: _editorOperations.MoveLineUp(true); break; case (int)VSConstants.VSStd2KCmdID.UP: _editorOperations.MoveLineUp(false); break; case (int)VSConstants.VSStd2KCmdID.HOME_EXT: _editorOperations.MoveToHome(true); break; case (int)VSConstants.VSStd2KCmdID.HOME: _editorOperations.MoveToHome(false); break; case (int)VSConstants.VSStd2KCmdID.PAGEUP_EXT: _editorOperations.PageUp(true); break; case (int)VSConstants.VSStd2KCmdID.PAGEUP: _editorOperations.PageUp(false); break; case (int)VSConstants.VSStd2KCmdID.PAGEDN_EXT: _editorOperations.PageDown(true); break; case (int)VSConstants.VSStd2KCmdID.PAGEDN: _editorOperations.PageDown(false); break; case (int)VSConstants.VSStd2KCmdID.END_EXT: _editorOperations.MoveToEndOfLine(true); break; case (int)VSConstants.VSStd2KCmdID.END: _editorOperations.MoveToEndOfLine(false); break; case (int)VSConstants.VSStd2KCmdID.BACKTAB: result = this.PerformEditAction(() => _editorOperations.Unindent()); break; case (int)VSConstants.VSStd2KCmdID.TAB: result = this.PerformEditAction(() => _editorOperations.Indent()); break; case (int)VSConstants.VSStd2KCmdID.RETURN: result = this.PerformEditAction(() => _editorOperations.InsertNewLine()); break; case (int)VSConstants.VSStd2KCmdID.CTLMOVERIGHT: _editorOperations.MoveToNextWord(true); break; case (int)VSConstants.VSStd2KCmdID.CTLMOVELEFT: _editorOperations.MoveToPreviousWord(true); break; case (int)VSConstants.VSStd2KCmdID.TOPLINE_EXT: _editorOperations.MoveToStartOfDocument(true); break; case (int)VSConstants.VSStd2KCmdID.BOTTOMLINE_EXT: _editorOperations.MoveToEndOfDocument(true); break; case (int)VSConstants.VSStd2KCmdID.INSERT: bool isEnabled = _editorOperations.Options.IsOverwriteModeEnabled(); _editorOperations.Options.SetOptionValue(DefaultTextViewOptions.OverwriteModeId, !isEnabled); break; case (int)VSConstants.VSStd2KCmdID.DELETEWORDLEFT: result = this.PerformEditAction(() => _editorOperations.DeleteWordToLeft()); break; case (int)VSConstants.VSStd2KCmdID.DELETEWORDRIGHT: result = this.PerformEditAction(() => _editorOperations.DeleteWordToRight()); break; case (int)VSConstants.VSStd2KCmdID.SELECTALL: _editorOperations.SelectAll(); break; case (int)VSConstants.VSStd2KCmdID.SELECTCURRENTWORD: _editorOperations.SelectCurrentWord(); break; case (int)VSConstants.VSStd2KCmdID.WORDNEXT: _editorOperations.MoveToNextWord(false); break; case (int)VSConstants.VSStd2KCmdID.WORDPREV: _editorOperations.MoveToPreviousWord(false); break; case (int)VSConstants.VSStd2KCmdID.TOPLINE: _editorOperations.MoveToStartOfDocument(false); break; case (int)VSConstants.VSStd2KCmdID.BOTTOMLINE: _editorOperations.MoveToEndOfDocument(false); break; case (int)VSConstants.VSStd2KCmdID.SCROLLUP: _editorOperations.ScrollUpAndMoveCaretIfNecessary(); break; case (int)VSConstants.VSStd2KCmdID.SCROLLDN: _editorOperations.ScrollDownAndMoveCaretIfNecessary(); break; case (int)VSConstants.VSStd2KCmdID.COPY: _editorOperations.CopySelection(); break; case (int)VSConstants.VSStd2KCmdID.CUT: return(this.PerformEditAction(() => _editorOperations.CutSelection())); case (int)VSConstants.VSStd2KCmdID.PASTE: string pastedText = args as string; if (pastedText != null) { return(this.PerformEditAction(() => _editorOperations.InsertText(pastedText))); } else { return(this.PerformEditAction(() => _editorOperations.Paste())); } case (int)VSConstants.VSStd2KCmdID.UNDO: if (UndoManager != null && UndoManager.TextBufferUndoHistory.CanUndo) { UndoManager.TextBufferUndoHistory.Undo(1); break; } return(CommandResult.Disabled); case (int)VSConstants.VSStd2KCmdID.REDO: if (UndoManager != null && UndoManager.TextBufferUndoHistory.CanRedo) { UndoManager.TextBufferUndoHistory.Redo(1); break; } return(CommandResult.Disabled); default: return(CommandResult.NotSupported); } _braceCompletionTarget.PostProcessInvoke(CommandResult.Executed, group, id, args, ref outargs); return(result); } return(CommandResult.NotSupported); }
/// <summary> /// Called from VS when we should handle a command or pass it on. /// </summary> public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { // preprocessing if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97) { switch ((VSConstants.VSStd97CmdID)nCmdID) { case VSConstants.VSStd97CmdID.Paste: if (Clipboard.ContainsData("VisualStudioEditorOperationsLineCutCopyClipboardTag")) { // Copying a full line, so we won't strip prompts. // Deferring to VS paste also inserts the entire // line rather than breaking up the current one. break; } string updated = RemoveReplPrompts(_pyService, Clipboard.GetText(), _textView.Options.GetNewLineCharacter()); if (updated != null) { _editorOps.ReplaceSelection(updated); return(VSConstants.S_OK); } break; case VSConstants.VSStd97CmdID.GotoDefn: GotoDefinition(); return(VSConstants.S_OK); case VSConstants.VSStd97CmdID.FindReferences: FindAllReferences(); return(VSConstants.S_OK); } } else if (pguidCmdGroup == CommonConstants.Std2KCmdGroupGuid) { SnapshotPoint?pyPoint; OutliningTaggerProvider.OutliningTagger tagger; switch ((VSConstants.VSStd2KCmdID)nCmdID) { case VSConstants.VSStd2KCmdID.RETURN: pyPoint = _textView.GetPythonCaret(); if (pyPoint != null) { // https://github.com/Microsoft/PTVS/issues/241 // If the current line is a full line comment and we // are splitting the text, automatically insert the // comment marker on the new line. var line = pyPoint.Value.GetContainingLine(); var lineText = line.GetText(); int comment = lineText.IndexOf('#'); if (comment >= 0 && pyPoint.Value < line.End && line.Start + comment < pyPoint.Value && string.IsNullOrWhiteSpace(lineText.Remove(comment))) { _editorOps.InsertNewLine(); _editorOps.InsertText(lineText.Substring(0, comment + 1)); return(VSConstants.S_OK); } } break; case VSConstants.VSStd2KCmdID.FORMATDOCUMENT: pyPoint = _textView.GetPythonCaret(); if (pyPoint != null) { FormatCode(new SnapshotSpan(pyPoint.Value.Snapshot, 0, pyPoint.Value.Snapshot.Length), false); } return(VSConstants.S_OK); case VSConstants.VSStd2KCmdID.FORMATSELECTION: foreach (var span in _textView.BufferGraph.MapDownToFirstMatch( _textView.Selection.StreamSelectionSpan.SnapshotSpan, SpanTrackingMode.EdgeInclusive, EditorExtensions.IsPythonContent )) { FormatCode(span, true); } return(VSConstants.S_OK); case VSConstants.VSStd2KCmdID.SHOWMEMBERLIST: case VSConstants.VSStd2KCmdID.COMPLETEWORD: var controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController)); if (controller != null) { IntellisenseController.ForceCompletions = true; try { controller.TriggerCompletionSession( (VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.COMPLETEWORD, true ); } finally { IntellisenseController.ForceCompletions = false; } return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.QUICKINFO: controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController)); if (controller != null) { controller.TriggerQuickInfo(); return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.PARAMINFO: controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController)); if (controller != null) { controller.TriggerSignatureHelp(); return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.OUTLN_STOP_HIDING_ALL: tagger = _textView.GetOutliningTagger(); if (tagger != null) { tagger.Disable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.OUTLN_START_AUTOHIDING: tagger = _textView.GetOutliningTagger(); if (tagger != null) { tagger.Enable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.COMMENT_BLOCK: case VSConstants.VSStd2KCmdID.COMMENTBLOCK: if (_textView.CommentOrUncommentBlock(comment: true)) { return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK: case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK: if (_textView.CommentOrUncommentBlock(comment: false)) { return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.EXTRACTMETHOD: ExtractMethod(); return(VSConstants.S_OK); case VSConstants.VSStd2KCmdID.RENAME: RefactorRename(); return(VSConstants.S_OK); } } else if (pguidCmdGroup == GuidList.guidPythonToolsCmdSet) { switch (nCmdID) { case PkgCmdIDList.cmdidRefactorRenameIntegratedShell: RefactorRename(); return(VSConstants.S_OK); case PkgCmdIDList.cmdidExtractMethodIntegratedShell: ExtractMethod(); return(VSConstants.S_OK); } } return(_next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)); }
// Helped by source of Microsoft.VisualStudio.Text.Editor.DragDrop.DropHandlerBase.cs in assembly Microsoft.VisualStudio.Text.UI.Wpf, Version=14.0.0.0 public static int HandleCommand(IWpfTextView textView, IClassifier classifier, IOleCommandTarget commandTarget, IEditorOperations editorOperations, bool shiftPressed = false) { //Guid cmdGroup = VSConstants.VSStd2K; var selectedText = editorOperations.SelectedText; ITrackingPoint trackingPoint = null; if (selectedText.Length == 0) { // if nothing is selected, we can consider the current line as a selection var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition; trackingPoint = textView.TextSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative); // Select all the text on the current line. Leaves caret at the start of the next line or end of line if last line. editorOperations.SelectLine(textView.Caret.ContainingTextViewLine, false); var text = editorOperations.SelectedText; // Clear the selection so new inserts will not overwrite the selected line. Caret stays at start of next line. editorOperations.ResetSelection(); // Hack for Last Line: If last line of file, introduce a new line character then delete it after duplicating the line. var endOfFile = !EndsWithNewLine(text); if (endOfFile) { // We are on the last line. Will need to insert a new line. Will be removed later. editorOperations.InsertNewLine(); } // Now we are at the beginning of the line we can insert the duplicate text. editorOperations.InsertText(text); // Clean up any newline character introduced by earlier hack if (endOfFile) { editorOperations.Delete(); } // Return the cursor to its original position, then move it down one line (unless doing reverse) textView.Caret.MoveTo(new VirtualSnapshotPoint(trackingPoint.GetPoint(textView.TextSnapshot)).TranslateTo(textView.TextSnapshot)); if (!shiftPressed) { editorOperations.MoveLineDown(false); } } else { var selection = textView.Selection; var isReversed = selection.IsReversed; var text = selectedText; var textSnapshot = textView.TextSnapshot; var list = new List <ITrackingSpan>(); //var shiftKeyPressed=textVie foreach (SnapshotSpan snapshotSpan in selection.SelectedSpans) { list.Add(textSnapshot.CreateTrackingSpan(snapshotSpan, SpanTrackingMode.EdgeExclusive)); } if (!selection.IsEmpty) { selection.Clear(); } if (list.Count < 2) { var offset = 0; var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition; var point = editorOperations.TextView.Caret.Position.BufferPosition; virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(point.Add(text.Length)) : !isReversed && shiftPressed ? new VirtualSnapshotPoint(point.Add(-text.Length)) : virtualBufferPosition; trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative); if (virtualBufferPosition.IsInVirtualSpace) { offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length; } textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot)); editorOperations.InsertText(text); var insertionPoint = trackingPoint.GetPoint(textView.TextSnapshot); if (offset != 0) { insertionPoint = insertionPoint.Add(offset); } var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint); var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(text.Length)); if (isReversed) { editorOperations.SelectAndMoveCaret(virtualSnapshotPoint2, virtualSnapshotPoint1, TextSelectionMode.Stream); } else { editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Stream); } } else { var trackingPointOffsetList = new List <Tuple <ITrackingPoint, int, int> >(); //Insert Text! if (isReversed) { list.Reverse(); } foreach (var trackingSpan in list) { var span = trackingSpan.GetSpan(textSnapshot); text = trackingSpan.GetText(textSnapshot); var offset = 0; var insertionPoint = !isReversed?trackingSpan.GetEndPoint(span.Snapshot) : trackingSpan.GetStartPoint(span.Snapshot); var virtualBufferPosition = new VirtualSnapshotPoint(insertionPoint); virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(text.Length)) : !isReversed && shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(-text.Length)) : virtualBufferPosition; trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative); if (virtualBufferPosition.IsInVirtualSpace) { offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length; } trackingPointOffsetList.Add(new Tuple <ITrackingPoint, int, int>(trackingPoint, offset, text.Length)); textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot)); editorOperations.InsertText(text); } //Make Selections { var trackingPointOffset = trackingPointOffsetList.First(); var insertionPoint = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot); if (trackingPointOffset.Item2 != 0) { insertionPoint = insertionPoint.Add(trackingPointOffset.Item2); } var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint.Add(!isReversed ? 0 : trackingPointOffset.Item3)); trackingPointOffset = trackingPointOffsetList.Last(); insertionPoint = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot); if (trackingPointOffset.Item2 != 0) { insertionPoint = insertionPoint.Add(trackingPointOffset.Item2); } var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(isReversed ? 0 : trackingPointOffset.Item3)); editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Box); } } } return(VSConstants.S_OK); }
protected override bool Execute(VSConstants.VSStd2KCmdID commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { editorOperations.InsertNewLine(); return(true); }