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); }
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); } }
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; } }