本文整理汇总了C#中TextEditor.OpenUndoGroup方法的典型用法代码示例。如果您正苦于以下问题:C# TextEditor.OpenUndoGroup方法的具体用法?C# TextEditor.OpenUndoGroup怎么用?C# TextEditor.OpenUndoGroup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextEditor
的用法示例。
在下文中一共展示了TextEditor.OpenUndoGroup方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Handle
public override bool Handle (TextEditor editor, DocumentContext ctx, KeyDescriptor descriptor)
{
char closingBrace;
if (!IsSupportedOpeningBrace (descriptor.KeyChar, out closingBrace) || !CheckCodeContext (editor, ctx, editor.CaretOffset - 1, descriptor.KeyChar, default (CancellationToken)))
return false;
var session = CreateEditorSession (editor, ctx, editor.CaretOffset, descriptor.KeyChar, default (CancellationToken));
session.SetEditor (editor);
if (session == null | !((ICheckPointEditSession)session).CheckOpeningPoint (editor, ctx, default (CancellationToken)))
return false;
using (var undo = editor.OpenUndoGroup ()) {
editor.EnsureCaretIsNotVirtual ();
editor.InsertAtCaret (closingBrace.ToString ());
editor.CaretOffset--;
editor.StartSession (session);
}
return true;
}
示例2: Insert
public int Insert (TextEditor editor, DocumentContext ctx, string text)
{
int offset = editor.LocationToOffset (Location);
using (var undo = editor.OpenUndoGroup ()) {
var line = editor.GetLineByOffset (offset);
int insertionOffset = line.Offset + Location.Column - 1;
offset = insertionOffset;
InsertNewLine (editor, LineBefore, ref offset);
int result = offset - insertionOffset;
editor.InsertText (offset, text);
offset += text.Length;
InsertNewLine (editor, LineAfter, ref offset);
CodeFormatterService.Format (editor, ctx, TextSegment.FromBounds (insertionOffset - 1, offset));
return result;
}
}
示例3: ClosingTagCompletion
protected virtual ICompletionDataList ClosingTagCompletion (TextEditor buf, DocumentLocation currentLocation)
{
//get name of current node in document that's being ended
var el = tracker.Engine.Nodes.Peek () as XElement;
if (el != null && el.Region.End >= currentLocation && !el.IsClosed && el.IsNamed) {
string tag = String.Concat ("</", el.Name.FullName, ">");
if (XmlEditorOptions.AutoCompleteElements) {
// //make sure we have a clean atomic undo so the user can undo the tag insertion
// //independently of the >
// bool wasInAtomicUndo = this.Editor.Document.IsInAtomicUndo;
// if (wasInAtomicUndo)
// this.Editor.Document.EndAtomicUndo ();
using (var undo = buf.OpenUndoGroup ()) {
buf.InsertText (buf.CaretOffset, tag);
buf.CaretOffset -= tag.Length;
}
// if (wasInAtomicUndo)
// this.Editor.Document.BeginAtomicUndo ();
return null;
} else {
var cp = new CompletionDataList ();
cp.Add (new XmlTagCompletionData (tag, 0, true));
return cp;
}
}
return null;
}
示例4: ExpandSelectionToLine
public static void ExpandSelectionToLine (TextEditor textEditor)
{
// from Mono.TextEditor.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;
}
}
示例5: RunSelectionAction
static void RunSelectionAction (TextEditor textEditor, Action<TextEditor> action)
{
using (var undo = textEditor.OpenUndoGroup ()) {
var anchor = textEditor.IsSomethingSelected ? textEditor.SelectionAnchorOffset : textEditor.CaretOffset;
action (textEditor);
textEditor.SetSelection (anchor, textEditor.CaretOffset);
}
}
示例6: InsertNewLineAtEnd
public static void InsertNewLineAtEnd (TextEditor textEditor)
{
if (textEditor.IsReadOnly)
return;
using (var undoGroup = textEditor.OpenUndoGroup ()) {
MoveCaretToLineEnd (textEditor);
InsertNewLine (textEditor);
}
}
示例7: InsertNewLinePreserveCaretPosition
public static void InsertNewLinePreserveCaretPosition (TextEditor textEditor)
{
if (textEditor.IsReadOnly)
return;
using (var undoGroup = textEditor.OpenUndoGroup ()) {
var loc = textEditor.CaretLocation;
InsertNewLine (textEditor);
textEditor.CaretLocation = loc;
}
}
示例8: DuplicateCurrentLine
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));
}
}
}
示例9: TransposeCharacters
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;
}
}
示例10: UndoChange
protected virtual void UndoChange (TextEditor fromEditor, TextEditor toEditor, Hunk hunk)
{
using (var undo = toEditor.OpenUndoGroup ()) {
var start = toEditor.Document.GetLine (hunk.InsertStart);
int toOffset = start != null ? start.Offset : toEditor.Document.TextLength;
int replaceLength = 0;
if (start != null && hunk.Inserted > 0) {
int line = Math.Min (hunk.InsertStart + hunk.Inserted - 1, toEditor.Document.LineCount);
var end = toEditor.Document.GetLine (line);
replaceLength = end.EndOffsetIncludingDelimiter - start.Offset;
}
if (hunk.Removed > 0) {
start = fromEditor.Document.GetLine (Math.Min (hunk.RemoveStart, fromEditor.Document.LineCount));
int line = Math.Min (hunk.RemoveStart + hunk.Removed - 1, fromEditor.Document.LineCount);
var end = fromEditor.Document.GetLine (line);
toEditor.Replace (
toOffset,
replaceLength,
fromEditor.Document.GetTextBetween (start.Offset, end.EndOffsetIncludingDelimiter)
);
} else if (replaceLength > 0) {
toEditor.Remove (toOffset, replaceLength);
}
}
}
示例11: Handle
public override bool Handle (TextEditor editor, DocumentContext ctx, KeyDescriptor descriptor)
{
int braceIndex = openBrackets.IndexOf (descriptor.KeyChar);
if (braceIndex < 0)
return false;
var extEditor = ((SourceEditorView)editor.Implementation).SourceEditorWidget.TextEditor;
var line = extEditor.Document.GetLine (extEditor.Caret.Line);
if (line == null)
return false;
bool inStringOrComment = false;
var stack = line.StartSpan.Clone ();
var sm = extEditor.Document.SyntaxMode as SyntaxMode;
if (sm != null)
// extEditor.Caret.Offset - 1 means we care if we were inside string
// before typing current char
Mono.TextEditor.Highlighting.SyntaxModeService.ScanSpans (extEditor.Document, sm, sm, stack, line.Offset, extEditor.Caret.Offset - 1);
foreach (var span in stack) {
if (string.IsNullOrEmpty (span.Color))
continue;
if (span.Color.StartsWith ("String", StringComparison.Ordinal) ||
span.Color.StartsWith ("Comment", StringComparison.Ordinal) ||
span.Color.StartsWith ("Xml Attribute Value", StringComparison.Ordinal)) {
inStringOrComment = true;
break;
}
}
char insertionChar = '\0';
bool insertMatchingBracket = false;
if (!inStringOrComment) {
char closingBrace = closingBrackets [braceIndex];
char openingBrace = openBrackets [braceIndex];
int count = 0;
foreach (char curCh in ExtensibleTextEditor.GetTextWithoutCommentsAndStrings(extEditor.Document, 0, extEditor.Document.TextLength)) {
if (curCh == openingBrace) {
count++;
} else if (curCh == closingBrace) {
count--;
}
}
if (count >= 0) {
insertMatchingBracket = true;
insertionChar = closingBrace;
}
}
if (insertMatchingBracket) {
using (var undo = editor.OpenUndoGroup ()) {
editor.EnsureCaretIsNotVirtual ();
editor.InsertAtCaret (insertionChar.ToString ());
editor.CaretOffset--;
editor.StartSession (new SkipCharSession (insertionChar));
}
return true;
}
return false;
}
示例12: Format
static void Format (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, TextEditor editor, DocumentContext context, int startOffset, int endOffset, bool exact, bool formatLastStatementOnly = false, OptionSet optionSet = null)
{
TextSpan span;
if (exact) {
span = new TextSpan (startOffset, endOffset - startOffset);
} else {
span = new TextSpan (0, endOffset);
}
var analysisDocument = context.AnalysisDocument;
if (analysisDocument == null)
return;
using (var undo = editor.OpenUndoGroup (/*OperationType.Format*/)) {
try {
var syntaxTree = analysisDocument.GetSyntaxTreeAsync ().Result;
if (formatLastStatementOnly) {
var root = syntaxTree.GetRoot ();
var token = root.FindToken (endOffset);
var tokens = ICSharpCode.NRefactory6.CSharp.FormattingRangeHelper.FindAppropriateRange (token);
if (tokens.HasValue) {
span = new TextSpan (tokens.Value.Item1.SpanStart, tokens.Value.Item2.Span.End - tokens.Value.Item1.SpanStart);
} else {
var parent = token.Parent;
if (parent != null)
span = parent.FullSpan;
}
}
if (optionSet == null) {
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var textPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain);
optionSet = policy.CreateOptions (textPolicy);
}
var doc = Formatter.FormatAsync (analysisDocument, span, optionSet).Result;
var newTree = doc.GetSyntaxTreeAsync ().Result;
var caretOffset = editor.CaretOffset;
int delta = 0;
foreach (var change in newTree.GetChanges (syntaxTree)) {
if (!exact && change.Span.Start + delta >= caretOffset)
continue;
var newText = change.NewText;
editor.ReplaceText (delta + change.Span.Start, change.Span.Length, newText);
delta = delta - change.Span.Length + newText.Length;
}
if (startOffset < caretOffset) {
var caretEndOffset = caretOffset + delta;
if (0 <= caretEndOffset && caretEndOffset < editor.Length)
editor.CaretOffset = caretEndOffset;
if (editor.CaretColumn == 1) {
if (editor.CaretLine > 1 && editor.GetLine (editor.CaretLine - 1).Length == 0)
editor.CaretLine--;
editor.CaretColumn = editor.GetVirtualIndentationColumn (editor.CaretLine);
}
}
} catch (Exception e) {
LoggingService.LogError ("Error in on the fly formatter", e);
}
}
}
示例13: UndoChange
protected virtual void UndoChange (TextEditor fromEditor, TextEditor toEditor, Hunk hunk)
{
using (var undo = toEditor.OpenUndoGroup ()) {
var start = toEditor.Document.GetLine (hunk.InsertStart);
int toOffset = start != null ? start.Offset : toEditor.Document.Length;
if (start != null && hunk.Inserted > 0) {
int line = Math.Min (hunk.InsertStart + hunk.Inserted - 1, toEditor.Document.LineCount);
var end = toEditor.Document.GetLine (line);
toEditor.Remove (start.Offset, end.EndOffset - start.Offset);
}
if (hunk.Removed > 0) {
start = fromEditor.Document.GetLine (Math.Min (hunk.RemoveStart, fromEditor.Document.LineCount));
int line = Math.Min (hunk.RemoveStart + hunk.Removed - 1, fromEditor.Document.LineCount);
var end = fromEditor.Document.GetLine (line);
toEditor.Insert (toOffset, start.Offset == end.EndOffset ? toEditor.EolMarker : fromEditor.Document.GetTextBetween (start.Offset, end.EndOffset));
}
}
}
示例14: AddRegisterDirective
public void AddRegisterDirective (WebFormsPageInfo.RegisterDirective directive, TextEditor editor, bool preserveCaretPosition)
{
if (doc == null)
return;
var node = GetRegisterInsertionPointNode ();
if (node == null)
return;
doc.Info.RegisteredTags.Add (directive);
var line = Math.Max (node.Region.EndLine, node.Region.BeginLine);
var pos = editor.LocationToOffset (line, editor.GetLine (line - 1).Length);
if (pos < 0)
return;
using (var undo = editor.OpenUndoGroup ()) {
var oldCaret = editor.CaretOffset;
var text = editor.FormatString (pos, editor.EolMarker + directive);
var inserted = text.Length;
editor.InsertText (pos, text);
if (preserveCaretPosition) {
editor.CaretOffset = (pos < oldCaret)? oldCaret + inserted : oldCaret;
}
}
}