|
|
|
@ -3,6 +3,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
using CSharpBinding.Parser; |
|
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
|
using ICSharpCode.NRefactory; |
|
|
|
using ICSharpCode.NRefactory; |
|
|
|
using ICSharpCode.NRefactory.CSharp; |
|
|
|
using ICSharpCode.NRefactory.CSharp; |
|
|
|
@ -10,6 +11,7 @@ using ICSharpCode.NRefactory.CSharp.Refactoring; |
|
|
|
using ICSharpCode.NRefactory.Editor; |
|
|
|
using ICSharpCode.NRefactory.Editor; |
|
|
|
using ICSharpCode.NRefactory.TypeSystem; |
|
|
|
using ICSharpCode.NRefactory.TypeSystem; |
|
|
|
using ICSharpCode.SharpDevelop.Editor; |
|
|
|
using ICSharpCode.SharpDevelop.Editor; |
|
|
|
|
|
|
|
using ICSharpCode.SharpDevelop.Parser; |
|
|
|
|
|
|
|
|
|
|
|
namespace CSharpBinding.Refactoring |
|
|
|
namespace CSharpBinding.Refactoring |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -20,33 +22,32 @@ namespace CSharpBinding.Refactoring |
|
|
|
{ |
|
|
|
{ |
|
|
|
int indentationSize = 4; |
|
|
|
int indentationSize = 4; |
|
|
|
readonly ITextEditor editor; |
|
|
|
readonly ITextEditor editor; |
|
|
|
readonly TextSegmentCollection<TextSegment> textSegmentCollection = new TextSegmentCollection<TextSegment>(); |
|
|
|
readonly IDocument originalDocument; |
|
|
|
readonly OffsetChangeMap offsetChangeMap = new OffsetChangeMap(); |
|
|
|
readonly TextSegmentCollection<TextSegment> textSegmentCollection; |
|
|
|
readonly List<Action> actions = new List<Action>(); |
|
|
|
readonly IDisposable undoGroup; |
|
|
|
|
|
|
|
|
|
|
|
public SDScript(ITextEditor editor, string eolMarker) : base(eolMarker, new CSharpFormattingOptions()) |
|
|
|
public SDScript(ITextEditor editor, string eolMarker) : base(eolMarker, new CSharpFormattingOptions()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
this.editor = editor; |
|
|
|
this.editor = editor; |
|
|
|
|
|
|
|
this.textSegmentCollection = new TextSegmentCollection<TextSegment>((TextDocument)editor.Document); |
|
|
|
|
|
|
|
this.originalDocument = editor.Document.CreateDocumentSnapshot(); |
|
|
|
|
|
|
|
undoGroup = editor.Document.OpenUndoGroup(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override int GetCurrentOffset(TextLocation originalDocumentLocation) |
|
|
|
public override int GetCurrentOffset(TextLocation originalDocumentLocation) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int offset = editor.Document.GetOffset(originalDocumentLocation); |
|
|
|
int offset = originalDocument.GetOffset(originalDocumentLocation); |
|
|
|
return offsetChangeMap.GetNewOffset(offset, AnchorMovementType.Default); |
|
|
|
return GetCurrentOffset(offset); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override int GetCurrentOffset(int originalDocumentOffset) |
|
|
|
public override int GetCurrentOffset(int originalDocumentOffset) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return offsetChangeMap.GetNewOffset(originalDocumentOffset, AnchorMovementType.Default); |
|
|
|
return originalDocument.Version.MoveOffsetTo(editor.Document.Version, originalDocumentOffset, AnchorMovementType.Default); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void Replace(int offset, int length, string newText) |
|
|
|
public override void Replace(int offset, int length, string newText) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var changeMapEntry = new OffsetChangeMapEntry(offset, length, newText.Length); |
|
|
|
editor.Document.Replace(offset, length, newText); |
|
|
|
textSegmentCollection.UpdateOffsets(changeMapEntry); |
|
|
|
|
|
|
|
offsetChangeMap.Add(changeMapEntry); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
actions.Add(delegate { editor.Document.Replace(offset, length, newText); }); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected override ISegment CreateTrackedSegment(int offset, int length) |
|
|
|
protected override ISegment CreateTrackedSegment(int offset, int length) |
|
|
|
@ -60,7 +61,7 @@ namespace CSharpBinding.Refactoring |
|
|
|
|
|
|
|
|
|
|
|
protected override int GetIndentLevelAt(int offset) |
|
|
|
protected override int GetIndentLevelAt(int offset) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int oldOffset = offsetChangeMap.Invert().GetNewOffset(offset, AnchorMovementType.Default); |
|
|
|
int oldOffset = editor.Document.Version.MoveOffsetTo(originalDocument.Version, offset, AnchorMovementType.Default); |
|
|
|
var line = editor.Document.GetLineByOffset(oldOffset); |
|
|
|
var line = editor.Document.GetLineByOffset(oldOffset); |
|
|
|
int spaces = 0; |
|
|
|
int spaces = 0; |
|
|
|
int indentationLevel = 0; |
|
|
|
int indentationLevel = 0; |
|
|
|
@ -84,29 +85,53 @@ namespace CSharpBinding.Refactoring |
|
|
|
|
|
|
|
|
|
|
|
public override void Rename(IEntity entity, string name) |
|
|
|
public override void Rename(IEntity entity, string name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// TODO
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void InsertWithCursor(string operation, AstNode node, InsertPosition defaultPosition) |
|
|
|
public override void InsertWithCursor(string operation, AstNode node, InsertPosition defaultPosition) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// TODO
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void FormatText(int offset, int length) |
|
|
|
public override void FormatText(int offset, int length) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
var parseInfo = ParserService.Parse(editor.FileName, editor.Document) as CSharpFullParseInformation; |
|
|
|
|
|
|
|
if (parseInfo != null) { |
|
|
|
|
|
|
|
//var startLocation = editor.Document.GetLocation(offset);
|
|
|
|
|
|
|
|
//var endLocation = editor.Document.GetLocation(offset + length);
|
|
|
|
|
|
|
|
//var node = parseInfo.CompilationUnit.GetNodeContaining(startLocation, endLocation);
|
|
|
|
|
|
|
|
var formatter = new AstFormattingVisitor(new CSharpFormattingOptions(), editor.Document, false, 4); |
|
|
|
|
|
|
|
parseInfo.CompilationUnit.AcceptVisitor(formatter); |
|
|
|
|
|
|
|
formatter.ApplyChanges(offset, length); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void Select(AstNode node) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var segment = GetSegment(node); |
|
|
|
|
|
|
|
int startOffset = segment.Offset; |
|
|
|
|
|
|
|
int endOffset = segment.EndOffset; |
|
|
|
|
|
|
|
// If the area to select includes a newline (e.g. node is a statement),
|
|
|
|
|
|
|
|
// exclude that newline from the selection.
|
|
|
|
|
|
|
|
if (endOffset > startOffset && editor.Document.GetLineByOffset(endOffset).Offset == endOffset) { |
|
|
|
|
|
|
|
endOffset = editor.Document.GetLineByOffset(endOffset).PreviousLine.EndOffset; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
editor.Select(startOffset, endOffset - startOffset); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void Select(int offset, int length) |
|
|
|
public override void Select(int offset, int length) |
|
|
|
{ |
|
|
|
{ |
|
|
|
actions.Add(delegate { editor.Select(offset, length); }); |
|
|
|
editor.Select(offset, length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void Link(params AstNode[] nodes) |
|
|
|
public override void Link(params AstNode[] nodes) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// TODO
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override void Dispose() |
|
|
|
public override void Dispose() |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach (var action in actions) |
|
|
|
undoGroup.Dispose(); |
|
|
|
action(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|