Pythoncomplete patch 2-supports code prompts for custom classes in the current file

Source: Internet
Author: User

Because pythoncomplete executes the code in the original file and prompts the code by executing the exec statement, all the objects used must be imported. Therefore, if you define a class in the current original file, there is no prompt to create an instance. For example:

Class Foo (object ):

Name = 'lucifer'

F = Foo ()

F. This will fail.

The solution is very simple: manually import this class. For example, if our original file is named TT. py, add this sentence: from TT import Foo, and the code prompts will work properly. Therefore, the patch is also very simple. Open the pythoncomplete. Vim file under autoload, and add

Import Vim

Import OS, re

Then add the following two sentences to the class completer (object): Get the file name of the current file.

 

Filename = OS. Path. basename (Vim. buffers [0]. Name)

Filename = filename [0:-3]

Then add the following to the namespace added last time in function def evalsource (self, text, line = 0 ):

If not namespace:

For item in SRC. Split ('/N '):

If item. startswith ('from') or item. startswith ('import '):

Namespace. append (item)

# Get the class name defined in the current file

If item. startswith ('class') and item. endswith (':'):

RegEx = Re. Compile (R' ^ class/S + (? P <Name>. + ?) /(. +? /): $ ')

Matchs = RegEx. findall (item)

If Len (matchs ):

Name = matchs [0]

From_import = 'from % s import % s' % (filename, name)

Namespace. append (from_import)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.