Problem

Get function declaration location from context

Posted 1 year ago by Avatar tal

Hey,

I’m experimenting on the Python Language demo and I’m trying to get the declaration (path + line number + column offset) of a function from the current selection.
I got the path but I couldn’t retrieve the rest of the information – is it possible?

var context = new PythonContextFactory().CreateContext(codeEditor.ActiveView.Selection.EndSnapshotOffset, PythonContextKind.Self);
if (context == null)
{
    return;
}
if ((context.Location != null) && ((context.Location == PythonContextLocations.Comment) || (context.Location == PythonContextLocations.LiteralString)))
{
    return;
}
IResolverResultSet set = context.Resolve();
if (((set == null) || (set.Results == null)) || (set.Results.Count == 0))
{
    return;
}
IFunctionResolverResult result = set.Results[0] as IFunctionResolverResult;
if ((result == null) || (result.Function == null))
{
    return;
}
bool isBuiltin = result.Function.DeclaringModule.IsBuiltin;
string path = result.Function.DeclaringModule.Path;

 (I'm looking into implementing a GoToDefinition functionality)

Thanks,

Tal


Comments (4)

Posted 1 year ago by Actipro Software Support - Cleveland, OH, USA

Hi Tal,

We don't currently store the declaration line/column.  We could possibly add something for that in a future version though.  Would you want to see the start offset of the declaration Name, or the text range of the entire declaration?


Actipro Software Support
Posted 1 year ago by tal

Hi,

Thanks for the fast reply, I would like at least the start offset, though the text range of the entire declaration would be a very nice addition if it is not too much trouble.

It would be very much appreciated if you could also add the same functionality for the Function.DeclaringType when the function is a method (that way, it will be possible to get the entire text range of the container class of a specific method).

Thanks again,

Tal

Posted 1 year ago by Actipro Software Support - Cleveland, OH, USA

Hi Tal,

Ok we will add a TextRange to both ITypeDefinition and IFunctionDefinition.  If you'd like to try a preview build of this functionality, please write our support address and mention this thread.


Actipro Software Support
Posted 1 year ago by tal

Thank you!

Information The latest build of this product (2017.1 build 0652) was released 0 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.