neovim

Personal neovim configuration files
git clone git://gtms.dev:neovim
Log | Files | Refs

commit 651314d87dad84c273f2c857fbfdcf2cf6c959b4
parent 86e9d12085b4c30342bd1966bc1ccdf8df8ce9c4
Author: Tomas Nemec <nemi@skaut.cz>
Date:   Wed,  7 Sep 2022 09:38:50 +0200

update

Diffstat:
Mafter/plugin/format.lua | 19++++++++++++-------
Aafter/plugin/ng_html.lua | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mafter/plugin/nulls.lua | 2+-
Mafter/plugin/treesitter.lua | 5++++-
4 files changed, 89 insertions(+), 9 deletions(-)

diff --git a/after/plugin/format.lua b/after/plugin/format.lua @@ -5,15 +5,20 @@ vim.api.nvim_create_autocmd('LspAttach', { callback = function(args) local buf = args.buf local client = vim.lsp.get_client_by_id(args.data.client_id) + local cap = client.server_capabilities; - vim.api.nvim_buf_set_option(buf, 'formatexpr', 'v:lua.vim.lsp.formatexpr()') + if cap.documentFormattingProvider then + vim.api.nvim_buf_set_option(buf, 'formatexpr', 'v:lua.vim.lsp.formatexpr()') - vim.keymap.set('n', 'Q', function() - local params = vim.lsp.util.make_formatting_params({}) - client.request('textDocument/formatting', params, nil, buf) - end, { buffer = buf, desc = 'LSP Format' }) + vim.keymap.set('n', 'Q', function() + local params = vim.lsp.util.make_formatting_params({}) + client.request('textDocument/formatting', params, nil, buf) + end, { buffer = buf, desc = 'LSP Format' }) + end - local rhs = ':<c-u>lua vim.lsp.buf.range_formatting()<cr>' - vim.keymap.set('v', 'Q', rhs, { buffer = buf, desc = 'LSP Range Format' }) + if cap.documentRangeFormattingProvider then + local rhs = ':<c-u>lua vim.lsp.buf.range_formatting()<cr>' + vim.keymap.set('v', 'Q', rhs, { buffer = buf, desc = 'LSP Range Format' }) + end end, }) diff --git a/after/plugin/ng_html.lua b/after/plugin/ng_html.lua @@ -0,0 +1,72 @@ +-- TODO(tms) 06.09.22: Handle single line template string + +local template_query = vim.treesitter.parse_query('dart', [[ +(annotation + (arguments + (named_argument + (label (identifier) @_identifier) + (string_literal) @template + ) + ) + (#eq? @_identifier "template") + (#offset! @template 0 3 0 -3) +) +]]) + +local get_root = function(bufnr) + local parser = vim.treesitter.get_parser(bufnr, 'dart', {}) + local tree = parser:parse()[1] + return tree:root(); +end + +local prettier_html = function(text) + local split = vim.split(text, '\n') + local result = table.concat(vim.list_slice(split, 2, #split - 1), '\n') + local job = require('plenary.job'):new { + command = 'prettier', + args = { '--parser=html', '--tab-width=4' }, + writer = { result }, + } + + return job:sync() +end + +local format_ng_html = function() + local bufnr = vim.api.nvim_get_current_buf() + + if vim.bo[bufnr].filetype ~= 'dart' then + vim.notify 'can only be used in dart' + return + end + + local root = get_root(bufnr) + + local changes = {} + + for id, node in template_query:iter_captures(root, bufnr, 0, -1) do + local name = template_query.captures[id] + if name == 'template' then + local range = { node:range() } + local formatted = prettier_html(vim.treesitter.get_node_text(node, bufnr)) + + table.insert(changes, 1, { start = range[1] + 1, final = range[3], formatted = formatted }) + end + end + + for _, change in ipairs(changes) do + vim.api.nvim_buf_set_lines(bufnr, change.start, change.final, false, change.formatted) + end +end + +vim.api.nvim_create_user_command('NgHtmlFormat', function() + format_ng_html() +end, {}) + +local group = vim.api.nvim_create_augroup('ng-html-format', { clear = true }) +vim.api.nvim_create_autocmd('BufWritePre', { + group = group, + pattern = '*.dart', + callback = function() + format_ng_html() + end, +}) diff --git a/after/plugin/nulls.lua b/after/plugin/nulls.lua @@ -28,7 +28,7 @@ null_ls.setup({ sources = { -- formatting builtins.formatting.prettier.with { - filetypes = { 'yaml', 'markdown', 'css', 'scss' }, + filetypes = { 'html', 'yaml', 'markdown', 'css', 'scss' }, args = h.range_formatting_args_factory({ '--parser', vim.api.nvim_buf_get_option(0, 'filetype'), diff --git a/after/plugin/treesitter.lua b/after/plugin/treesitter.lua @@ -1,4 +1,6 @@ -if not pcall(require, 'nvim-treesitter') then return end +if not pcall(require, 'nvim-treesitter') then + return +end local parser_configs = require('nvim-treesitter.parsers').get_parser_configs() @@ -28,6 +30,7 @@ parser_configs.norg_table = { require'nvim-treesitter.configs'.setup { ensure_intalled = 'all', + ignore_install = { 'lua', 'vim', 'c' }, highlight = { enable = true --[[ , disable = {'scss'} --]] }, indent = { enable = true },