File size: 11,561 Bytes
b1b3bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
// Copyright (c) 2010 Joe Moorhouse
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
{
/// <summary>
/// Interface console to AvalonEdit and handle autocompletion.
/// </summary>
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.SetApartmentState(ApartmentState.STA);
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\n");
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);
//writeBuffer.Clear();
}
MoveToEnd();
PerformTextInput(toWrite);
writeCompletedEvent.Set();
});
while (true)
{
// Clear writeBuffer and write out.
textArea.Dispatcher.BeginInvoke(action, DispatcherPriority.Normal);
// Check if writeBuffer has refilled in the meantime; if so clear and write out again.
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);
//using (textArea.Document.RunUpdate())
//{
// textArea.Selection.ReplaceSelectionWithText(textArea, newLine);
//}
}
else
this.textEditor.AppendText(text);
textArea.Caret.BringCaretToView();
}
public int Column
{
get { return textArea.Caret.Column; }
set { textArea.Caret.Column = value; }
}
/// <summary>
/// Gets the current cursor line.
/// </summary>
public int Line
{
get { return textArea.Caret.Line; }
set { textArea.Caret.Line = value; }
}
/// <summary>
/// Gets the total number of lines in the text editor.
/// </summary>
public int TotalLines
{
get { return textArea.Document.LineCount; }
}
public delegate string StringAction();
/// <summary>
/// Gets the text for the specified line.
/// </summary>
public string GetLine(int index)
{
return (string)textArea.Dispatcher.Invoke(new StringAction(delegate()
{
DocumentLine line = textArea.Document.Lines[index];
return textArea.Document.GetText(line);
}));
}
/// <summary>
/// Replaces the text at the specified index on the current line with the specified text.
/// </summary>
public void Replace(int index, int length, string text)
{
//int currentLine = textArea.Caret.Line - 1;
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)
{
// send Ctrl-C abort
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();
}
/// <summary>
/// Perform completion actions on the background completion thread.
/// </summary>
void Completion()
{
while (true)
{
int action = WaitHandle.WaitAny(completionWaitHandles);
if (action == completionEventIndex && completionProvider != null) BackgroundShowCompletionWindow();
if (action == descriptionEventIndex && completionProvider != null && completionWindow != null) BackgroundUpdateCompletionDescription();
}
}
/// <summary>
/// Obtain completions (this runs in its own thread)
/// </summary>
internal void BackgroundShowCompletionWindow() //ICompletionItemProvider
{
// provide AvalonEdit with the data:
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);
// if autocompletion still in progress, terminate
StopCompletion();
}
}
}
|