Skip to content

antono2/vlang.kak

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

Repository files navigation

vlang.kak

Screenshot

vlang.kak enables support for the V programming language in the Kakoune text editor. It provides syntax highlighting and includes functions to run your program and review the output. v fmt is available for key mappings and shell commands are customizable.

Installation

Put this repo in your autoload directory. Read all about installing plugins here.

cd YOUR/AUTOLOAD/DIRECTORY/
git clone https://github.com/antono2/vlang.kak.git

Alternatively you can manually source the vlang.kak script in your configuration file

source "path_to/rc/vlang.kak"

Usage

You will have syntax highlighting for these V file types: v vsh vv v.mod c.v

The plugin provides the commands

  • v-run to run v in the v.mod directory. It looks for the v.mod file in up to 3 parent directories and executes v. The output is put into the info box and the *debug* buffer. Default "v -keepc -cg run ."

  • v-fmt to run v fmt -w on the current file and save it. Default "v fmt -w $kak_buffile"

  • v-enable-indenting and v-disable-indenting to set the indenting as you wish. Default on.

You can change each shell command by setting one or both options in your kakrc

# Use filetype hook to ensure the options are defined
hook global WinSetOption filetype=v %{
  set-option buffer v_run_command 'YOUR RUN COMMAND'
  set-option buffer v_fmt_command 'YOUR FMT COMMAND'
}

Note: Be veeery careful when changing the v_fmt_command, because it also saves the file and any issue will break your V code. Best test it on the hello world program before adding it to your kakrc. You can set the option from inside Kakoune by typing :set-option buffer v_fmt_command 'YOUR FMT COMMAND' and then test it by pressing the v_fmt key mapped below. Look at the current value with :echo %opt{v_fmt_command}

Kakrc

You can map these commands to some keys whenever a V file is opened.
For example you could map <F5> - <F8> to quickly format -> run -> read output -> go back.

hook global WinSetOption filetype=v %{
  require-module v
  
  map -docstring "Format and save file"      window normal <F5> ":v-fmt<ret>"
  map -docstring 'Run v in v.mod directory'  window normal <F6> ":v-run<ret>"
  map -docstring 'Switch to *debug* buffer'  window normal <F7> ":buffer *debug*<ret>"
  map -docstring 'Switch to previous buffer' global normal <F8> ":buffer-previous;delete-buffer *debug*<ret>"
  
  # Optionally set true or false for displaying the v_output in the info box and/or debug buffer.
  set-option buffer v_output_to_info_box     true
  set-option buffer v_output_to_debug_buffer true
}

Make sure to adapt the keys to your needs. Also, you can change v_output_to_info_box to false, if you don't want to see the V output in the info box and the same with v_output_to_debug_buffer for the *debug* buffer. The default values are set to true, so these don't need to be set in your kakrc.

Test Files

This plugin supports the :alt command of Kakoune, which switches the buffer to the corresponding V test file and back. You can bind it to a key the same way as described above.

  • currently editing main.v will try to open main_test.v
  • currently editing main.c.v will try to open main.c_test.v

Customizing Colors

Colors are called faces in Kakoune. The predefined faces can be looked up in the /share/kak/colors directory. Changing colors is pretty easy if you can dig through all the regex in vlang.kak.
Search for # Highlighters and below that you can - for example - go to ## TYPES and change the color for all the types to yellow by changing 0:type to 0:yellow. Take a look at the <regex> <capture_id>:<face> function.

Code Completion

Although this is completely separate from vlang.kak, I can still tell you how to set it up. Who could stop me? Nobody can stop you with all that raw code editing power at your fingertips!
Note: VLS is early software.

The goal is to get vlang vls to work with Kakoune's kak-lsp and get the full list of capabilities.
Install kak-lsp -> put it in kakrc -> install vls -> configure kak-lsp -> configure key mappings.

First install the Kakoune language server protocol client.
Note: Get the most current download URL for your system from the releases.

Afterkak-lsp is found in $PATH, you can add the start script to your Kakoune configuration.

eval %sh{kak-lsp --kakoune -s $kak_session}

Note: The kak-lsp toml config file path can be configured with --config.

Then install the V language server as described in the README.

v ls --install

Running it for the first time will give you the message If you are using this for the first time, please run 'v ls --install' first to download and install VLS. So, let's do that.

v ls --install

Now there should be a new directory $HOME/.vls and running v ls should give no errors. If that's the case, you can continue with configuring kak-lsp in the next step.

Though on older systems you might get an error like this

... /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ...

There is no fixing this by updating glibc, as it's a system package and changing the version would break the system.
One solution is to build vls from source, which then uses the system default.

cd ~/.vls
## Clone the project:
git clone https://github.com/vlang/vls && cd vls

## Build the project
## Use "v run build.vsh gcc" if you're compiling VLS with GCC.
v run build.vsh clang

# The binary will be created in the `bin` directory inside the vls folder.
# Move it to ~/.vls/bin and replace the original.
# In case of linux_x64
cp ./bin/vls ../bin/vls_linux_x64

Now kak-lsp can be configured to recognize the V language. Use this in your configuration toml file.

[language.v]
# The filetype variable is set in vlang.kak for .v, .vsh, .vv, .c.v under the name "v"
filetypes = ["v"]
roots = ["mod.v", ".git/"]
command = "v"
args = ["ls"]

Start your Kakoune on a V file and type :lsp-enable to check if all the lsp-commands are defined and finish up your kakrc.

eval %sh{ kak-lsp --kakoune --config $HOME/PATH_TO_YOUR_CONFIG_TOML/kak-lsp/config.toml -s $kak_session }
# Enable kak-lsp for V files
hook global WinSetOption filetype=v %{ lsp-enable-window }

# Close kak-lsp when kakoune is closed
hook global KakEnd .* lsp-exit
# When VLS throws errors after a Kakoune restart is
# when you absolutely, positively, have to kill a process
#hook global KakEnd .* %sh{ kill $(ps ax | grep "kak-lsp" | awk '{print $1}') }

You can start typing and switch through the autocomplete suggestions with [Ctrl+n] or [Ctrl+p]. V autocompletion The rest is trivial and left to the reader.

About

V language support plugin for the Kakoune text editor

Topics

Resources

License

Stars

Watchers

Forks