From f74a0bbaca35dfe2a3fa1c818975f63a469f6016 Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 17 May 2025 16:44:00 +0000 Subject: [PATCH] neovim: configure filetype matching for nix-shell ysh --- hosts/common/programs/neovim/nix_shell.lua | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/hosts/common/programs/neovim/nix_shell.lua b/hosts/common/programs/neovim/nix_shell.lua index ed7541986..48c9842d6 100644 --- a/hosts/common/programs/neovim/nix_shell.lua +++ b/hosts/common/programs/neovim/nix_shell.lua @@ -6,6 +6,10 @@ vim.filetype.add { pattern = { ['.*'] = { function(path, bufnr) + -- returns true if the input is a shebang likely to evaluate to `nix-shell`, e.g. + -- - "#!/usr/bin/env nix-shell" => true + -- - "#!nix-shell -i bash -p coreutils" => true + -- - "#!/usr/bin/env python" => false function test_for_nix_shell_shebang(maybe_hashbang) local bang_payload = string.match(maybe_hashbang, '^#!(.*)$') if not bang_payload then @@ -19,7 +23,12 @@ vim.filetype.add { end end - -- extract `$interpreter` from some `#!nix-shell -i $interpreter ...` line + -- extract `$interpreter` from some `#!nix-shell -i $interpreter ...` line. + -- returns `nil` if no `-i $interpreter` was specified. + -- - "#!nix-shell -i my-beautiful-int -p foo" => "my-beautiful-int" + -- - "#!nix-shell -p python3 -i python" => "python" + -- - "#!/usr/bin/env python" => nil + -- - "#!nix-shell -p python" => nil function parse_nix_shell(maybe_nix_shell) local shell_payload = string.match(maybe_nix_shell, "^#!nix%-shell(.*)$") @@ -43,10 +52,25 @@ vim.filetype.add { return interpreters[1] end + -- given an interpreter (basename), guess a suitable vim filetype. + -- for many languages, these are equivalent and this function is a no-op. + -- "python2.7" => "python" + -- "bash" => "bash" + -- "totally-unknown" => "totally-unknown" function filetype_from_interpreter(i) + -- list of known vim filetypes may be found in if string.match(i, "^python") then -- python3, python2.7, etc return "python" + elseif i == "ysh" then + -- XXX(2025-05-16): neovim has no `ysh`/oil-shell filetype. + -- consider one of these filetypes: + -- - "sh": but ysh curly braces will confuse the syntax highlighting + -- - "csh": no highlighting bugs, just omits some highlights + -- - "cpp": better highlighting of functions, but thinks `#` comments are compiler directives + -- - "fish": highlights a LOT, but still occasional bugs + -- - "zsh": slightly more capable than csh; no bugs + return "zsh" else -- very common for interpreter name to be the same as filetype return i