Browse Source

fix #300: Implement 'Link Mode' (Script.Link)

pull/331/head
Siegfried Pammer 12 years ago
parent
commit
f6fd05a7eb
  1. 28
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/EditorScript.cs
  2. 20
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs

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

@ -30,6 +30,7 @@ using System.Windows.Threading; @@ -30,6 +30,7 @@ using System.Windows.Threading;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Snippets;
using CSharpBinding.Parser;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.NRefactory;
@ -82,12 +83,31 @@ namespace CSharpBinding.Refactoring @@ -82,12 +83,31 @@ namespace CSharpBinding.Refactoring
editor.Select(startOffset, endOffset - startOffset);
}
static readonly Task completedTask = Task.FromResult<object>(null);
public override Task Link(params AstNode[] nodes)
{
// TODO
return completedTask;
var segs = nodes.Select(node => GetSegment(node)).ToArray();
InsertionContext c = new InsertionContext(editor.GetRequiredService<TextArea>(), segs.Min(seg => seg.Offset));
c.InsertionPosition = segs.Max(seg => seg.EndOffset);
var tcs = new TaskCompletionSource<bool>();
c.Deactivated += (sender, e) => tcs.SetResult(true);
if (segs.Length > 0) {
// try to use node in identifier context to avoid the code completion popup.
var identifier = nodes.OfType<Identifier>().FirstOrDefault();
ISegment first;
if (identifier == null)
first = segs[0];
else
first = GetSegment(identifier);
c.Link(first, segs.Except(new[]{first}).ToArray());
c.RaiseInsertionCompleted(EventArgs.Empty);
} else {
c.RaiseInsertionCompleted(EventArgs.Empty);
c.Deactivate(new SnippetEventArgs(DeactivateReason.NoActiveElements));
}
return tcs.Task;
}
public override Task<Script> InsertWithCursor(string operation, InsertPosition defaultPosition, IList<AstNode> nodes)

20
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs

@ -278,5 +278,25 @@ namespace ICSharpCode.AvalonEdit.Snippets @@ -278,5 +278,25 @@ namespace ICSharpCode.AvalonEdit.Snippets
}
return false;
}
/// <summary>
/// Adds existing segments as snippet elements.
/// </summary>
public void Link(ISegment mainElement, ISegment[] boundElements)
{
var main = new SnippetReplaceableTextElement { Text = Document.GetText(mainElement) };
RegisterActiveElement(main, new ReplaceableActiveElement(this, mainElement.Offset, mainElement.EndOffset));
foreach (var boundElement in boundElements) {
var bound = new SnippetBoundElement { TargetElement = main };
var start = Document.CreateAnchor(boundElement.Offset);
start.MovementType = AnchorMovementType.BeforeInsertion;
start.SurviveDeletion = true;
var end = Document.CreateAnchor(boundElement.EndOffset);
end.MovementType = AnchorMovementType.BeforeInsertion;
end.SurviveDeletion = true;
RegisterActiveElement(bound, new BoundActiveElement(this, main, bound, new AnchorSegment(start, end)));
}
}
}
}

Loading…
Cancel
Save