Browse Source

partial fix for #526: InsertWithCursor broken in Create Enum refactoring

pull/505/merge
Siegfried Pammer 11 years ago
parent
commit
569e086cae
  1. 35
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/EditorScript.cs
  2. 35
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/InsertionCursorLayer.cs

35
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/EditorScript.cs

@ -18,6 +18,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Threading; using System.Windows.Threading;
@ -132,21 +133,21 @@ namespace CSharpBinding.Refactoring
switch (defaultPosition) { switch (defaultPosition) {
case InsertPosition.Start: case InsertPosition.Start:
layer.CurrentInsertionPoint = 0; layer.CurrentInsertionPointIndex = 0;
break; break;
case InsertPosition.End: case InsertPosition.End:
layer.CurrentInsertionPoint = insertionPoints.Count - 1; layer.CurrentInsertionPointIndex = insertionPoints.Count - 1;
break; break;
case InsertPosition.Before: case InsertPosition.Before:
for (int i = 0; i < insertionPoints.Count; i++) { for (int i = 0; i < insertionPoints.Count; i++) {
if (insertionPoints[i].Location < loc) if (insertionPoints[i].Location < loc)
layer.CurrentInsertionPoint = i; layer.CurrentInsertionPointIndex = i;
} }
break; break;
case InsertPosition.After: case InsertPosition.After:
for (int i = 0; i < insertionPoints.Count; i++) { for (int i = 0; i < insertionPoints.Count; i++) {
if (insertionPoints[i].Location > loc) { if (insertionPoints[i].Location > loc) {
layer.CurrentInsertionPoint = i; layer.CurrentInsertionPointIndex = i;
break; break;
} }
} }
@ -174,11 +175,20 @@ namespace CSharpBinding.Refactoring
} }
int offset = currentScript.GetCurrentOffset(args.InsertionPoint.Location); int offset = currentScript.GetCurrentOffset(args.InsertionPoint.Location);
int indentLevel = currentScript.GetIndentLevelAt(offset); int indentLevel = currentScript.GetIndentLevelAt(Math.Max(0, offset - 1));
foreach (var node in nodes.Reverse()) { foreach (var node in nodes.Reverse()) {
var output = currentScript.OutputNode(indentLevel, node); var output = currentScript.OutputNode(indentLevel, node);
int delta = args.InsertionPoint.Insert(target, output.Text); var text = output.Text;
var insertionPoint = args.InsertionPoint;
if (node is EnumMemberDeclaration) {
insertionPoint.LineAfter = NewLineInsertion.Eol;
insertionPoint.LineBefore = NewLineInsertion.None;
if (args.InsertionPoint != layer.InsertionPoints.Last()) {
text += ",";
}
}
int delta = insertionPoint.Insert(target, text);
output.RegisterTrackedSegments(currentScript, delta + offset); output.RegisterTrackedSegments(currentScript, delta + offset);
} }
currentScript.FormatText(nodes); currentScript.FormatText(nodes);
@ -239,19 +249,6 @@ namespace CSharpBinding.Refactoring
var layer = new InsertionCursorLayer(area, operation, insertionPoints); var layer = new InsertionCursorLayer(area, operation, insertionPoints);
area.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)area.TextView.InvalidateVisual); area.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)area.TextView.InvalidateVisual);
if (declaringType.Kind == TypeKind.Enum) {
foreach (var node in nodes.Reverse()) {
int indentLevel = GetIndentLevelAt(area.Document.GetOffset(declaringType.BodyRegion.Begin));
var output = OutputNode(indentLevel, node);
var point = insertionPoints[0];
var offset = area.Document.GetOffset(point.Location);
var text = output.Text + ",";
var delta = point.Insert(area.Document, text);
output.RegisterTrackedSegments(script, delta + offset);
}
tcs.SetResult(script);
return tcs.Task;
}
InsertWithCursorOnLayer(script, layer, tcs, nodes, area.Document); InsertWithCursorOnLayer(script, layer, tcs, nodes, area.Document);
return tcs.Task; return tcs.Task;
} }

35
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/InsertionCursorLayer.cs

@ -24,6 +24,7 @@ using System.Windows.Controls;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using ICSharpCode.AvalonEdit; using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.AvalonEdit.Rendering;
@ -31,6 +32,7 @@ using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.NRefactory; using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Editor;
namespace CSharpBinding.Refactoring namespace CSharpBinding.Refactoring
{ {
class InsertionCursorLayer : Canvas, IDisposable class InsertionCursorLayer : Canvas, IDisposable
@ -41,9 +43,10 @@ namespace CSharpBinding.Refactoring
readonly TextArea editor; readonly TextArea editor;
public int CurrentInsertionPoint { public int CurrentInsertionPointIndex { get; set; }
get;
set; public InsertionPoint[] InsertionPoints {
get { return insertionPoints; }
} }
int insertionPointNextToMouse = -1; int insertionPointNextToMouse = -1;
@ -73,8 +76,8 @@ namespace CSharpBinding.Refactoring
protected override void OnRender(DrawingContext drawingContext) protected override void OnRender(DrawingContext drawingContext)
{ {
DrawLineForInsertionPoint(CurrentInsertionPoint, markerPen, drawingContext); DrawLineForInsertionPoint(CurrentInsertionPointIndex, markerPen, drawingContext);
if (insertionPointNextToMouse > -1 && insertionPointNextToMouse != CurrentInsertionPoint) if (insertionPointNextToMouse > -1 && insertionPointNextToMouse != CurrentInsertionPointIndex)
DrawLineForInsertionPoint(insertionPointNextToMouse, tempMarkerPen, drawingContext); DrawLineForInsertionPoint(insertionPointNextToMouse, tempMarkerPen, drawingContext);
SetGroupBoxPosition(); SetGroupBoxPosition();
// HACK: why OnRender() override? we could just use Line objects instead // HACK: why OnRender() override? we could just use Line objects instead
@ -128,7 +131,7 @@ namespace CSharpBinding.Refactoring
else { else {
insertionPointNextToMouse = FindNextInsertionPoint(e.GetPosition(this)); insertionPointNextToMouse = FindNextInsertionPoint(e.GetPosition(this));
if (insertionPointNextToMouse >= 0) if (insertionPointNextToMouse >= 0)
CurrentInsertionPoint = insertionPointNextToMouse; CurrentInsertionPointIndex = insertionPointNextToMouse;
InvalidateVisual(); InvalidateVisual();
} }
e.Handled = true; e.Handled = true;
@ -181,9 +184,9 @@ namespace CSharpBinding.Refactoring
{ {
return (sender, e) => { return (sender, e) => {
if (up) if (up)
layer.CurrentInsertionPoint = Math.Max(0, layer.CurrentInsertionPoint - 1); layer.CurrentInsertionPointIndex = Math.Max(0, layer.CurrentInsertionPointIndex - 1);
else else
layer.CurrentInsertionPoint = Math.Min(layer.insertionPoints.Length - 1, layer.CurrentInsertionPoint + 1); layer.CurrentInsertionPointIndex = Math.Min(layer.insertionPoints.Length - 1, layer.CurrentInsertionPointIndex + 1);
layer.InvalidateVisual(); layer.InvalidateVisual();
layer.ScrollToInsertionPoint(); layer.ScrollToInsertionPoint();
}; };
@ -192,9 +195,9 @@ namespace CSharpBinding.Refactoring
ExecutedRoutedEventHandler MoveMarkerPage(bool up) ExecutedRoutedEventHandler MoveMarkerPage(bool up)
{ {
return (sender, e) => { return (sender, e) => {
TextLocation current = layer.insertionPoints[layer.CurrentInsertionPoint].Location; TextLocation current = layer.insertionPoints[layer.CurrentInsertionPointIndex].Location;
double currentVPos = layer.editor.TextView.GetVisualTopByDocumentLine(current.Line); double currentVPos = layer.editor.TextView.GetVisualTopByDocumentLine(current.Line);
int newIndex = layer.CurrentInsertionPoint; int newIndex = layer.CurrentInsertionPointIndex;
double newVPos; double newVPos;
do { do {
if (up) { if (up) {
@ -214,7 +217,7 @@ namespace CSharpBinding.Refactoring
newVPos = layer.editor.TextView.GetVisualTopByDocumentLine(layer.insertionPoints[newIndex].Location.Line); newVPos = layer.editor.TextView.GetVisualTopByDocumentLine(layer.insertionPoints[newIndex].Location.Line);
} }
while (Math.Abs(currentVPos - newVPos) < layer.editor.ActualHeight); while (Math.Abs(currentVPos - newVPos) < layer.editor.ActualHeight);
layer.CurrentInsertionPoint = newIndex; layer.CurrentInsertionPointIndex = newIndex;
layer.InvalidateVisual(); layer.InvalidateVisual();
layer.ScrollToInsertionPoint(); layer.ScrollToInsertionPoint();
}; };
@ -224,9 +227,9 @@ namespace CSharpBinding.Refactoring
{ {
return (sender, e) => { return (sender, e) => {
if (up) if (up)
layer.CurrentInsertionPoint = 0; layer.CurrentInsertionPointIndex = 0;
else else
layer.CurrentInsertionPoint = layer.insertionPoints.Length - 1; layer.CurrentInsertionPointIndex = layer.insertionPoints.Length - 1;
layer.InvalidateVisual(); layer.InvalidateVisual();
layer.ScrollToInsertionPoint(); layer.ScrollToInsertionPoint();
}; };
@ -247,14 +250,14 @@ namespace CSharpBinding.Refactoring
internal void ScrollToInsertionPoint() internal void ScrollToInsertionPoint()
{ {
var location = insertionPoints[CurrentInsertionPoint].Location; var location = insertionPoints[CurrentInsertionPointIndex].Location;
editor.GetService<TextEditor>().ScrollTo(location.Line, location.Column); editor.GetService<TextEditor>().ScrollTo(location.Line, location.Column);
SetGroupBoxPosition(); SetGroupBoxPosition();
} }
void SetGroupBoxPosition() void SetGroupBoxPosition()
{ {
var boxPosition = GetLinePosition(CurrentInsertionPoint) + new Vector(editor.TextView.ActualWidth * 0.6 - 5, -groupBox.ActualHeight / 2.0); var boxPosition = GetLinePosition(CurrentInsertionPointIndex) + new Vector(editor.TextView.ActualWidth * 0.6 - 5, -groupBox.ActualHeight / 2.0);
Canvas.SetTop(groupBox, boxPosition.Y); Canvas.SetTop(groupBox, boxPosition.Y);
Canvas.SetLeft(groupBox, boxPosition.X); Canvas.SetLeft(groupBox, boxPosition.X);
} }
@ -267,7 +270,7 @@ namespace CSharpBinding.Refactoring
void FireExited(bool success) void FireExited(bool success)
{ {
if (Exited != null) { if (Exited != null) {
Exited(this, new InsertionCursorEventArgs(insertionPoints[CurrentInsertionPoint], success)); Exited(this, new InsertionCursorEventArgs(insertionPoints[CurrentInsertionPointIndex], success));
} }
} }

Loading…
Cancel
Save