void ToggleCodeCommentWithBlockComments() { var blockStarts = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentStart"); var blockEnds = TextEditorFactory.GetSyntaxProperties(textEditor.MimeType, "BlockCommentEnd"); if (blockStarts == null || blockEnds == null || blockStarts.Length == 0 || blockEnds.Length == 0) { return; } string blockStart = blockStarts[0]; string blockEnd = blockEnds[0]; using (var undo = textEditor.OpenUndoGroup()) { IDocumentLine startLine; IDocumentLine endLine; if (textEditor.IsSomethingSelected) { startLine = textEditor.GetLineByOffset(textEditor.SelectionRange.Offset); endLine = textEditor.GetLineByOffset(textEditor.SelectionRange.EndOffset); // If selection ends at begining of line... This is visible as previous line // is selected, hence we want to select previous line Bug 26287 if (endLine.Offset == textEditor.SelectionRange.EndOffset) { endLine = endLine.PreviousLine; } } else { startLine = endLine = textEditor.GetLine(textEditor.CaretLine); } string startLineText = textEditor.GetTextAt(startLine.Offset, startLine.Length); string endLineText = textEditor.GetTextAt(endLine.Offset, endLine.Length); if (startLineText.StartsWith(blockStart, StringComparison.Ordinal) && endLineText.EndsWith(blockEnd, StringComparison.Ordinal)) { textEditor.RemoveText(endLine.Offset + endLine.Length - blockEnd.Length, blockEnd.Length); textEditor.RemoveText(startLine.Offset, blockStart.Length); if (textEditor.IsSomethingSelected) { textEditor.SelectionAnchorOffset -= blockEnd.Length; } } else { textEditor.InsertText(endLine.Offset + endLine.Length, blockEnd); textEditor.InsertText(startLine.Offset, blockStart); if (textEditor.IsSomethingSelected) { textEditor.SelectionAnchorOffset += blockEnd.Length; } } } }
public static void ExpandSelectionToLine(TextEditor textEditor) { // from MonoDevelop.Ide.Editor.SelectionActions.ExpandSelectionToLine using (var undoGroup = textEditor.OpenUndoGroup()) { var curLineSegment = textEditor.GetLine(textEditor.CaretLine).SegmentIncludingDelimiter; var range = textEditor.SelectionRange; var selection = TextSegment.FromBounds( System.Math.Min(range.Offset, curLineSegment.Offset), System.Math.Max(range.EndOffset, curLineSegment.EndOffset)); textEditor.CaretOffset = selection.EndOffset; textEditor.SelectionRange = selection; } }
static IEnumerable <IDocumentLine> GetSelectedLines(TextEditor textEditor) { if (!textEditor.IsSomethingSelected) { yield return(textEditor.GetLine(textEditor.CaretLine)); yield break; } var selection = textEditor.SelectionRegion; var line = textEditor.GetLine(selection.EndLine); if (selection.EndColumn == 1) { line = line.PreviousLine; } while (line != null && line.LineNumber >= selection.BeginLine) { yield return(line); line = line.PreviousLine; } }
public static void DuplicateCurrentLine(TextEditor textEditor) { // Code from Mono.TextEditor.MiscActions.DuplicateLine using (var undoGroup = textEditor.OpenUndoGroup()) { if (textEditor.IsSomethingSelected) { var selectedText = textEditor.SelectedText; textEditor.ClearSelection(); textEditor.InsertAtCaret(selectedText); } else { var line = textEditor.GetLine(textEditor.CaretLine); if (line == null) { return; } textEditor.InsertText(line.Offset, textEditor.GetTextAt(line.SegmentIncludingDelimiter)); } } }
static DocumentLocation LimitColumn(TextEditor data, DocumentLocation loc) { return(new DocumentLocation(loc.Line, System.Math.Min(loc.Column, data.GetLine(loc.Line).Length + 1))); }
public static void TransposeCharacters(TextEditor textEditor) { // Code from Mono.TextEditor.MiscActions.TransposeCharacters if (textEditor.CaretOffset == 0) { return; } var line = textEditor.GetLine(textEditor.CaretLine); if (line == null) { return; } using (var undoGroup = textEditor.OpenUndoGroup()) { int transposeOffset = textEditor.CaretOffset - 1; char ch; if (textEditor.CaretColumn == 0) { var lineAbove = textEditor.GetLine(textEditor.CaretLine - 1); if (lineAbove.Length == 0 && line.Length == 0) { return; } if (line.Length != 0) { ch = textEditor.GetCharAt(textEditor.CaretOffset); textEditor.RemoveText(textEditor.CaretOffset, 1); textEditor.InsertText(lineAbove.Offset + lineAbove.Length, ch.ToString()); return; } int lastCharOffset = lineAbove.Offset + lineAbove.Length - 1; ch = textEditor.GetCharAt(lastCharOffset); textEditor.RemoveText(lastCharOffset, 1); textEditor.InsertAtCaret(ch.ToString()); return; } int offset = textEditor.CaretOffset; if (textEditor.CaretColumn >= line.Length + 1) { offset = line.Offset + line.Length - 1; transposeOffset = offset - 1; // case one char in line: if (transposeOffset < line.Offset) { var lineAbove = textEditor.GetLine(textEditor.CaretLine - 1); transposeOffset = lineAbove.Offset + lineAbove.Length; ch = textEditor.GetCharAt(offset); textEditor.RemoveText(offset, 1); textEditor.InsertText(transposeOffset, ch.ToString()); textEditor.CaretOffset = line.Offset; return; } } ch = textEditor.GetCharAt(offset); textEditor.ReplaceText(offset, 1, textEditor.GetCharAt(transposeOffset).ToString()); textEditor.ReplaceText(transposeOffset, 1, ch.ToString()); if (textEditor.CaretColumn < line.Length + 1) { textEditor.CaretOffset = offset + 1; } } }