Use stylua for autoformat code (#1480)

This commit is contained in:
Santos Gallegos 2021-07-04 16:12:17 -05:00 committed by GitHub
parent 90f15d9bf7
commit be8f656087
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 1181 additions and 979 deletions

View file

@ -1,7 +1,7 @@
local api = vim.api
local parsers = require'nvim-treesitter.parsers'
local utils = require'nvim-treesitter.utils'
local parsers = require "nvim-treesitter.parsers"
local utils = require "nvim-treesitter.utils"
local M = {}
@ -11,20 +11,22 @@ local M = {}
-- @return list of lines of text of the node
function M.get_node_text(node, bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
if not node then return {} end
if not node then
return {}
end
-- We have to remember that end_col is end-exclusive
local start_row, start_col, end_row, end_col = M.get_node_range(node)
if start_row ~= end_row then
local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row+1, false)
lines[1] = string.sub(lines[1], start_col+1)
local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row + 1, false)
lines[1] = string.sub(lines[1], start_col + 1)
lines[#lines] = string.sub(lines[#lines], 1, end_col)
return lines
else
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row+1, false)[1]
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
-- If line is nil then the line is empty
return line and { string.sub(line, start_col+1, end_col) } or {}
return line and { string.sub(line, start_col + 1, end_col) } or {}
end
end
@ -32,7 +34,9 @@ end
-- @param dest the possible parent
-- @param source the possible child node
function M.is_parent(dest, source)
if not (dest and source) then return false end
if not (dest and source) then
return false
end
local current = source
while current ~= nil do
@ -54,9 +58,11 @@ function M.get_next_node(node, allow_switch_parents, allow_next_parent)
local destination_node
local parent = node:parent()
if not parent then return end
if not parent then
return
end
local found_pos = 0
for i = 0,parent:named_child_count()-1,1 do
for i = 0, parent:named_child_count() - 1, 1 do
if parent:named_child(i) == node then
found_pos = i
break
@ -83,10 +89,12 @@ end
function M.get_previous_node(node, allow_switch_parents, allow_previous_parent)
local destination_node
local parent = node:parent()
if not parent then return end
if not parent then
return
end
local found_pos = 0
for i = 0,parent:named_child_count()-1,1 do
for i = 0, parent:named_child_count() - 1, 1 do
if parent:named_child(i) == node then
found_pos = i
break
@ -107,8 +115,8 @@ end
function M.get_named_children(node)
local nodes = {}
for i=0,node:named_child_count() - 1,1 do
nodes[i+1] = node:named_child(i)
for i = 0, node:named_child_count() - 1, 1 do
nodes[i + 1] = node:named_child(i)
end
return nodes
end
@ -118,19 +126,23 @@ function M.get_node_at_cursor(winnr)
local cursor_range = { cursor[1] - 1, cursor[2] }
local root = M.get_root_for_position(unpack(cursor_range))
if not root then return end
if not root then
return
end
return root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2])
end
function M.get_root_for_position(line, col, root_lang_tree)
if not root_lang_tree then
if not parsers.has_parser() then return end
if not parsers.has_parser() then
return
end
root_lang_tree = parsers.get_parser()
end
local lang_tree = root_lang_tree:language_for_range({ line, col, line, col })
local lang_tree = root_lang_tree:language_for_range { line, col, line, col }
for _, tree in ipairs(lang_tree:trees()) do
local root = tree:root()
@ -157,13 +169,15 @@ function M.get_root_for_node(node)
end
function M.highlight_node(node, buf, hl_namespace, hl_group)
if not node then return end
M.highlight_range({node:range()}, buf, hl_namespace, hl_group)
if not node then
return
end
M.highlight_range({ node:range() }, buf, hl_namespace, hl_group)
end
function M.highlight_range(range, buf, hl_namespace, hl_group)
local start_row, start_col, end_row, end_col = unpack(range)
vim.highlight.range(buf, hl_namespace, hl_group, {start_row, start_col}, {end_row, end_col})
vim.highlight.range(buf, hl_namespace, hl_group, { start_row, start_col }, { end_row, end_col })
end
-- Set visual selection to node
@ -173,8 +187,8 @@ function M.update_selection(buf, node, selection_mode)
selection_mode = selection_mode or "charwise"
local start_row, start_col, end_row, end_col = M.get_node_range(node)
if end_row == vim.fn.line('$') then
end_col = #vim.fn.getline('$')
if end_row == vim.fn.line "$" then
end_col = #vim.fn.getline "$"
end
-- Convert to 1-based indices
@ -186,12 +200,10 @@ function M.update_selection(buf, node, selection_mode)
vim.fn.setpos(".", { buf, start_row, start_col, 0 })
-- Start visual selection in appropriate mode
local v_table = {charwise = "v", linewise = "V", blockwise = "<C-v>"}
local v_table = { charwise = "v", linewise = "V", blockwise = "<C-v>" }
---- Call to `nvim_replace_termcodes()` is needed for sending appropriate
---- command to enter blockwise mode
local mode_string = vim.api.nvim_replace_termcodes(
v_table[selection_mode] or selection_mode, true, true, true
)
local mode_string = vim.api.nvim_replace_termcodes(v_table[selection_mode] or selection_mode, true, true, true)
vim.cmd("normal! " .. mode_string)
-- Convert exclusive end position to inclusive
@ -231,7 +243,7 @@ function M.is_in_node_range(node, line, col)
end
function M.get_node_range(node_or_range)
if type(node_or_range) == 'table' then
if type(node_or_range) == "table" then
return unpack(node_or_range)
else
return node_or_range:range()
@ -242,7 +254,7 @@ function M.node_to_lsp_range(node)
local start_line, start_col, end_line, end_col = M.get_node_range(node)
local rtn = {}
rtn.start = { line = start_line, character = start_col }
rtn['end'] = { line = end_line, character = end_col }
rtn["end"] = { line = end_line, character = end_col }
return rtn
end
@ -277,13 +289,13 @@ function M.memoize_by_buf_tick(fn, options)
-- Clean up logic only!
api.nvim_buf_attach(bufnr, false, {
on_detach = detach_handler,
on_reload = detach_handler
on_reload = detach_handler,
})
end
cache[key] = {
result = fn(...),
last_tick = tick
last_tick = tick,
}
return cache[key].result
@ -291,24 +303,28 @@ function M.memoize_by_buf_tick(fn, options)
end
function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second)
if not node_or_range1 or not node_or_range2 then return end
if not node_or_range1 or not node_or_range2 then
return
end
local range1 = M.node_to_lsp_range(node_or_range1)
local range2 = M.node_to_lsp_range(node_or_range2)
local text1 = M.get_node_text(node_or_range1)
local text2 = M.get_node_text(node_or_range2)
local edit1 = { range = range1, newText = table.concat(text2, '\n') }
local edit2 = { range = range2, newText = table.concat(text1, '\n') }
vim.lsp.util.apply_text_edits({edit1, edit2}, bufnr)
local edit1 = { range = range1, newText = table.concat(text2, "\n") }
local edit2 = { range = range2, newText = table.concat(text1, "\n") }
vim.lsp.util.apply_text_edits({ edit1, edit2 }, bufnr)
if cursor_to_second then
utils.set_jump()
local char_delta = 0
local line_delta = 0
if range1["end"].line < range2.start.line
or (range1["end"].line == range2.start.line and range1["end"].character < range2.start.character) then
if
range1["end"].line < range2.start.line
or (range1["end"].line == range2.start.line and range1["end"].character < range2.start.character)
then
line_delta = #text2 - #text1
end
@ -320,29 +336,32 @@ function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second)
--space_between_ranges = range2.start.character - range1["end"].character
--char_delta = correction_after_line_change + text_now_before_range2 + space_between_ranges
--- Equivalent to:
char_delta = #(text2[#text2]) - range1["end"].character
char_delta = #text2[#text2] - range1["end"].character
-- add range1.start.character if last line of range1 (now text2) does not start at 0
if range1.start.line == range2.start.line + line_delta then
char_delta = char_delta + range1.start.character
end
else
char_delta = #(text2[#text2]) - #(text1[#text1])
char_delta = #text2[#text2] - #text1[#text1]
end
end
api.nvim_win_set_cursor(api.nvim_get_current_win(),
{range2.start.line + 1 + line_delta,
range2.start.character + char_delta})
api.nvim_win_set_cursor(
api.nvim_get_current_win(),
{ range2.start.line + 1 + line_delta, range2.start.character + char_delta }
)
end
end
function M.goto_node(node, goto_end, avoid_set_jump)
if not node then return end
if not node then
return
end
if not avoid_set_jump then
utils.set_jump()
end
local range = {node:range()}
local range = { node:range() }
local position
if not goto_end then
position = { range[1], range[2] }