neoclide / coc.nvim

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
Other
24.15k stars 953 forks source link

help: completion of variables from dotenv files in shell scripts #5020

Closed r10r closed 1 month ago

r10r commented 1 month ago

How can I enable cross-file completion for shell files ? My use case is to complete variables from an environment (dotenv) file.

Let's say I have an environment file my.env with the following content:

MY_VAR=coc.nvim
OTHER_VAR="rocks"

The environment file is sourced in a script with. my.env called myscript.sh How can I get completion for the environment variables defined in my.env when editing myscript.sh

Thanks for your help!

fannheyward commented 1 month ago

Use coc-sh:

image
r10r commented 1 month ago

@fannheyward Thanks for the quick answer. Is there a way to do this without the coc-sh languageserver extension ? My mistake I should have noted that I'm searching a more general way, e.g specify a newline separated file as completion source.

fannheyward commented 1 month ago
image

Open my.env in another buffer, coc.nvim will provide completion item from buffer source.

r10r commented 1 month ago

Hmm, unfortunately this doesn't work for me. Any ideas ?

[root@ruben.devpod:~/.vim/pack/vendor/start/coc.nvim]# git log
commit a0f3e2c1b13157a25063b32b49debf46cc96a873 (grafted, HEAD -> release, origin/release)
Author: Heyward Fann <fannheyward@gmail.com>
Date:   Mon Mar 11 10:59:55 2024 +0800

    commit 84ce03120925c980288665028aedcf06f6cb837e
[root@ruben.devpod:~]# vim --version
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Nov 24 2023 11:25:06)
Included patches: 1-2127
Compiled by Alpine Linux
Huge version without GUI.  Features included (+) or not (-):
+acl               +file_in_path      +mouse_urxvt       -tag_any_white
+arabic            +find_in_path      +mouse_xterm       -tcl
+autocmd           +float             +multi_byte        +termguicolors
+autochdir         +folding           +multi_lang        +terminal
-autoservername    -footer            -mzscheme          +terminfo
-balloon_eval      +fork()            +netbeans_intg     +termresponse
+balloon_eval_term -gettext           +num64             +textobjects
-browse            -hangul_input      +packages          +textprop
++builtin_terms    +iconv             +path_extra        +timers
+byte_offset       +insert_expand     -perl              +title
+channel           +ipv6              +persistent_undo   -toolbar
+cindent           +job               +popupwin          +user_commands
-clientserver      +jumplist          +postscript        +vartabs
-clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +vim9script
+cmdline_hist      +langmap           -python            +viminfo
+cmdline_info      +libcall           +python3/dyn       +virtualedit
+comments          +linebreak         +quickfix          +visual
+conceal           +lispindent        +reltime           +visualextra
+cryptv            +listcmds          +rightleft         +vreplace
+cscope            +localmap          +ruby/dyn          +wildignore
+cursorbind        +lua/dyn           +scrollbind        +wildmenu
+cursorshape       +menu              +signs             +windows
+dialog_con        +mksession         +smartindent       +writebackup
+diff              +modify_fname      -sodium            -X11
+digraphs          +mouse             -sound             +xattr
-dnd               -mouseshape        +spell             -xfontset
-ebcdic            +mouse_dec         +startuptime       -xim
+emacs_tags        -mouse_gpm         +statusline        -xpm
+eval              -mouse_jsbterm     -sun_workshop      -xsmp
+ex_extra          +mouse_netterm     +syntax            -xterm_clipboard
+extra_search      +mouse_sgr         +tag_binary        -xterm_save
-farsi             -mouse_sysmouse    -tag_old_static    
   system vimrc file: "/etc/vim/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -Os -fstack-clash-protection -Wformat -Werror=format-security -fno-plt -flto=auto -D_REENTRANT -U_FORTIFY_SOURCE -
D_FORTIFY_SOURCE=1 
Linking: gcc -Wl,--as-needed,-O1,--sort-common -Wl,-z,pack-relative-relocs -L/usr/local/lib -o vim -lm -lncurses 
Bildschirmfoto 2024-05-23 um 11 38 01
fannheyward commented 1 month ago

Can't reproduce, the :CocList sources already has buffer source. Try minimal vimrc.

r10r commented 1 month ago

Can't get it working on alpine, even with a minimal vimrc.

(NOTE for myself: buffer resource is builtin. See also https://github.com/neoclide/coc.nvim/wiki/Completion-with-sources#bundled-sources)

I've also tested it in a debian:latest container with the default vimrc and latest coc.nvim from the release branch but the buffer completion only completes from the active buffer.

c=$(buildah from debian:latest)
buildah run $c /bin/bash
apt-get update
apt-get install vim nodejs git
mkdir -p ~/.vim/pack/vendor/start
cd ~/.vim/pack/vendor/start
git clone https://github.com/neoclide/coc.nvim.git --branch release
cd
#
vim myscript.sh my.env
r10r commented 1 month ago

Can't reproduce, the :CocList sources already has buffer source. Try minimal vimrc.

Did you try it yourself with a minimal vimrc and emtpy coc settings ?

fannheyward commented 1 month ago

buffer completion only completes from the active buffer

Yes, this is expected, coc.nvim needs to active a buffer first before to use as completion source.

r10r commented 1 month ago

Ok now I understand that it only works in split view because both buffers are active then. Thanks for your help!

Bildschirmfoto 2024-05-24 um 08 57 23