Implementing "Intellisense" for IronPython

Viewed 180

In my C# application I have a text editor that allows the user to enter IronPython scripts. I have implemented a set of C# classes that are made available to the python environment.

I would now like to implement an "intellisense" type of system where the user enters a variable name then a dot and it prompts the user for a list of available methods and properties.

For example here is an IronPython script:

foo = MyClass()
foo.

At this point the cursor is just after the dot. MyClass example in C#:

public class MyClass {
    public void Func1() ...
    public void Func2() ...
}

Now I would like to give the user a pop up list showing Func1(), Func2(), etc.

What I need to do is take the variable name "foo" and get the class MyClass.

Note that I can't execute the IronPython code to do this because it performs actions in the user interface.

This is how far I have been able to get:

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;
        }
    }
}

I can see that the last line of the script foo. is an ExpressionStatement and I can drill down from there to get the NameExpression of 'foo' but I can't see how to get the type of the variable.

Is there a better way to do this? Is it even possible?

Thanks!

0 Answers
Related