Cleanup the home kakoune module, restore tree-sitter

This commit is contained in:
Natsu Kagami 2024-10-07 22:25:59 +02:00
parent eb3d53b829
commit 8608362922
Signed by: nki
GPG key ID: 55A032EB38B49ADB
8 changed files with 59 additions and 696 deletions

View file

@ -1,40 +1,17 @@
{ config, pkgs, lib, ... }:
{ config, options, pkgs, lib, ... }:
with lib;
let
cfg = config.programs.my-kakoune;
autoloadModule = types.submodule {
options = {
name = mkOption {
type = types.str;
description = "Name of the autoload script/folder. It might affect kakoune's load order.";
};
src = mkOption {
type = types.path;
description = "Path to the autoload script/folder.";
};
wrapAsModule = mkOption {
type = types.bool;
default = false;
description = "Wrap the given source file in a `provide-module` command. Fails if the `src` is not a single file.";
};
activationScript = mkOption {
type = types.nullOr types.lines;
default = null;
description = "Add an activation script to the module. It will be wrapped in a `hook global KakBegin .*` wrapper.";
};
};
};
in
{
imports = [ ./kak-lsp.nix ./fish-session.nix ./tree-sitter.nix ];
imports = [ ./fish-session.nix ./tree-sitter.nix ];
options.programs.my-kakoune = {
enable = mkEnableOption "My version of the kakoune configuration";
package = mkOption {
type = types.package;
default = pkgs.kakoune;
default = pkgs.nki-kakoune;
description = "The kakoune package to be installed";
};
rc = mkOption {
@ -42,22 +19,16 @@ in
default = "";
description = "Content of the kakrc file. A line-concatenated string";
};
autoload = mkOption {
type = types.listOf autoloadModule;
default = [ ];
description = "Sources to autoload";
};
themes = mkOption {
type = types.attrsOf types.path;
default = { };
description = "Themes to load";
};
extraFaces = mkOption {
type = types.attrsOf types.str;
default = { };
description = "Extra faces to include";
};
autoloadFile = mkOption {
type = options.xdg.configFile.type;
default = { };
description = "Extra autoload files";
};
};
config = mkIf cfg.enable {
@ -65,37 +36,6 @@ in
xdg.configFile =
let
kakouneAutoload = { name, src, wrapAsModule ? false, activationScript ? null }:
[
(if !wrapAsModule then {
name = "kak/autoload/${name}";
value.source = src;
} else {
name = "kak/autoload/${name}/module.kak";
value.text = ''
provide-module ${name} %
${readFile src}
'';
})
] ++ (if activationScript == null then [ ] else [{
name = "kak/autoload/on-load/${name}.kak";
value.text = ''
hook global KakBegin .* %{
${activationScript}
}
'';
}]);
kakouneThemes = builtins.listToAttrs (builtins.attrValues (
builtins.mapAttrs
(name: src: {
name = "kak/colors/${name}.kak";
value.source = src;
})
cfg.themes
));
kakouneFaces =
let
txt = strings.concatStringsSep "\n" (builtins.attrValues (builtins.mapAttrs (name: face: "face global ${name} \"${face}\"") cfg.extraFaces));
@ -103,6 +43,7 @@ in
pkgs.writeText "faces.kak" txt;
in
{
"kak/autoload/builtin".source = "${cfg.package}/share/kak/autoload";
# kakrc
"kak/kakrc".text = ''
${cfg.rc}
@ -110,15 +51,14 @@ in
# Load faces
source ${kakouneFaces}
'';
} //
(builtins.listToAttrs (lib.lists.flatten (map kakouneAutoload ([
# include the original autoload files
{
name = "rc";
src = "${cfg.package}/share/kak/autoload/rc";
}
] ++ cfg.autoload))))
// kakouneThemes;
} // lib.mapAttrs'
(name: attrs: {
name = "kak/autoload/${name}";
value = attrs // {
target = "kak/autoload/${name}";
};
})
cfg.autoloadFile;
};
}

View file

@ -1,213 +0,0 @@
{ config, pkgs, lib, ... }:
with lib;
let
lspConfig =
{
language_ids = {
c = "c_cpp";
cpp = "c_cpp";
javascript = "javascriptreact";
typescript = "typescriptreact";
protobuf = "proto";
sh = "shellscript";
};
language_servers = {
ccls = {
args = [ "-v=2" "-log-file=/tmp/ccls.log" ];
command = "ccls";
filetypes = [ "c" "cpp" ];
roots = [ "compile_commands.json" ".cquery" ".git" ];
};
gopls = {
command = "gopls";
filetypes = [ "go" ];
offset_encoding = "utf-8";
roots = [ "Gopkg.toml" "go.mod" ".git" ".hg" ];
settings = { gopls = { hoverKind = "SynopsisDocumentation"; semanticTokens = true; }; };
settings_section = "gopls";
};
haskell-language-server = {
args = [ "--lsp" ];
command = "haskell-language-server-wrapper";
filetypes = [ "haskell" ];
roots = [ "Setup.hs" "stack.yaml" "*.cabal" "package.yaml" ];
settings_section = "haskell";
};
nil = {
command = "${pkgs.nil}/bin/nil";
filetypes = [ "nix" ];
roots = [ "flake.nix" "shell.nix" ".git" ];
settings.nil = {
formatting.command = [ "${getExe pkgs.nixpkgs-fmt}" ];
};
};
pyls = {
command = "pyls";
filetypes = [ "python" ];
offset_encoding = "utf-8";
roots = [ "requirements.txt" "setup.py" ".git" ".hg" ];
};
};
semantic_tokens.faces = [
## Items
# (Rust) Macros
{ face = "attribute"; token = "attribute"; }
{ face = "attribute"; token = "derive"; }
{ face = "macro"; token = "macro"; } # Function-like Macro
# Keyword and Fixed Tokens
{ face = "keyword"; token = "keyword"; }
{ face = "operator"; token = "operator"; }
# Functions and Methods
{ face = "function"; token = "function"; }
{ face = "method"; token = "method"; }
# Constants
{ face = "string"; token = "string"; }
{ face = "format_specifier"; token = "formatSpecifier"; }
# Variables
{ face = "variable"; token = "variable"; modifiers = [ "readonly" ]; }
{ face = "mutable_variable"; token = "variable"; }
{ face = "module"; token = "namespace"; }
{ face = "variable"; token = "type_parameter"; }
{ face = "class"; token = "enum"; }
{ face = "class"; token = "struct"; }
{ face = "class"; token = "trait"; }
{ face = "class"; token = "union"; }
{ face = "class"; token = "class"; }
## Comments
{ face = "documentation"; token = "comment"; modifiers = [ "documentation" ]; }
{ face = "comment"; token = "comment"; }
];
server = { timeout = 1800; };
snippet_support = false;
verbosity = 255;
};
languageServerOption = types.submodule {
options = {
filetypes = mkOption {
type = types.listOf types.str;
description = "The list of filetypes to assign the language to";
};
roots = mkOption {
type = types.listOf types.str;
description = "The list of root filenames that are used to determine the project root";
};
command = mkOption {
type = types.str;
description = "The LSP server command to be called.";
};
args = mkOption {
type = types.listOf types.str;
default = [ ];
description = "The arguments passed onto the LSP server.";
};
offset_encoding = mkOption {
type = types.nullOr (types.enum [ "utf-8" ]);
default = null;
description = "The offset encoding used by the LSP server.";
};
settings_section = mkOption {
type = types.nullOr types.str;
default = null;
description = "The settings section to be sent to LSP server.";
};
settings = mkOption {
type = types.nullOr (types.attrsOf types.anything);
default = null;
description = "Additional settings to be passed to the LSP server.";
};
package = mkOption {
type = types.nullOr types.package;
default = null;
description = "The default package of the language server. Will be appended as the ending segments of the PATH to kak-lsp";
};
};
};
cfg = config.programs.kak-lsp;
serverPackages =
filter (v: v != null)
(lib.mapAttrsToList (_: serv: serv.package) cfg.languageServers);
wrappedPackage = pkgs.symlinkJoin {
name = "kak-lsp-wrapped";
nativeBuildInputs = [ pkgs.makeWrapper ];
paths = [ cfg.package ];
postBuild = ''
wrapProgram $out/bin/kak-lsp --suffix PATH ":" ${lib.makeBinPath serverPackages}
'';
};
in
{
options.programs.kak-lsp = {
enable = mkEnableOption "Enable kak-lsp support";
package = mkOption {
type = types.package;
default = pkgs.kak-lsp;
};
enableSnippets = mkOption {
type = types.bool;
default = false;
description = "Enable snippet support";
};
semanticTokens.faces = mkOption {
type = types.listOf types.anything;
default = lspConfig.semantic_tokens.faces;
description = "The semantic tokens faces mapping given to kak";
};
semanticTokens.additionalFaces = mkOption {
type = types.listOf types.anything;
default = [ ];
description = "The semantic tokens faces mapping given to kak";
};
serverTimeout = mkOption {
type = types.int;
default = 1000;
description = "Server timeout";
};
languageServers = mkOption {
type = types.attrsOf languageServerOption;
default = { };
description = "The language options";
};
languageIds = mkOption {
type = types.attrsOf types.str;
default = { };
description = "Language IDs to be sent to the LSP";
};
};
config = mkIf cfg.enable
{
home.packages = [ wrappedPackage ];
# Configurations
xdg.configFile."kak-lsp/kak-lsp.toml" =
let
toml = pkgs.formats.toml { };
toLspConfig = lib.filterAttrsRecursive (n: v: n != "package" && v != null);
in
{
source = toml.generate "config.toml"
{
semantic_tokens.faces = cfg.semanticTokens.faces ++ cfg.semanticTokens.additionalFaces;
server.timeout = cfg.serverTimeout;
snippet_support = cfg.enableSnippets;
verbosity = 255;
language_server = toLspConfig (lspConfig.language_servers // cfg.languageServers);
language_ids = lspConfig.language_ids // cfg.languageIds;
};
};
};
}

View file

@ -33,6 +33,27 @@ let
};
};
};
mkGrammarPackage =
{ name
, src
, grammarPath ? "src"
, grammarCompileArgs ? [ "-O3" "-c" "-fpic" "../parser.c" "../scanner.c" "-I" ".." ]
, grammarLinkArgs ? [ "-shared" "-fpic" "parser.o" "scanner.o" ]
}: pkgs.stdenv.mkDerivation {
inherit src;
name = "kak-tree-sitter-grammar-${name}.so";
version = "latest";
buildPhase = ''
mkdir ${grammarPath}/build
cd ${grammarPath}/build
$CC ${lib.concatStringsSep " " grammarCompileArgs}
$CC ${lib.concatStringsSep " " grammarLinkArgs} -o ${name}.so
'';
installPhase = ''
cp ${name}.so $out
'';
};
in
{
options.programs.my-kakoune.tree-sitter = {
@ -200,24 +221,15 @@ in
toml = pkgs.formats.toml { };
srcName = src: lib.removePrefix "/nix/store/" src.outPath;
mkGitRepo = src: pkgs.runCommandLocal "${src.name}-git" { } ''
cp -r --no-preserve=all ${src} $out
cd $out
if ! test -d $out/.git; then
${lib.getExe pkgs.git} init -b ${srcName src}
${lib.getExe pkgs.git} config user.email "a@b.com"
${lib.getExe pkgs.git} config user.name "a"
${lib.getExe pkgs.git} add .
${lib.getExe pkgs.git} commit -m "Just making a git commit"
fi
'';
toLanguageConf = name: lang: with lang; {
grammar = {
inherit (grammar) path;
source.git.url = "${mkGitRepo grammar.src}";
source.git.pin = "${srcName grammar.src}";
source.local.path = mkGrammarPackage {
inherit name;
src = grammar.src;
grammarPath = grammar.path;
grammarCompileArgs = grammar.compile.flags ++ grammar.compile.args;
grammarLinkArgs = grammar.link.flags ++ grammar.link.args;
};
compile = grammar.compile.command;
compile_args = grammar.compile.args;
compile_flags = grammar.compile.flags;
@ -225,10 +237,9 @@ in
link_args = grammar.link.args ++ [ "-o" "${name}.so" ];
link_flags = grammar.link.flags;
};
queries = {
source.git.url = "${mkGitRepo queries.src}";
source.git.pin = "${srcName queries.src}";
queries = rec {
path = if queries.path == null then "runtime/queries/${name}" else queries.path;
source.local.path = "${queries.src}/${path}";
};
};
in
@ -249,14 +260,14 @@ in
features = cfg.features;
language = builtins.mapAttrs toLanguageConf cfg.languages;
};
onChange = ''
export PATH=$PATH:${lib.getBin pkgs.gcc}
${cfg.package}/bin/ktsctl sync -a
'';
};
programs.my-kakoune.extraFaces = faces;
programs.my-kakoune.autoloadFile."kak-tree-sitter.kak".text = ''
# Enable kak-tree-sitter
eval %sh{kak-tree-sitter --kakoune -d --server --init $kak_session}
map global normal <c-t> ": enter-user-mode tree-sitter<ret>"
'';
};
}