SmiteshP/nvim-gps

Python class methods are shown as functions

Hubro opened this issue ยท 4 comments

Hubro commented

Class methods in Python are currently shown as regular functions:

image

They should probably be shown as class methods.

The query can probably be modified to treat all function definitions that are direct children of a class definitions as methods. I'm not savvy enough with the query language to fix it myself though.

Yep! I am aware of this issue.

The query can probably be modified to treat all function definitions that are direct children of a class definitions as methods.

Yeah, you are right! I have tried doing this before but treesitter just does not capture this query when invoked on function_definition node inside of a class, might be a bug with treesitter but I am not sure. The query I had tried looks like this.

; Method
((class_definition
	body: (block
		(function_definition
			name: (identifier) @method-name) @scope-root)))

This issue doesn't occur in other languages because the parser itself distinguishes between methods and functions. For example: c++ parser would have the name: of a method as field_identifier and just identifier for regular functions. Such things make it easy for us to work with them.

@Hubro have a look at the latest commit. It should fix this issue :)

Hubro commented

@Hubro have a look at the latest commit. It should fix this issue :)

Works! ๐Ÿ˜

It's a little hacky though:

image

image

These are edge cases, of course. A function outside of a class taking a first argument called "self" is rare, and a method with a first argument other than "self" is even more so.

Another question is how to handle class-methods and static-methods:

image

Since these don't have the first argument "self" they are currently regarded as regular functions. If the query was matching for function definitions that are direct children of class definitions, they would be considered methods. I'm not sure which is more correct.

I'm quite happy with the current situation though, it's a major improvement.

It's a little hacky though

Yeah, I agree. Its not a perfect solution. XD

If the query was matching for function definitions that are direct children of class definitions, they would be considered methods.

Yep, but unfortunately the iter_captures doesn't capture the scope-root when called on the function definition node and instead it gets captured when on class definition node. There doesn't seem a way around this. I would like to come back to this issue with a better solution.