From 177540d59592ab6897d99cc11aa535a540005348 Mon Sep 17 00:00:00 2001 From: Wittano Date: Thu, 28 Dec 2023 00:49:48 +0100 Subject: [PATCH] feat(neovim): added first templates when is created a new file --- lib/imports.nix | 8 +- modules/editors/neovim.nix | 486 +++++++++--------- modules/editors/plugins/template.nvim.nix | 30 ++ modules/editors/plugins/templates/main.go | 5 + modules/editors/plugins/templates/module.nix | 17 + modules/editors/plugins/templates/standard.go | 2 + 6 files changed, 306 insertions(+), 242 deletions(-) create mode 100644 modules/editors/plugins/template.nvim.nix create mode 100644 modules/editors/plugins/templates/main.go create mode 100644 modules/editors/plugins/templates/module.nix create mode 100644 modules/editors/plugins/templates/standard.go diff --git a/lib/imports.nix b/lib/imports.nix index ff928d3a..138fe51b 100644 --- a/lib/imports.nix +++ b/lib/imports.nix @@ -1,10 +1,10 @@ { lib, ... }: rec { inherit (lib.attrsets) mapAttrsToList filterAttrs; - inherit (lib.lists) flatten; + inherit (lib.lists) flatten any; inherit (lib.strings) hasSuffix hasPrefix; - importModulesPath = path: - flatten (mapAttrsToList + importModulesPath = path: builtins.filter (e: e != null) + (flatten (mapAttrsToList (n: v: let newPath = "${path}/${n}"; @@ -15,5 +15,5 @@ then newPath else null else importModulesPath newPath) - (filterAttrs (n: v: n != "apps" && n != "utils") (builtins.readDir path))); + (filterAttrs (n: v: n != "apps" && n != "utils" && n != "plugins") (builtins.readDir path)))); } diff --git a/modules/editors/neovim.nix b/modules/editors/neovim.nix index b308ebb5..b7dc9ce8 100644 --- a/modules/editors/neovim.nix +++ b/modules/editors/neovim.nix @@ -16,280 +16,290 @@ in config = mkIf cfg.enable { home-manager.users.wittano.home.packages = with pkgs; [ ripgrep ]; - programs.nixvim = { - enable = true; - enableMan = true; + programs.nixvim = + let + templateNvimPlugin = pkgs.callPackage ./plugins/template.nvim.nix { }; + in + { + enable = true; + enableMan = true; - extraPlugins = with pkgs.vimPlugins; [ - vim-wakatime - vimsence - ]; + extraPlugins = with pkgs.vimPlugins; [ + templateNvimPlugin.plugin + vim-wakatime + vimsence + ]; - extraConfigLua = '' - -- nvim-autopairs - require("nvim-autopairs").setup {} - ''; + extraConfigLua = '' + -- nvim-autopairs + require("nvim-autopairs").setup {} - globals.mapleader = " "; + -- template.nvim + ${templateNvimPlugin.luaConfig} + ''; - options = { - number = true; - relativenumber = true; - swapfile = false; - wrap = true; - smartindent = true; - tabstop = 4; - softtabstop = 4; - shiftwidth = 4; - expandtab = true; + globals.mapleader = " "; - hlsearch = true; - incsearch = true; - }; + options = { + number = true; + relativenumber = true; + swapfile = false; + wrap = true; + smartindent = true; + tabstop = 4; + softtabstop = 4; + shiftwidth = 4; + expandtab = true; - colorschemes.catppuccin = - let - theme = "macchiato"; - in - { - enable = true; - background = { - dark = theme; - light = theme; - }; - flavour = theme; + hlsearch = true; + incsearch = true; }; - plugins = { - undotree = { - autoOpenDiff = true; - focusOnToggle = true; - enable = true; - }; - - dap = { - enable = true; - extensions = { - dap-go.enable = true; - dap-python = { - enable = true; - adapterPythonPath = "${pkgs.python3}/bin/python"; - }; - dap-ui = { - enable = true; - mappings = { - toggle = "dd"; - }; + colorschemes.catppuccin = + let + theme = "macchiato"; + in + { + enable = true; + background = { + dark = theme; + light = theme; }; - dap-virtual-text.enable = true; + flavour = theme; }; - }; - lsp = { - enable = true; - capabilities = "require('cmp_nvim_lsp').default_capabilities()"; - keymaps = { - diagnostic = { - "j" = "goto_next"; - "k" = "goto_prev"; - }; - lspBuf = { - K = "hover"; - gD = "references"; - gd = "definition"; - gi = "implementation"; - gt = "type_definition"; - R = "rename"; - }; + plugins = { + undotree = { + autoOpenDiff = true; + focusOnToggle = true; + enable = true; }; - servers = { - rnix-lsp.enable = true; - rust-analyzer = { - enable = true; - installRustc = true; - installCargo = true; + + dap = { + enable = true; + extensions = { + dap-go.enable = true; + dap-python = { + enable = true; + adapterPythonPath = "${pkgs.python3}/bin/python"; + }; + dap-ui = { + enable = true; + mappings = { + toggle = "dd"; + }; + }; + dap-virtual-text.enable = true; }; - tsserver.enable = true; - yamlls.enable = true; - pylsp = { - enable = true; - settings.plugins = { - rope.enabled = true; - pylsp_mypy.enabled = true; - pylint.enabled = true; - isort.enabled = true; - flake8.enabled = true; - black.enabled = true; + }; + + lsp = { + enable = true; + capabilities = "require('cmp_nvim_lsp').default_capabilities()"; + keymaps = { + diagnostic = { + "j" = "goto_next"; + "k" = "goto_prev"; + }; + lspBuf = { + K = "hover"; + gD = "references"; + gd = "definition"; + gi = "implementation"; + gt = "type_definition"; + R = "rename"; }; }; - html.enable = true; - gopls.enable = true; - cmake.enable = true; - gdscript.enable = true; - eslint.enable = true; - cssls.enable = true; - clangd.enable = true; - bashls.enable = true; - jsonls.enable = true; - tailwindcss.enable = true; - taplo.enable = true; - terraformls.enable = true; + servers = { + rnix-lsp.enable = true; + rust-analyzer = { + enable = true; + installRustc = true; + installCargo = true; + }; + tsserver.enable = true; + yamlls.enable = true; + pylsp = { + enable = true; + settings.plugins = { + rope.enabled = true; + pylsp_mypy.enabled = true; + pylint.enabled = true; + isort.enabled = true; + flake8.enabled = true; + black.enabled = true; + }; + }; + html.enable = true; + gopls.enable = true; + cmake.enable = true; + gdscript.enable = true; + eslint.enable = true; + cssls.enable = true; + clangd.enable = true; + bashls.enable = true; + jsonls.enable = true; + tailwindcss.enable = true; + taplo.enable = true; + terraformls.enable = true; - # TODO Enable LSP server, when will be added to stable version of nixvim (26.12.2023) + # TODO Enable LSP server, when will be added to stable version of nixvim (26.12.2023) - #graphql.enable = true; - #dockerls.enable = true; - #ansiblels.enable = true; + #graphql.enable = true; + #dockerls.enable = true; + #ansiblels.enable = true; + }; }; - }; - nvim-cmp = { - enable = true; - sources = [ - { name = "nvim_lsp"; } - { name = "path"; } - { name = "snippy"; } - { name = "spell"; } - { name = "buffer"; } - ]; - snippet.expand = "luasnip"; - mappingPresets = [ "insert" ]; - mapping = { - "" = "cmp.mapping.scroll_docs(-4)"; - "" = "cmp.mapping.scroll_docs(4)"; - "" = "cmp.mapping.complete()"; - "" = "cmp.mapping.abort()"; - "" = "cmp.mapping.confirm({ select = true })"; + nvim-cmp = { + enable = true; + sources = [ + { name = "nvim_lsp"; } + { name = "path"; } + { name = "snippy"; } + { name = "spell"; } + { name = "buffer"; } + ]; + snippet.expand = "luasnip"; + mappingPresets = [ "insert" ]; + mapping = { + "" = "cmp.mapping.scroll_docs(-4)"; + "" = "cmp.mapping.scroll_docs(4)"; + "" = "cmp.mapping.complete()"; + "" = "cmp.mapping.abort()"; + "" = "cmp.mapping.confirm({ select = true })"; + }; }; - }; - luasnip.enable = true; - cmp-nvim-lua.enable = true; - cmp-nvim-lsp.enable = true; - cmp-path.enable = true; - cmp-snippy.enable = true; - cmp-buffer.enable = true; - cmp-spell.enable = true; - cmp-vim-lsp.enable = true; + luasnip.enable = true; + cmp-nvim-lua.enable = true; + cmp-nvim-lsp.enable = true; + cmp-path.enable = true; + cmp-snippy.enable = true; + cmp-buffer.enable = true; + cmp-spell.enable = true; + cmp-vim-lsp.enable = true; - lsp-format = { - enable = true; - lspServersToEnable = "all"; - }; + lsp-format = { + enable = true; + lspServersToEnable = "all"; + }; - barbar = { - enable = true; - autoHide = true; - insertAtEnd = true; - keymaps = { - close = "bd"; - goTo1 = "b1"; - goTo2 = "b2"; - goTo3 = "b3"; - goTo4 = "b4"; - goTo5 = "b5"; - goTo6 = "b6"; - goTo7 = "b7"; - goTo8 = "b8"; - goTo9 = "b9"; - next = "bj"; - previous = "bk"; + barbar = { + enable = true; + autoHide = true; + insertAtEnd = true; + keymaps = { + close = "bd"; + goTo1 = "b1"; + goTo2 = "b2"; + goTo3 = "b3"; + goTo4 = "b4"; + goTo5 = "b5"; + goTo6 = "b6"; + goTo7 = "b7"; + goTo8 = "b8"; + goTo9 = "b9"; + next = "bj"; + previous = "bk"; + }; }; - }; - tmux-navigator.enable = true; + tmux-navigator.enable = true; - lightline = { - enable = true; - colorscheme = "catppuccin"; - }; + lightline = { + enable = true; + colorscheme = "catppuccin"; + }; - nix.enable = true; + nix.enable = true; - surround.enable = true; - todo-comments.enable = true; - fugitive.enable = true; + surround.enable = true; + todo-comments.enable = true; + fugitive.enable = true; - telescope = { - enable = true; - extensions.file_browser = { + telescope = { enable = true; - hidden = true; - }; - keymaps = { - "ff" = "find_files"; - "fg" = "git_files"; - "gg" = "live_grep"; - "bf" = "buffers"; - "" = "lsp_definitions"; + extensions.file_browser = { + enable = true; + hidden = true; + }; + keymaps = { + "ff" = "find_files"; + "fg" = "git_files"; + "gg" = "live_grep"; + "bf" = "buffers"; + "" = "lsp_definitions"; + }; }; - }; - endwise.enable = true; - gitblame.enable = true; + endwise.enable = true; + gitblame.enable = true; - nvim-autopairs = { - enable = true; - disableInReplaceMode = true; + nvim-autopairs = { + enable = true; + disableInReplaceMode = true; + }; }; - }; - keymaps = [ - # Custom - { - action = '' - function() - local currectFile = vim.fn.expand("%:p") - local directory = vim.fn.expand('%:p:h') - local newFileName = vim.fn.input("Enter new file name: ") - local newFilePath = directory .. '/' .. newFileName + keymaps = [ + # Custom + { + # TODO Add generate template for new files + action = '' + function() + local currectFile = vim.fn.expand("%:p") + local directory = vim.fn.expand('%:p:h') + local newFileName = vim.fn.input("Enter new file name: ") + local newFilePath = directory .. '/' .. newFileName - if string.match(newFilePath, "/") then - vim.fn.mkdir(vim.fs.dirname(newFilePath), 'p') - end + if string.match(newFilePath, "/") then + vim.fn.mkdir(vim.fs.dirname(newFilePath), 'p') + end - vim.cmd('edit ' .. newFilePath) - end - ''; - key = "fc"; - lua = true; - } - { - action = '' - function() - local newDirectoryName = vim.fn.input("Enter new directory name: ") - local currentDirectory = vim.fn.expand('%:p:h') - local newDirectoryPath = currentDirectory .. '/' .. newDirectoryName + vim.cmd('edit ' .. newFilePath) + vim.cmd("Telescope find_template type=insert filter_ft=true") + end + ''; + key = "fc"; + lua = true; + } + { + action = '' + function() + local newDirectoryName = vim.fn.input("Enter new directory name: ") + local currentDirectory = vim.fn.expand('%:p:h') + local newDirectoryPath = currentDirectory .. '/' .. newDirectoryName - vim.fn.mkdir(newDirectoryPath, 'p') - print("Created new directory: " .. newDirectoryPath) - end - ''; - key = "fd"; - lua = true; - } - # TmuxNavigate - { - action = " TmuxNavifateLeft"; - key = ""; - } - { - action = " TmuxNavifateRight"; - key = ""; - } - { - action = " TmuxNavifateDown"; - key = ""; - } - { - action = " TmuxNavifateUp"; - key = ""; - } - # Undotree - { - action = " UndotreeToggle"; - key = "U"; - } - ]; - }; + vim.fn.mkdir(newDirectoryPath, 'p') + print("Created new directory: " .. newDirectoryPath) + end + ''; + key = "fd"; + lua = true; + } + # TmuxNavigate + { + action = " TmuxNavifateLeft"; + key = ""; + } + { + action = " TmuxNavifateRight"; + key = ""; + } + { + action = " TmuxNavifateDown"; + key = ""; + } + { + action = " TmuxNavifateUp"; + key = ""; + } + # Undotree + { + action = " UndotreeToggle"; + key = "U"; + } + ]; + }; }; } diff --git a/modules/editors/plugins/template.nvim.nix b/modules/editors/plugins/template.nvim.nix new file mode 100644 index 00000000..3f240320 --- /dev/null +++ b/modules/editors/plugins/template.nvim.nix @@ -0,0 +1,30 @@ +{ srcOnly, vimUtils, lib, fetchFromGitHub, ... }: +let + inherit (lib.attrsets) mapAttrsToList; + + templateDir = srcOnly { + name = "templateDir"; + src = ./templates; + }; in +{ + luaConfig = '' + require('template').setup({ + temp_dir = "${templateDir}", + author = "Wittano", + }) + + require("telescope").load_extension('find_template') + ''; + + plugin = vimUtils.buildVimPlugin { + pname = "template.nvim"; + version = "27-12-2023"; + src = fetchFromGitHub { + owner = "nvimdev"; + repo = "template.nvim"; + rev = "41a41541f6af19be5403c0a5bf371d8ccb86c9c4"; + sha256 = "sha256-rBD0AnPJMU8Fkgu6UJp4+hpro8tn1OMCQi4VirRlFNA="; + }; + meta.homepage = "https://github.com/nvimdev/template.nvim"; + }; +} diff --git a/modules/editors/plugins/templates/main.go b/modules/editors/plugins/templates/main.go new file mode 100644 index 00000000..e2c7e745 --- /dev/null +++ b/modules/editors/plugins/templates/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + {{_cursor_}} +} diff --git a/modules/editors/plugins/templates/module.nix b/modules/editors/plugins/templates/module.nix new file mode 100644 index 00000000..5261d172 --- /dev/null +++ b/modules/editors/plugins/templates/module.nix @@ -0,0 +1,17 @@ +{ pkgs, lib, config, ... }: +with lib; +with builtins; +let + cfg = config.{{_lua:vim.fn.expand("%:h"):gsub("/", ".")_}}.{{_file_name_}}; +in +{ + options = { + {{_lua:vim.fn.expand("%:h"):gsub("/", ".")_}}.{{_file_name_}} = { + enable = mkEnableOption "Enable "; + }; + }; + + config = mkIf (cfg.enable) { + {{_cursor_}} + }; +} diff --git a/modules/editors/plugins/templates/standard.go b/modules/editors/plugins/templates/standard.go new file mode 100644 index 00000000..3e42e0b1 --- /dev/null +++ b/modules/editors/plugins/templates/standard.go @@ -0,0 +1,2 @@ +package {{_lua:vim.fs.basename(vim.fn.expand('%:p:h'))_}} +{{_cursor_}}