Browse Source

Fixed AnchorMovementMode of SnippetBoundElement and SnippetAnchorElement.

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

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

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

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

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

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

@ -35,13 +35,17 @@ namespace ICSharpCode.AvalonEdit.Snippets @@ -35,13 +35,17 @@ namespace ICSharpCode.AvalonEdit.Snippets
public override void Insert(InsertionContext context)
{
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;
if (inputText != null) {
context.InsertText(ConvertText(inputText));
}
int end = context.InsertionPosition;
AnchorSegment segment = new AnchorSegment(context.Document, start, end - start);
TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition);
end.MovementType = AnchorMovementType.BeforeInsertion;
end.SurviveDeletion = true;
AnchorSegment segment = new AnchorSegment(start, end);
context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
}
}
@ -91,10 +95,14 @@ namespace ICSharpCode.AvalonEdit.Snippets @@ -91,10 +95,14 @@ namespace ICSharpCode.AvalonEdit.Snippets
int offset = segment.Offset;
int length = segment.Length;
string text = boundElement.ConvertText(targetElement.Text);
context.Document.Replace(offset, length, text);
if (length == 0) {
// replacing an empty anchor segment with text won't enlarge it, so we have to recreate it
segment = new AnchorSegment(context.Document, offset, text.Length);
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);
if (length == 0) {
// replacing an empty anchor segment with text won't enlarge it, so we have to recreate it
segment = new AnchorSegment(context.Document, offset, text.Length);
}
}
}
}

Loading…
Cancel
Save