Giter Site home page Giter Site logo

actionmenu.nvim's Introduction

A nice context menu for vim.

Tests

Example

This is intended for use within vim plugins or scripts, to open a context menu and provide a callback with the selected item.

Usage

The menu is opened with a simple function. It will open at the current cursor location with whatever items your provide.

call actionmenu#open(items, callback, {opts})
  • items each item can be a string or a dictionary (see complete-items)
  • callback either a string or a funcref()
    • Will be invoked with the selected index (zero based) and item
    • Cancelling a selection will invoke the callback with an index of -1
  • opts (optional) Currently allows you to specify the icon for the menu

A simple example

Just paste the snippet below into your .vimrc then execute :call Demo()

func! Demo()
  call actionmenu#open(['First', 'Second', 'Third'], 'Callback')
endfunc

func! Callback(index, item)
  echo "I selected index " . a:index
endfunc

Example with Coc Code Actions (ie. context menu for a language server)

Use the actionmenu to list and execue available code actions from coc.nvim plugin

Once both actionmenu.nvim and coc.nvim are installed, put the folowing in your .vimrc file

let s:code_actions = []

func! ActionMenuCodeActions() abort
  if coc#float#has_float()
    call coc#float#close_all()
  endif

  let s:code_actions = CocAction('codeActions')
  let l:menu_items = map(copy(s:code_actions), { index, item -> item['title'] })
  call actionmenu#open(l:menu_items, 'ActionMenuCodeActionsCallback')
endfunc

func! ActionMenuCodeActionsCallback(index, item) abort
  if a:index >= 0
    let l:selected_code_action = s:code_actions[a:index]
    let l:response = CocAction('doCodeAction', l:selected_code_action)
  endif
endfunc

then map it to a keybinding eg. perhaps <Leader>s

nnoremap <silent> <Leader>s :call ActionMenuCodeActions()<CR>

asciicast

More examples

Below is an example using vim's complete-items allowing you to pass in more complex data

func! Demo()
  let l:items = [
    \ { 'word': 'First', 'abbr': '1st', 'user_data': 'Custom data 1' },
    \ { 'word': 'Second', 'abbr': '2nd', 'user_data': 'Custom data 2' },
    \ { 'word': 'Third', 'abbr': '3rd', 'user_data': 'Custom data 3' }
    \ ]

  call actionmenu#open(
    \ l:items,
    \ { index, item -> Callback(index, item) }
    \ )
endfunc

func! Callback(index, item)
  if a:index >= 0
    echo "Custom data is ". a:item['user_data']
  endif
endfunc

You can open the actionmenu with a custom icon (i've recently been using nerdfonts for this)

func! Demo()
  call actionmenu#open(
    \ ['First', 'Second', 'Third'],
    \ 'Callback',
    \ { 'icon': { 'character': 'X', 'foreground': 'yellow' } }
    \ )
endfunc

func! Callback(index, item)
  echo "I selected index " . a:index
endfunc

Requirements

The latest version of neovim (which provides the "popup window" functionaity that is leveraged)

brew install --HEAD neovim

and this plugin (show below using vim-plug installation - don't forget to run :PlugInstall)

if has('nvim')
  Plug 'kizza/actionmenu.nvim'
endif

Once installed, you can try it out by running :call actionmenu#example()

Finally

Build it into your existing plugin or useful scripts however you like!

actionmenu.nvim's People

Contributors

dependabot[bot] avatar gee19 avatar kizza avatar voldikss avatar wos-dot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

actionmenu.nvim's Issues

Filetype not detected.

I submitted a pull request that fixes opening splits in the callback. But neovim fails to detect the filetype of the file opened in the split. I have to run 'filetype detect' to fix it, like this:

 function! OpenConfigurationFileInSplit(name_index, name) abort
      if a:name_index >= 0
          execute "tabedit " . g:my_config_dir . a:name
          filetype detect
      endif
  endfunction

  function! SelectAndOpenConfigurationFile() abort
      call actionmenu#open(["init.vim", "plugins.vim"], 'OpenConfigurationFileInSplit')
  endfunction

Could you check if this is correct? (Calling actionmenu >= 2 times fails)

I used actionmenu.nvim to show the list of same items, passing user selection into callback method.

If I call actionmenu#open again after doing the selection once, it keeps returning -1 and it looks like the input will go into somewhere else (another window?) until I press ESC .

I solved this for myself by commenting out the referenced line. I don't know Vimscript, does it make sense to you?

Thank you!

Doesn't work when buffer is not modifiable

I tried to set up this with defx, but I constantly get an error that buffer is not modifiable. I tried in regular buffer, same error. Steps to reproduce:

  1. Open any file
  2. enter set nomodifiable in command line
  3. call actionmenu#example()
  4. Select any option
  5. See error in command line

I couldn't find what's causing this. Maybe you can.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.