|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Diagnostics; |
|
|
using System.Windows; |
|
|
using System.Windows.Controls; |
|
|
using System.Windows.Controls.Primitives; |
|
|
using System.Windows.Data; |
|
|
using System.Windows.Input; |
|
|
using System.Windows.Threading; |
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
using ICSharpCode.AvalonEdit.Editing; |
|
|
using ICSharpCode.AvalonEdit.Rendering; |
|
|
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
|
using System.Reflection; |
|
|
using System.ComponentModel; |
|
|
|
|
|
namespace PythonConsoleControl |
|
|
{ |
|
|
public delegate void DescriptionUpdateDelegate(string description); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PythonConsoleCompletionWindow : CompletionWindowBase |
|
|
{ |
|
|
readonly CompletionList completionList = new CompletionList(); |
|
|
ToolTip toolTip = new ToolTip(); |
|
|
DispatcherTimer updateDescription; |
|
|
TimeSpan updateDescriptionInterval; |
|
|
PythonTextEditor textEditor; |
|
|
PythonConsoleCompletionDataProvider completionDataProvider; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CompletionList CompletionList |
|
|
{ |
|
|
get { return completionList; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PythonConsoleCompletionWindow(TextArea textArea, PythonTextEditor textEditor) |
|
|
: base(textArea) |
|
|
{ |
|
|
|
|
|
this.completionDataProvider = textEditor.CompletionProvider; |
|
|
this.textEditor = textEditor; |
|
|
this.CloseAutomatically = true; |
|
|
this.SizeToContent = SizeToContent.Height; |
|
|
this.MaxHeight = 300; |
|
|
this.Width = 250; |
|
|
this.Content = completionList; |
|
|
|
|
|
this.MinHeight = 15; |
|
|
this.MinWidth = 30; |
|
|
|
|
|
toolTip.PlacementTarget = this; |
|
|
toolTip.Placement = PlacementMode.Right; |
|
|
toolTip.Closed += toolTip_Closed; |
|
|
|
|
|
completionList.InsertionRequested += completionList_InsertionRequested; |
|
|
completionList.SelectionChanged += completionList_SelectionChanged; |
|
|
AttachEvents(); |
|
|
|
|
|
updateDescription = new DispatcherTimer(); |
|
|
updateDescription.Tick += new EventHandler(completionList_UpdateDescription); |
|
|
updateDescriptionInterval = TimeSpan.FromSeconds(0.3); |
|
|
|
|
|
EventInfo eventInfo = typeof(TextView).GetEvent("ScrollOffsetChanged"); |
|
|
Delegate methodDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, (this as CompletionWindowBase), "TextViewScrollOffsetChanged"); |
|
|
eventInfo.RemoveEventHandler(this.TextArea.TextView, methodDelegate); |
|
|
} |
|
|
|
|
|
#region ToolTip handling |
|
|
void toolTip_Closed(object sender, RoutedEventArgs e) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if (toolTip != null) |
|
|
toolTip.Content = null; |
|
|
} |
|
|
|
|
|
void completionList_SelectionChanged(object sender, SelectionChangedEventArgs e) |
|
|
{ |
|
|
var item = completionList.SelectedItem; |
|
|
if (item == null) |
|
|
{ |
|
|
updateDescription.Stop(); |
|
|
return; |
|
|
} |
|
|
else |
|
|
{ |
|
|
updateDescription.Interval = updateDescriptionInterval; |
|
|
updateDescription.Start(); |
|
|
} |
|
|
} |
|
|
|
|
|
void completionList_UpdateDescription(Object sender, EventArgs e) |
|
|
{ |
|
|
updateDescription.Stop(); |
|
|
textEditor.UpdateCompletionDescription(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void UpdateCurrentItemDescription() |
|
|
{ |
|
|
if (textEditor.StopCompletion()) |
|
|
{ |
|
|
updateDescription.Interval = updateDescriptionInterval; |
|
|
updateDescription.Start(); |
|
|
return; |
|
|
} |
|
|
string stub = ""; |
|
|
string item = ""; |
|
|
bool isInstance = false; |
|
|
textEditor.textEditor.Dispatcher.Invoke(new Action(delegate() |
|
|
{ |
|
|
PythonCompletionData data = (completionList.SelectedItem as PythonCompletionData); |
|
|
if (data == null || toolTip == null) |
|
|
return; |
|
|
stub = data.Stub; |
|
|
item = data.Text; |
|
|
isInstance = data.IsInstance; |
|
|
})); |
|
|
|
|
|
completionDataProvider.GenerateDescription(stub, item, completionList_WriteDescription, isInstance); |
|
|
} |
|
|
|
|
|
void completionList_WriteDescription(string description) |
|
|
{ |
|
|
textEditor.textEditor.Dispatcher.Invoke(new Action(delegate() { |
|
|
if (toolTip != null) |
|
|
{ |
|
|
if (description != null) |
|
|
{ |
|
|
toolTip.Content = description; |
|
|
toolTip.IsOpen = true; |
|
|
} |
|
|
else |
|
|
{ |
|
|
toolTip.IsOpen = false; |
|
|
} |
|
|
} |
|
|
})); |
|
|
} |
|
|
|
|
|
#endregion |
|
|
|
|
|
void completionList_InsertionRequested(object sender, EventArgs e) |
|
|
{ |
|
|
Close(); |
|
|
|
|
|
|
|
|
var item = completionList.SelectedItem; |
|
|
if (item != null) |
|
|
item.Complete(this.TextArea, new AnchorSegment(this.TextArea.Document, this.StartOffset, this.EndOffset - this.StartOffset), e); |
|
|
} |
|
|
|
|
|
void AttachEvents() |
|
|
{ |
|
|
this.TextArea.Caret.PositionChanged += CaretPositionChanged; |
|
|
this.TextArea.MouseWheel += textArea_MouseWheel; |
|
|
this.TextArea.PreviewTextInput += textArea_PreviewTextInput; |
|
|
} |
|
|
|
|
|
|
|
|
protected override void DetachEvents() |
|
|
{ |
|
|
this.TextArea.Caret.PositionChanged -= CaretPositionChanged; |
|
|
this.TextArea.MouseWheel -= textArea_MouseWheel; |
|
|
this.TextArea.PreviewTextInput -= textArea_PreviewTextInput; |
|
|
base.DetachEvents(); |
|
|
} |
|
|
|
|
|
|
|
|
protected override void OnClosed(EventArgs e) |
|
|
{ |
|
|
base.OnClosed(e); |
|
|
if (toolTip != null) |
|
|
{ |
|
|
toolTip.IsOpen = false; |
|
|
toolTip = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected override void OnKeyDown(KeyEventArgs e) |
|
|
{ |
|
|
base.OnKeyDown(e); |
|
|
if (!e.Handled) |
|
|
{ |
|
|
completionList.HandleKey(e); |
|
|
} |
|
|
} |
|
|
|
|
|
void textArea_PreviewTextInput(object sender, TextCompositionEventArgs e) |
|
|
{ |
|
|
e.Handled = RaiseEventPair(this, PreviewTextInputEvent, TextInputEvent, |
|
|
new TextCompositionEventArgs(e.Device, e.TextComposition)); |
|
|
} |
|
|
|
|
|
void textArea_MouseWheel(object sender, MouseWheelEventArgs e) |
|
|
{ |
|
|
e.Handled = RaiseEventPair(GetScrollEventTarget(), |
|
|
PreviewMouseWheelEvent, MouseWheelEvent, |
|
|
new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)); |
|
|
} |
|
|
|
|
|
UIElement GetScrollEventTarget() |
|
|
{ |
|
|
if (completionList == null) |
|
|
return this; |
|
|
return completionList.ScrollViewer ?? completionList.ListBox ?? (UIElement)completionList; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool CloseAutomatically { get; set; } |
|
|
|
|
|
|
|
|
protected override bool CloseOnFocusLost |
|
|
{ |
|
|
get { return this.CloseAutomatically; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool CloseWhenCaretAtBeginning { get; set; } |
|
|
|
|
|
void CaretPositionChanged(object sender, EventArgs e) |
|
|
{ |
|
|
int offset = this.TextArea.Caret.Offset; |
|
|
if (offset == this.StartOffset) |
|
|
{ |
|
|
if (CloseAutomatically && CloseWhenCaretAtBeginning) |
|
|
{ |
|
|
Close(); |
|
|
} |
|
|
else |
|
|
{ |
|
|
completionList.SelectItem(string.Empty); |
|
|
} |
|
|
return; |
|
|
} |
|
|
if (offset < this.StartOffset || offset > this.EndOffset) |
|
|
{ |
|
|
if (CloseAutomatically) |
|
|
{ |
|
|
Close(); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
TextDocument document = this.TextArea.Document; |
|
|
if (document != null) |
|
|
{ |
|
|
completionList.SelectItem(document.GetText(this.StartOffset, offset - this.StartOffset)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|