Skip to content

pskpatil/vim-codequery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

78 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

> Choose what you want to know! 😼
" Open menu
:CodeQueryMenu Unite Full

" Or query directly
:CodeQuery Definition get_user_id


vim-codequery

This Vim plugin is built on top of the great tool CodeQuery created by ruben2020, and aims at providing three primary functions to help you to:

  1. Search source code gracefully within Vim.
    • You can find: definition, call, caller, callee, symbol, class, parent, child and more for a string.
    • Convenient menus are created for you.
    • Well-formated results are shown in a custom Quickfix window with usable key bindings.
  2. Manage your database easily.
    • Load, make and move your database file by custom Vim commands.
    • Note: CodeQuery's SQLite database is built on top of ctags and cscope.
  3. Know your code more instantly.
    • (TBD)

Supported languages

  • Python 😎
  • Javascript
  • Ruby
  • Go
  • Java
  • C, C++

Demo

Choose a query from menu βž™ Get results

... βž™ Switch to different query βž™ Get results ... βž™ Filter them βž™ Get Results βž™ Undo βž™ Redo

more demo and screenshots


Schedule

This project is almost ready to be released.

Main TODO:

  • Use Vim8's new features to enhance usability.
  • Do lazy-loading.
  • Test it.

Completeness: 92%
Current Version: v0.8

It is Ok to try it! πŸ‘Œ


Installation

1. Make sure these commands are in your system

echo mkdir mv cut find awk stat git(optional)

2. Install CodeQuery

  • Linux => Follow instructions here.
  • Mac => brew install qscintilla2 qt5 cmake and then pull latest CodeQuery code to build it.

You will be able to simply type brew install codequery to install a new enough version of codequery in the future. πŸ˜‘

3. Install additional ctags/cscope tools for your languages

Language Tools How to install
Python PyCscope sudo pip install pycscope
Javascript Starscope sudo gem install starscope
Ruby Starscope sudo gem install starscope
Go Starscope sudo gem install starscope
Java N/A
C N/A
C++ N/A

Starscope has been packaged for Arch Linux

4. Install Vim plugins

" Recommemded => it helps vim-codequery to build DB asynchrously without blocking Vim
Plug 'tpope/vim-dispatch'  
   
" Recommemded => if you don't have an :Ack (or :Ag) liked command => install it !
Plug 'mileszs/ack.vim'  
    
" Highly Recommemded => if you want to use my custom Unite menu
Plug 'Shougo/unite.vim'  
   
" The Must Have One
Plug 'devjoe/vim-codequery'

Usage

1. Setup

  • Go to the (git) root directory of your project. Open vim and type:
" Index Python files
:CodeQueryMakeDB python
  
" Or index Python + Javascript files
:CodeQueryMakeDB python javascript 
  • That's all. python.db, javascript.db ... will be created in the root directory.
  • It's recommended that you should use :CodeQueryMoveDBToGitDir python to hide the DB file to .git/codequery/ directory. If you do so, next time you can call :CodeQueryMakeDB directly in any opened Python buffer to rebuild the DB file.

2. Search

  • Find symbol under cursor
:CodeQuery
  • Find ? under cursor
:CodeQuery [SubCommand]  
  
" Supported SubCommands are: `Symbol, Text, Call, Caller, Callee, Class, Parent, Child, Member, FunctionList, FileImporter`.  
  • Find arbitrary word
:CodeQuery [SubCommand] [word]  
  • Find ? again with the same word
:CodeQueryAgain [SubCommand]  
  • With fuzzy Option
:CodeQuery [SubCommand] [word] -f  
  
" [word] can be: get_* or *user_info or find_*_by_id
  • With append Option (results will be added to current Quickfix)
:CodeQuery [SubCommand] [word] -a  
  • Filter Search Results
:CodeQueryFilter [string]  
  
" [string] can be a regex

3. Use Quickfix

  • Move your cursor inside Quickfix window to use these key bindings
Key Action Note
s :CodeQueryAgain Symbol
x :CodeQueryAgain Text use :Ack! by default. #1
c :CodeQueryAgain Call
r :CodeQueryAgain Caller
e :CodeQueryAgain Callee
d :CodeQueryAgain Definition
C :CodeQueryAgain Class
M :CodeQueryAgain Member
P :CodeQueryAgain Parent
D :CodeQueryAgain Child
m :CodeQueryMenu Unite Magic
q :cclose
\ :CodeQueryFilter
p <CR><C-W>p Preview
u :colder | CodeQueryShowQF Older Quickfix Result
<C-R> :cnewer | CodeQueryShowQF Newer Quickfix Result

#1 You can override g:codequery_find_text_cmd to change it.

  • Show Quickfix with above key bindings
:CodeQueryShowQF  
  
" This command can also be used to **patch** standard Quickfix.

4. Open Menu

Currently, vim-codequery only provides Unite menu because I love and use it a lot ⭐. There are two types of menu:

  • Open a full Unite menu
:CodeQueryMenu Unite Full  
  
" The string between :: and :: is the word under cursor
" [F] means this action is for 'function variable only'
" [C] is for 'class variable only'  

  • Open a magic Unite menu
:CodeQueryMenu Unite Magic  
  
" This menu changes dynamically:
" 1. If word under cursor is capital (possible be class): remove [F] actions
" 2. If word under cursor is non-capital (possible be function): remove [C] actions
" 3. Show reasonable actions only within Quickfix


Tips

Open Menu Smartly

nnoremap <space>c :CodeQueryMenu Unite Full<CR>
nnoremap <space>; :CodeQueryMenu Unite Magic<CR>
  
" Or enable typing (to search menu items) by default
nnoremap <space>\ :CodeQueryMenu Unite Magic<CR>A

Query Smartly

nnoremap <space><CR> :CodeQuery Symbol<CR>
  
" Chain commands to find possible tests (for python)
nnoremap <space>t :CodeQuery Caller<CR>:CodeQueryFilter test_<CR>

Find Text

" Custom your `CodeQuery Text` commands
let g:codequery_find_text_cmd = 'Ack!'
  
let g:codequery_find_text_from_current_file_dir = 0
" 0 => search from project dir (git root directory / the directory containing xxx.db file)
" 1 => search from the directory containing current file

Load Ctags File

" Set tags option
set tags=./javascript_tags;/
set tags+=./python_tags;/
set tags+=./ruby_tags;/
set tags+=./go_tags;/
set tags+=./java_tags;/
set tags+=./c_tags;/

Clean Ctags, Cscope ... Files by Languages

" It accpepts a list of your languages written in lowercase
let g:codequery_enable_auto_clean_languages = ['python']

Custom Database Building

" Make sure to generate a python.db or xxxx.db file as a result
let g:codequery_build_python_db_cmd = '...'
let g:codequery_build_javascript_db_cmd = '...'
let g:codequery_build_ruby_db_cmd = '...'
let g:codequery_build_go_db_cmd = '...'  
let g:codequery_build_java_db_cmd = '...'  
let g:codequery_build_c_db_cmd = '...'  

FAQ

Why writing this plugin?

Because I need it.

I already shared the story of making this plugin in local Python user groups Taipei.py and Tainan.py.
Slides are available here: Taipei.py / Tainan.py (Language: Traditional Chinese)

Why not using Ctags or Cscope directly?

Read what @ruben2020 the author of CodeQuery said: Link

In addittion, vim-codequery provides:

  1. Good interface.
  2. Separated Database Management Mechanism.
    (You can open as many projects as you wish in a single Vim session without worrying about messing up Ctags or Cscope files or getting wrong result!)

for Vim users.

More Questions

Ask here or create an issue.


How to Contribute

Use It

And give me feedback or bug report.

Fork It

And give me PR. It would be better if you open an issue and discuss with me before sending PR.

Star It

If you like it. πŸ‘


Credits

Thank all for working on these great projects!

About

Search + Browse + Understand your code more efficiently.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Vim Script 100.0%