From 044f4a31cc3312a291f6ec01fc97306c7f57bd8b Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 7 Oct 2024 21:11:48 +0200 Subject: [PATCH] Make tree-sitter kinda work --- home/common.nix | 2 +- overlay.nix | 2 +- .../default.nix} | 1 + .../common/kak-tree-sitter/user_config.patch | 129 ++++++++++++++++++ packages/common/nki-kakoune/default.nix | 4 +- packages/common/nki-kakoune/faces.nix | 31 +++++ packages/common/nki-kakoune/lsp.nix | 14 +- .../nki-kakoune/tree-sitter/default.nix | 28 ++-- .../nki-kakoune/tree-sitter/grammars.nix | 1 + packages/common/nki-kakoune/utils.nix | 2 +- 10 files changed, 187 insertions(+), 27 deletions(-) rename packages/common/{kak-tree-sitter.nix => kak-tree-sitter/default.nix} (98%) create mode 100644 packages/common/kak-tree-sitter/user_config.patch create mode 100644 packages/common/nki-kakoune/faces.nix diff --git a/home/common.nix b/home/common.nix index be94e66..1928065 100644 --- a/home/common.nix +++ b/home/common.nix @@ -2,7 +2,7 @@ { imports = [ - ./kakoune/kak.nix + # ./kakoune/kak.nix ./fish/fish.nix ./modules/programs/my-broot.nix ./modules/programs/my-sway diff --git a/overlay.nix b/overlay.nix index 745c979..2855cd3 100644 --- a/overlay.nix +++ b/overlay.nix @@ -101,7 +101,7 @@ let }; overlay-packages = final: prev: { - kak-tree-sitter = final.callPackage ./packages/common/kak-tree-sitter.nix { rustPlatform = final.unstable.rustPlatform; }; + kak-tree-sitter = final.callPackage ./packages/common/kak-tree-sitter { rustPlatform = final.unstable.rustPlatform; }; kak-lsp = let diff --git a/packages/common/kak-tree-sitter.nix b/packages/common/kak-tree-sitter/default.nix similarity index 98% rename from packages/common/kak-tree-sitter.nix rename to packages/common/kak-tree-sitter/default.nix index 9819e3a..69da116 100644 --- a/packages/common/kak-tree-sitter.nix +++ b/packages/common/kak-tree-sitter/default.nix @@ -34,6 +34,7 @@ let self.runtime_dir.join("sources").join(url_dir) '') + ./user_config.patch ]; meta.mainProgram = "kak-tree-sitter"; diff --git a/packages/common/kak-tree-sitter/user_config.patch b/packages/common/kak-tree-sitter/user_config.patch new file mode 100644 index 0000000..b2b1b6a --- /dev/null +++ b/packages/common/kak-tree-sitter/user_config.patch @@ -0,0 +1,129 @@ +diff --git a/kak-tree-sitter-config/src/lib.rs b/kak-tree-sitter-config/src/lib.rs +index e4edc42..a9c92cb 100644 +--- a/kak-tree-sitter-config/src/lib.rs ++++ b/kak-tree-sitter-config/src/lib.rs +@@ -52,9 +52,13 @@ impl Config { + } + + /// Load the default configuration, the user configuration, and merge both. +- pub fn load_default_user() -> Result { ++ pub fn load_default_user(path: Option>) -> Result { + let mut config = Self::load_default_config()?; +- match UserConfig::load_from_xdg() { ++ let user_config = match path { ++ Some(p) => UserConfig::load(p), ++ None => UserConfig::load_from_xdg(), ++ }; ++ match user_config { + Ok(user_config) => { + config.merge_user_config(user_config)?; + } +@@ -448,7 +452,7 @@ impl UserConfig { + } + + /// Load the configuration from a given path. +- fn load(path: impl AsRef) -> Result { ++ pub fn load(path: impl AsRef) -> Result { + let path = path.as_ref(); + + log::debug!("loading configuration at {path}", path = path.display()); +diff --git a/kak-tree-sitter/src/cli.rs b/kak-tree-sitter/src/cli.rs +index b8102cd..923312c 100644 +--- a/kak-tree-sitter/src/cli.rs ++++ b/kak-tree-sitter/src/cli.rs +@@ -22,6 +22,10 @@ pub struct Cli { + #[clap(short, long)] + pub server: bool, + ++ /// Specify a custom path for the user config. ++ #[clap(short, long)] ++ pub user_config: Option, ++ + /// Try to daemonize, if not already done. + #[clap(short, long)] + pub daemonize: bool, +diff --git a/kak-tree-sitter/src/main.rs b/kak-tree-sitter/src/main.rs +index bee9698..cbd7b39 100644 +--- a/kak-tree-sitter/src/main.rs ++++ b/kak-tree-sitter/src/main.rs +@@ -43,7 +43,7 @@ fn start() -> Result<(), OhNo> { + } + } + +- let config = Config::load_default_user()?; ++ let config = Config::load_default_user(cli.user_config.as_ref())?; + + // inject rc if we start from Kakoune + if cli.kakoune && cli.init.is_some() { +diff --git a/kak-tree-sitter/src/server.rs b/kak-tree-sitter/src/server.rs +index f3b7723..000c81d 100644 +--- a/kak-tree-sitter/src/server.rs ++++ b/kak-tree-sitter/src/server.rs +@@ -73,6 +73,7 @@ impl Server { + log::debug!("creating IO handler"); + let io_handler = IOHandler::new( + config, ++ cli.user_config.clone(), + cli.is_standalone(), + cli.with_highlighting || config.features.highlighting, + resources, +@@ -165,6 +166,7 @@ struct IOHandler { + connections: HashMap, + enqueue_response: EnqueueResponse, + handler: Handler, ++ user_config_path: Option, + } + + impl IOHandler { +@@ -173,6 +175,7 @@ impl IOHandler { + + fn new( + config: &Config, ++ user_config_path: Option, + is_standalone: bool, + with_highlighting: bool, + resources: ServerResources, +@@ -203,6 +206,7 @@ impl IOHandler { + connections, + enqueue_response, + handler, ++ user_config_path, + }) + } + +@@ -450,7 +454,7 @@ impl IOHandler { + } + + fn reload(&mut self) { +- let config = match Config::load_default_user() { ++ let config = match Config::load_default_user(self.user_config_path.as_ref()) { + Ok(config) => config, + Err(err) => { + log::error!("reloading config failed: {err}"); +diff --git a/ktsctl/src/cli.rs b/ktsctl/src/cli.rs +index dfac5c3..09f86f3 100644 +--- a/ktsctl/src/cli.rs ++++ b/ktsctl/src/cli.rs +@@ -11,6 +11,9 @@ pub struct Cli { + #[clap(long)] + pub verbose: bool, + ++ #[clap(short, long)] ++ pub user_config: Option, ++ + #[clap(subcommand)] + pub cmd: Cmd, + } +diff --git a/ktsctl/src/main.rs b/ktsctl/src/main.rs +index f9a3499..f823633 100644 +--- a/ktsctl/src/main.rs ++++ b/ktsctl/src/main.rs +@@ -37,7 +37,7 @@ fn start() -> Result<(), HellNo> { + simple_logger::init_with_level(log::Level::Debug)?; + } + +- let config = Config::load_default_user()?; ++ let config = Config::load_default_user(cli.user_config.as_ref())?; + log::debug!("ktsctl configuration:\n{config:#?}"); + + match cli.cmd { diff --git a/packages/common/nki-kakoune/default.nix b/packages/common/nki-kakoune/default.nix index 0d48b06..4cb34c7 100644 --- a/packages/common/nki-kakoune/default.nix +++ b/packages/common/nki-kakoune/default.nix @@ -11,6 +11,7 @@ in ++ callPackage ./themes.nix { } ++ [ (callPackage ./kaktex { }) + (callPackage ./faces.nix { }) rc tree-sitter.plugin lsp.plugin @@ -23,6 +24,7 @@ in rm "$out/bin/kak" makeWrapper "${kakoune-unwrapped}/bin/kak" "$out/bin/kak" \ --set KAKOUNE_RUNTIME "$out/share/kak" \ - --suffix PATH ":" "${lsp.extraPaths}" + --suffix PATH ":" "${lsp.extraPaths}" \ + --suffix PATH ":" "${tree-sitter.extraPaths}" ''; }) diff --git a/packages/common/nki-kakoune/faces.nix b/packages/common/nki-kakoune/faces.nix new file mode 100644 index 0000000..367472c --- /dev/null +++ b/packages/common/nki-kakoune/faces.nix @@ -0,0 +1,31 @@ +{ callPackage, ... } : +let +utils = callPackage ./utils.nix { }; +faces = { + Default = "%opt{text},%opt{base}"; + BufferPadding = "%opt{base},%opt{base}"; + MenuForeground = "%opt{blue},white+bF"; + MenuBackground = "%opt{sky},white+F"; + Information = "%opt{sky},white"; + # Markdown help color scheme + InfoDefault = "Information"; + InfoBlock = "@block"; + InfoBlockQuote = "+i@block"; + InfoBullet = "@bullet"; + InfoHeader = "@header"; + InfoLink = "@link"; + InfoLinkMono = "+b@mono"; + InfoMono = "@mono"; + InfoRule = "+b@Information"; + InfoDiagnosticError = "@DiagnosticError"; + InfoDiagnosticHint = "@DiagnosticHint"; + InfoDiagnosticInformation = "@Information"; + InfoDiagnosticWarning = "@DiagnosticWarning"; + # Extra faces + macro = "+u@function"; + method = "@function"; + format_specifier = "+i@string"; + mutable_variable = "+i@variable"; + class = "+b@variable"; +}; +in utils.mkFacesScript "default-faces" faces diff --git a/packages/common/nki-kakoune/lsp.nix b/packages/common/nki-kakoune/lsp.nix index 3712d6f..6511ee9 100644 --- a/packages/common/nki-kakoune/lsp.nix +++ b/packages/common/nki-kakoune/lsp.nix @@ -248,7 +248,7 @@ let kak-lsp-config = let toml = formats.toml { }; - toLspConfig = attrs: builtins.removeAttrs attrs [ "package" ]; + toLspConfig = builtins.mapAttrs (_: attrs: builtins.removeAttrs attrs [ "package" ]); in toml.generate "kak-lsp.toml" ({ semantic_tokens.faces = config.faces; @@ -265,16 +265,16 @@ in plugin = writeTextDir "share/kak/autoload/kak-lsp.kak" '' hook global KakBegin .* %{ try %{ - eval %sh{${lib.getExe kak-lsp} --config ${kak-lsp-config} -s $kak_session} + eval %sh{${lib.getExe kak-lsp} --kakoune --config ${kak-lsp-config} -s $kak_session} } lsp-enable - map window lsp N -docstring "Display the next message request" ": lsp-show-message-request-next" - map window normal ": enter-user-mode lsp" - map window normal ": lsp-hover" - map window normal ": lsp-hover-buffer" + map global lsp N -docstring "Display the next message request" ": lsp-show-message-request-next" + map global normal ": enter-user-mode lsp" + map global normal ": lsp-hover" + map global normal ": lsp-hover-buffer" # lsp-auto-hover-insert-mode-enable - set window lsp_hover_anchor true + set global lsp_hover_anchor true map global insert ':try lsp-snippets-select-next-placeholders catch %{ execute-keys -with-hooks tab> }' -docstring 'Select next snippet placeholder' map global object a 'lsp-object' -docstring 'LSP any symbol' map global object 'lsp-object' -docstring 'LSP any symbol' diff --git a/packages/common/nki-kakoune/tree-sitter/default.nix b/packages/common/nki-kakoune/tree-sitter/default.nix index ce16343..b94a771 100644 --- a/packages/common/nki-kakoune/tree-sitter/default.nix +++ b/packages/common/nki-kakoune/tree-sitter/default.nix @@ -129,25 +129,19 @@ let include = "keyword_control_import"; }; - configDir = + configFile = let toScm = name: lib.concatStringsSep "." (lib.splitString "_" name); - toml = formats.toml { }; - file = - toml.generate "config.toml" { - highlight.groups = builtins.map toScm (builtins.attrNames highlighterGroups ++ builtins.attrNames aliases); - features = { - highlighting = true; - text_objects = true; - }; - language = grammars; - }; in - runCommandLocal "kak-tree-sitter-config" { } '' - mkdir -p $out/kak-tree-sitter - ln -s ${file} $out/kak-tree-sitter/config.toml - ''; + toml.generate "config.toml" { + highlight.groups = builtins.map toScm (builtins.attrNames highlighterGroups ++ builtins.attrNames aliases); + features = { + highlighting = true; + text_objects = true; + }; + language = grammars; + }; extraFaces = let @@ -162,10 +156,12 @@ in { rc = '' # Enable kak-tree-sitter - eval %sh{env XDG_CONFIG_DIR=${configDir} ${lib.getExe' kak-tree-sitter "kak-tree-sitter"} --kakoune -d --server --init $kak_session} + eval %sh{kak-tree-sitter --kakoune -d --server --init $kak_session --user-config ${configFile}} map global normal ": enter-user-mode tree-sitter" ''; + extraPaths = "${kak-tree-sitter}/bin"; + plugin = utils.mkFacesScript "kak-tree-sitter" extraFaces; } diff --git a/packages/common/nki-kakoune/tree-sitter/grammars.nix b/packages/common/nki-kakoune/tree-sitter/grammars.nix index 426edb3..d1f6417 100644 --- a/packages/common/nki-kakoune/tree-sitter/grammars.nix +++ b/packages/common/nki-kakoune/tree-sitter/grammars.nix @@ -33,6 +33,7 @@ let , }: { grammar.source.local.path = "${mkGrammarPackage args}"; + grammar.link_args = grammarLinkArgs ++ [ "-o" "${name}.so" ]; queries.source.local.path = querySrc; queries.path = queryPath; }; diff --git a/packages/common/nki-kakoune/utils.nix b/packages/common/nki-kakoune/utils.nix index 4183aa6..0307bc6 100644 --- a/packages/common/nki-kakoune/utils.nix +++ b/packages/common/nki-kakoune/utils.nix @@ -1,6 +1,6 @@ { lib, writeTextDir, ... }: { mkFacesScript = name: faces: writeTextDir "share/kak/autoload/${name}/faces.kak" '' - hook global KakBegin .* { + hook global KakBegin .* %{ ${lib.concatStringsSep "\n" (builtins.attrValues (builtins.mapAttrs (name: face: " face global ${name} \"${face}\"") faces))} } '';