private static void SurroundRectangleSelectionWithBlockComment(EdiTextEditor editor, BlockDefinition block, RectangleSelection sel) { if (sel == null) return; // Backup current view position of rectangular selection int selectionStart = editor.SelectionStart; int selectionLength = editor.SelectionLength; int caretOffset = editor.CaretOffset; TextViewPosition startPos = new TextViewPosition(sel.StartPosition); TextViewPosition endPos = new TextViewPosition(sel.EndPosition); TextReplaceBlockRegion[] region = new TextReplaceBlockRegion[sel.Segments.Count()]; bool bFoundNoMatch = true; for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); // Attempt to find the currently set comment before and after the current selection switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: region[i] = FindSelectedStartCommentRegion(block.StartBlock, editor.Document, editor.Document.GetText(item), item.StartOffset, item.Length); break; case BlockDefinition.BlockAt.End: region[i] = FindSelectedEndCommentRegion(block.EndBlock, editor.Document, editor.Document.GetText(item), item.StartOffset); break; case BlockDefinition.BlockAt.StartAndEnd: region[i] = FindSelectedCommentRegion(block.StartBlock, block.EndBlock, editor.Document, editor.Document.GetText(item), item.StartOffset, item.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } if (region[i] == null) bFoundNoMatch = false; } if (bFoundNoMatch == true) { for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); // Remove the block surround (comment) if there is a match available switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length); break; case BlockDefinition.BlockAt.End: editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length); break; case BlockDefinition.BlockAt.StartAndEnd: editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length); editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } } else { for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Insert(item.StartOffset, block.StartBlock); break; case BlockDefinition.BlockAt.End: editor.Document.Insert(item.EndOffset, block.EndBlock); break; case BlockDefinition.BlockAt.StartAndEnd: // Insert a new comment since we could not find one ... editor.Document.Insert(item.EndOffset, block.EndBlock); editor.Document.Insert(item.StartOffset, block.StartBlock); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } // Move original selection to the rigth and apply rectangular selection editor.CaretOffset = caretOffset; editor.SelectionStart = selectionStart; editor.SelectionLength = selectionLength; //startPos.Column += block.StartBlock.Length; //endPos.Column += block.StartBlock.Length; // Reset selection to keep the text that was originally selected editor.TextArea.Selection = new RectangleSelection(editor.TextArea, startPos, endPos); } }
/// <summary> /// Insert a start string and an end string /// before and after the currently selected text. /// </summary> /// <param name="editor"></param> /// <param name="block"></param> static public void SurroundSelectionWithBlockComment(EdiTextEditor editor, BlockDefinition block) { if (editor == null) { return; } RectangleSelection sel = null; // Rectangle selection is a different deal... if ((editor.TextArea.Selection is RectangleSelection) == true) { sel = editor.TextArea.Selection as RectangleSelection; if (sel == null) { return; } // Single line block selection can be treaded like normal selection if (sel.IsMultiline == true) { SurroundRectangleSelectionWithBlockComment(editor, block, sel); return; } } // BlockSurround the current cursor location if there is no current selection ////if (editor.SelectionLength == 0) //// return; // Attempt to find the currently set comment before and after the current selection TextReplaceBlockRegion region = null; switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: region = FindSelectedStartCommentRegion(block.StartBlock, editor.Document, editor.SelectedText, editor.SelectionStart, editor.SelectionLength); break; case BlockDefinition.BlockAt.End: region = FindSelectedEndCommentRegion(block.EndBlock, editor.Document, editor.SelectedText, editor.SelectionStart); break; case BlockDefinition.BlockAt.StartAndEnd: region = FindSelectedCommentRegion(block.StartBlock, block.EndBlock, editor.Document, editor.SelectedText, editor.SelectionStart, editor.SelectionLength); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } // Remove the block surround (comment) if there is a match available if (region != null) { switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Remove(region.StartOffset, region.CommentStart.Length); break; case BlockDefinition.BlockAt.End: editor.Document.Remove(region.EndOffset, region.CommentEnd.Length); break; case BlockDefinition.BlockAt.StartAndEnd: editor.Document.Remove(region.EndOffset, region.CommentEnd.Length); editor.Document.Remove(region.StartOffset, region.CommentStart.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } else { int startOffset = editor.SelectionStart; int endOffset = editor.SelectionStart + editor.SelectionLength; int lenOffset = editor.SelectionLength; switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Insert(startOffset, block.StartBlock); break; case BlockDefinition.BlockAt.End: editor.Document.Insert(endOffset, block.EndBlock); break; case BlockDefinition.BlockAt.StartAndEnd: // Insert a new comment since we could not find one ... editor.Document.Insert(endOffset, block.EndBlock); editor.Document.Insert(startOffset, block.StartBlock); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } // Reset selection to keep the text that was originally selected editor.Select(startOffset + block.StartBlock.Length, lenOffset); } }
/// <summary> /// Insert a start string and an end string /// before and after the currently selected text. /// </summary> /// <param name="editor"></param> /// <param name="block"></param> public static void SurroundSelectionWithBlockComment(EdiTextEditor editor, BlockDefinition block) { if (editor == null) return; RectangleSelection sel = null; // Rectangle selection is a different deal... if ((editor.TextArea.Selection is RectangleSelection) == true) { sel = editor.TextArea.Selection as RectangleSelection; if (sel == null) return; // Single line block selection can be treaded like normal selection if (sel.IsMultiline == true) { SurroundRectangleSelectionWithBlockComment(editor, block, sel); return; } } // BlockSurround the current cursor location if there is no current selection ////if (editor.SelectionLength == 0) //// return; // Attempt to find the currently set comment before and after the current selection TextReplaceBlockRegion region = null; switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: region = FindSelectedStartCommentRegion(block.StartBlock, editor.Document, editor.SelectedText, editor.SelectionStart, editor.SelectionLength); break; case BlockDefinition.BlockAt.End: region = FindSelectedEndCommentRegion(block.EndBlock, editor.Document, editor.SelectedText, editor.SelectionStart); break; case BlockDefinition.BlockAt.StartAndEnd: region = FindSelectedCommentRegion(block.StartBlock, block.EndBlock, editor.Document, editor.SelectedText, editor.SelectionStart, editor.SelectionLength); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } // Remove the block surround (comment) if there is a match available if (region != null) { switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Remove(region.StartOffset, region.CommentStart.Length); break; case BlockDefinition.BlockAt.End: editor.Document.Remove(region.EndOffset, region.CommentEnd.Length); break; case BlockDefinition.BlockAt.StartAndEnd: editor.Document.Remove(region.EndOffset, region.CommentEnd.Length); editor.Document.Remove(region.StartOffset, region.CommentStart.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } else { int startOffset = editor.SelectionStart; int endOffset = editor.SelectionStart + editor.SelectionLength; int lenOffset = editor.SelectionLength; switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Insert(startOffset, block.StartBlock); break; case BlockDefinition.BlockAt.End: editor.Document.Insert(endOffset, block.EndBlock); break; case BlockDefinition.BlockAt.StartAndEnd: // Insert a new comment since we could not find one ... editor.Document.Insert(endOffset, block.EndBlock); editor.Document.Insert(startOffset, block.StartBlock); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } // Reset selection to keep the text that was originally selected editor.Select(startOffset + block.StartBlock.Length, lenOffset); } }
private static void SurroundRectangleSelectionWithBlockComment(EdiTextEditor editor, BlockDefinition block, RectangleSelection sel) { if (sel == null) { return; } // Backup current view position of rectangular selection int selectionStart = editor.SelectionStart; int selectionLength = editor.SelectionLength; int caretOffset = editor.CaretOffset; TextViewPosition startPos = new TextViewPosition(sel.StartPosition); TextViewPosition endPos = new TextViewPosition(sel.EndPosition); TextReplaceBlockRegion[] region = new TextReplaceBlockRegion[sel.Segments.Count()]; bool bFoundNoMatch = true; for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); // Attempt to find the currently set comment before and after the current selection switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: region[i] = FindSelectedStartCommentRegion(block.StartBlock, editor.Document, editor.Document.GetText(item), item.StartOffset, item.Length); break; case BlockDefinition.BlockAt.End: region[i] = FindSelectedEndCommentRegion(block.EndBlock, editor.Document, editor.Document.GetText(item), item.StartOffset); break; case BlockDefinition.BlockAt.StartAndEnd: region[i] = FindSelectedCommentRegion(block.StartBlock, block.EndBlock, editor.Document, editor.Document.GetText(item), item.StartOffset, item.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } if (region[i] == null) { bFoundNoMatch = false; } } if (bFoundNoMatch == true) { for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); // Remove the block surround (comment) if there is a match available switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length); break; case BlockDefinition.BlockAt.End: editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length); break; case BlockDefinition.BlockAt.StartAndEnd: editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length); editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } } else { for (int i = sel.Segments.Count() - 1; i >= 0; i--) { var item = sel.Segments.ElementAt(i); switch (block.TypeOfBlock) { case BlockDefinition.BlockAt.Start: editor.Document.Insert(item.StartOffset, block.StartBlock); break; case BlockDefinition.BlockAt.End: editor.Document.Insert(item.EndOffset, block.EndBlock); break; case BlockDefinition.BlockAt.StartAndEnd: // Insert a new comment since we could not find one ... editor.Document.Insert(item.EndOffset, block.EndBlock); editor.Document.Insert(item.StartOffset, block.StartBlock); break; default: throw new NotImplementedException(block.TypeOfBlock.ToString()); } } // Move original selection to the rigth and apply rectangular selection editor.CaretOffset = caretOffset; editor.SelectionStart = selectionStart; editor.SelectionLength = selectionLength; //startPos.Column += block.StartBlock.Length; //endPos.Column += block.StartBlock.Length; // Reset selection to keep the text that was originally selected editor.TextArea.Selection = new RectangleSelection(editor.TextArea, startPos, endPos); } }