-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPythonConsoleCompletionDataProvider.cs
192 lines (179 loc) · 8.35 KB
/
PythonConsoleCompletionDataProvider.cs
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
// Copyright (c) 2010 Joe Moorhouse
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
{
/// <summary>
/// Provides code completion for the Python Console window.
/// </summary>
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)//IMemberProvider memberProvider)
{
this.commandLine = commandLine;
}
/// <summary>
/// Generates completion data for the specified text. The text should be everything before
/// the dot character that triggered the completion. The text can contain the command line prompt
/// '>>>' as this will be ignored.
/// </summary>
public ICompletionData[] GenerateCompletionData(string line)
{
List<PythonCompletionData> items = new List<PythonCompletionData>(); //DefaultCompletionData
string name = GetName(line);
// A very simple test of callables!
if (excludeCallables && name.Contains(')')) return null;
if (!String.IsNullOrEmpty(name))
{
System.IO.Stream stream = commandLine.ScriptScope.Engine.Runtime.IO.OutputStream;
try
{
AutocompletionInProgress = true;
// Another possibility:
//commandLine.ScriptScope.Engine.Runtime.IO.SetOutput(new System.IO.MemoryStream(), Encoding.UTF8);
//object value = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(name, SourceCodeKind.Expression).Execute(commandLine.ScriptScope);
//IList<string> members = commandLine.ScriptScope.Engine.Operations.GetMemberNames(value);
Type type = TryGetType(name);
// Use Reflection for everything except in-built Python types and COM pbjects.
if (type != null && type.Namespace != "IronPython.Runtime" && (type.Name != "__ComObject"))
{
PopulateFromCLRType(items, type, name);
}
else
{
string dirCommand = "dir(" + name + ")";
object value = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(dirCommand, SourceCodeKind.Expression).Execute(commandLine.ScriptScope);
AutocompletionInProgress = false;
foreach (object member in (value as IronPython.Runtime.List))
{
items.Add(new PythonCompletionData((string)member, name, commandLine, false));
}
}
}
catch (ThreadAbortException tae)
{
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort();
}
catch
{
// Do nothing.
}
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
{
// Do nothing.
}
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));
}
}
/// <summary>
/// Generates completion data for the specified text. The text should be everything before
/// the dot character that triggered the completion. The text can contain the command line prompt
/// '>>>' as this will be ignored.
/// </summary>
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;
// Another possibility:
//commandLine.ScriptScope.Engine.Runtime.IO.SetOutput(new System.IO.MemoryStream(), Encoding.UTF8);
//object value = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(item, SourceCodeKind.Expression).Execute(commandLine.ScriptScope);
//description = commandLine.ScriptScope.Engine.Operations.GetDocumentation(value);
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;
AutocompletionInProgress = false;
}
catch (ThreadAbortException tae)
{
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort();
AutocompletionInProgress = false;
}
catch
{
AutocompletionInProgress = false;
// Do nothing.
}
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('.');
}
}
}