Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 569e086

Browse files
partial fix for #526: InsertWithCursor broken in Create Enum refactoring
1 parent 8ae347f commit 569e086

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Diagnostics;
2122
using System.Linq;
2223
using System.Threading.Tasks;
2324
using System.Windows.Threading;
@@ -132,21 +133,21 @@ public override Task<Script> InsertWithCursor(string operation, InsertPosition d
132133

133134
switch (defaultPosition) {
134135
case InsertPosition.Start:
135-
layer.CurrentInsertionPoint = 0;
136+
layer.CurrentInsertionPointIndex = 0;
136137
break;
137138
case InsertPosition.End:
138-
layer.CurrentInsertionPoint = insertionPoints.Count - 1;
139+
layer.CurrentInsertionPointIndex = insertionPoints.Count - 1;
139140
break;
140141
case InsertPosition.Before:
141142
for (int i = 0; i < insertionPoints.Count; i++) {
142143
if (insertionPoints[i].Location < loc)
143-
layer.CurrentInsertionPoint = i;
144+
layer.CurrentInsertionPointIndex = i;
144145
}
145146
break;
146147
case InsertPosition.After:
147148
for (int i = 0; i < insertionPoints.Count; i++) {
148149
if (insertionPoints[i].Location > loc) {
149-
layer.CurrentInsertionPoint = i;
150+
layer.CurrentInsertionPointIndex = i;
150151
break;
151152
}
152153
}
@@ -174,11 +175,20 @@ void InsertWithCursorOnLayer(EditorScript currentScript, InsertionCursorLayer la
174175
}
175176

176177
int offset = currentScript.GetCurrentOffset(args.InsertionPoint.Location);
177-
int indentLevel = currentScript.GetIndentLevelAt(offset);
178+
int indentLevel = currentScript.GetIndentLevelAt(Math.Max(0, offset - 1));
178179

179180
foreach (var node in nodes.Reverse()) {
180181
var output = currentScript.OutputNode(indentLevel, node);
181-
int delta = args.InsertionPoint.Insert(target, output.Text);
182+
var text = output.Text;
183+
var insertionPoint = args.InsertionPoint;
184+
if (node is EnumMemberDeclaration) {
185+
insertionPoint.LineAfter = NewLineInsertion.Eol;
186+
insertionPoint.LineBefore = NewLineInsertion.None;
187+
if (args.InsertionPoint != layer.InsertionPoints.Last()) {
188+
text += ",";
189+
}
190+
}
191+
int delta = insertionPoint.Insert(target, text);
182192
output.RegisterTrackedSegments(currentScript, delta + offset);
183193
}
184194
currentScript.FormatText(nodes);
@@ -239,19 +249,6 @@ public override Task<Script> InsertWithCursor(string operation, ITypeDefinition
239249
var layer = new InsertionCursorLayer(area, operation, insertionPoints);
240250
area.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)area.TextView.InvalidateVisual);
241251

242-
if (declaringType.Kind == TypeKind.Enum) {
243-
foreach (var node in nodes.Reverse()) {
244-
int indentLevel = GetIndentLevelAt(area.Document.GetOffset(declaringType.BodyRegion.Begin));
245-
var output = OutputNode(indentLevel, node);
246-
var point = insertionPoints[0];
247-
var offset = area.Document.GetOffset(point.Location);
248-
var text = output.Text + ",";
249-
var delta = point.Insert(area.Document, text);
250-
output.RegisterTrackedSegments(script, delta + offset);
251-
}
252-
tcs.SetResult(script);
253-
return tcs.Task;
254-
}
255252
InsertWithCursorOnLayer(script, layer, tcs, nodes, area.Document);
256253
return tcs.Task;
257254
}

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
using System.Windows.Documents;
2525
using System.Windows.Input;
2626
using System.Windows.Media;
27+
2728
using ICSharpCode.AvalonEdit;
2829
using ICSharpCode.AvalonEdit.Editing;
2930
using ICSharpCode.AvalonEdit.Rendering;
3031
using ICSharpCode.AvalonEdit.Utils;
3132
using ICSharpCode.NRefactory;
3233
using ICSharpCode.SharpDevelop;
3334
using ICSharpCode.SharpDevelop.Editor;
35+
3436
namespace CSharpBinding.Refactoring
3537
{
3638
class InsertionCursorLayer : Canvas, IDisposable
@@ -41,9 +43,10 @@ class InsertionCursorLayer : Canvas, IDisposable
4143

4244
readonly TextArea editor;
4345

44-
public int CurrentInsertionPoint {
45-
get;
46-
set;
46+
public int CurrentInsertionPointIndex { get; set; }
47+
48+
public InsertionPoint[] InsertionPoints {
49+
get { return insertionPoints; }
4750
}
4851

4952
int insertionPointNextToMouse = -1;
@@ -73,8 +76,8 @@ public InsertionCursorLayer(TextArea editor, string operation, IList<InsertionPo
7376

7477
protected override void OnRender(DrawingContext drawingContext)
7578
{
76-
DrawLineForInsertionPoint(CurrentInsertionPoint, markerPen, drawingContext);
77-
if (insertionPointNextToMouse > -1 && insertionPointNextToMouse != CurrentInsertionPoint)
79+
DrawLineForInsertionPoint(CurrentInsertionPointIndex, markerPen, drawingContext);
80+
if (insertionPointNextToMouse > -1 && insertionPointNextToMouse != CurrentInsertionPointIndex)
7881
DrawLineForInsertionPoint(insertionPointNextToMouse, tempMarkerPen, drawingContext);
7982
SetGroupBoxPosition();
8083
// HACK: why OnRender() override? we could just use Line objects instead
@@ -128,7 +131,7 @@ protected override void OnMouseDown(MouseButtonEventArgs e)
128131
else {
129132
insertionPointNextToMouse = FindNextInsertionPoint(e.GetPosition(this));
130133
if (insertionPointNextToMouse >= 0)
131-
CurrentInsertionPoint = insertionPointNextToMouse;
134+
CurrentInsertionPointIndex = insertionPointNextToMouse;
132135
InvalidateVisual();
133136
}
134137
e.Handled = true;
@@ -181,9 +184,9 @@ ExecutedRoutedEventHandler MoveMarker(bool up)
181184
{
182185
return (sender, e) => {
183186
if (up)
184-
layer.CurrentInsertionPoint = Math.Max(0, layer.CurrentInsertionPoint - 1);
187+
layer.CurrentInsertionPointIndex = Math.Max(0, layer.CurrentInsertionPointIndex - 1);
185188
else
186-
layer.CurrentInsertionPoint = Math.Min(layer.insertionPoints.Length - 1, layer.CurrentInsertionPoint + 1);
189+
layer.CurrentInsertionPointIndex = Math.Min(layer.insertionPoints.Length - 1, layer.CurrentInsertionPointIndex + 1);
187190
layer.InvalidateVisual();
188191
layer.ScrollToInsertionPoint();
189192
};
@@ -192,9 +195,9 @@ ExecutedRoutedEventHandler MoveMarker(bool up)
192195
ExecutedRoutedEventHandler MoveMarkerPage(bool up)
193196
{
194197
return (sender, e) => {
195-
TextLocation current = layer.insertionPoints[layer.CurrentInsertionPoint].Location;
198+
TextLocation current = layer.insertionPoints[layer.CurrentInsertionPointIndex].Location;
196199
double currentVPos = layer.editor.TextView.GetVisualTopByDocumentLine(current.Line);
197-
int newIndex = layer.CurrentInsertionPoint;
200+
int newIndex = layer.CurrentInsertionPointIndex;
198201
double newVPos;
199202
do {
200203
if (up) {
@@ -214,7 +217,7 @@ ExecutedRoutedEventHandler MoveMarkerPage(bool up)
214217
newVPos = layer.editor.TextView.GetVisualTopByDocumentLine(layer.insertionPoints[newIndex].Location.Line);
215218
}
216219
while (Math.Abs(currentVPos - newVPos) < layer.editor.ActualHeight);
217-
layer.CurrentInsertionPoint = newIndex;
220+
layer.CurrentInsertionPointIndex = newIndex;
218221
layer.InvalidateVisual();
219222
layer.ScrollToInsertionPoint();
220223
};
@@ -224,9 +227,9 @@ ExecutedRoutedEventHandler MoveMarkerFull(bool up)
224227
{
225228
return (sender, e) => {
226229
if (up)
227-
layer.CurrentInsertionPoint = 0;
230+
layer.CurrentInsertionPointIndex = 0;
228231
else
229-
layer.CurrentInsertionPoint = layer.insertionPoints.Length - 1;
232+
layer.CurrentInsertionPointIndex = layer.insertionPoints.Length - 1;
230233
layer.InvalidateVisual();
231234
layer.ScrollToInsertionPoint();
232235
};
@@ -247,14 +250,14 @@ void InsertCode(object sender, ExecutedRoutedEventArgs e)
247250

248251
internal void ScrollToInsertionPoint()
249252
{
250-
var location = insertionPoints[CurrentInsertionPoint].Location;
253+
var location = insertionPoints[CurrentInsertionPointIndex].Location;
251254
editor.GetService<TextEditor>().ScrollTo(location.Line, location.Column);
252255
SetGroupBoxPosition();
253256
}
254257

255258
void SetGroupBoxPosition()
256259
{
257-
var boxPosition = GetLinePosition(CurrentInsertionPoint) + new Vector(editor.TextView.ActualWidth * 0.6 - 5, -groupBox.ActualHeight / 2.0);
260+
var boxPosition = GetLinePosition(CurrentInsertionPointIndex) + new Vector(editor.TextView.ActualWidth * 0.6 - 5, -groupBox.ActualHeight / 2.0);
258261
Canvas.SetTop(groupBox, boxPosition.Y);
259262
Canvas.SetLeft(groupBox, boxPosition.X);
260263
}
@@ -267,7 +270,7 @@ void Cancel(object sender, ExecutedRoutedEventArgs e)
267270
void FireExited(bool success)
268271
{
269272
if (Exited != null) {
270-
Exited(this, new InsertionCursorEventArgs(insertionPoints[CurrentInsertionPoint], success));
273+
Exited(this, new InsertionCursorEventArgs(insertionPoints[CurrentInsertionPointIndex], success));
271274
}
272275
}
273276

0 commit comments

Comments
 (0)