diff --git a/lua/ansible/init.lua b/lua/ansible/init.lua index 19e7dff..aa4fd74 100644 --- a/lua/ansible/init.lua +++ b/lua/ansible/init.lua @@ -12,6 +12,10 @@ local M = {} M.example = function() local lines = {} + -- TOOD + -- correctly handle comments + -- removing all empty lines is back for raw blocks + -- remove all whitespace, we will create new blocks separated by empty lines after local last_search = vim.fn.getreg('/') vim.api.nvim_command('g/^$/d') @@ -20,23 +24,21 @@ M.example = function() local buf_handle = vim.api.nvim_win_get_buf(0) local last_line_nr = vim.api.nvim_buf_line_count(buf_handle) while (current_line_nr <= last_line_nr) do - -- skip empty lines - if vim.fn.getline(current_line_nr) == '' then - current_line_nr = current_line_nr + 1 - else - vim.api.nvim_win_set_cursor(0, {current_line_nr, 1}) - local node = ts_utils.get_node_at_cursor(0) - local parent = node:parent() - while (parent ~= nil and node:type() ~= 'block_mapping_pair') do - node = parent - parent = node:parent() - end - local start_row, start_column, end_row, end_column = node:range() - - lines[current_line_nr] = vim.fn.getline(start_row + 1, end_row + 1) - - current_line_nr = current_line_nr + 1 + (end_row - start_row) + vim.api.nvim_win_set_cursor(0, {current_line_nr, 1}) + local node = ts_utils.get_node_at_cursor(0) + local parent = node:parent() + while (parent ~= nil and node:type() ~= 'block_mapping_pair') do + node = parent + parent = node:parent() end + local start_row, start_column, end_row, end_column = node:range() + -- print('start_row=', start_row) + -- print('end_row=', end_row) + -- print('line:', vim.inspect(vim.fn.getline(start_row + 1, end_row + 1))) + + lines[current_line_nr] = vim.fn.getline(start_row + 1, end_row + 1) + + current_line_nr = current_line_nr + 1 + (end_row - start_row) end -- sort does not work if the index is not a continuous integer, so lets make sure it is. if we are sure that we dont need the original line number, we can do this from the start in `lines` @@ -48,6 +50,7 @@ M.example = function() return k1[1] < k2[1] end ) + -- print(vim.inspect(a)) insert_row = 1 last_prefix = vim.split(a[1][1], '__')[1]