borko84
borko84

Reputation: 41

No omnicompletion for python class members in vim?

I want to create tags (ctags 5.8) file for my classes in python.For functions, and class members defined outside the class definition omnicompletion works ok. However if I define data member in a constructor ( self.x=2 ) I cannot see the ctags completion ?

class A(object):

    def __init__(self):
        self.x = "whatever"    # x will not seen in ctags/omnicompletion!!!

Am I doing sth wrong ? Why there is no omnicompletion (ctags file looks ok) ?

Upvotes: 4

Views: 699

Answers (1)

Roman Susi
Roman Susi

Reputation: 4199

If I understood your problem right, you can always add attributes in the class definition:

class A(object):

    x = None

    def __init__(self):
        self.x = whatever

This way everyone reading your code sees, which attributes (you are calling them "class members") a class has.

UPDATE: checked with

$ ctags --version
Exuberant Ctags 5.9~svn20110310, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Mar 18 2011, 10:38:14

The resulting tags file looks like this:

!_TAG_FILE_FORMAT       2       /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED       1       /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR    Darren Hiebert  /[email protected]/
!_TAG_PROGRAM_NAME      Exuberant Ctags //
!_TAG_PROGRAM_URL       http://ctags.sourceforge.net    /official site/
!_TAG_PROGRAM_VERSION   5.9~svn20110310 //
A       aaa.py  /^class A(object):$/;"  c
__init__        aaa.py  /^   def __init__(self, x):$/;" m       class:A
x       aaa.py  /^   x = None$/;"       v       class:A

As can be seen, x attribute has its own record.

Also checked with Emacs by creating emacs-compatible tags file first:

ctags -e aaa.py  # where aaa.py - file with code snippet above

(this created TAGS file)

Inside Emacs:

M-. x   (enter)
~/TAGS   (enter)

...and voila! Cursor is at x = None line.

Also, your original snippet doesn't work. So my advice to initialize attribute in the class namespace is valid.

Upvotes: 1

Related Questions