Make tree-sitter kinda work

This commit is contained in:
Natsu Kagami 2024-10-07 21:11:48 +02:00
parent e6d6ec2a85
commit 044f4a31cc
Signed by: nki
GPG key ID: 55A032EB38B49ADB
10 changed files with 187 additions and 27 deletions

View file

@ -2,7 +2,7 @@
{
imports = [
./kakoune/kak.nix
# ./kakoune/kak.nix
./fish/fish.nix
./modules/programs/my-broot.nix
./modules/programs/my-sway

View file

@ -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

View file

@ -34,6 +34,7 @@ let
self.runtime_dir.join("sources").join(url_dir)
'')
./user_config.patch
];
meta.mainProgram = "kak-tree-sitter";

View file

@ -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<Self, ConfigError> {
+ pub fn load_default_user(path: Option<impl AsRef<Path>>) -> Result<Self, ConfigError> {
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<Path>) -> Result<Self, ConfigError> {
+ pub fn load(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
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<String>,
+
/// 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<Token, BufferedClient>,
enqueue_response: EnqueueResponse,
handler: Handler,
+ user_config_path: Option<String>,
}
impl IOHandler {
@@ -173,6 +175,7 @@ impl IOHandler {
fn new(
config: &Config,
+ user_config_path: Option<String>,
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<String>,
+
#[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 {

View file

@ -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}"
'';
})

View file

@ -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

View file

@ -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<ret>"
map window normal <c-l> ": enter-user-mode lsp<ret>"
map window normal <c-h> ": lsp-hover<ret>"
map window normal <c-s-h> ": lsp-hover-buffer<ret>"
map global lsp N -docstring "Display the next message request" ": lsp-show-message-request-next<ret>"
map global normal <c-l> ": enter-user-mode lsp<ret>"
map global normal <c-h> ": lsp-hover<ret>"
map global normal <c-s-h> ": lsp-hover-buffer<ret>"
# lsp-auto-hover-insert-mode-enable
set window lsp_hover_anchor true
set global lsp_hover_anchor true
map global insert <tab> '<a-;>:try lsp-snippets-select-next-placeholders catch %{ execute-keys -with-hooks <lt>tab> }<ret>' -docstring 'Select next snippet placeholder'
map global object a '<a-semicolon>lsp-object<ret>' -docstring 'LSP any symbol'
map global object <a-a> '<a-semicolon>lsp-object<ret>' -docstring 'LSP any symbol'

View file

@ -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 <c-t> ": enter-user-mode tree-sitter<ret>"
'';
extraPaths = "${kak-tree-sitter}/bin";
plugin = utils.mkFacesScript "kak-tree-sitter" extraFaces;
}

View file

@ -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;
};

View file

@ -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))}
}
'';