|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using ICSharpCode.AvalonEdit.CodeCompletion; |
|
|
using ICSharpCode.AvalonEdit; |
|
|
using ICSharpCode.AvalonEdit.Editing; |
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
using Microsoft.Scripting.Hosting.Shell; |
|
|
using Microsoft.Scripting.Hosting; |
|
|
using Microsoft.Scripting; |
|
|
using System.Threading; |
|
|
using System.Reflection; |
|
|
|
|
|
namespace PythonConsoleControl |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public class PythonConsoleCompletionDataProvider |
|
|
{ |
|
|
CommandLine commandLine; |
|
|
internal volatile bool AutocompletionInProgress = false; |
|
|
|
|
|
bool excludeCallables; |
|
|
public bool ExcludeCallables { get { return excludeCallables; } set { excludeCallables = value; } } |
|
|
|
|
|
public PythonConsoleCompletionDataProvider(CommandLine commandLine) |
|
|
{ |
|
|
this.commandLine = commandLine; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ICompletionData[] GenerateCompletionData(string line) |
|
|
{ |
|
|
List<PythonCompletionData> items = new List<PythonCompletionData>(); |
|
|
|
|
|
string name = GetName(line); |
|
|
|
|
|
if (excludeCallables && name.Contains(')')) return null; |
|
|
|
|
|
if (!String.IsNullOrEmpty(name)) |
|
|
{ |
|
|
System.IO.Stream stream = commandLine.ScriptScope.Engine.Runtime.IO.OutputStream; |
|
|
try |
|
|
{ |
|
|
AutocompletionInProgress = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Type type = TryGetType(name); |
|
|
|
|
|
if (type != null && type.Namespace != "IronPython.Runtime" && (type.Name != "__ComObject")) |
|
|
{ |
|
|
PopulateFromCLRType(items, type, name); |
|
|
} |
|
|
} |
|
|
catch (ThreadAbortException tae) |
|
|
{ |
|
|
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort(); |
|
|
} |
|
|
catch |
|
|
{ |
|
|
|
|
|
} |
|
|
commandLine.ScriptScope.Engine.Runtime.IO.SetOutput(stream, Encoding.UTF8); |
|
|
AutocompletionInProgress = false; |
|
|
} |
|
|
return items.ToArray(); |
|
|
} |
|
|
|
|
|
protected Type TryGetType(string name) |
|
|
{ |
|
|
string tryGetType = name + ".GetType()"; |
|
|
object type = null; |
|
|
try |
|
|
{ |
|
|
type = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(tryGetType, SourceCodeKind.Expression).Execute(commandLine.ScriptScope); |
|
|
} |
|
|
catch (ThreadAbortException tae) |
|
|
{ |
|
|
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort(); |
|
|
} |
|
|
catch |
|
|
{ |
|
|
|
|
|
} |
|
|
return type as Type; |
|
|
} |
|
|
|
|
|
protected void PopulateFromCLRType(List<PythonCompletionData> items, Type type, string name) |
|
|
{ |
|
|
List<string> completionsList = new List<string>(); |
|
|
MethodInfo[] methodInfo = type.GetMethods(); |
|
|
PropertyInfo[] propertyInfo = type.GetProperties(); |
|
|
FieldInfo[] fieldInfo = type.GetFields(); |
|
|
foreach (MethodInfo methodInfoItem in methodInfo) |
|
|
{ |
|
|
if ((methodInfoItem.IsPublic) |
|
|
&& (methodInfoItem.Name.IndexOf("get_") != 0) && (methodInfoItem.Name.IndexOf("set_") != 0) |
|
|
&& (methodInfoItem.Name.IndexOf("add_") != 0) && (methodInfoItem.Name.IndexOf("remove_") != 0) |
|
|
&& (methodInfoItem.Name.IndexOf("__") != 0)) |
|
|
completionsList.Add(methodInfoItem.Name); |
|
|
} |
|
|
foreach (PropertyInfo propertyInfoItem in propertyInfo) |
|
|
{ |
|
|
completionsList.Add(propertyInfoItem.Name); |
|
|
} |
|
|
foreach (FieldInfo fieldInfoItem in fieldInfo) |
|
|
{ |
|
|
completionsList.Add(fieldInfoItem.Name); |
|
|
} |
|
|
completionsList.Sort(); |
|
|
string last = ""; |
|
|
for (int i = completionsList.Count - 1; i > 0; --i) |
|
|
{ |
|
|
if (completionsList[i] == last) completionsList.RemoveAt(i); |
|
|
else last = completionsList[i]; |
|
|
} |
|
|
foreach (string completion in completionsList) |
|
|
{ |
|
|
items.Add(new PythonCompletionData(completion, name, commandLine, true)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void GenerateDescription(string stub, string item, DescriptionUpdateDelegate updateDescription, bool isInstance) |
|
|
{ |
|
|
System.IO.Stream stream = commandLine.ScriptScope.Engine.Runtime.IO.OutputStream; |
|
|
string description = ""; |
|
|
if (!String.IsNullOrEmpty(item)) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
AutocompletionInProgress = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string docCommand = ""; |
|
|
if (isInstance) docCommand = "type(" + stub + ")" + "." + item + ".__doc__"; |
|
|
else docCommand = stub + "." + item + ".__doc__"; |
|
|
object value = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(docCommand, SourceCodeKind.Expression).Execute(commandLine.ScriptScope); |
|
|
description = (string)value; |
|
|
description = description?.Replace($"){item}", $")\r\n{item}"); |
|
|
AutocompletionInProgress = false; |
|
|
} |
|
|
catch (ThreadAbortException tae) |
|
|
{ |
|
|
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort(); |
|
|
AutocompletionInProgress = false; |
|
|
} |
|
|
catch |
|
|
{ |
|
|
AutocompletionInProgress = false; |
|
|
|
|
|
} |
|
|
commandLine.ScriptScope.Engine.Runtime.IO.SetOutput(stream, Encoding.UTF8); |
|
|
updateDescription(description); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
string GetName(string text) |
|
|
{ |
|
|
text = text.Replace("\t", " "); |
|
|
int startIndex = text.LastIndexOf(' '); |
|
|
return text.Substring(startIndex + 1).Trim('.'); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|