nix-dotfiles/modules/kakoune.nix
2021-09-24 09:27:13 +02:00

369 lines
13 KiB
Nix

{ config, lib, pkgs, ... }:
let
strPalette = with pkgs.rice; palette.toRgbShortHex colorPalette;
cfg = {
enableCppSupport = true;
enableDataSupport = true;
enableGoSupport = true;
enableHtmlSupport = true;
enableJavascriptSupport = true;
enableLatexSupport = true;
enableNixSupport = true;
enablePythonSupport = true;
enableRustSupport = true;
enableShellSupport = true;
enableSpellingSupport = true;
};
packages = with pkgs; [
# editorconfig-core-c
python3.pkgs.editorconfig
] ++ (
lib.optionals cfg.enableCppSupport [
clang-tools
]
) ++ (
lib.optionals cfg.enableDataSupport [
yaml-language-server
nodePackages.vscode-langservers-extracted
]
) ++ (
lib.optionals cfg.enableHtmlSupport [
nodePackages.vscode-langservers-extracted
]
) ++ (
lib.optionals cfg.enableJavascriptSupport [
flow
]
) ++ (
lib.optionals cfg.enableLatexSupport [
texlab
]
) ++ (
lib.optionals cfg.enableNixSupport [
rnix-lsp
]
) ++ (
with python3.pkgs; lib.optionals cfg.enablePythonSupport [
black
# pyls-black
pyls-flake8
pyls-isort
# pyls-mypy
python-lsp-server
]
) ++ (
lib.optionals cfg.enableRustSupport [
cargo
cargo-watch
clippy
rust-analyzer
rustc
rustfmt
]
) ++ (
lib.optionals cfg.enableShellSupport [
nodePackages.bash-language-server
]
) ++ (
lib.optionals cfg.enableSpellingSupport [
aspell
aspellDicts.en
aspellDicts.en-computers
aspellDicts.en-science
aspellDicts.it
]
);
plugins = with pkgs; [
kakounePlugins.prelude-kak
kakounePlugins.kak-lsp
# kakounePlugins.auto-pairs-kak
kakounePlugins.powerline-kak
kakounePlugins.connect-kak
];
in
{
home.sessionVariables = { EDITOR = "kak"; VISUAL = "kak"; };
programs.bash.shellAliases = { k = "kak"; };
programs.zsh.shellAliases = { k = "kak"; };
home.packages = packages;
programs.kakoune = {
enable = true;
config = {
colorScheme = "nord";
tabStop = 2;
indentWidth = 2;
alignWithTabs = true;
scrollOff = {
lines = 5;
columns = 3;
};
ui = {
setTitle = true;
changeColors = true;
enableMouse = true;
};
wrapLines = {
enable = true;
word = true;
};
numberLines = {
enable = true;
highlightCursor = true;
};
keyMappings = with pkgs; [
{ mode = "normal"; docstring = "Edit file"; key = "<c-e>"; effect = ":edit<space>"; }
{ mode = "user"; docstring = "Code actions"; key = "a"; effect = ":lsp-code-actions<ret>"; }
{ mode = "user"; docstring = "Comment block"; key = "b"; effect = ":comment-block<ret>"; }
{ mode = "user"; docstring = "Comment line"; key = "l"; effect = ":comment-line<ret>"; }
{ mode = "user"; docstring = "Copy to clipboard"; key = "y"; effect = "<a-|>${xclip}/bin/xclip -i -selection clipboard<ret>"; }
{ mode = "user"; docstring = "Format code with formatter"; key = "f"; effect = ":format<ret>"; }
{ mode = "user"; docstring = "Format code with LSP"; key = "F"; effect = ":lsp-formatting-sync<ret>"; }
{ mode = "user"; docstring = "Jump to definition"; key = "d"; effect = ":lsp-definition<ret>"; }
{ mode = "user"; docstring = "Jump to type definition"; key = "t"; effect = ":lsp-type-definition<ret>"; }
{ mode = "user"; docstring = "List project diagnostics"; key = "i"; effect = ":lsp-diagnostics<ret>"; }
{ mode = "user"; docstring = "Paste from clipboard (after)"; key = "P"; effect = "<a-!>${xclip}/bin/xclip -selection clipboard -o<ret>"; }
{ mode = "user"; docstring = "Paste from clipboard (before)"; key = "p"; effect = "!${xclip}/bin/xclip -selection clipboard -o<ret>"; }
{ mode = "user"; docstring = "Show hover info"; key = "q"; effect = ":lsp-hover<ret>"; }
{ mode = "user"; docstring = "Spellcheck English"; key = "S"; effect = ":spell en<ret>"; }
{ mode = "user"; docstring = "Spellcheck"; key = "s"; effect = ":spell "; }
{ mode = "normal"; docstring = "Try next snippet placeholder"; key = "<c-n>"; effect = "<a-;>: insert-c-n<ret>"; }
];
hooks = with pkgs; [
{ name = "BufCreate"; option = ".*"; commands = "editorconfig-load"; }
{ name = "ModuleLoaded"; option = "auto-pairs"; commands = "auto-pairs-enable"; }
{ name = "ModuleLoaded"; option = "powerline"; commands = builtins.concatStringsSep "\n" [ "powerline-enable" ]; }
# { name = "WinSetOption"; option = "filetype=(rust|python|c|cpp|latex|javascript|go|nix)"; commands = builtins.concatStringsSep "\n" [ "lsp-enable-window" ]; }
] ++ (
lib.optionals cfg.enableLatexSupport [
{ name = "BufSetOption"; option = "filetype=latex"; commands = "set-option buffer formatcmd ${texlive.bin.latexindent}/bin/latexindent"; }
]
) ++ (
lib.optionals cfg.enableDataSupport [
{ name = "BufSetOption"; option = "filetype=markdown"; commands = "set-option buffer formatcmd ${python3.pkgs.mdformat}/bin/mdformat"; }
]
);
# TODO add more formatters from https://github.com/mawww/kakoune/wiki/Format
};
extraConfig = builtins.concatStringsSep "\n" [
"# Custom commands"
"define-command -docstring 'save and quit' x 'write-all; quit' # Save and quit with 'x'"
"add-highlighter global/ regex \\h+$ 0:Error # Highlight trailing spaces"
"eval %sh{kak-lsp --kakoune -s $kak_session}"
"lsp-enable"
# Kak-lsp snippet support
''
def -hidden insert-c-n %{
try %{
lsp-snippets-select-next-placeholders
exec '<a-;>d'
} catch %{
exec -with-hooks '<c-n>'
}
}
''
"powerline-start"
"require-module connect-broot"
"require-module connect-lf"
"require-module connect-rofi"
];
plugins = plugins;
};
xdg.configFile."kak/colors/nord.kak".text = with strPalette; ''
set-face global value rgb:${normal.magenta},default
set-face global type rgb:${bright.white},default
set-face global identifier rgb:${normal.cyan},default
set-face global string rgb:${normal.green},default
set-face global error rgb:${normal.red},default
set-face global keyword rgb:${normal.blue},default
set-face global operator rgb:${normal.blue},default
set-face global attribute rgb:${bright.blue},default
set-face global comment rgb:${bright.blue},default
set-face global meta rgb:${normal.yellow},default
set-face global Default rgb:${normal.white},default
set-face global PrimarySelection rgb:${normal.black},rgb:${dark.yellow}
set-face global SecondarySelection rgb:${normal.black},rgb:${dark.white}
set-face global PrimaryCursor rgb:${normal.black},rgb:${normal.yellow}
set-face global SecondaryCursor rgb:${normal.black},rgb:${normal.white}
set-face global MenuForeground rgb:${normal.white},rgb:${bright.black}
set-face global MenuBackground default,rgb:${normal.black}
set-face global MenuInfo default,rgb:${normal.black}
set-face global Information rgb:${dark.black},rgb:${normal.cyan}
set-face global StatusLine rgb:${normal.white},rgb:${normal.black}
set-face global StatusLineMode rgb:${normal.blue},rgb:${normal.black}
set-face global StatusLineInfo rgb:${normal.blue},rgb:${normal.black}
set-face global StatusLineValue rgb:${normal.blue},rgb:${normal.black}
set-face global StatusCursor rgb:${dark.black},rgb:${normal.yellow}
set-face global Prompt rgb:${normal.cyan},rgb:${normal.black}
set-face global BufferPadding default,default
'';
xdg.configFile."kak-lsp/kak-lsp.toml".text = with pkgs; builtins.concatStringsSep "\n" (
[
''
snippet_support = false
verbosity = 2
[server]
timeout = 1800 # seconds = 30 minutes
''
]
++ (
lib.optional cfg.enableCppSupport ''
[language.c_cpp]
filetypes = ["c", "cpp"]
roots = ["compile_commands.json", ".clangd"]
command = "${clang-tools}/bin/clangd"
offset_encoding = "utf-8"
''
) ++ (
lib.optional cfg.enableDataSupport ''
[language.json]
filetypes = ["json"]
roots = ["package.json"]
command = "${nodePackages.vscode-langservers-extracted}/bin/vscode-json-language-server"
args = ["--stdio"]
[language.yaml]
filetypes = ["yaml"]
roots = [".git"]
command = "${yaml-language-server}/bin/yaml-language-server"
args = ["--stdio"]
[language.yaml.settings]
# See https://github.com/redhat-developer/yaml-language-server#language-server-settings
# Defaults are at https://github.com/redhat-developer/yaml-language-server/blob/master/src/yamlSettings.ts
yaml.format.enable = true
''
) ++ (
lib.optional cfg.enableGoSupport ''
[language.go]
filetypes = ["go"]
roots = ["Gopkg.toml", "go.mod", ".git", ".hg"]
command = "gopls"
offset_encoding = "utf-8"
settings_section = "gopls"
[language.go.settings.gopls]
# See https://github.com/golang/tools/blob/master/gopls/doc/settings.md
# "build.buildFlags" = []
''
)
++ (
lib.optional cfg.enableHtmlSupport ''
[language.html]
filetypes = ["html"]
roots = ["package.json"]
command = "${nodePackages.vscode-langservers-extracted}/bin/vscode-html-language-server"
args = ["--stdio"]
[language.css]
filetypes = ["css"]
roots = ["package.json", ".git"]
command = "${nodePackages.vscode-langservers-extracted}/bin/vscode-css-language-server"
args = ["--stdio"]
[language.less]
filetypes = ["less"]
roots = ["package.json", ".git"]
command = "${nodePackages.vscode-langservers-extracted}/bin/vscode-css-language-server"
args = ["--stdio"]
[language.scss]
filetypes = ["scss"]
roots = ["package.json", ".git"]
command = "${nodePackages.vscode-langservers-extracted}/bin/vscode-css-language-server"
args = ["--stdio"]
''
) ++ (
lib.optional cfg.enableJavascriptSupport ''
[language.javascript]
filetypes = ["javascript"]
roots = [".flowconfig"]
command = "${flow}/bin/flow"
args = ["lsp"]
''
) ++ (
lib.optional cfg.enableLatexSupport ''
[language.latex]
filetypes = ["latex"]
roots = [".git"]
command = "${texlab}/bin/texlab"
settings_section = "texlab"
[language.latex.settings.texlab]
# See https://github.com/latex-lsp/texlab/blob/master/src/options.rs
bibtexFormatter = "${texlab}/bin/texlab"
''
) ++ (
lib.optional cfg.enableNixSupport ''
[language.nix]
filetypes = ["nix"]
roots = ["flake.nix", "shell.nix", ".git"]
command = "${rnix-lsp}/bin/rnix-lsp"
''
) ++ (
lib.optional cfg.enablePythonSupport ''
[language.python]
filetypes = ["python"]
roots = ["requirements.txt", "setup.py", ".git", ".hg"]
command = "${python3.pkgs.python-lsp-server}/bin/pylsp"
offset_encoding = "utf-8"
[language.python.settings]
# See https://github.com/palantir/python-language-server#configuration
# and https://github.com/palantir/python-language-server/blob/develop/vscode-client/package.json
# "pyls.configurationSources" = ["flake8"]
''
) ++ (
lib.optional cfg.enableRustSupport ''
[language.rust]
filetypes = ["rust"]
roots = ["Cargo.toml"]
command = "${rust-analyzer}/bin/rust-analyzer"
settings_section = "rust-analyzer"
[language.rust.settings.rust-analyzer]
hoverActions.enable = false # kak-lsp doesn't support this at the moment
# cargo.features = []
# See https://rust-analyzer.github.io/manual.html#configuration
# If you get 'unresolved proc macro' warnings, you have two options
# 1. The safe choice is two disable the warning:
diagnostics.disabled = ["unresolved-proc-macro"]
# 2. Or you can opt-in for proc macro support
procMacro.enable = true
cargo.loadOutDirsFromCheck = true
# See https://github.com/rust-analyzer/rust-analyzer/issues/6448
''
) ++ (
lib.optional cfg.enableShellSupport ''
[language.bash]
filetypes = ["sh"]
roots = [".git", ".hg"]
command = "${nodePackages.bash-language-server}/bin/bash-language-server"
args = ["start"]
''
)
);
xdg.dataFile."applications/kakoune.desktop".source =
let
desktopItem = pkgs.makeDesktopItem
{
name = "kakoune";
type = "Application";
exec = "kak %U";
icon = "kakoune";
desktopName = "Kakoune";
comment = "Kakoune text editor";
terminal = true;
categories = "Development;Office;";
mimeType = "text/plain;text/*";
};
in
builtins.toPath
"${desktopItem}/share/applications/kakoune.desktop";
}