/vim-python-domain-knowledge

Vim plugin for Python 3+ written in Python 3 for project specific autocomplete of functions and classes and automatic import filing

Primary LanguagePythonMIT LicenseMIT

Python Domain Knowledge - Vim plugin for Python 3+

Vim plugin for Python 3+ 🐍 written in Python 3+ 🐍 for project specific autocomplete and search of functions and classes and automatic import filling



Overview

The current state of vim-python-domain-knowledgehas 3 responsibilities:

  1. Global "project specific" autocomplete for all classes and functions that are defined inside a given projects

More about this: here

NOTE: This plugin is not a replacement of jedi-vim. It provides a first-level autocomplete for everything inside the project (jedi provides detailed attributes specific autocomplete only for variables in the context)

  1. Global search for all classes and functions in the projects using the vim embedded search regex matching

  2. Automatically autofill the import for:

  • every class that's defined inside the project (no matter in which file)
  • every function that's defined inside the project (no matter in which file)
  • every imported stuff (from any third party library inside the project)

More about this: here

Quick Demo

Motivation

A really common action in the python development is using functions that are not in the same file. The process usually looks like this:

  1. Start typing the name (probably forgot the exact name since is long and explicitly descriptive)
  2. Jump to the top of the file looking at the imports
  3. Remember the place where the function lives (probably grep inside the project)
  4. Find the right place in the imports to put the new function import
  5. Start typing by autosuggesting the name using the import location
  6. Jump back to the place where you want to use the function and autocomlete it

The aim of this plugin is to automate this process by generating knowledge for your existing codebase and use it to autosuggest the function/class/constant you want to use at the moment of typing + suggest the right import and put it at the right place.

Getting Started

Installation

Vundle

Make sure you have installed Sqlite first!

Place this in your .vimrc

Plugin 'HackSoftware/vim-python-domain-knowledge'

… then run the following in Vim:

:source %

PluginInstall

Plug

Make sure you have installed Sqlite first!

Place this in your .vimrc

Plug 'HackSoftware/vim-python-domain-knowledge'

… then run the following in Vim:

:source %

PlugInstall

Pathogen

Make sure you have installed Sqlite first!

Run the following in a terminal:

cd ~/.vim/bundle
git clone https://github.com/HackSoftware/vim-python-domain-knowledge.git

IMPORTANT NOTE: The only external dependency of this plugin is SQLite3 (https://www.sqlite.org/index.html). Make sure you have it installed on your operating system :)

Setup for a project

NOTE: this should be done only once for a project

Go to the project root folder

cd /path/to/project

Open Vim and run:

:call PythonDomainKnowledgeCollectImports()

NOTE: It could take a few seconds until it parse the whole project's Abstract syntax tree and extract the need. If everything is successfull you should see .vim_domain_knowledge/ folder inside you project

Restart Vim (This is necessary since the plugin is setting up custom autocomplete function)

Add this to .gitignore (optionally)

.vim_domain_knowledge/

Enjoy

Setup demo

Configuration

" To map your shortcut for autofilling import for the word under the cursor
nnoremap <your_custom_mapping> :call PythonDomainKnowledgeFillImport()<CR>

" To map your shortcut for global searching
noremap <C-m> :call PythonDomainKnowledgeSearch()<Cr>

Sample configuration:

" This will autofill the import for the word under the cursor when you press F9 key (in normal mode) :)
nnoremap <F9> :call PythonDomainKnowledgeFillImport()<CR>

" This will open the search as soon as you press "ctrl + m" in normal mode
noremap <C-m> :call PythonDomainKnowledgeSearch()<Cr>

Usage

Global autocomplete

Start typing and press Ctrl + x and then Ctrl + u (while in insert mode)

NOTE: You can remap this ^ . It's the default vim shortcut for autocomplete from custom completefunc

Autocomplete demo

Global search

In normal model run

:call PythonDomainKnowledgeSearch()

(or a keybinding for this)

Global search demo

Automatic import filling

Write the full name of the function/class you want to use. Then in normal mode run:

:call PythonDomainKnowledgeFillImport()

(or a keybinding for this)

Import autofil demo

Import autofil demo

Feedback

Feedback is the best power for making things better. Any form of feedback is highly appreaciated:

  • open an issue - for bug 🐛 or new feature proposal
  • give a star ⭐
  • contribute (fork the repo + open pull request)