Browse Source

Fixed AnchorMovementMode of SnippetBoundElement and SnippetAnchorElement.

pull/14/head
Daniel Grunwald 15 years ago
parent
commit
8e5e013c97
  1. 2
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs
  2. 11
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs
  3. 14
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs

2
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs

@ -63,11 +63,13 @@ namespace SharpRefactoring.Gui
LanguageProperties language = parseInfo.CompilationUnit.Language; LanguageProperties language = parseInfo.CompilationUnit.Language;
IClass current = parseInfo.CompilationUnit.GetInnermostClass(anchor.Line, anchor.Column); IClass current = parseInfo.CompilationUnit.GetInnermostClass(anchor.Line, anchor.Column);
using (editor.Document.OpenUndoGroup()) {
// GenerateCode could modify the document. // GenerateCode could modify the document.
// So read anchor.Offset after code generation. // So read anchor.Offset after code generation.
string code = GenerateCode(language, current) ?? ""; string code = GenerateCode(language, current) ?? "";
editor.Document.Insert(anchor.Offset, code); editor.Document.Insert(anchor.Offset, code);
} }
}
Deactivate(); Deactivate();
} }

11
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs

@ -27,9 +27,11 @@ namespace ICSharpCode.AvalonEdit.Snippets
/// <inheritdoc /> /// <inheritdoc />
public override void Insert(InsertionContext context) public override void Insert(InsertionContext context)
{ {
int start = context.InsertionPosition; TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
AnchorSegment segment = new AnchorSegment(context.Document, start, 0); start.MovementType = AnchorMovementType.BeforeInsertion;
context.RegisterActiveElement(this, new AnchorElement(segment, "", Name, context)); start.SurviveDeletion = true;
AnchorSegment segment = new AnchorSegment(start, start);
context.RegisterActiveElement(this, new AnchorElement(segment, Name, context));
} }
} }
@ -54,11 +56,10 @@ namespace ICSharpCode.AvalonEdit.Snippets
/// <summary> /// <summary>
/// Creates a new AnchorElement. /// Creates a new AnchorElement.
/// </summary> /// </summary>
public AnchorElement(AnchorSegment segment, string text, string name, InsertionContext context) public AnchorElement(AnchorSegment segment, string name, InsertionContext context)
{ {
this.segment = segment; this.segment = segment;
this.context = context; this.context = context;
this.Text = text;
this.Name = name; this.Name = name;
} }

14
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs

@ -35,13 +35,17 @@ namespace ICSharpCode.AvalonEdit.Snippets
public override void Insert(InsertionContext context) public override void Insert(InsertionContext context)
{ {
if (targetElement != null) { if (targetElement != null) {
int start = context.InsertionPosition; TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
start.MovementType = AnchorMovementType.BeforeInsertion;
start.SurviveDeletion = true;
string inputText = targetElement.Text; string inputText = targetElement.Text;
if (inputText != null) { if (inputText != null) {
context.InsertText(ConvertText(inputText)); context.InsertText(ConvertText(inputText));
} }
int end = context.InsertionPosition; TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition);
AnchorSegment segment = new AnchorSegment(context.Document, start, end - start); end.MovementType = AnchorMovementType.BeforeInsertion;
end.SurviveDeletion = true;
AnchorSegment segment = new AnchorSegment(start, end);
context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment)); context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
} }
} }
@ -91,6 +95,9 @@ namespace ICSharpCode.AvalonEdit.Snippets
int offset = segment.Offset; int offset = segment.Offset;
int length = segment.Length; int length = segment.Length;
string text = boundElement.ConvertText(targetElement.Text); string text = boundElement.ConvertText(targetElement.Text);
if (length != text.Length || text != context.Document.GetText(offset, length)) {
// Call replace only if we're actually changing something.
// Without this check, we would generate an empty undo group when the user pressed undo.
context.Document.Replace(offset, length, text); context.Document.Replace(offset, length, text);
if (length == 0) { if (length == 0) {
// replacing an empty anchor segment with text won't enlarge it, so we have to recreate it // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it
@ -98,6 +105,7 @@ namespace ICSharpCode.AvalonEdit.Snippets
} }
} }
} }
}
public void Deactivate(SnippetEventArgs e) public void Deactivate(SnippetEventArgs e)
{ {

Loading…
Cancel
Save