C# 实施";智能感知“;对于IronPython
在我的C#应用程序中,我有一个文本编辑器,允许用户输入IronPython脚本。我实现了一组可供python环境使用的C#类 我现在想实现一个“intellisense”类型的系统,其中用户输入一个变量名,然后输入一个点,然后提示用户提供可用方法和属性的列表 例如,下面是一个IronPython脚本:C# 实施";智能感知“;对于IronPython,c#,ironpython,code-completion,C#,Ironpython,Code Completion,在我的C#应用程序中,我有一个文本编辑器,允许用户输入IronPython脚本。我实现了一组可供python环境使用的C#类 我现在想实现一个“intellisense”类型的系统,其中用户输入一个变量名,然后输入一个点,然后提示用户提供可用方法和属性的列表 例如,下面是一个IronPython脚本: foo = MyClass() foo. 此时光标正好位于点之后。C#中的MyClass示例: 现在我想给用户一个弹出列表,显示Func1()、Func2()等 我需要做的是取变量名“foo”并
foo = MyClass()
foo.
此时光标正好位于点之后。C#中的MyClass示例:
现在我想给用户一个弹出列表,显示Func1()、Func2()等
我需要做的是取变量名“foo”并获取类MyClass
请注意,我无法执行IronPython代码来执行此操作,因为它在用户界面中执行操作
这就是我能做到的程度:
ScriptSource Source = engine.CreateScriptSourceFromString(pycode, SourceCodeKind.File);
SourceUnit su = HostingHelpers.GetSourceUnit(Source);
Microsoft.Scripting.Runtime.CompilerContext Ctxt = new Microsoft.Scripting.Runtime.CompilerContext(su, new IronPython.Compiler.PythonCompilerOptions(), ErrorSink.Null);
IronPython.Compiler.Parser Parser = IronPython.Compiler.Parser.CreateParser(Ctxt, new IronPython.PythonOptions());
IronPython.Compiler.Ast.PythonAst ast = Parser.ParseFile(false);
if (ast.Body is IronPython.Compiler.Ast.SuiteStatement)
{
IronPython.Compiler.Ast.SuiteStatement ss = ast.Body as IronPython.Compiler.Ast.SuiteStatement;
foreach (IronPython.Compiler.Ast.Statement s in ss.Statements)
{
if (s is IronPython.Compiler.Ast.ExpressionStatement)
{
IronPython.Compiler.Ast.ExpressionStatement es = s as IronPython.Compiler.Ast.ExpressionStatement;
}
}
}
我可以看到脚本foo.
的最后一行是ExpressionStatement,我可以从那里向下钻取'foo'的名称表达式,但我看不到如何获取变量的类型
有更好的方法吗?有可能吗
谢谢
ScriptSource Source = engine.CreateScriptSourceFromString(pycode, SourceCodeKind.File);
SourceUnit su = HostingHelpers.GetSourceUnit(Source);
Microsoft.Scripting.Runtime.CompilerContext Ctxt = new Microsoft.Scripting.Runtime.CompilerContext(su, new IronPython.Compiler.PythonCompilerOptions(), ErrorSink.Null);
IronPython.Compiler.Parser Parser = IronPython.Compiler.Parser.CreateParser(Ctxt, new IronPython.PythonOptions());
IronPython.Compiler.Ast.PythonAst ast = Parser.ParseFile(false);
if (ast.Body is IronPython.Compiler.Ast.SuiteStatement)
{
IronPython.Compiler.Ast.SuiteStatement ss = ast.Body as IronPython.Compiler.Ast.SuiteStatement;
foreach (IronPython.Compiler.Ast.Statement s in ss.Statements)
{
if (s is IronPython.Compiler.Ast.ExpressionStatement)
{
IronPython.Compiler.Ast.ExpressionStatement es = s as IronPython.Compiler.Ast.ExpressionStatement;
}
}
}