From ced46f834dc4885ece79f6e60b1df2d48248690e Mon Sep 17 00:00:00 2001 From: matt1432 Date: Fri, 24 Nov 2023 20:59:07 -0500 Subject: [PATCH] feat(neovim): add hypr grammar --- common/home/neovim/default.nix | 216 +++++++++++++--------- common/home/neovim/grammar.nix | 62 +++++++ common/home/neovim/plugins/treesitter.vim | 7 +- 3 files changed, 194 insertions(+), 91 deletions(-) create mode 100644 common/home/neovim/grammar.nix diff --git a/common/home/neovim/default.nix b/common/home/neovim/default.nix index 6a8fa26b..773fed80 100644 --- a/common/home/neovim/default.nix +++ b/common/home/neovim/default.nix @@ -4,14 +4,22 @@ ... }: let # installs a vim plugin from git with a given tag / branch - plugin = owner: repo: rev: hash: + plugin = src: pkgs.vimUtils.buildVimPlugin { - pname = "${lib.strings.sanitizeDerivationName repo}"; - version = rev; - src = pkgs.fetchFromGitHub { - inherit rev owner repo hash; - }; + pname = "${lib.strings.sanitizeDerivationName src.repo}"; + version = src.rev; + inherit src; }; + + buildGrammar = name: grammar: + pkgs.callPackage ./grammar.nix {} { + language = grammar.language or name; + version = grammar.src.rev; + src = grammar.src; + location = grammar.location or null; + generate = grammar.generate or false; + }; + fileContents = lib.strings.fileContents; in { # TODO: make a gradle module and have java in device-vars.nix @@ -48,6 +56,7 @@ in { nodePackages.npm nodePackages.neovim gradle + gcc bat @@ -134,95 +143,122 @@ in { extraConfig = fileContents ./base.vim; extraLuaConfig = fileContents ./base.lua; - plugins = with pkgs.vimPlugins; [ - # Coc configured - coc-css - coc-eslint - coc-java - coc-sh - coc-stylelintplus - { - plugin = coc-snippets; - type = "viml"; - config = fileContents ./plugins/snippets.vim; - } + plugins = let + # custom packages + hypr-src = pkgs.fetchFromGitHub { + owner = "luckasRanarison"; + repo = "tree-sitter-hypr"; + rev = "90b3ddf8a85b5ea3d9dc4920fddb16182a192e14"; + hash = "sha256-ErFs2eCC0eZEyDldrTUj4JJ0Eu+exDHNQx4g8WXh/UQ="; + }; - ## Lua - coc-sumneko-lua - neodev-nvim + tree-sitter-hypr = plugin hypr-src; - ## Fzf - coc-fzf - fzfWrapper - fzf-vim + hypr-grammar = buildGrammar "hypr" { + src = hypr-src; + generate = true; + }; - coc-highlight - coc-json - coc-pyright - coc-vimlsp - coc-yaml - coc-toml - coc-markdownlint - coc-tsserver - fugitive + indent-blankline = plugin (pkgs.fetchFromGitHub { + owner = "lukas-reineke"; + repo = "indent-blankline.nvim"; + rev = "046e2cf04e08ece927bacbfb87c5b35c0b636546"; + hash = "sha256-bhoep8aTYje5K/dZ/XmpwBPn4PBEMPrmw33QJdfFe6M="; + }); + in + with pkgs.vimPlugins; [ + # Coc configured + coc-css + coc-eslint + coc-java + coc-sh + coc-stylelintplus + { + plugin = coc-snippets; + type = "viml"; + config = fileContents ./plugins/snippets.vim; + } - { - plugin = dracula-nvim; - type = "viml"; - config = fileContents ./plugins/dracula.vim; - } - { - plugin = lualine-nvim; - type = "lua"; - config = fileContents ./plugins/lualine.lua; - } - { - plugin = todo-comments-nvim; - type = "lua"; - config = "require('todo-comments').setup()"; - } - { - plugin = gitsigns-nvim; - type = "lua"; - config = fileContents ./plugins/gitsigns.lua; - } - { - plugin = nvim-autopairs; - type = "lua"; - config = fileContents ./plugins/autopairs.lua; - } - nvim-treesitter-context - nvim-treesitter-textobjects - { - plugin = nvim-treesitter.withAllGrammars; - type = "viml"; - config = fileContents ./plugins/treesitter.vim; - } - { - plugin = - plugin "lukas-reineke" "indent-blankline.nvim" - "046e2cf04e08ece927bacbfb87c5b35c0b636546" - "sha256-bhoep8aTYje5K/dZ/XmpwBPn4PBEMPrmw33QJdfFe6M="; - type = "lua"; - config = fileContents ./plugins/indent.lua; - } - { - plugin = mini-nvim; - type = "lua"; - config = fileContents ./plugins/mini.lua; - } - { - plugin = neo-tree-nvim; - type = "viml"; - config = '' - ${fileContents ./plugins/neotree.vim} + ## Lua + coc-sumneko-lua + neodev-nvim - lua << EOF - ${fileContents ./plugins/neotree.lua} - EOF - ''; - } - ]; + ## Fzf + coc-fzf + fzfWrapper + fzf-vim + + coc-highlight + coc-json + coc-pyright + coc-vimlsp + coc-yaml + coc-toml + coc-markdownlint + coc-tsserver + fugitive + + { + plugin = dracula-nvim; + type = "viml"; + config = fileContents ./plugins/dracula.vim; + } + { + plugin = lualine-nvim; + type = "lua"; + config = fileContents ./plugins/lualine.lua; + } + { + plugin = todo-comments-nvim; + type = "lua"; + config = "require('todo-comments').setup()"; + } + { + plugin = gitsigns-nvim; + type = "lua"; + config = fileContents ./plugins/gitsigns.lua; + } + { + plugin = nvim-autopairs; + type = "lua"; + config = fileContents ./plugins/autopairs.lua; + } + nvim-treesitter-context + nvim-treesitter-textobjects + { + plugin = tree-sitter-hypr; + type = "lua"; + config = '' + vim.treesitter.language.require_language("hypr", "${hypr-grammar}/parser") + ''; + } + { + plugin = nvim-treesitter.withAllGrammars; + type = "viml"; + config = fileContents ./plugins/treesitter.vim; + } + { + plugin = indent-blankline; + type = "lua"; + config = fileContents ./plugins/indent.lua; + } + { + plugin = mini-nvim; + type = "lua"; + config = fileContents ./plugins/mini.lua; + } + { + plugin = neo-tree-nvim; + type = "viml"; + config = '' + ${fileContents ./plugins/neotree.vim} + + lua << EOF + ${fileContents ./plugins/neotree.lua} + EOF + ''; + } + ]; }; }; } diff --git a/common/home/neovim/grammar.nix b/common/home/neovim/grammar.nix new file mode 100644 index 00000000..18adb057 --- /dev/null +++ b/common/home/neovim/grammar.nix @@ -0,0 +1,62 @@ +{ stdenv +, nodejs +, tree-sitter +, lib +}: + +# https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/parsing/tree-sitter/grammar.nix +# Build a parser grammar and put the resulting shared object in `$out/parser` + +{ + # language name + language +, version +, src +, location ? null +, generate ? false +, ... +}@args: + +stdenv.mkDerivation ({ + pname = "${language}-grammar"; + + inherit src version; + + nativeBuildInputs = lib.optionals generate [ nodejs tree-sitter ]; + + CFLAGS = [ "-Isrc" "-O2" ]; + CXXFLAGS = [ "-Isrc" "-O2" ]; + + stripDebugList = [ "parser" ]; + + configurePhase = lib.optionalString (location != null) '' + cd ${location} + '' + lib.optionalString generate '' + tree-sitter generate + ''; + + # When both scanner.{c,cc} exist, we should not link both since they may be the same but in + # different languages. Just randomly prefer C++ if that happens. + buildPhase = '' + runHook preBuild + if [[ -e src/scanner.cc ]]; then + $CXX -fPIC -c src/scanner.cc -o scanner.o $CXXFLAGS + elif [[ -e src/scanner.c ]]; then + $CC -fPIC -c src/scanner.c -o scanner.o $CFLAGS + fi + $CC -fPIC -c src/parser.c -o parser.o $CFLAGS + rm -rf parser + $CXX -shared -o parser *.o + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + mkdir $out + mv parser $out/ + if [[ -d queries ]]; then + cp -r queries $out + fi + runHook postInstall + ''; +} // removeAttrs args [ "language" "location" "generate" ]) diff --git a/common/home/neovim/plugins/treesitter.vim b/common/home/neovim/plugins/treesitter.vim index 2801be8d..228573e7 100644 --- a/common/home/neovim/plugins/treesitter.vim +++ b/common/home/neovim/plugins/treesitter.vim @@ -1,8 +1,12 @@ lua << EOF +local path = "/home/matt/.cache/nvim/" + require('nvim-treesitter.configs').setup({ highlight = { enable = true }, - indent = { enable = true } + indent = { enable = true }, + parser_install_dir = path, + auto_install = true, }) require('treesitter-context').setup({ @@ -11,6 +15,7 @@ require('treesitter-context').setup({ min_window_height = 20, }) +vim.opt.runtimepath:append(path) EOF " Add line under context