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 @@ @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Threading;
@ -132,21 +133,21 @@ namespace CSharpBinding.Refactoring @@ -132,21 +133,21 @@ namespace CSharpBinding.Refactoring
switch (defaultPosition) {
case InsertPosition.Start:
layer.CurrentInsertionPoint = 0;
layer.CurrentInsertionPointIndex = 0;
break;
case InsertPosition.End:
layer.CurrentInsertionPoint = insertionPoints.Count - 1;
layer.CurrentInsertionPointIndex = insertionPoints.Count - 1;
break;
case InsertPosition.Before:
for (int i = 0; i < insertionPoints.Count; i++) {
if (insertionPoints[i].Location < loc)
layer.CurrentInsertionPoint = i;
layer.CurrentInsertionPointIndex = i;
}
break;
case InsertPosition.After:
for (int i = 0; i < insertionPoints.Count; i++) {
if (insertionPoints[i].Location > loc) {
layer.CurrentInsertionPoint = i;
layer.CurrentInsertionPointIndex = i;
break;
}
}
@ -174,11 +175,20 @@ namespace CSharpBinding.Refactoring @@ -174,11 +175,20 @@ namespace CSharpBinding.Refactoring
}
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()) {
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);
}
currentScript.FormatText(nodes);
@ -239,19 +249,6 @@ namespace CSharpBinding.Refactoring @@ -239,19 +249,6 @@ namespace CSharpBinding.Refactoring
var layer = new InsertionCursorLayer(area, operation, insertionPoints);
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);
return tcs.Task;
}

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

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

Loading…
Cancel
Save