favicon here hometagsblog

How To Setup Julia with Kakoune

#editor #kakoune #julia

uncomfyhalomacro | 2024-06-08 | reading time: ~7min

This short post talks about how I setup Julia for Kakoune based of from my config. I rarely use Julia nowadays but some people still ask me how to setup Julia for X and Y editor lmao. So I'm just going to write it here to help others out a bit.

What is Kakoune?§

Kakoune is a modal editor that tries to be better than Vim. It reverses the action-verb syntax of Vim e.g. dw for delete word in Vim, wd in Kakoune. And it also removes some inconsistencies of the Vim editor when it comes to other functions of which I won't discuss and which you have to find out yourself.

The editor also has multi-cursors which is a controversial topic among terminal editor enthusiasts. But it's a good fit for Kakoune because of its "haptic" feedback or more accurately visual feedback that shows a selection before we can do any action.

The editor also has some kind of session/server support where you can edit in the same server socket. This allows multi-window editing without the actual need of a multiplexer (although, most users do actually use a multiplexer to make the experience better). Here is an example script to allow me to connect an existing session with sk or skim (an fzf clone in Rust).

#!/bin/bash

set -euo pipefail

shopt -s lastpipe

SESSION=$(kak -l \
	| sk \
	--prompt "choose an existing kakoune session> "\
	--header "sessions" --reverse)

export SESSION
[ -z "${SESSION}" ] && exit 1
kak -c $SESSION $@ && exit 0

The editor also has its own configuration language called kakscript. Although it's better for you to explore what it is since it will be discussed in this post.

This editor is highly opinionated and has many mixed opinions among Vimmers. I don't consider myself a Vim expert nor a Kakoune expert but I have experienced using many editors since the start of COVID-19.

Setup§

Some prerequisites needed:

  • Installed cargo from Rust
  • Kakoune itself
  • Julia

Assuming you already have the prerequisites, head to $XDG_CONFIG_HOME/kak or ~/.config/kak. If the directory does not exist, create it with md or mkdir. Next, at the root of the $XDG_CONFIG_HOME/kak or ~/.config/kak let's add a bootstrap install script for kak-bundle in the file kakrc.

evaluate-commands %sh{
  # We're assuming the default bundle_path here...
  plugins="$kak_config/bundle"
  mkdir -p "$plugins"
  [ ! -e "$plugins/kak-bundle" ] && \
    git clone -q https://github.com/jdugan6240/kak-bundle "$plugins/kak-bundle"
  printf "%s\n" "source '$plugins/kak-bundle/rc/kak-bundle.kak'"
}

bundle-noload kak-bundle https://github.com/jdugan6240/kak-bundle

The script was written in a configuration language called kakoune script or kakscript. The idea behind the language is to utilise the built-in shell that is assumed to be POSIX-compliant or POSIX-compatible e.g. bash, zsh, sh, dash, et cetera.

Installing Kakoune LSP§

Some people including me can't live without LSP so let's include that to our kakrc file.

bundle kakoune-lsp 'git clone --depth 1 -b v17.0.1 https://github.com/kakoune-lsp/kakoune-lsp'  %{

    hook global WinSetOption filetype=(julia) %{
        set global lsp_hover_anchor false
        set global lsp_auto_show_code_actions true
        lsp-enable-window
        map global user l %{: enter-user-mode lsp<ret>} -docstring "lsp mode commands"
        map global goto w '<esc>: lsp-hover-buffer lsp-info-window <ret>' -docstring 'lsp-info-window'
        # define-command -docstring 'lsp-logs: shows lsp logs on tmux window' lsp-logs -params 0 %{
            # terminal sh -c 'less +F /tmp/kak-lsp.log'
        # }
        # map global goto L '<esc>: lsp-logs <ret>' -docstring 'show lsp logs on another window'
    }

    hook global KakEnd .* %{
        lsp-exit
        nop %sh{
            rm -v /tmp/kak-lsp.log
        }
    }

} %{}

We also need to add a bootstrap script to install the LanguageServer.jl package with PackageCompiler.jl. At the root of the $XDG_CONFIG_HOME/kak or ~/.config/kak, create the directory scripts. Then create the file scripts/julia-ls-install with the following contents

import Pkg; Pkg.add("PackageCompiler"); 
Pkg.add(url="https://github.com/julia-vscode/LanguageServer.jl", rev="master"); 
Pkg.update();
using PackageCompiler; create_sysimage(:LanguageServer, sysimage_path=dirname(Pkg.Types.Context().env.project_file) * "/languageserver.so")

And in the $XDG_CONFIG_HOME/kak/kakrc or ~/.config/kak/kakrc file, append the following contents

bundle-install-hook kakoune-lsp %{
    cargo install --path . --root "${HOME}/.local"
    julia --project=@kak-lsp "${kak_config}"/scripts/julia-ls-install
}

This will allow you to install kakoune-lsp as well as Julia's LanguageServer.jl and PackageCompiler.jl.

The script allows you to run a precompiled sysimage of the Julia LanguageServer.jl (although it's not needed that much ever since precompilation has improved since version 1.9 of Julia).

Configuring the LSP§

kakoune-lsp also needs to have a file that configures the LSP of any language. At the root of the $XDG_CONFIG_HOME/kak-lsp or ~/.config/kak-lsp, create the file kak-lsp.toml. If the directory does not exist, create it first before creating the file. Add the following contents to the file

snippet_support = true
verbosity = 2

[server]
timeout = 1800 # seconds = 30 minutes

[language_server.julia]
filetypes = ["julia"]
roots = ["Project.toml", ".git", ".hg", "Manifest.toml"]
command = "sh"
args = ["-c",
   """
   julia --startup-file=no --history-file=no --project=@kak-lsp ~/.config/kak/scripts/julia-ls-kak
   """,
]

[language_server.julia.settings]
julia.format.indent = 4
julia.lint.call = true
julia.lint.run = true
julia.missingrefs = "all"
julia.lint.iter = true
julia.modname = true

[semantic_tokens]
faces = [
    {face="documentation", token="comment", modifiers=["documentation"]},
    {face="comment", token="comment"},
    {face="function", token="function"},
    {face="keyword", token="keyword"},
    {face="module", token="namespace"},
    {face="operator", token="operator"},
    {face="string", token="string"},
    {face="type", token="type"},
    {face="default+d", token="variable", modifiers=["readonly"]},
    {face="default+d", token="variable", modifiers=["constant"]},
    {face="variable", token="variable"},
]

Then at the root of the $XDG_CONFIG_HOME/kak or ~/.config/kak, add a new file scripts/julia-ls-kak with the following content

import Pkg

"""
  `buffer_file_path`

Gets `buffer-file-name` value which gives
the full path to the file associated with the buffer.
"""
buffer_file_path = if haskey(ENV, "kak_buffile")
    ENV["kak_buffile"]
elseif haskey(ENV, "KAK_LSP_PROJECT_ROOT_JULIA")
    ENV["kak_buffile"] = ENV["KAK_LSP_PROJECT_ROOT_JULIA"]
else
    ENV["kak_buffile"] = Pkg.Types.Context().env.project_file
end

"""
  `project_path`

Gets the full path of the project where the LSP should
start. The steps starts with the following:

1. Check if there is an explicitly set project.
2. Check for Project.toml from buffer's full file path exluding the file name.
3. Check for Project.toml in current working directory.
4. Fallback to global environment.
"""
project_path = let
    dirname(something(

        Base.load_path_expand((
            p = get(ENV, "JULIA_PROJECT", nothing);
            p === nothing ? nothing : isempty(p) ? nothing : p
        )),
        Base.current_project(strip(buffer_file_path)),
        Base.current_project(pwd()),
        Pkg.Types.Context().env.project_file,
        Base.active_project()
    ))
end

# Activate the project
Pkg.activate(project_path)
# Install packages if they weren't installed
Pkg.instantiate(; verbose=true)
# Then remove it from the LOAD_PATH
# We did this just to get the packages for the
# project, we do not need it for the LSP.
popfirst!(LOAD_PATH)

ls_install_path = joinpath(get(DEPOT_PATH, 1, joinpath(homedir(), ".julia")), "environments", "kak-lsp");
pushfirst!(LOAD_PATH, ls_install_path);
using LanguageServer;
popfirst!(LOAD_PATH);
depot_path = get(ENV, "JULIA_DEPOT_PATH", "")
symbol_server_path = joinpath(homedir(), ".cache", "julia_lsp_symbol_server")
mkpath(symbol_server_path)
server = LanguageServer.LanguageServerInstance(stdin, stdout, project_path, depot_path, nothing, symbol_server_path, true)
server.runlinter = true
run(server)

This is a custom LSP script I made to avoid the limitations of just running run(server). See the comments above the project_path variable.

Conclusion§

That's it. You have finally reached the end of the post and have configured your kakoune editor to work with Julia.

Here are what the result should look like if (ignoring the statusline and the gruvbox theme) if you have the config:

Hover: LSP Hover

Snippets and Completions: Completions

Versions of software used at the time of writing§

  • Julia v1.10.4
  • Rust 1.78
  • Kakoune v2023.08.05
  • Kakoune LSP v17.0.1

Articles from blogs I follow around the net

Perma-Vuln: D-Link DIR-859, CVE-2024-0769

Recently Sift caught an interesting payload. As it turns out, the exploit was CVE-2024-0769, which is now tagged here: D-Link DIR-859 Information Disclosure Attempt . This vulnerability is a path traversal leading to information disclosure. But, perhaps mo…

via GreyNoise LabsJune 25, 2024

Synergy Greg

Synergy Greg would like to see you in His office, it is the one down the hall, past the cubicles and dreary faces, uplifted only when He deigns to venture forth. You will know Him when you see Him, He is the one composed, of a thousand writhing forms,…

via LudicityJune 22, 2024

Status update, June 2024

Hi all! This status update will be shorter than usual because I had a lot less free time for my open-source projects than usual this month. Indeed, I recently joined SNCF Réseau (the company responsible for the French railway infrastructure) to work on OSR…

via emersionJune 18, 2024

Why People are Angry over Go 1.23 Iterators

NOTE: This is based on, but completely rewritten, from a Twitter post: https://x.com/TheGingerBill/status/1802645945642799423 TL;DR It makes Go feel too “functional” rather than being an unabashed imperative language. I recently saw a post on Twitter showi…

via Articles on gingerBillJune 17, 2024

My RSS feed has been upgraded ✨

I did some integration work to include posts written for other publications in my RSS feed. Apologies if you see some duplicated items! 📪

via Rob O'LearyJune 15, 2024

Programming at the edge with Fastly Compute

So you’ve heard about computing at the edge, and you’ve heard that Fastly let’s you run JavaScript, Go, Rust and any other language that compiles to Wasm at the edge… well, let’s take a look and while we’re at it let’s try and understand how caching works …

via Posts on integralistJune 12, 2024

Generated by openring-rs