|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using ICSharpCode.AvalonEdit; |
|
|
using ICSharpCode.AvalonEdit.Editing; |
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
|
using System.Windows; |
|
|
using System.Windows.Media; |
|
|
using System.Windows.Threading; |
|
|
using System.Windows.Input; |
|
|
using System.Threading; |
|
|
using System.Diagnostics; |
|
|
|
|
|
namespace PythonConsoleControl |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public class PythonTextEditor |
|
|
{ |
|
|
internal TextEditor textEditor; |
|
|
internal TextArea textArea; |
|
|
StringBuilder writeBuffer = new StringBuilder(); |
|
|
volatile bool writeInProgress = false; |
|
|
PythonConsoleCompletionWindow completionWindow = null; |
|
|
int completionEventIndex = 0; |
|
|
int descriptionEventIndex = 1; |
|
|
WaitHandle[] completionWaitHandles; |
|
|
AutoResetEvent completionRequestedEvent = new AutoResetEvent(false); |
|
|
AutoResetEvent descriptionRequestedEvent = new AutoResetEvent(false); |
|
|
Thread completionThread; |
|
|
PythonConsoleCompletionDataProvider completionProvider = null; |
|
|
|
|
|
public PythonTextEditor(TextEditor textEditor) |
|
|
{ |
|
|
this.textEditor = textEditor; |
|
|
this.textArea = textEditor.TextArea; |
|
|
completionWaitHandles = new WaitHandle[] { completionRequestedEvent, descriptionRequestedEvent }; |
|
|
completionThread = new Thread(new ThreadStart(Completion)); |
|
|
completionThread.Priority = ThreadPriority.Lowest; |
|
|
|
|
|
completionThread.IsBackground = true; |
|
|
completionThread.Start(); |
|
|
} |
|
|
|
|
|
public bool WriteInProgress |
|
|
{ |
|
|
get { return writeInProgress; } |
|
|
} |
|
|
|
|
|
public ICollection<CommandBinding> CommandBindings |
|
|
{ |
|
|
get { return (this.textArea.ActiveInputHandler as TextAreaDefaultInputHandler).CommandBindings; } |
|
|
} |
|
|
|
|
|
public void Write(string text) |
|
|
{ |
|
|
Write(text, false); |
|
|
} |
|
|
|
|
|
Stopwatch sw; |
|
|
|
|
|
public void Write(string text, bool allowSynchronous) |
|
|
{ |
|
|
|
|
|
text = text.Replace("\r\r\n", "\r"); |
|
|
text = text.Replace("\r\n", "\r"); |
|
|
if (allowSynchronous) |
|
|
{ |
|
|
MoveToEnd(); |
|
|
PerformTextInput(text); |
|
|
return; |
|
|
} |
|
|
lock (writeBuffer) |
|
|
{ |
|
|
writeBuffer.Append(text); |
|
|
} |
|
|
if (!writeInProgress) |
|
|
{ |
|
|
writeInProgress = true; |
|
|
ThreadPool.QueueUserWorkItem(new WaitCallback(CheckAndOutputWriteBuffer)); |
|
|
sw = Stopwatch.StartNew(); |
|
|
} |
|
|
} |
|
|
|
|
|
private void CheckAndOutputWriteBuffer(Object stateInfo) |
|
|
{ |
|
|
AutoResetEvent writeCompletedEvent = new AutoResetEvent(false); |
|
|
Action action = new Action(delegate() |
|
|
{ |
|
|
string toWrite; |
|
|
lock (writeBuffer) |
|
|
{ |
|
|
toWrite = writeBuffer.ToString(); |
|
|
writeBuffer.Remove(0, writeBuffer.Length); |
|
|
|
|
|
} |
|
|
MoveToEnd(); |
|
|
PerformTextInput(toWrite); |
|
|
writeCompletedEvent.Set(); |
|
|
}); |
|
|
while (true) |
|
|
{ |
|
|
|
|
|
textArea.Dispatcher.BeginInvoke(action, DispatcherPriority.Normal); |
|
|
|
|
|
writeCompletedEvent.WaitOne(); |
|
|
lock (writeBuffer) |
|
|
{ |
|
|
if (writeBuffer.Length == 0) |
|
|
{ |
|
|
writeInProgress = false; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private void MoveToEnd() |
|
|
{ |
|
|
int lineCount = textArea.Document.LineCount; |
|
|
if (textArea.Caret.Line != lineCount) textArea.Caret.Line = textArea.Document.LineCount; |
|
|
int column = textArea.Document.Lines[lineCount - 1].Length + 1; |
|
|
if (textArea.Caret.Column != column) textArea.Caret.Column = column; |
|
|
} |
|
|
|
|
|
private void PerformTextInput(string text) |
|
|
{ |
|
|
if (text == "\n" || text == "\r\n") |
|
|
{ |
|
|
string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line); |
|
|
this.textEditor.AppendText(newLine); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
else |
|
|
this.textEditor.AppendText(text); |
|
|
textArea.Caret.BringCaretToView(); |
|
|
} |
|
|
|
|
|
public int Column |
|
|
{ |
|
|
get { return textArea.Caret.Column; } |
|
|
set { textArea.Caret.Column = value; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int Line |
|
|
{ |
|
|
get { return textArea.Caret.Line; } |
|
|
set { textArea.Caret.Line = value; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int TotalLines |
|
|
{ |
|
|
get { return textArea.Document.LineCount; } |
|
|
} |
|
|
|
|
|
public delegate string StringAction(); |
|
|
|
|
|
|
|
|
|
|
|
public string GetLine(int index) |
|
|
{ |
|
|
return (string)textArea.Dispatcher.Invoke(new StringAction(delegate() |
|
|
{ |
|
|
DocumentLine line = textArea.Document.Lines[index]; |
|
|
return textArea.Document.GetText(line); |
|
|
})); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Replace(int index, int length, string text) |
|
|
{ |
|
|
|
|
|
int currentLine = textArea.Document.LineCount - 1; |
|
|
int startOffset = textArea.Document.Lines[currentLine].Offset; |
|
|
textArea.Document.Replace(startOffset + index, length, text); |
|
|
} |
|
|
|
|
|
public event TextCompositionEventHandler TextEntering |
|
|
{ |
|
|
add { textArea.TextEntering += value; } |
|
|
remove { textArea.TextEntering -= value; } |
|
|
} |
|
|
|
|
|
public event TextCompositionEventHandler TextEntered |
|
|
{ |
|
|
add { textArea.TextEntered += value; } |
|
|
remove { textArea.TextEntered -= value; } |
|
|
} |
|
|
|
|
|
public event KeyEventHandler PreviewKeyDown |
|
|
{ |
|
|
add { textArea.PreviewKeyDown += value; } |
|
|
remove { textArea.PreviewKeyDown -= value; } |
|
|
} |
|
|
|
|
|
public int SelectionStart |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return textArea.Selection.SurroundingSegment.Offset; |
|
|
} |
|
|
} |
|
|
|
|
|
public int SelectionLength |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return textArea.Selection.Length; |
|
|
} |
|
|
} |
|
|
|
|
|
public bool SelectionIsMultiline |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return textArea.Selection.IsMultiline; |
|
|
} |
|
|
} |
|
|
|
|
|
public int SelectionStartColumn |
|
|
{ |
|
|
get |
|
|
{ |
|
|
int startOffset = textArea.Selection.SurroundingSegment.Offset; |
|
|
return startOffset - textArea.Document.GetLineByOffset(startOffset).Offset + 1; |
|
|
} |
|
|
} |
|
|
|
|
|
public int SelectionEndColumn |
|
|
{ |
|
|
get |
|
|
{ |
|
|
int endOffset = textArea.Selection.SurroundingSegment.EndOffset; |
|
|
return endOffset - textArea.Document.GetLineByOffset(endOffset).Offset + 1; |
|
|
} |
|
|
} |
|
|
|
|
|
public PythonConsoleCompletionDataProvider CompletionProvider |
|
|
{ |
|
|
get { return completionProvider; } |
|
|
set { completionProvider = value; } |
|
|
} |
|
|
|
|
|
public Thread CompletionThread |
|
|
{ |
|
|
get { return completionThread; } |
|
|
} |
|
|
|
|
|
public bool StopCompletion() |
|
|
{ |
|
|
if (completionProvider.AutocompletionInProgress) |
|
|
{ |
|
|
|
|
|
completionThread.Abort(new Microsoft.Scripting.KeyboardInterruptException("")); |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
public PythonConsoleCompletionWindow CompletionWindow |
|
|
{ |
|
|
get { return completionWindow; } |
|
|
} |
|
|
|
|
|
public void ShowCompletionWindow() |
|
|
{ |
|
|
completionRequestedEvent.Set(); |
|
|
} |
|
|
|
|
|
public void UpdateCompletionDescription() |
|
|
{ |
|
|
descriptionRequestedEvent.Set(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Completion() |
|
|
{ |
|
|
while (true) |
|
|
{ |
|
|
int action = WaitHandle.WaitAny(completionWaitHandles); |
|
|
if (action == completionEventIndex && completionProvider != null) BackgroundShowCompletionWindow(); |
|
|
if (action == descriptionEventIndex && completionProvider != null && completionWindow != null) BackgroundUpdateCompletionDescription(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void BackgroundShowCompletionWindow() |
|
|
{ |
|
|
|
|
|
string itemForCompletion = ""; |
|
|
textArea.Dispatcher.Invoke(new Action(delegate() |
|
|
{ |
|
|
DocumentLine line = textArea.Document.Lines[textArea.Caret.Line - 1]; |
|
|
itemForCompletion = textArea.Document.GetText(line); |
|
|
})); |
|
|
|
|
|
ICompletionData[] completions = completionProvider.GenerateCompletionData(itemForCompletion); |
|
|
|
|
|
if (completions != null && completions.Length > 0) textArea.Dispatcher.BeginInvoke(new Action(delegate() |
|
|
{ |
|
|
completionWindow = new PythonConsoleCompletionWindow(textArea, this); |
|
|
IList<ICompletionData> data = completionWindow.CompletionList.CompletionData; |
|
|
foreach (ICompletionData completion in completions) |
|
|
{ |
|
|
data.Add(completion); |
|
|
} |
|
|
completionWindow.Show(); |
|
|
completionWindow.Closed += delegate |
|
|
{ |
|
|
completionWindow = null; |
|
|
}; |
|
|
})); |
|
|
|
|
|
} |
|
|
|
|
|
internal void BackgroundUpdateCompletionDescription() |
|
|
{ |
|
|
completionWindow.UpdateCurrentItemDescription(); |
|
|
} |
|
|
|
|
|
public void RequestCompletioninsertion(TextCompositionEventArgs e) |
|
|
{ |
|
|
if (completionWindow != null) completionWindow.CompletionList.RequestInsertion(e); |
|
|
|
|
|
StopCompletion(); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|