public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg) {
if (!REditorSettings.FormatOnPaste || TextView.Selection.Mode != TextSelectionMode.Stream) {
return CommandResult.NotSupported;
}
string text = ClipboardDataProvider.GetData(DataFormats.UnicodeText) as string;
if (text == null) {
text = ClipboardDataProvider.GetData(DataFormats.Text) as string;
}
if (text != null) {
var rSpans = TextView.BufferGraph.MapDownToFirstMatch(
TextView.Selection.StreamSelectionSpan.SnapshotSpan,
SpanTrackingMode.EdgeInclusive,
snapshot => snapshot.TextBuffer.ContentType.IsOfType(RContentTypeDefinition.ContentType)
);
if (rSpans.Count > 0) {
var targetSpan = rSpans[rSpans.Count - 1];
IREditorDocument document = REditorDocument.TryFromTextBuffer(targetSpan.Snapshot.TextBuffer);
if (document != null) {
int insertionPoint = targetSpan.Start;
document.TextBuffer.Replace(targetSpan, text);
document.EditorTree.EnsureTreeReady();
// We don't want to format inside strings
if (!document.EditorTree.AstRoot.IsPositionInsideString(insertionPoint)) {
RangeFormatter.FormatRange(TextView, document.TextBuffer,
new TextRange(insertionPoint, text.Length), REditorSettings.FormatOptions, EditorShell);
}
}
}
}
return CommandResult.Executed;
}
}