using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Tango.Core.Commands;
using Tango.SharedUI;
using Tango.SharedUI.Helpers;
namespace Tango.SharedUI.Controls
{
///
/// Represents a C# script editor control.
///
///
///
public partial class ScriptEditorControl : UserControl
{
#region Completion
///
/// Represents an auto complete item.
///
///
internal class CompletionData : ICompletionData
{
private String _description;
///
/// Gets or sets the icon source.
///
public BitmapSource Source { get; set; }
///
/// Initializes a new instance of the class.
///
/// The text.
/// The description.
public CompletionData(string text, String description)
{
this.Text = text;
_description = description;
}
///
/// Gets the image.
///
public System.Windows.Media.ImageSource Image
{
get { return Source; }
}
///
/// Gets the text. This property is used to filter the list of visible elements.
///
public string Text { get; private set; }
// Use this property if you want to show a fancy UIElement in the drop down list.
public object Content
{
get { return this.Text; }
}
///
/// Gets the description.
///
public object Description
{
get { return _description; }
}
///
/// Gets the priority. This property is used in the selection logic. You can use it to prefer selecting those items
/// which the user is accessing most frequently.
///
public double Priority { get { return 0; } }
///
/// Perform the completion.
///
/// The text area on which completion is performed.
/// The text segment that was used by the completion window if
/// the user types (segment between CompletionWindow.StartOffset and CompletionWindow.EndOffset).
/// The EventArgs used for the insertion request.
/// These can be TextCompositionEventArgs, KeyEventArgs, MouseEventArgs, depending on how
/// the insertion was triggered.
public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
{
textArea.Document.Replace(completionSegment, this.Text);
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return Text;
}
}
#endregion
private CompletionWindow completionWindow; //Holds the auto-complete window instance.
#region Constructors
///
/// Initializes a new instance of the class.
///
public ScriptEditorControl()
{
InitializeComponent();
textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy();
textEditor.TextArea.TextEntering += textEditor_TextArea_TextEntering;
textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered;
}
#endregion
#region Event Handlers
///
/// Handles the TextEntered event of the textEditor_TextArea control.
///
/// The source of the event.
/// The instance containing the event data.
private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e)
{
if (e.Text == ".")
{
String keyword = textEditor.TextArea.GetJustCurrentWord();
if (keyword != null)
{
completionWindow = new CompletionWindow(textEditor.TextArea);
completionWindow.WindowStyle = WindowStyle.None;
completionWindow.AllowsTransparency = true;
completionWindow.ResizeMode = ResizeMode.NoResize;
IList data = completionWindow.CompletionList.CompletionData;
bool ok = false;
List> types = new List>();
types.Add(new KeyValuePair("Thread", typeof(Thread)));
types.Add(new KeyValuePair("DateTime", typeof(DateTime)));
types.Add(new KeyValuePair("TimeSpan", typeof(TimeSpan)));
types.Add(new KeyValuePair("Dispatcher", typeof(Dispatcher)));
types.Add(new KeyValuePair("Task", typeof(Task)));
types.Add(new KeyValuePair("List", typeof(IList