style: use stylua format

This commit is contained in:
cbows 2023-07-29 00:06:32 +02:00
parent 0787230f20
commit 24d4901439
42 changed files with 1277 additions and 1154 deletions

19
.github/workflows/lint.yml vendored Normal file
View file

@ -0,0 +1,19 @@
---
name: Linting and style checking
on:
push:
pull_request:
jobs:
stylua:
name: StyLua
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: JohnnyMorganz/stylua-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --check .

6
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,6 @@
---
repos:
- repo: https://github.com/JohnnyMorganz/StyLua
rev: v0.18.1
hooks:
- id: stylua-github

3
.stylua.toml Normal file
View file

@ -0,0 +1,3 @@
column_width = 80
indent_width = 2
indent_type = "Spaces"

1
.tool-versions Normal file
View file

@ -0,0 +1 @@
rust 1.71.0

View file

@ -1,8 +1,8 @@
local namespace = vim.api.nvim_create_namespace("ts_node_action_conceal")
return function(char, level, cursor)
char = char or ""
level = level or 2
char = char or ""
level = level or 2
cursor = cursor or "nc"
local function action(node)
@ -12,7 +12,11 @@ return function(char, level, cursor)
local start_row, start_col, end_row, end_col = node:range()
local extmark_id = unpack(
vim.api.nvim_buf_get_extmarks(
0, namespace, { start_row, start_col }, { end_row, end_col }, {}
0,
namespace,
{ start_row, start_col },
{ end_row, end_col },
{}
)[1] or {}
)
@ -20,7 +24,11 @@ return function(char, level, cursor)
vim.api.nvim_buf_del_extmark(0, namespace, extmark_id)
else
vim.api.nvim_buf_set_extmark(
0, namespace, start_row, start_col, { end_row = end_row, end_col = end_col, conceal = char }
0,
namespace,
start_row,
start_col,
{ end_row = end_row, end_col = end_col, conceal = char }
)
end
end

View file

@ -31,7 +31,7 @@ local format_table = {
end,
standardize = function(text)
return vim.split(string.lower(text), "_", { trimempty = true })
end
end,
},
camel_case = {
pattern = "^%l+[%u%l]*$",
@ -44,46 +44,61 @@ local format_table = {
end,
standardize = function(text)
return vim.split(
text:gsub(".%f[%l]", " %1"):gsub("%l%f[%u]", "%1 "):gsub("^.", string.upper), " ",
text
:gsub(".%f[%l]", " %1")
:gsub("%l%f[%u]", "%1 ")
:gsub("^.", string.upper),
" ",
{ trimempty = true }
)
end
end,
},
pascal_case = {
pattern = "^%u%l+[%u%l]*$",
apply = function(tbl)
local value, _ = table.concat(
vim.tbl_map(function(word) return word:gsub("^.", string.upper) end, tbl), ""
vim.tbl_map(function(word)
return word:gsub("^.", string.upper)
end, tbl),
""
)
return value
end,
standardize = function(text)
return vim.split(
text:gsub(".%f[%l]", " %1"):gsub("%l%f[%u]", "%1 "):gsub("^.", string.upper), " ",
text
:gsub(".%f[%l]", " %1")
:gsub("%l%f[%u]", "%1 ")
:gsub("^.", string.upper),
" ",
{ trimempty = true }
)
end
end,
},
screaming_snake_case = {
pattern = "^%u+[%u_]*$",
apply = function(tbl)
local value, _ = table.concat(
vim.tbl_map(function(word) return word:upper() end, tbl), "_"
vim.tbl_map(function(word)
return word:upper()
end, tbl),
"_"
)
return value
end,
standardize = function(text)
return vim.split(string.lower(text), "_", { trimempty = true })
end
}
end,
},
}
local function check_pattern(text, pattern)
return not not string.find(text, pattern)
end
local default_formats = { "snake_case", "pascal_case", "screaming_snake_case", "camel_case" }
local default_formats =
{ "snake_case", "pascal_case", "screaming_snake_case", "camel_case" }
return function(user_formats)
user_formats = user_formats or default_formats
@ -106,8 +121,8 @@ return function(user_formats)
for i, format in ipairs(formats) do
if check_pattern(text, format.pattern) then
local next_i = i + 1 > #formats and 1 or i + 1
local apply = formats[next_i].apply
local next_i = i + 1 > #formats and 1 or i + 1
local apply = formats[next_i].apply
local standardize = format.standardize
return apply(standardize(text))

View file

@ -7,7 +7,7 @@ return function(quotes)
local text = helpers.node_text(node)
for i, char in ipairs(quotes) do
if string.sub(text, 1, #char[1]) == char[1] then
local next = quotes[i + 1 > #quotes and 1 or i + 1]
local next = quotes[i + 1 > #quotes and 1 or i + 1]
local substring = string.sub(text, #char[1] + 1, -(#char[2] + 1))
return next[1] .. substring .. next[2], { cursor = {} }

View file

@ -1,9 +1,11 @@
return {
cycle_case = require("ts-node-action.actions.cycle_case"),
toggle_boolean = require("ts-node-action.actions.toggle_boolean"),
toggle_multiline = require("ts-node-action.actions.toggle_multiline"),
toggle_operator = require("ts-node-action.actions.toggle_operator"),
cycle_quotes = require("ts-node-action.actions.cycle_quotes"),
conceal_string = require("ts-node-action.actions.conceal_string"),
toggle_int_readability = require("ts-node-action.actions.toggle_int_readability"),
cycle_case = require("ts-node-action.actions.cycle_case"),
toggle_boolean = require("ts-node-action.actions.toggle_boolean"),
toggle_multiline = require("ts-node-action.actions.toggle_multiline"),
toggle_operator = require("ts-node-action.actions.toggle_operator"),
cycle_quotes = require("ts-node-action.actions.cycle_quotes"),
conceal_string = require("ts-node-action.actions.conceal_string"),
toggle_int_readability = require(
"ts-node-action.actions.toggle_int_readability"
),
}

View file

@ -1,17 +1,20 @@
local helpers = require("ts-node-action.helpers")
local boolean_pair_default = {
["true"] = "false",
["true"] = "false",
["false"] = "true",
["True"] = "False",
["True"] = "False",
["False"] = "True",
["TRUE"] = "FALSE",
["FALSE"] = "TRUE",
}
return function(boolean_pair_override)
local boolean_pair = vim.tbl_deep_extend("force",
boolean_pair_default, boolean_pair_override or {})
local boolean_pair = vim.tbl_deep_extend(
"force",
boolean_pair_default,
boolean_pair_override or {}
)
local function action(node)
return boolean_pair[helpers.node_text(node)] or helpers.node_text(node)

View file

@ -22,7 +22,8 @@ return function(delimiter)
if string.find(text, delimiter) then
text = text:gsub(delimiter, "")
else
text = table.concat(group_string(text:reverse(), 3), delimiter):reverse()
text =
table.concat(group_string(text:reverse(), 3), delimiter):reverse()
end
end

View file

@ -14,7 +14,9 @@ local function collapse_child_nodes(padding, uncollapsible)
for child, _ in node:iter_children() do
if can_be_collapsed(child) then
local child_text = collapse_child_nodes(padding, uncollapsible)(child)
if not child_text then return end -- We found a comment, abort
if not child_text then
return
end -- We found a comment, abort
table.insert(replacement, child_text)
elseif child:type() == "comment" then -- TODO: use child:extra() when that API gets merged into stable
@ -38,7 +40,8 @@ local function expand_child_nodes(node)
table.insert(replacement, helpers.node_text(child))
else
if child:next_sibling() and child:prev_sibling() then
replacement[#replacement] = replacement[#replacement] .. helpers.node_text(child)
replacement[#replacement] = replacement[#replacement]
.. helpers.node_text(child)
elseif not child:prev_sibling() then -- Opening brace
table.insert(replacement, helpers.node_text(child))
else -- Closing brace
@ -54,7 +57,7 @@ end
---@param uncollapsible table
---@return table
return function(padding, uncollapsible)
padding = padding or {}
padding = padding or {}
uncollapsible = uncollapsible or {}
local function action(node)

View file

@ -3,15 +3,15 @@ local helpers = require("ts-node-action.helpers")
local default_operators = {
["!="] = "==",
["=="] = "!=",
[">"] = "<",
["<"] = ">",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
}
return function(operator_override)
local operators = vim.tbl_extend("force",
default_operators, operator_override or {})
local operators =
vim.tbl_extend("force", default_operators, operator_override or {})
local function action(node)
if node:child_count() == 0 then

View file

@ -1,10 +1,10 @@
local actions = require("ts-node-action.actions")
return {
["true"] = actions.toggle_boolean(),
["false"] = actions.toggle_boolean(),
["boolean"] = actions.toggle_boolean(),
["identifier"] = actions.cycle_case(),
["true"] = actions.toggle_boolean(),
["false"] = actions.toggle_boolean(),
["boolean"] = actions.toggle_boolean(),
["identifier"] = actions.cycle_case(),
["variable_name"] = actions.cycle_case(),
["string"] = actions.cycle_quotes(),
["string"] = actions.cycle_quotes(),
}

View file

@ -1,5 +1,5 @@
local actions = require("ts-node-action.actions")
return {
["attribute_value"] = actions.conceal_string()
["attribute_value"] = actions.conceal_string(),
}

View file

@ -1,21 +1,21 @@
return {
["*"] = require("ts-node-action.filetypes.global"),
lua = require("ts-node-action.filetypes.lua"),
json = require("ts-node-action.filetypes.json"),
julia = require("ts-node-action.filetypes.julia"),
yaml = require("ts-node-action.filetypes.yaml"),
ruby = require("ts-node-action.filetypes.ruby"),
eruby = require("ts-node-action.filetypes.ruby"),
python = require("ts-node-action.filetypes.python"),
php = require("ts-node-action.filetypes.php"),
rust = require("ts-node-action.filetypes.rust"),
html = require("ts-node-action.filetypes.html"),
javascript = require("ts-node-action.filetypes.javascript"),
["*"] = require("ts-node-action.filetypes.global"),
lua = require("ts-node-action.filetypes.lua"),
json = require("ts-node-action.filetypes.json"),
julia = require("ts-node-action.filetypes.julia"),
yaml = require("ts-node-action.filetypes.yaml"),
ruby = require("ts-node-action.filetypes.ruby"),
eruby = require("ts-node-action.filetypes.ruby"),
python = require("ts-node-action.filetypes.python"),
php = require("ts-node-action.filetypes.php"),
rust = require("ts-node-action.filetypes.rust"),
html = require("ts-node-action.filetypes.html"),
javascript = require("ts-node-action.filetypes.javascript"),
javascriptreact = require("ts-node-action.filetypes.javascript"),
typescript = require("ts-node-action.filetypes.javascript"),
typescript = require("ts-node-action.filetypes.javascript"),
typescriptreact = require("ts-node-action.filetypes.javascript"),
svelte = require("ts-node-action.filetypes.javascript"),
sql = require("ts-node-action.filetypes.sql"),
r = require("ts-node-action.filetypes.r"),
git_rebase = require("ts-node-action.filetypes.git_rebase"),
svelte = require("ts-node-action.filetypes.javascript"),
sql = require("ts-node-action.filetypes.sql"),
r = require("ts-node-action.filetypes.r"),
git_rebase = require("ts-node-action.filetypes.git_rebase"),
}

View file

@ -1,14 +1,14 @@
local actions = require("ts-node-action.actions")
local operators = {
["!="] = "==",
["!="] = "==",
["!=="] = "===",
["=="] = "!=",
["=="] = "!=",
["==="] = "!==",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
}
local padding = {
@ -20,15 +20,15 @@ local padding = {
return {
["property_identifier"] = actions.cycle_case(),
["string_fragment"] = actions.conceal_string(),
["binary_expression"] = actions.toggle_operator(operators),
["object"] = actions.toggle_multiline(padding),
["array"] = actions.toggle_multiline(padding),
["statement_block"] = actions.toggle_multiline(padding),
["object_pattern"] = actions.toggle_multiline(padding),
["object_type"] = actions.toggle_multiline(padding),
["formal_parameters"] = actions.toggle_multiline(padding),
["argument_list"] = actions.toggle_multiline(padding),
["method_parameters"] = actions.toggle_multiline(padding),
["number"] = actions.toggle_int_readability(),
["string_fragment"] = actions.conceal_string(),
["binary_expression"] = actions.toggle_operator(operators),
["object"] = actions.toggle_multiline(padding),
["array"] = actions.toggle_multiline(padding),
["statement_block"] = actions.toggle_multiline(padding),
["object_pattern"] = actions.toggle_multiline(padding),
["object_type"] = actions.toggle_multiline(padding),
["formal_parameters"] = actions.toggle_multiline(padding),
["argument_list"] = actions.toggle_multiline(padding),
["method_parameters"] = actions.toggle_multiline(padding),
["number"] = actions.toggle_int_readability(),
}

View file

@ -7,5 +7,5 @@ local padding = {
return {
["object"] = actions.toggle_multiline(padding),
["array"] = actions.toggle_multiline(padding),
["array"] = actions.toggle_multiline(padding),
}

View file

@ -1,35 +1,35 @@
local operators = {
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
["!="] = "==",
["=="] = "!=",
[""] = "",
[""] = "",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
["!="] = "==",
["=="] = "!=",
[""] = "",
[""] = "",
}
local boolean = {
["true"] = "false",
["false"] = "true",
["true"] = "false",
["false"] = "true",
}
local padding = {
[","] = "%s ",
[";"] = "%s ",
[","] = "%s ",
[";"] = "%s ",
}
local actions = require "ts-node-action.actions"
local actions = require("ts-node-action.actions")
return {
["identifier"] = actions.cycle_case(),
["boolean_literal"] = actions.toggle_boolean(boolean),
["integer_literal"] = actions.toggle_int_readability(),
["argument_list"] = actions.toggle_multiline(padding, {}),
["vector_expression"] = actions.toggle_multiline(padding, {}),
["tuple_expression"] = actions.toggle_multiline(padding, {}),
["operator"] = actions.toggle_operator(operators),
["identifier"] = actions.cycle_case(),
["boolean_literal"] = actions.toggle_boolean(boolean),
["integer_literal"] = actions.toggle_int_readability(),
["argument_list"] = actions.toggle_multiline(padding, {}),
["vector_expression"] = actions.toggle_multiline(padding, {}),
["tuple_expression"] = actions.toggle_multiline(padding, {}),
["operator"] = actions.toggle_operator(operators),
}

View file

@ -2,17 +2,17 @@ local actions = require("ts-node-action.actions")
local helpers = require("ts-node-action.helpers")
local padding = {
[","] = "%s ",
["{"] = "%s ",
["}"] = " %s",
["="] = " %s ",
["or"] = " %s ",
[","] = "%s ",
["{"] = "%s ",
["}"] = " %s",
["="] = " %s ",
["or"] = " %s ",
["and"] = " %s ",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
[".."] = " %s ",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
[".."] = " %s ",
}
local operator_override = {
@ -23,7 +23,7 @@ local operator_override = {
local quote_override = {
{ "'", "'" },
{ '"', '"' },
{ '[[', ']]' },
{ "[[", "]]" },
}
local function toggle_function(node)
@ -36,7 +36,10 @@ local function toggle_function(node)
local body = struct.body and (struct.body .. " ") or ""
return "function" .. struct.parameters .. " " .. body .. "end"
else
return { "function" .. struct.parameters, struct.body or "", "end" }, { format = true, cursor = {} }
return { "function" .. struct.parameters, struct.body or "", "end" }, {
format = true,
cursor = {},
}
end
end
@ -48,24 +51,29 @@ local function toggle_named_function(node)
if helpers.node_is_multiline(node) then
return (struct["local"] and "local " or "")
.. "function "
.. struct.name
.. struct.parameters .. " "
.. struct.body .. " end"
.. "function "
.. struct.name
.. struct.parameters
.. " "
.. struct.body
.. " end"
else
return {
(struct["local"] and "local " or "") .. "function " .. struct.name .. struct.parameters,
(struct["local"] and "local " or "")
.. "function "
.. struct.name
.. struct.parameters,
struct.body,
"end"
"end",
}, { format = true, cursor = { col = struct["local"] and 6 or 0 } }
end
end
return {
["table_constructor"] = actions.toggle_multiline(padding),
["arguments"] = actions.toggle_multiline(padding),
["binary_expression"] = actions.toggle_operator(operator_override),
["string"] = actions.cycle_quotes(quote_override),
["function_definition"] = { { toggle_function, "Toggle Function" } },
["function_declaration"] = { { toggle_named_function, "Toggle Function" } }
["table_constructor"] = actions.toggle_multiline(padding),
["arguments"] = actions.toggle_multiline(padding),
["binary_expression"] = actions.toggle_operator(operator_override),
["string"] = actions.cycle_quotes(quote_override),
["function_definition"] = { { toggle_function, "Toggle Function" } },
["function_declaration"] = { { toggle_named_function, "Toggle Function" } },
}

View file

@ -1,41 +1,41 @@
local actions = require("ts-node-action.actions")
local padding = {
[","] = "%s ",
[","] = "%s ",
["=>"] = " %s ",
["="] = " %s ",
["["] = "%s",
["]"] = "%s",
["}"] = " %s",
["{"] = "%s ",
["="] = " %s ",
["["] = "%s",
["]"] = "%s",
["}"] = " %s",
["{"] = "%s ",
["||"] = " %s ",
["&&"] = " %s ",
["."] = " %s ",
["+"] = " %s ",
["*"] = " %s ",
["-"] = " %s ",
["/"] = " %s ",
["."] = " %s ",
["+"] = " %s ",
["*"] = " %s ",
["-"] = " %s ",
["/"] = " %s ",
}
local operators = {
["!="] = "==",
["!="] = "==",
["!=="] = "===",
["=="] = "!=",
["=="] = "!=",
["==="] = "!==",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
}
return {
["array_creation_expression"] = actions.toggle_multiline(padding),
["formal_parameters"] = actions.toggle_multiline(padding),
["arguments"] = actions.toggle_multiline(padding),
["subscript_expression"] = actions.toggle_multiline(padding),
["compound_statement"] = actions.toggle_multiline(padding),
["name"] = actions.cycle_case(),
["encapsed_string"] = actions.cycle_quotes(),
["binary_expression"] = actions.toggle_operator(operators),
["integer"] = actions.toggle_int_readability(),
["formal_parameters"] = actions.toggle_multiline(padding),
["arguments"] = actions.toggle_multiline(padding),
["subscript_expression"] = actions.toggle_multiline(padding),
["compound_statement"] = actions.toggle_multiline(padding),
["name"] = actions.cycle_case(),
["encapsed_string"] = actions.cycle_quotes(),
["binary_expression"] = actions.toggle_operator(operators),
["integer"] = actions.toggle_int_readability(),
}

View file

@ -18,42 +18,48 @@ local actions = require("ts-node-action.actions")
-- prev_text=nil, so we can use that to detect the unary case with a special
-- key "prev_nil", to represent it.
local padding = {
[","] = "%s ",
[":"] = "%s ",
["{"] = "%s",
["}"] = "%s",
["for"] = " %s ",
["if"] = " %s ",
["else"] = " %s ",
["and"] = " %s ",
["or"] = " %s ",
["is"] = " %s ",
["not"] = { " %s ", ["is"] = "%s " },
["in"] = { " %s ", ["not"] = "%s " },
["=="] = " %s ",
["!="] = " %s ",
[">="] = " %s ",
["<="] = " %s ",
[">"] = " %s ",
["<"] = " %s ",
["+"] = " %s ",
["-"] = { " %s ", ["prev_nil"] = "%s", },
["*"] = " %s ",
["/"] = " %s ",
["//"] = " %s ",
["%"] = " %s ",
["**"] = " %s ",
[","] = "%s ",
[":"] = "%s ",
["{"] = "%s",
["}"] = "%s",
["for"] = " %s ",
["if"] = " %s ",
["else"] = " %s ",
["and"] = " %s ",
["or"] = " %s ",
["is"] = " %s ",
["not"] = { " %s ", ["is"] = "%s " },
["in"] = { " %s ", ["not"] = "%s " },
["=="] = " %s ",
["!="] = " %s ",
[">="] = " %s ",
["<="] = " %s ",
[">"] = " %s ",
["<"] = " %s ",
["+"] = " %s ",
["-"] = { " %s ", ["prev_nil"] = "%s" },
["*"] = " %s ",
["/"] = " %s ",
["//"] = " %s ",
["%"] = " %s ",
["**"] = " %s ",
["lambda"] = " %s ",
["with"] = " %s ",
["as"] = " %s ",
["with"] = " %s ",
["as"] = " %s ",
["import"] = " %s ",
["from"] = "%s ",
["from"] = "%s ",
}
--- @param node TSNode
local function node_trim_whitespace(node)
local start_row, _, end_row, _ = node:range()
vim.cmd("silent! keeppatterns " .. (start_row + 1) .. "," .. (end_row + 1) .. "s/\\s\\+$//g")
vim.cmd(
"silent! keeppatterns "
.. (start_row + 1)
.. ","
.. (end_row + 1)
.. "s/\\s\\+$//g"
)
end
-- When inlined, these nodes must be parenthesized to avoid changing the
@ -89,7 +95,6 @@ end
--- @param padding_override table
--- @return function
local function collapse_child_nodes(padding_override)
--- @param node TSNode
--- @return string
local function action(node)
@ -117,10 +122,10 @@ end
--- @param node TSNode
--- @return string|nil, string|nil, string
local function node_text_lhs_rhs(node, padding_override)
local lhs = nil
local rhs = nil
local type = node:type()
local child = node:named_child(0)
local lhs = nil
local rhs = nil
local type = node:type()
local child = node:named_child(0)
local collapse = collapse_child_nodes(padding_override)
if type == "return_statement" then
@ -128,7 +133,7 @@ local function node_text_lhs_rhs(node, padding_override)
rhs = collapse(child)
elseif type == "expression_statement" then
type = child:type()
lhs = ""
lhs = ""
if type == "assignment" then
local identifiers = {}
@ -141,13 +146,11 @@ local function node_text_lhs_rhs(node, padding_override)
rhs = collapse(child)
elseif type == "call" then
local identifier = helpers.node_text(child:named_child(0))
child = child:named_child(1)
rhs = identifier .. collapse(child)
elseif type == "boolean_operator" or
type == "parenthesized_expression" then
child = child:named_child(1)
rhs = identifier .. collapse(child)
elseif type == "boolean_operator" or type == "parenthesized_expression" then
rhs = collapse(child)
end
end
return lhs, rhs, type, child
@ -169,10 +172,11 @@ end
--- @return TSNode, string
--- @return nil
local function find_row_parent(parent, parent_type, start_row)
while parent ~= nil and
parent_type ~= "if_statement" and
parent_type ~= "for_statement" do
while
parent ~= nil
and parent_type ~= "if_statement"
and parent_type ~= "for_statement"
do
parent = parent:parent()
if parent == nil then
return nil
@ -203,11 +207,13 @@ end
--- @return TSNode, string
local function skip_parens_by_reparenting(parent, parent_type)
if parent_type == "parenthesized_expression" then
local paren_parent = parent:parent()
local paren_parent = parent:parent()
local paren_parent_type = paren_parent:type()
if paren_parent_type == "assignment" or
paren_parent_type == "return_statement" then
parent = paren_parent
if
paren_parent_type == "assignment"
or paren_parent_type == "return_statement"
then
parent = paren_parent
parent_type = paren_parent_type
end
end
@ -252,7 +258,7 @@ local function destructure_if_statement(if_statement)
local condition
local consequence = {}
local alternative = {}
local comments = {}
local comments = {}
for child in if_statement:iter_children() do
if child:named() then
@ -269,16 +275,15 @@ local function destructure_if_statement(if_statement)
else
condition = child
end
end
end
return {
node = if_statement,
condition = condition,
node = if_statement,
condition = condition,
consequence = consequence,
alternative = alternative,
comments = comments
comments = comments,
}
end
@ -291,11 +296,11 @@ local function destructure_conditional_expression(node)
collect_named_children(node, children, comments)
return {
node = node,
condition = children[2],
node = node,
condition = children[2],
consequence = { children[1] }, -- as a table for consistency
alternative = { children[3] }, -- which allows for sharing
comments = comments,
comments = comments,
}
end
@ -303,7 +308,7 @@ end
--- @return string, table, TSNode
--- @return nil
local function expand_cond_expr(stmt, padding_override)
local parent = stmt.node:parent()
local parent = stmt.node:parent()
local parent_type = parent:type()
parent, parent_type = skip_parens_by_reparenting(parent, parent_type)
@ -330,24 +335,24 @@ local function expand_cond_expr(stmt, padding_override)
end
local start_row, start_col = parent:start()
local row_parent = find_row_parent(parent, parent_type, start_row)
local cursor = {}
local row_parent = find_row_parent(parent, parent_type, start_row)
local cursor = {}
-- when we are embedded on the end of an inlined if/for statement, we need
-- to expand on to the next line and shift the cursor/indent
local if_indent = ""
local else_indent = ""
local if_indent = ""
local else_indent = ""
if row_parent then
local _, row_start_col = row_parent:start()
-- cursor position is relative to the node being replaced (parent)
cursor = { row = 1, col = row_start_col - start_col + 4 }
if_indent = string.rep(" ", row_start_col + 4)
else_indent = if_indent
cursor = { row = 1, col = row_start_col - start_col + 4 }
if_indent = string.rep(" ", row_start_col + 4)
else_indent = if_indent
else
else_indent = string.rep(" ", start_col)
end
local body_indent = else_indent .. string.rep(" ", 4)
local collapse = collapse_child_nodes(padding_override)
local collapse = collapse_child_nodes(padding_override)
local replacement = {
if_indent .. "if " .. collapse(stmt.condition) .. ":",
body_indent .. lhs .. collapse(stmt.consequence[1]),
@ -364,15 +369,18 @@ local function expand_cond_expr(stmt, padding_override)
local callback = nil
if row_parent then
table.insert(replacement, 1, "")
callback = function() node_trim_whitespace(parent) end
callback = function()
node_trim_whitespace(parent)
end
end
return replacement, {
cursor = cursor,
callback = callback,
format = true,
target = parent,
}
return replacement,
{
cursor = cursor,
callback = callback,
format = true,
target = parent,
}
end
--- @param stmt table { node, condition, consequence, alternative, comments }
@ -380,11 +388,8 @@ end
--- @return string, table, TSNode
--- @return nil
local function inline_if(stmt, padding_override)
local lhs, rhs, _, child = node_text_lhs_rhs(
stmt.consequence[1],
padding_override
)
local lhs, rhs, _, child =
node_text_lhs_rhs(stmt.consequence[1], padding_override)
if lhs == nil then
return
end
@ -410,12 +415,12 @@ local function body_types_are_inlineable(cons_type, alt_type, cons_lhs, alt_lhs)
end
-- these do not depend on a common lhs and can freely appear on either side
local mixable_match_body_types = {
["call"] = true,
["boolean_operator"] = true,
["call"] = true,
["boolean_operator"] = true,
["parenthesized_expression"] = true,
}
return mixable_match_body_types[cons_type] and
mixable_match_body_types[alt_type]
return mixable_match_body_types[cons_type]
and mixable_match_body_types[alt_type]
end
--- @param stmt table { node, condition, consequence, alternative, comments }
@ -423,21 +428,19 @@ end
--- @return string, table, TSNode
--- @return nil
local function inline_ifelse(stmt, padding_override)
local cons_lhs, cons_rhs, cons_type, cons_child = node_text_lhs_rhs(
stmt.consequence[1],
padding_override
)
local cons_lhs, cons_rhs, cons_type, cons_child =
node_text_lhs_rhs(stmt.consequence[1], padding_override)
if cons_lhs == nil then
return
end
cons_rhs = parenthesize_if_needed(cons_child, cons_rhs)
local alt_lhs, alt_rhs, alt_type, alt_child = node_text_lhs_rhs(
stmt.alternative[1],
padding_override
)
if alt_rhs == nil or not body_types_are_inlineable(cons_type, alt_type, cons_lhs, alt_lhs) then
local alt_lhs, alt_rhs, alt_type, alt_child =
node_text_lhs_rhs(stmt.alternative[1], padding_override)
if
alt_rhs == nil
or not body_types_are_inlineable(cons_type, alt_type, cons_lhs, alt_lhs)
then
return
end
@ -445,13 +448,17 @@ local function inline_ifelse(stmt, padding_override)
local cond_text = collapse_child_nodes(padding_override)(stmt.condition)
local replacement = cons_lhs .. cons_rhs ..
" if " .. cond_text ..
" else " .. alt_rhs
local replacement = cons_lhs
.. cons_rhs
.. " if "
.. cond_text
.. " else "
.. alt_rhs
return replacement, {
cursor = { col = string.len(cons_lhs .. cons_rhs) + 1 },
}
return replacement,
{
cursor = { col = string.len(cons_lhs .. cons_rhs) + 1 },
}
end
--- @param padding_override table
@ -485,7 +492,6 @@ local function inline_if_statement(padding_override)
-- and this knows how to expand it
return expand_cond_expr(stmt, padding_override)
end
end
return { action, name = "Inline Conditional" }
@ -500,7 +506,9 @@ local function expand_conditional_expression(padding_override)
--- @return string, table, TSNode
local function action(conditional_expression)
local stmt = destructure_conditional_expression(conditional_expression)
if #stmt.comments > 0 then return end
if #stmt.comments > 0 then
return
end
return expand_cond_expr(stmt, padding_override)
end
@ -509,18 +517,18 @@ local function expand_conditional_expression(padding_override)
end
return {
["dictionary"] = actions.toggle_multiline(padding),
["set"] = actions.toggle_multiline(padding),
["list"] = actions.toggle_multiline(padding),
["tuple"] = actions.toggle_multiline(padding),
["argument_list"] = actions.toggle_multiline(padding),
["parameters"] = actions.toggle_multiline(padding),
["list_comprehension"] = actions.toggle_multiline(padding),
["set_comprehension"] = actions.toggle_multiline(padding),
["dictionary"] = actions.toggle_multiline(padding),
["set"] = actions.toggle_multiline(padding),
["list"] = actions.toggle_multiline(padding),
["tuple"] = actions.toggle_multiline(padding),
["argument_list"] = actions.toggle_multiline(padding),
["parameters"] = actions.toggle_multiline(padding),
["list_comprehension"] = actions.toggle_multiline(padding),
["set_comprehension"] = actions.toggle_multiline(padding),
["dictionary_comprehension"] = actions.toggle_multiline(padding),
["generator_expression"] = actions.toggle_multiline(padding),
["comparison_operator"] = actions.toggle_operator(),
["integer"] = actions.toggle_int_readability(),
["conditional_expression"] = { expand_conditional_expression(padding), },
["if_statement"] = { inline_if_statement(padding), },
["generator_expression"] = actions.toggle_multiline(padding),
["comparison_operator"] = actions.toggle_operator(),
["integer"] = actions.toggle_int_readability(),
["conditional_expression"] = { expand_conditional_expression(padding) },
["if_statement"] = { inline_if_statement(padding) },
}

View file

@ -2,56 +2,61 @@ local actions = require("ts-node-action.actions")
local helpers = require("ts-node-action.helpers")
local operators = {
["!="] = "==",
["=="] = "!=",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
["|"] = "&",
["&"] = "|",
["||"] = "&&",
["&&"] = "||",
["!="] = "==",
["=="] = "!=",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
["|"] = "&",
["&"] = "|",
["||"] = "&&",
["&&"] = "||",
}
local padding = {
[","] = "%s ",
["="] = " %s ",
[","] = "%s ",
["="] = " %s ",
}
--- @param node TSNode
local function toggle_multiline_args(node)
local structure = helpers.destructure_node(node)
if (type(structure["arguments"]) == "table") or (type(structure["arguments"]) == "string") then
else
vim.print("No arguments")
return
end
local structure = helpers.destructure_node(node)
if
(type(structure["arguments"]) == "table")
or (type(structure["arguments"]) == "string")
then
else
vim.print("No arguments")
return
end
local range_end = {}
range_end = { node:named_child(0):range() }
local replacement
local range_end = {}
range_end = { node:named_child(0):range() }
local replacement
if helpers.node_is_multiline(node) then
local tbl = actions.toggle_multiline(padding)
replacement = tbl[1][1](node)
else
replacement = { structure["function"] .. "(" }
for k in string.gmatch(structure.arguments, "([^,]+)") do
table.insert(replacement, k .. ",")
end
replacement[#replacement] = string.gsub(replacement[#replacement], "(.*)%,$", "%1")
table.insert(replacement, ")")
end
if helpers.node_is_multiline(node) then
local tbl = actions.toggle_multiline(padding)
replacement = tbl[1][1](node)
else
replacement = { structure["function"] .. "(" }
for k in string.gmatch(structure.arguments, "([^,]+)") do
table.insert(replacement, k .. ",")
end
replacement[#replacement] =
string.gsub(replacement[#replacement], "(.*)%,$", "%1")
table.insert(replacement, ")")
end
return replacement, { cursor = { col = range_end[4] - range_end[2] }, format = true }
return replacement,
{ cursor = { col = range_end[4] - range_end[2] }, format = true }
end
return {
["binary"] = actions.toggle_operator(operators),
["call"] = { { toggle_multiline_args, name = "Toggle Multiline Arguments" } },
["formal_parameters"] = actions.toggle_multiline(padding),
["binary"] = actions.toggle_operator(operators),
["call"] = { { toggle_multiline_args, name = "Toggle Multiline Arguments" } },
["formal_parameters"] = actions.toggle_multiline(padding),
}

View file

@ -2,33 +2,40 @@ local helpers = require("ts-node-action.helpers")
local actions = require("ts-node-action.actions")
local padding = {
[","] = "%s ",
[":"] = { "%s ", ["next_nil"] = "%s" },
["{"] = "%s ",
[","] = "%s ",
[":"] = { "%s ", ["next_nil"] = "%s" },
["{"] = "%s ",
["=>"] = " %s ",
["="] = " %s ",
["}"] = " %s",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
["="] = " %s ",
["}"] = " %s",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
}
local identifier_formats = { "snake_case", "pascal_case", "screaming_snake_case" }
local identifier_formats =
{ "snake_case", "pascal_case", "screaming_snake_case" }
local uncollapsible = {
["conditional"] = true
["conditional"] = true,
}
local function toggle_block(node)
local structure = helpers.destructure_node(node)
if type(structure.body) == "table" then return end
if type(structure.body) == "table" then
return
end
local replacement
if helpers.node_is_multiline(node) then
if structure.parameters then
replacement = "{ " .. structure.parameters .. " " .. structure.body .. " }"
replacement = "{ "
.. structure.parameters
.. " "
.. structure.body
.. " }"
else
replacement = "{ " .. structure.body .. " }"
end
@ -51,10 +58,11 @@ local function inline_conditional(structure)
local replacement = {
structure.consequence,
structure["if"] or structure["unless"],
structure.condition
structure.condition,
}
return table.concat(replacement, " "), { cursor = { col = #structure.consequence + 1 } }
return table.concat(replacement, " "),
{ cursor = { col = #structure.consequence + 1 } }
end
local function collapse_ternary(structure)
@ -63,7 +71,7 @@ local function collapse_ternary(structure)
" ? ",
structure.consequence,
" : ",
structure.alternative[2]
structure.alternative[2],
}
return table.concat(replacement), { cursor = { col = #replacement[1] + 1 } }
@ -88,7 +96,7 @@ local function expand_ternary(node)
structure.consequence,
"else",
structure.alternative,
"end"
"end",
}
return replacement, { cursor = {}, format = true }
@ -99,18 +107,21 @@ local function multiline_conditional(node)
local replacement = {
(structure["if"] or structure["unless"]) .. " " .. structure.condition,
structure.body,
"end"
"end",
}
return replacement, { cursor = {}, format = true }
end
local function toggle_hash_style(node)
local styles = { ["=>"] = ": ", [":"] = " => " }
local styles = { ["=>"] = ": ", [":"] = " => " }
local structure = helpers.destructure_node(node)
-- Not handling non string/symbol keys
if not structure.key:sub(1):match([[^"']]) and not structure.key:sub(1):match("%a") then
if
not structure.key:sub(1):match([[^"']])
and not structure.key:sub(1):match("%a")
then
return
end
@ -121,27 +132,35 @@ local function toggle_hash_style(node)
structure.key = structure.key:sub(2)
end
local replacement = structure.key .. styles[structure[":"] or structure["=>"]] .. structure.value
local opts = { cursor = { col = structure[":"] and #structure.key + 1 or #structure.key } }
local replacement = structure.key
.. styles[structure[":"] or structure["=>"]]
.. structure.value
local opts = {
cursor = { col = structure[":"] and #structure.key + 1 or #structure.key },
}
return replacement, opts
end
return {
["identifier"] = actions.cycle_case(identifier_formats),
["constant"] = actions.cycle_case(identifier_formats),
["binary"] = actions.toggle_operator(),
["array"] = actions.toggle_multiline(padding, uncollapsible),
["hash"] = actions.toggle_multiline(padding, uncollapsible),
["argument_list"] = actions.toggle_multiline(padding, uncollapsible),
["identifier"] = actions.cycle_case(identifier_formats),
["constant"] = actions.cycle_case(identifier_formats),
["binary"] = actions.toggle_operator(),
["array"] = actions.toggle_multiline(padding, uncollapsible),
["hash"] = actions.toggle_multiline(padding, uncollapsible),
["argument_list"] = actions.toggle_multiline(padding, uncollapsible),
["method_parameters"] = actions.toggle_multiline(padding, uncollapsible),
["integer"] = actions.toggle_int_readability(),
["block"] = { { toggle_block, name = "Toggle Block" } },
["do_block"] = { { toggle_block, name = "Toggle Block" } },
["if"] = { { handle_conditional, name = "Handle Conditional" } },
["unless"] = { { handle_conditional, name = "Handle Conditional" } },
["if_modifier"] = { { multiline_conditional, name = "Multiline Conditional" } },
["unless_modifier"] = { { multiline_conditional, name = "Multiline Conditional" } },
["conditional"] = { { expand_ternary, name = "Expand Ternary" } },
["pair"] = { { toggle_hash_style, name = "Toggle Hash Style" } },
["integer"] = actions.toggle_int_readability(),
["block"] = { { toggle_block, name = "Toggle Block" } },
["do_block"] = { { toggle_block, name = "Toggle Block" } },
["if"] = { { handle_conditional, name = "Handle Conditional" } },
["unless"] = { { handle_conditional, name = "Handle Conditional" } },
["if_modifier"] = {
{ multiline_conditional, name = "Multiline Conditional" },
},
["unless_modifier"] = {
{ multiline_conditional, name = "Multiline Conditional" },
},
["conditional"] = { { expand_ternary, name = "Expand Ternary" } },
["pair"] = { { toggle_hash_style, name = "Toggle Hash Style" } },
}

View file

@ -1,26 +1,26 @@
local actions = require("ts-node-action.actions")
local padding = {
[","] = "%s ",
[":"] = "%s ",
["{"] = "%s ",
[","] = "%s ",
[":"] = "%s ",
["{"] = "%s ",
["=>"] = " %s ",
["="] = " %s ",
["}"] = " %s",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
["="] = " %s ",
["}"] = " %s",
["+"] = " %s ",
["-"] = " %s ",
["*"] = " %s ",
["/"] = " %s ",
}
return {
["field_declaration_list"] = actions.toggle_multiline(padding),
["parameters"] = actions.toggle_multiline(padding),
["enum_variant_list"] = actions.toggle_multiline(padding),
["block"] = actions.toggle_multiline(padding),
["array_expression"] = actions.toggle_multiline(padding),
["tuple_expression"] = actions.toggle_multiline(padding),
["tuple_pattern"] = actions.toggle_multiline(padding),
["boolean_literal"] = actions.toggle_boolean(),
["integer_literal"] = actions.toggle_int_readability(),
["parameters"] = actions.toggle_multiline(padding),
["enum_variant_list"] = actions.toggle_multiline(padding),
["block"] = actions.toggle_multiline(padding),
["array_expression"] = actions.toggle_multiline(padding),
["tuple_expression"] = actions.toggle_multiline(padding),
["tuple_pattern"] = actions.toggle_multiline(padding),
["boolean_literal"] = actions.toggle_boolean(),
["integer_literal"] = actions.toggle_int_readability(),
}

View file

@ -1,37 +1,37 @@
local actions = require("ts-node-action.actions")
local operators = {
["!="] = "=",
["="] = "!=",
["AND"] = "OR",
["OR"] = "AND",
["or"] = "and",
["and"] = "or",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
["!="] = "=",
["="] = "!=",
["AND"] = "OR",
["OR"] = "AND",
["or"] = "and",
["and"] = "or",
[">"] = "<",
["<"] = ">",
[">="] = "<=",
["<="] = ">=",
["+"] = "-",
["-"] = "+",
["*"] = "/",
["/"] = "*",
}
local padding = {
[","] = "%s ",
[","] = "%s ",
}
local uncollapsible = {
["term"] = true,
["column_definition"] = true,
["term"] = true,
["column_definition"] = true,
}
return {
["keyword_true"] = actions.toggle_boolean(),
["keyword_false"] = actions.toggle_boolean(),
["binary_expression"] = actions.toggle_operator(operators),
["keyword_and"] = actions.toggle_operator(operators),
["keyword_or"] = actions.toggle_operator(operators),
["select_expression"] = actions.toggle_multiline(padding, uncollapsible),
["column_definitions"] = actions.toggle_multiline(padding, uncollapsible),
["keyword_true"] = actions.toggle_boolean(),
["keyword_false"] = actions.toggle_boolean(),
["binary_expression"] = actions.toggle_operator(operators),
["keyword_and"] = actions.toggle_operator(operators),
["keyword_or"] = actions.toggle_operator(operators),
["select_expression"] = actions.toggle_multiline(padding, uncollapsible),
["column_definitions"] = actions.toggle_multiline(padding, uncollapsible),
}

View file

@ -1,5 +1,5 @@
local actions = require("ts-node-action.actions")
return {
["boolean_scalar"] = actions.toggle_boolean(),
["boolean_scalar"] = actions.toggle_boolean(),
}

View file

@ -5,14 +5,20 @@ local M = {}
--- @param node TSNode
--- @return table|string|nil
function M.node_text(node)
if not node then return end
if not node then
return
end
local text
if vim.treesitter.get_node_text then
text = vim.trim(vim.treesitter.get_node_text(node, vim.api.nvim_get_current_buf()))
text = vim.trim(
vim.treesitter.get_node_text(node, vim.api.nvim_get_current_buf())
)
else
-- TODO: Remove in 0.10
text = vim.trim(vim.treesitter.query.get_node_text(node, vim.api.nvim_get_current_buf()))
text = vim.trim(
vim.treesitter.query.get_node_text(node, vim.api.nvim_get_current_buf())
)
end
if text:match("\n") then
@ -59,10 +65,12 @@ end
--- @param padding table
--- @return string|table|nil
function M.padded_node_text(node, padding)
local text = M.node_text(node)
local text = M.node_text(node)
local format = padding[text]
if not format then return text end
if not format then
return text
end
if type(format) == "table" then
local context_prev = M.node_text(node:prev_sibling())
@ -89,10 +97,11 @@ end
--- @param node TSNode
--- @return nil
function M.debug_print_tree(node)
local tree = {}
local tree = {}
local index = 1
for child, id in node:iter_children() do
tree[tostring(index)] = { type = child:type(), text = M.node_text(child), id = id }
tree[tostring(index)] =
{ type = child:type(), text = M.node_text(child), id = id }
index = index + 1
end

View file

@ -12,17 +12,18 @@ local function replace_node(node, replacement, opts)
local start_row, start_col, end_row, end_col = (opts.target or node):range()
vim.api.nvim_buf_set_text(
vim.api.nvim_get_current_buf(),
start_row, start_col, end_row, end_col, replacement
start_row,
start_col,
end_row,
end_col,
replacement
)
if opts.cursor then
vim.api.nvim_win_set_cursor(
vim.api.nvim_get_current_win(),
{
start_row + (opts.cursor.row or 0) + 1,
start_col + (opts.cursor.col or 0)
}
)
vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), {
start_row + (opts.cursor.row or 0) + 1,
start_col + (opts.cursor.col or 0),
})
end
if opts.format then
@ -38,7 +39,11 @@ end
--- @param message string
--- @return nil
local function info(message)
vim.notify(message, vim.log.levels.INFO, { title = "Node Action", icon = "" })
vim.notify(
message,
vim.log.levels.INFO,
{ title = "Node Action", icon = "" }
)
end
--- @private
@ -100,7 +105,6 @@ function M._get_node()
return node, langtree:lang()
end
M.node_action = require("ts-node-action.repeat").set(function()
local node, lang = M._get_node()
if not node then
@ -115,17 +119,23 @@ M.node_action = require("ts-node-action.repeat").set(function()
if #action == 1 then
do_action(action[1][1], node)
else
vim.ui.select(
action,
{
prompt = "Select Action",
format_item = function(choice) return choice.name end
},
function(choice) do_action(choice[1], node) end
)
vim.ui.select(action, {
prompt = "Select Action",
format_item = function(choice)
return choice.name
end,
}, function(choice)
do_action(choice[1], node)
end)
end
else
info("No action defined for '" .. lang .. "' node type: '" .. node:type() .. "'")
info(
"No action defined for '"
.. lang
.. "' node type: '"
.. node:type()
.. "'"
)
end
end)
@ -138,8 +148,10 @@ function M.available_actions()
local function format_action(tbl)
return {
action = function() do_action(tbl[1], node) end,
title = tbl.name or "Anonymous Node Action",
action = function()
do_action(tbl[1], node)
end,
title = tbl.name or "Anonymous Node Action",
}
end
@ -158,20 +170,18 @@ function M.debug()
return
end
print(vim.inspect(
{
node = {
lang = lang,
filetype = vim.o.filetype,
node_type = node:type(),
named = node:named(),
named_children = node:named_child_count(),
},
plugin = {
node_actions = M.node_actions,
}
}
))
print(vim.inspect({
node = {
lang = lang,
filetype = vim.o.filetype,
node_type = node:type(),
named = node:named(),
named_children = node:named_child_count(),
},
plugin = {
node_actions = M.node_actions,
},
}))
end
return M

View file

@ -3,21 +3,24 @@ local M = {}
function M.set(fn)
return function(...)
local args = { ... }
local nargs = select('#', ...)
local args = { ... }
local nargs = select("#", ...)
vim.go.operatorfunc = "v:lua.require'ts-node-action.repeat'.repeat_action"
M.repeat_action = function()
fn(unpack(args, 1, nargs))
local action = vim.api.nvim_replace_termcodes(
string.format('<cmd>call %s()<cr>', vim.go.operatorfunc), true, true, true
string.format("<cmd>call %s()<cr>", vim.go.operatorfunc),
true,
true,
true
)
pcall(vim.fn["repeat#set"], action, -1)
end
vim.cmd('normal! g@l')
vim.cmd("normal! g@l")
end
end

View file

@ -28,29 +28,23 @@ end)
describe("array", function()
it("expands single line array to multiple lines", function()
assert.are.same(
{
"[",
" 1,",
" 2,",
" 3",
"]"
},
Helper:call({ "[1, 2, 3]" })
)
assert.are.same({
"[",
" 1,",
" 2,",
" 3",
"]",
}, Helper:call({ "[1, 2, 3]" }))
end)
it("doesn't expand child arrays", function()
assert.are.same(
{
"[",
" 1,",
" 2,",
" [3, 4, 5]",
"]"
},
Helper:call({ "[1, 2, [3, 4, 5]]" })
)
assert.are.same({
"[",
" 1,",
" 2,",
" [3, 4, 5]",
"]",
}, Helper:call({ "[1, 2, [3, 4, 5]]" }))
end)
it("collapses multi-line array to single line", function()
@ -61,7 +55,7 @@ describe("array", function()
" 1,",
" 2,",
" 3",
"]"
"]",
})
)
end)
@ -78,7 +72,7 @@ describe("array", function()
" 4,",
" 5",
" ]",
"]"
"]",
})
)
end)

View file

@ -3,137 +3,152 @@ dofile("spec/spec_helper.lua")
local Helper = SpecHelper.new("julia", { shiftwidth = 2 })
describe("boolean", function()
it("toggles 'true' into 'false'", function()
assert.are.same({ "i = true" }, Helper:call({ "i = false" }, { 1, 6 }))
end)
it("toggles 'true' into 'false'", function()
assert.are.same({ "i = true" }, Helper:call({ "i = false" }, { 1, 6 }))
end)
it("toggles 'false' into 'true'", function()
assert.are.same({ "i = false" }, Helper:call({ "i = true" }, { 1, 6 }))
end)
it("toggles 'false' into 'true'", function()
assert.are.same({ "i = false" }, Helper:call({ "i = true" }, { 1, 6 }))
end)
end)
describe("operator", function()
it("toggles '<= into '>='", function()
assert.are.same({ "i <= 8" }, Helper:call({ "i >= 8" }, { 1, 3 }))
end)
it("toggles '<= into '>='", function()
assert.are.same({ "i <= 8" }, Helper:call({ "i >= 8" }, { 1, 3 }))
end)
it("toggles '>=' into '<='", function()
assert.are.same({ "i >= 8" }, Helper:call({ "i <= 8" }, { 1, 3 }))
end)
it("toggles '>=' into '<='", function()
assert.are.same({ "i >= 8" }, Helper:call({ "i <= 8" }, { 1, 3 }))
end)
it("toggles '>' into '<'", function()
assert.are.same({ "i > 8" }, Helper:call({ "i < 8" }, { 1, 3 }))
end)
it("toggles '>' into '<'", function()
assert.are.same({ "i > 8" }, Helper:call({ "i < 8" }, { 1, 3 }))
end)
it("toggles '<' into '>'", function()
assert.are.same({ "i < 8" }, Helper:call({ "i > 8" }, { 1, 3 }))
end)
it("toggles '<' into '>'", function()
assert.are.same({ "i < 8" }, Helper:call({ "i > 8" }, { 1, 3 }))
end)
it("toggles '∉' into '∈'", function()
assert.are.same({ "i ∈ 8" }, Helper:call({ "i ∉ 8" }, { 1, 3 }))
end)
it("toggles '∉' into '∈'", function()
assert.are.same({ "i ∈ 8" }, Helper:call({ "i ∉ 8" }, { 1, 3 }))
end)
it("toggles '∈' into '∉'", function()
assert.are.same({ "i ∈ 8" }, Helper:call({ "i ∉ 8" }, { 1, 3 }))
end)
it("toggles '∈' into '∉'", function()
assert.are.same({ "i ∈ 8" }, Helper:call({ "i ∉ 8" }, { 1, 3 }))
end)
it("toggles '+' into '-'", function()
assert.are.same({ "i + 8" }, Helper:call({ "i - 8" }, { 1, 3 }))
end)
it("toggles '+' into '-'", function()
assert.are.same({ "i + 8" }, Helper:call({ "i - 8" }, { 1, 3 }))
end)
it("toggles '*' into '/'", function()
assert.are.same({ "i * 8" }, Helper:call({ "i / 8" }, { 1, 3 }))
end)
it("toggles '*' into '/'", function()
assert.are.same({ "i * 8" }, Helper:call({ "i / 8" }, { 1, 3 }))
end)
it("toggles '==' into '!='", function()
assert.are.same({ "i == 8" }, Helper:call({ "i != 8" }, { 1, 3 }))
end)
it("toggles '==' into '!='", function()
assert.are.same({ "i == 8" }, Helper:call({ "i != 8" }, { 1, 3 }))
end)
it("toggles '!=' into '=='", function()
assert.are.same({ "i != 8" }, Helper:call({ "i == 8" }, { 1, 3 }))
end)
it("toggles '!=' into '=='", function()
assert.are.same({ "i != 8" }, Helper:call({ "i == 8" }, { 1, 3 }))
end)
end)
describe("expand/collapse", function()
it("expands vector_expression", function()
assert.are.same({ "x = [1, 2]" }, Helper:call({
"x = [",
' 1,',
' 2',
']' }, { 1, 5 }))
end)
it("expands vector_expression", function()
assert.are.same(
{ "x = [1, 2]" },
Helper:call({
"x = [",
" 1,",
" 2",
"]",
}, { 1, 5 })
)
end)
it("collapses vector_expression", function()
assert.are.same({ "x = [",
' 1,',
' 2',
']' },
Helper:call({ "x = [1, 2]" },
{ 1, 5 }))
end)
it("collapses vector_expression", function()
assert.are.same(
{ "x = [", " 1,", " 2", "]" },
Helper:call({ "x = [1, 2]" }, { 1, 5 })
)
end)
it("expands function definition", function()
assert.are.same({ "fn(1, 2; x=true)" }, Helper:call({
"fn(",
' 1,',
' 2;',
' x=true',
')' }, { 1, 3 }))
end)
it("expands function definition", function()
assert.are.same(
{ "fn(1, 2; x=true)" },
Helper:call({
"fn(",
" 1,",
" 2;",
" x=true",
")",
}, { 1, 3 })
)
end)
it("collapses function definition", function()
assert.are.same({ "fn(",
' 1,',
' 2;',
' x=true',
')' },
Helper:call({ "fn(1, 2; x=true)" },
{ 1, 3 }))
end)
it("collapses function definition", function()
assert.are.same(
{ "fn(", " 1,", " 2;", " x=true", ")" },
Helper:call({ "fn(1, 2; x=true)" }, { 1, 3 })
)
end)
it("expands tuple_expression", function()
assert.are.same({ "x = (1, 2)" }, Helper:call({
"x = (",
' 1,',
' 2',
')' }, { 1, 5 }))
end)
it("expands tuple_expression", function()
assert.are.same(
{ "x = (1, 2)" },
Helper:call({
"x = (",
" 1,",
" 2",
")",
}, { 1, 5 })
)
end)
it("collapses tuple_expression", function()
assert.are.same({ "x = (",
' 1,',
' 2',
')' },
Helper:call({ "x = (1, 2)" },
{ 1, 5 }))
end)
it("collapses tuple_expression", function()
assert.are.same(
{ "x = (", " 1,", " 2", ")" },
Helper:call({ "x = (1, 2)" }, { 1, 5 })
)
end)
it("expands dict", function()
assert.are.same({ 'Dict("key1"=>1, "key2"=>2)' }, Helper:call({
"Dict(",
' "key1"=>1,',
' "key2"=>2',
')' }, { 1, 5 }))
end)
it("expands dict", function()
assert.are.same(
{ 'Dict("key1"=>1, "key2"=>2)' },
Helper:call({
"Dict(",
' "key1"=>1,',
' "key2"=>2',
")",
}, { 1, 5 })
)
end)
it("collapses dict", function()
assert.are.same({ "Dict(",
' "key1"=>1,',
' "key2"=>2',
')' },
Helper:call({ 'Dict("key1"=>1, "key2"=>2)' },
{ 1, 5 }))
end)
it("collapses dict", function()
assert.are.same(
{ "Dict(", ' "key1"=>1,', ' "key2"=>2', ")" },
Helper:call({ 'Dict("key1"=>1, "key2"=>2)' }, { 1, 5 })
)
end)
end)
describe("friendly integers", function()
it("1 million to friendly", function()
assert.are.same({ "x = 1000000" }, Helper:call({
"x = 1_000_000",}, { 1, 5 }))
end)
it("1 million to friendly", function()
assert.are.same(
{ "x = 1000000" },
Helper:call({
"x = 1_000_000",
}, { 1, 5 })
)
end)
it("1 million to unfriendly", function()
assert.are.same({ "x = 1_000_000" }, Helper:call({
"x = 1000000",}, { 1, 5 }))
end)
it("1 million to unfriendly", function()
assert.are.same(
{ "x = 1_000_000" },
Helper:call({
"x = 1000000",
}, { 1, 5 })
)
end)
end)

View file

@ -4,26 +4,29 @@ local Helper = SpecHelper.new("lua", { shiftwidth = 4 })
describe("boolean", function()
it("turns 'true' into 'false'", function()
assert.are.same({ "local bool = false" }, Helper:call({ "local bool = true" }, { 1, 14 }))
assert.are.same(
{ "local bool = false" },
Helper:call({ "local bool = true" }, { 1, 14 })
)
end)
it("turns 'false' into 'true'", function()
assert.are.same({ "local bool = true" }, Helper:call({ "local bool = false" }, { 1, 14 }))
assert.are.same(
{ "local bool = true" },
Helper:call({ "local bool = false" }, { 1, 14 })
)
end)
end)
describe("table_constructor", function()
it("expands single line table to multiple lines", function()
assert.are.same(
{
"{",
" 1,",
" 2,",
" 3",
"}"
},
Helper:call({ "{ 1, 2, 3 }" })
)
assert.are.same({
"{",
" 1,",
" 2,",
" 3",
"}",
}, Helper:call({ "{ 1, 2, 3 }" }))
end)
it("collapses multi line table to single lines", function()
@ -34,22 +37,19 @@ describe("table_constructor", function()
" 1,",
" 2,",
" 3",
"}"
"}",
})
)
end)
it("expands single line table to multiple lines", function()
assert.are.same(
{
"{",
" a = 1,",
" b = 2,",
" ['c'] = 3",
"}"
},
Helper:call({ "{ a = 1, b = 2, ['c'] = 3 }" })
)
assert.are.same({
"{",
" a = 1,",
" b = 2,",
" ['c'] = 3",
"}",
}, Helper:call({ "{ a = 1, b = 2, ['c'] = 3 }" }))
end)
it("collapses multi line table to single lines", function()
@ -60,7 +60,7 @@ describe("table_constructor", function()
" a = 1,",
" b = 2,",
" ['c'] = 3",
"}"
"}",
})
)
end)
@ -73,20 +73,20 @@ describe("function_definition (anon)", function()
Helper:call({
"local a = function(a, b, c)",
" return 1",
"end"
"end",
}, { 1, 11 })
)
end)
it("expands single-line function to multi-line", function()
assert.are.same(
{
"local a = function(a, b, c)",
" return 1",
"end"
},
Helper:call({ "local a = function(a, b, c) return 1 end" }, { 1, 11 })
)
assert.are.same({
"local a = function(a, b, c)",
" return 1",
"end",
}, Helper:call(
{ "local a = function(a, b, c) return 1 end" },
{ 1, 11 }
))
end)
it("doesn't collapse function with multi-line body", function()
@ -94,32 +94,29 @@ describe("function_definition (anon)", function()
"local a = function(a, b, c)",
" local d = a + b + c",
" return d",
"end"
"end",
}
assert.are.same(text, Helper:call(text, { 1, 11 }))
end)
it("expands single-line function to multi-line (no-body)", function()
assert.are.same(
{
"local a = function(a, b, c)",
"",
"end"
},
Helper:call({ "local a = function(a, b, c) end" }, { 1, 11 })
)
assert.are.same({
"local a = function(a, b, c)",
"",
"end",
}, Helper:call({ "local a = function(a, b, c) end" }, { 1, 11 }))
end)
it("collapses multi-line function to single-line (no-body)", function()
assert.are.same(
{
"local a = function(a, b, c) end"
"local a = function(a, b, c) end",
},
Helper:call({
"local a = function(a, b, c)",
"",
"end"
"end",
}, { 1, 11 })
)
end)
@ -132,20 +129,17 @@ describe("function_declaration (named)", function()
Helper:call({
"local function a(a, b, c)",
" return 1",
"end"
"end",
}, { 1, 11 })
)
end)
it("expands single-line function to multi-line", function()
assert.are.same(
{
"local function a(a, b, c)",
" return 1",
"end"
},
Helper:call({ "local function a(a, b, c) return 1 end" }, { 1, 11 })
)
assert.are.same({
"local function a(a, b, c)",
" return 1",
"end",
}, Helper:call({ "local function a(a, b, c) return 1 end" }, { 1, 11 }))
end)
it("doesn't collapse function with multi-line body", function()
@ -153,7 +147,7 @@ describe("function_declaration (named)", function()
"local function a(a, b, c)",
" local d = a + b + c",
" return d",
"end"
"end",
}
assert.are.same(text, Helper:call(text, { 1, 11 }))

View file

@ -3,7 +3,6 @@ dofile("./spec/spec_helper.lua")
local Helper = SpecHelper.new("python", { shiftwidth = 4 })
describe("comparison_operator", function()
it("toggles operator in multiline context", function()
assert.are.same(
{
@ -17,8 +16,7 @@ describe("comparison_operator", function()
[[ foo(x,]],
[[ y)):]],
[[ x = 1]],
}, {1, 9})
}, { 1, 9 })
)
end)
end)

View file

@ -3,53 +3,40 @@ dofile("./spec/spec_helper.lua")
local Helper = SpecHelper.new("python", { shiftwidth = 4 })
describe("conditional_expression", function()
it("expands with a single assignment", function()
assert.are.same(
{
[[if foo(y):]],
[[ x = 1]],
[[else:]],
[[ x = 2]],
},
Helper:call({ [[x = 1 if foo(y) else 2]] }, { 1, 7 })
)
assert.are.same({
[[if foo(y):]],
[[ x = 1]],
[[else:]],
[[ x = 2]],
}, Helper:call({ [[x = 1 if foo(y) else 2]] }, { 1, 7 }))
end)
it("expands with a multi assignment", function()
assert.are.same(
{
[[if foo(y):]],
[[ x = y = z = 1]],
[[else:]],
[[ x = y = z = 2]],
},
Helper:call({ [[x = y = z = 1 if foo(y) else 2]] }, { 1, 15 })
)
assert.are.same({
[[if foo(y):]],
[[ x = y = z = 1]],
[[else:]],
[[ x = y = z = 2]],
}, Helper:call({ [[x = y = z = 1 if foo(y) else 2]] }, { 1, 15 }))
end)
it("expands with a return", function()
assert.are.same(
{
[[if foo(y):]],
[[ return 1]],
[[else:]],
[[ return 2]],
},
Helper:call({ [[return 1 if foo(y) else 2]] }, { 1, 10 })
)
assert.are.same({
[[if foo(y):]],
[[ return 1]],
[[else:]],
[[ return 2]],
}, Helper:call({ [[return 1 if foo(y) else 2]] }, { 1, 10 }))
end)
it("expands with function calls", function()
assert.are.same(
{
[[if foo(y):]],
[[ bar()]],
[[else:]],
[[ baz()]],
},
Helper:call({ [[bar() if foo(y) else baz()]] }, { 1, 7 })
)
assert.are.same({
[[if foo(y):]],
[[ bar()]],
[[else:]],
[[ baz()]],
}, Helper:call({ [[bar() if foo(y) else baz()]] }, { 1, 7 }))
end)
it("expands with both parenthesized lambda expr", function()
@ -67,12 +54,15 @@ describe("conditional_expression", function()
)
end)
it("doesn't expand with a bare consequence lambda expr ('if' is inside lambda)", function()
local text = {
[[x = lambda y: y + 1 if z is not None else lambda y: y - 1]]
}
assert.are.same(text, Helper:call(text, { 1, 23 }))
end)
it(
"doesn't expand with a bare consequence lambda expr ('if' is inside lambda)",
function()
local text = {
[[x = lambda y: y + 1 if z is not None else lambda y: y - 1]],
}
assert.are.same(text, Helper:call(text, { 1, 23 }))
end
)
it("expands with a parenthesized consequence lambda expr", function()
assert.are.same(
@ -129,15 +119,12 @@ describe("conditional_expression", function()
[[else:]],
[[ y = x = (2 or 4)]],
},
Helper:call(
{
[[y = x = (1 or]],
[[ 3) if (foo() > 100 or]],
[[ foo() < 200) else (2 or]],
[[ 4)]],
},
{ 2, 13 }
)
Helper:call({
[[y = x = (1 or]],
[[ 3) if (foo() > 100 or]],
[[ foo() < 200) else (2 or]],
[[ 4)]],
}, { 2, 13 })
)
end)
@ -149,21 +136,18 @@ describe("conditional_expression", function()
[[else:]],
[[ return {4, 5, 6}]],
},
Helper:call(
{
[[return []],
[[ 3,]],
[[ 4,]],
[[ 5]],
[[] if foo(x,]],
[[ y) else {]],
[[ 4,]],
[[ 5,]],
[[ 6]],
[[}]],
},
{ 5, 3 }
)
Helper:call({
[[return []],
[[ 3,]],
[[ 4,]],
[[ 5]],
[[] if foo(x,]],
[[ y) else {]],
[[ 4,]],
[[ 5,]],
[[ 6]],
[[}]],
}, { 5, 3 })
)
end)
@ -175,21 +159,18 @@ describe("conditional_expression", function()
[[else:]],
[[ return {4, 5, 6} # j]],
},
Helper:call(
{
[[return []],
[[ 3,]],
[[ 4,]],
[[ 5]],
[[] if foo(x,]],
[[ y) else {]],
[[ 4,]],
[[ 5,]],
[[ 6]],
[[} # j]],
},
{ 5, 3 }
)
Helper:call({
[[return []],
[[ 3,]],
[[ 4,]],
[[ 5]],
[[] if foo(x,]],
[[ y) else {]],
[[ 4,]],
[[ 5,]],
[[ 6]],
[[} # j]],
}, { 5, 3 })
)
end)
@ -209,7 +190,6 @@ describe("conditional_expression", function()
assert.are.same(text, Helper:call(text, { 5, 3 }))
end)
it("doesn't expand a condition inside a fn call", function()
local text = {
[[foo("param1", 4 if foo() > 100 else 5)]],
@ -237,5 +217,4 @@ describe("conditional_expression", function()
}
assert.are.same(text, Helper:call(text, { 1, 16 }))
end)
end)

View file

@ -6,26 +6,28 @@ describe("conditional padding", function()
it("checking 'is not'", function()
assert.are.same(
{
[[x = 1 if y is not None and foo() > 100 else 2]]
[[x = 1 if y is not None and foo() > 100 else 2]],
},
Helper:call({
'if y is not None and foo() > 100:',
' x = 1',
'else:',
' x = 2',
"if y is not None and foo() > 100:",
" x = 1",
"else:",
" x = 2",
})
)
end)
it("checking unary and binary '-' operator", function()
assert.are.same(
{ "xs = [x for x in range(10) if x + -3 or -x and x - 3 == 0 and abs(x - 1) < 2]" },
{
"xs = [x for x in range(10) if x + -3 or -x and x - 3 == 0 and abs(x - 1) < 2]",
},
Helper:call({
"xs = [",
" x",
" for x in range(10)",
" if x + -3 or -x and x - 3 == 0 and abs(x - 1) < 2",
"]"
"]",
}, { 1, 6 })
)
end)
@ -36,7 +38,7 @@ describe("conditional padding", function()
Helper:call({
"print(",
" 5 not in list1",
")"
")",
}, { 1, 6 })
)
end)

View file

@ -3,7 +3,6 @@ dofile("./spec/spec_helper.lua")
local Helper = SpecHelper.new("python", { shiftwidth = 4 })
describe("if_statement", function()
it("if/else inlines with a single assignment", function()
assert.are.same(
{ [[x = 1 if foo(y) else 2]] },
@ -28,26 +27,31 @@ describe("if_statement", function()
)
end)
it("if/else doesn't inline a multi assignment when identifiers differ (consequence)", function()
local text = {
[[if foo(a):]],
[[ c = y = z = 1]],
[[else:]],
[[ x = y = z = 2]],
}
assert.are.same(text, text)
end)
it("if/else doesn't inline a multi assignment when identifiers differ (alternative)", function()
local text = {
[[if foo(a):]],
[[ x = y = z = 1]],
[[else:]],
[[ x = y = c = 2]],
}
assert.are.same(text, text)
end)
it(
"if/else doesn't inline a multi assignment when identifiers differ (consequence)",
function()
local text = {
[[if foo(a):]],
[[ c = y = z = 1]],
[[else:]],
[[ x = y = z = 2]],
}
assert.are.same(text, text)
end
)
it(
"if/else doesn't inline a multi assignment when identifiers differ (alternative)",
function()
local text = {
[[if foo(a):]],
[[ x = y = z = 1]],
[[else:]],
[[ x = y = c = 2]],
}
assert.are.same(text, text)
end
)
it("if/else inlines with a return", function()
assert.are.same(
@ -109,25 +113,29 @@ describe("if_statement", function()
)
end)
it("if/else inlines with bare conditional_expression (auto parens)", function()
assert.are.same(
{ [[x = (3 if a else 4) if z is not None else (5 if b else 6)]] },
Helper:call({
[[if z is not None:]],
[[ x = 3 if a else 4]],
[[else:]],
[[ x = 5 if b else 6]],
})
)
end)
it(
"if/else inlines with bare conditional_expression (auto parens)",
function()
assert.are.same(
{ [[x = (3 if a else 4) if z is not None else (5 if b else 6)]] },
Helper:call({
[[if z is not None:]],
[[ x = 3 if a else 4]],
[[else:]],
[[ x = 5 if b else 6]],
})
)
end
)
it("if/else inlines with multiline parenthized fn args, boolean op, structures", function()
assert.are.same(
{
[=[y = x = [1, 3] if (foo(a, b) > 100 or foo(c, d) < 200) else (False or True)]=]
},
Helper:call(
it(
"if/else inlines with multiline parenthized fn args, boolean op, structures",
function()
assert.are.same(
{
[=[y = x = [1, 3] if (foo(a, b) > 100 or foo(c, d) < 200) else (False or True)]=],
},
Helper:call({
[[if (foo(a,]],
[[ b) > 100 or]],
[[ foo(c,]],
@ -137,10 +145,10 @@ describe("if_statement", function()
[[else:]],
[[ y = x = (False or]],
[[ True)]],
}
})
)
)
end)
end
)
it("if inlines with a single assignment", function()
assert.are.same(
@ -237,7 +245,7 @@ describe("if_statement", function()
[[ x = 1]],
[[else:]],
[[ # comment]],
[[ x = 2]]
[[ x = 2]],
}
assert.are.same(text, Helper:call(text))
end)
@ -250,5 +258,4 @@ describe("if_statement", function()
}
assert.are.same(text, Helper:call(text))
end)
end)

View file

@ -3,59 +3,59 @@ dofile("spec/spec_helper.lua")
local Helper = SpecHelper.new("r", { shiftwidth = 2 })
describe("boolean", function()
it("turns 'TRUE' into 'FALSE'", function()
assert.are.same({ "i == FALSE" }, Helper:call({ "i == TRUE" }, { 1, 7 }))
end)
it("turns 'TRUE' into 'FALSE'", function()
assert.are.same({ "i == FALSE" }, Helper:call({ "i == TRUE" }, { 1, 7 }))
end)
it("turns 'FALSE' into 'TRUE'", function()
assert.are.same({ "i == TRUE" }, Helper:call({ "i == FALSE" }, { 1, 7 }))
end)
it("turns 'FALSE' into 'TRUE'", function()
assert.are.same({ "i == TRUE" }, Helper:call({ "i == FALSE" }, { 1, 7 }))
end)
end)
describe("multiline", function()
it("expand single line formal parameters to multiline", function()
assert.are.same({
"foo <- function(",
" bar,",
" baz",
")",
}, Helper:call({ "foo <- function(bar, baz)" }, { 1, 16 }))
end)
it("expand single line formal parameters to multiline", function()
assert.are.same({
"foo <- function(",
" bar,",
" baz",
")",
}, Helper:call({ "foo <- function(bar, baz)" }, { 1, 16 }))
end)
it("collapse multiline formal parameters to single line", function()
assert.are.same(
{ "foo <- function(bar, baz)" },
Helper:call({
"foo <- function(",
" bar,",
" baz",
")",
}, { 1, 16 })
)
end)
it("collapse multiline formal parameters to single line", function()
assert.are.same(
{ "foo <- function(bar, baz)" },
Helper:call({
"foo <- function(",
" bar,",
" baz",
")",
}, { 1, 16 })
)
end)
end)
describe("multiline_args", function()
it("expand single line arguments to multiline", function()
assert.are.same({
"foo(",
" bar = buf,",
" 'baz',",
" 'bap'",
")",
}, Helper:call({ "foo(bar = buf, 'baz', 'bap')" }, { 1, 4 }))
end)
it("expand single line arguments to multiline", function()
assert.are.same({
"foo(",
" bar = buf,",
" 'baz',",
" 'bap'",
")",
}, Helper:call({ "foo(bar = buf, 'baz', 'bap')" }, { 1, 4 }))
end)
it("collapse multiline arguments to single line", function()
assert.are.same(
{ "foo(bar = buf, 'baz', 'bap')" },
Helper:call({
"foo(",
" bar = buf,",
" 'baz',",
" 'bap'",
" )",
}, { 1, 4 })
)
end)
it("collapse multiline arguments to single line", function()
assert.are.same(
{ "foo(bar = buf, 'baz', 'bap')" },
Helper:call({
"foo(",
" bar = buf,",
" 'baz',",
" 'bap'",
" )",
}, { 1, 4 })
)
end)
end)

View file

@ -18,44 +18,39 @@ end)
describe("if", function()
it("expands ternary to multiline expression", function()
assert.are.same(
{
[[if greet?]],
[[ puts("hello")]],
[[else]],
[[ puts("booooo")]],
[[end]],
},
Helper:call({ [[greet? ? puts("hello") : puts("booooo")]] }, { 1, 7 })
)
assert.are.same({
[[if greet?]],
[[ puts("hello")]],
[[else]],
[[ puts("booooo")]],
[[end]],
}, Helper:call(
{ [[greet? ? puts("hello") : puts("booooo")]] },
{ 1, 7 }
))
end)
it("inlines to ternary statement", function()
assert.are.same(
{ [[greet? ? puts("hello") : puts("booooo")]] },
Helper:call(
{
[[if greet?]],
[[ puts("hello")]],
[[else]],
[[ puts("booooo")]],
[[end]],
}
)
Helper:call({
[[if greet?]],
[[ puts("hello")]],
[[else]],
[[ puts("booooo")]],
[[end]],
})
)
end)
end)
describe("if_modifier", function()
it("expands from one line to three", function()
assert.are.same(
{
[[if greet?]],
[[ puts "hello"]],
[[end]],
},
Helper:call({ [[puts "hello" if greet?]], }, { 1, 13 })
)
assert.are.same({
[[if greet?]],
[[ puts "hello"]],
[[end]],
}, Helper:call({ [[puts "hello" if greet?]] }, { 1, 13 }))
end)
it("collapses from three lines to one", function()
@ -76,7 +71,10 @@ describe("if_modifier", function()
[[ puts "hello"]],
[[end]],
},
Helper:call({ [[puts "hello" if greet? && 1 == 2 || something * 3 <= 10]], }, { 1, 13 })
Helper:call(
{ [[puts "hello" if greet? && 1 == 2 || something * 3 <= 10]] },
{ 1, 13 }
)
)
end)
@ -95,14 +93,11 @@ end)
describe("unless_modifier", function()
it("expands from one line to three", function()
assert.are.same(
{
[[unless rude?]],
[[ puts "hello"]],
[[end]],
},
Helper:call({ [[puts "hello" unless rude?]] }, { 1, 13 })
)
assert.are.same({
[[unless rude?]],
[[ puts "hello"]],
[[end]],
}, Helper:call({ [[puts "hello" unless rude?]] }, { 1, 13 }))
end)
it("collapses from three lines to one", function()
@ -123,7 +118,10 @@ describe("unless_modifier", function()
[[ puts "hello"]],
[[end]],
},
Helper:call({ [[puts "hello" unless rude? && 1 == 2 || something * 3 <= 10]], }, { 1, 13 })
Helper:call(
{ [[puts "hello" unless rude? && 1 == 2 || something * 3 <= 10]] },
{ 1, 13 }
)
)
end)
end)
@ -166,29 +164,23 @@ end)
describe("array", function()
it("expands single line array to multiple lines", function()
assert.are.same(
{
"[",
" 1,",
" 2,",
" 3",
"]"
},
Helper:call({ "[1, 2, 3]" })
)
assert.are.same({
"[",
" 1,",
" 2,",
" 3",
"]",
}, Helper:call({ "[1, 2, 3]" }))
end)
it("doesn't expand child arrays", function()
assert.are.same(
{
"[",
" 1,",
" 2,",
" [3, 4, 5]",
"]"
},
Helper:call({ "[1, 2, [3, 4, 5]]" })
)
assert.are.same({
"[",
" 1,",
" 2,",
" [3, 4, 5]",
"]",
}, Helper:call({ "[1, 2, [3, 4, 5]]" }))
end)
it("collapses multi-line array to single line", function()
@ -199,7 +191,7 @@ describe("array", function()
" 1,",
" 2,",
" 3",
"]"
"]",
})
)
end)
@ -216,7 +208,7 @@ describe("array", function()
" 4,",
" 5",
" ]",
"]"
"]",
})
)
end)
@ -231,7 +223,7 @@ describe("array", function()
"and one more line",
"=end",
" 3 # d",
"]"
"]",
}
assert.are.same(text, Helper:call(text))
@ -251,7 +243,7 @@ describe("array", function()
"=end",
" 5 # d",
" ]",
"]"
"]",
}
assert.are.same(text, Helper:call(text))
end)
@ -261,25 +253,21 @@ describe("array", function()
"[",
" 1, # no comment",
" 2,",
"]"
"]",
}
assert.are.same(text, Helper:call(text))
end)
end)
describe("hash", function()
it("expands single line hash to multiple lines", function()
assert.are.same(
{
"{",
" a: 1,",
" b: 2,",
" c: 3",
"}"
},
Helper:call({ "{ a: 1, b: 2, c: 3 }" })
)
assert.are.same({
"{",
" a: 1,",
" b: 2,",
" c: 3",
"}",
}, Helper:call({ "{ a: 1, b: 2, c: 3 }" }))
end)
it("collapses multi-line hash to single lines", function()
@ -290,22 +278,19 @@ describe("hash", function()
" a: 1,",
" b: 2,",
" c: 3",
"}"
"}",
})
)
end)
it("doesn't expand children", function()
assert.are.same(
{
"{",
" a: 1,",
" b: ['foo', 'bar'],",
" c: { d: 3, e: 4 }",
"}"
},
Helper:call({ "{ a: 1, b: ['foo', 'bar'], c: { d: 3, e: 4 } }" })
)
assert.are.same({
"{",
" a: 1,",
" b: ['foo', 'bar'],",
" c: { d: 3, e: 4 }",
"}",
}, Helper:call({ "{ a: 1, b: ['foo', 'bar'], c: { d: 3, e: 4 } }" }))
end)
it("collapses nested children", function()
@ -322,7 +307,7 @@ describe("hash", function()
" d: 3,",
" e: 4",
" }",
"}"
"}",
})
)
end)
@ -335,7 +320,7 @@ describe("block", function()
Helper:call({
"[1, 2, 3].each do |n|",
" print n",
"end"
"end",
}, { 1, 16 })
)
end)
@ -346,71 +331,65 @@ describe("block", function()
Helper:call({
"[1, 2, 3].each do",
" print n",
"end"
"end",
}, { 1, 16 })
)
end)
it("collapses a multi-line block into one line (with destructured param)", function()
assert.are.same(
{ "[1, 2, 3].each { |(a, b), c| print n }" },
Helper:call({
"[1, 2, 3].each do |(a, b), c|",
" print n",
"end"
}, { 1, 16 })
)
end)
it(
"collapses a multi-line block into one line (with destructured param)",
function()
assert.are.same(
{ "[1, 2, 3].each { |(a, b), c| print n }" },
Helper:call({
"[1, 2, 3].each do |(a, b), c|",
" print n",
"end",
}, { 1, 16 })
)
end
)
end)
describe("do_block", function()
it("expands a single-line block into multi line (with param)", function()
assert.are.same(
{
"[1, 2, 3].each do |n|",
" print n",
"end"
},
Helper:call({ "[1, 2, 3].each { |n| print n }" }, { 1, 16 })
)
assert.are.same({
"[1, 2, 3].each do |n|",
" print n",
"end",
}, Helper:call({ "[1, 2, 3].each { |n| print n }" }, { 1, 16 }))
end)
it("expands a single-line block into multi line (without param)", function()
assert.are.same(
{
"[1, 2, 3].each do",
" print n",
"end"
},
Helper:call({ "[1, 2, 3].each { print n }" }, { 1, 16 })
)
assert.are.same({
"[1, 2, 3].each do",
" print n",
"end",
}, Helper:call({ "[1, 2, 3].each { print n }" }, { 1, 16 }))
end)
it("expands a single-line block into multi line (with destructured param)", function()
assert.are.same(
{
it(
"expands a single-line block into multi line (with destructured param)",
function()
assert.are.same({
"[1, 2, 3].each do |(a, b), c|",
" print n",
"end"
},
Helper:call({ "[1, 2, 3].each { |(a, b), c| print n }" }, { 1, 16 })
)
end)
"end",
}, Helper:call(
{ "[1, 2, 3].each { |(a, b), c| print n }" },
{ 1, 16 }
))
end
)
end)
describe("pair", function()
it("converts old style hashes into new style", function()
assert.are.same(
{ "{ a: 1 }" },
Helper:call({ "{ :a => 1 }" }, { 1, 6 })
)
assert.are.same({ "{ a: 1 }" }, Helper:call({ "{ :a => 1 }" }, { 1, 6 }))
end)
it("converts new style hashes into old style", function()
assert.are.same(
{ "{ :a => 1 }" },
Helper:call({ "{ a: 1 }" }, { 1, 4 })
)
assert.are.same({ "{ :a => 1 }" }, Helper:call({ "{ a: 1 }" }, { 1, 4 }))
end)
it("doesn't change non-string/symbol keys", function()
@ -423,70 +402,52 @@ end)
describe("argument_list", function()
it("expands one positional arg", function()
assert.are.same(
{
"call(",
" a",
")"
},
Helper:call({ "call(a)" }, { 1, 5 })
)
assert.are.same({
"call(",
" a",
")",
}, Helper:call({ "call(a)" }, { 1, 5 }))
end)
it("expands multiple positional args", function()
assert.are.same(
{
"call(",
" a,",
" b",
")"
},
Helper:call({ "call(a, b)" }, { 1, 5 })
)
assert.are.same({
"call(",
" a,",
" b",
")",
}, Helper:call({ "call(a, b)" }, { 1, 5 }))
end)
it("expands one keyword arg with explicit value", function()
assert.are.same(
{
"call(",
[[ arg: "something"]],
")"
},
Helper:call({ [[call(arg: "something")]] }, { 1, 5 })
)
assert.are.same({
"call(",
[[ arg: "something"]],
")",
}, Helper:call({ [[call(arg: "something")]] }, { 1, 5 }))
end)
it("expands keyword arg with implicit value", function()
assert.are.same(
{
"call(",
" arg:",
")"
},
Helper:call({ [[call(arg:)]] }, { 1, 5 })
)
assert.are.same({
"call(",
" arg:",
")",
}, Helper:call({ [[call(arg:)]] }, { 1, 5 }))
end)
it("expands with passed block", function()
assert.are.same(
{
"call(",
" &block",
")"
},
Helper:call({ [[call(&block)]] }, { 1, 5 })
)
assert.are.same({
"call(",
" &block",
")",
}, Helper:call({ [[call(&block)]] }, { 1, 5 }))
end)
it("expands with provided block", function()
assert.are.same(
{
"call(",
" arg",
") { |n| puts n }"
},
Helper:call({ [[call(arg) { |n| puts n }]] }, { 1, 5 })
)
assert.are.same({
"call(",
" arg",
") { |n| puts n }",
}, Helper:call({ [[call(arg) { |n| puts n }]] }, { 1, 5 }))
end)
it("expands keyword arg with expression value", function()
@ -494,10 +455,10 @@ describe("argument_list", function()
{
"call(",
" arg: count? ? 1 : 2",
")"
")",
},
Helper:call({
[[call(arg: count? ? 1 : 2)]]
[[call(arg: count? ? 1 : 2)]],
}, { 1, 5 })
)
end)
@ -507,27 +468,24 @@ describe("argument_list", function()
{
"call(",
" count? ? 1 : 2",
")"
")",
},
Helper:call({
[[call(count? ? 1 : 2)]]
[[call(count? ? 1 : 2)]],
}, { 1, 5 })
)
end)
it("expands with a mix of all", function()
assert.are.same(
{
"call(",
" a,",
" b,",
" arg: true,",
" arg2:,",
" &blk",
")"
},
Helper:call({ [[call(a, b, arg: true, arg2:, &blk)]] }, { 1, 5 })
)
assert.are.same({
"call(",
" a,",
" b,",
" arg: true,",
" arg2:,",
" &blk",
")",
}, Helper:call({ [[call(a, b, arg: true, arg2:, &blk)]] }, { 1, 5 }))
end)
it("collapses one positional arg", function()
@ -536,7 +494,7 @@ describe("argument_list", function()
Helper:call({
"call(",
" a",
")"
")",
}, { 1, 5 })
)
end)
@ -548,7 +506,7 @@ describe("argument_list", function()
"call(",
" a,",
" b",
")"
")",
}, { 1, 5 })
)
end)
@ -559,7 +517,7 @@ describe("argument_list", function()
Helper:call({
"call(",
[[ arg: "something"]],
")"
")",
}, { 1, 5 })
)
end)
@ -570,7 +528,7 @@ describe("argument_list", function()
Helper:call({
"call(",
" arg:",
")"
")",
}, { 1, 5 })
)
end)
@ -581,7 +539,7 @@ describe("argument_list", function()
Helper:call({
"call(",
" arg: count? ? 1 : 2",
")"
")",
}, { 1, 5 })
)
end)
@ -592,7 +550,7 @@ describe("argument_list", function()
Helper:call({
"call(",
" &block",
")"
")",
}, { 1, 5 })
)
end)
@ -603,7 +561,7 @@ describe("argument_list", function()
Helper:call({
"call(",
" arg",
") { |n| puts n }"
") { |n| puts n }",
}, { 1, 5 })
)
end)
@ -618,7 +576,7 @@ describe("argument_list", function()
" arg: true,",
" arg2:,",
" &blk",
")"
")",
}, { 1, 5 })
)
end)
@ -626,92 +584,103 @@ end)
describe("method_parameters", function()
it("expands positional argument", function()
assert.are.same({
"def something(",
" a,",
" b,",
" c",
")",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(",
" a,",
" b,",
" c",
")",
" puts a + b + c",
"end",
},
Helper:call({
"def something(a, b, c)",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("expands keyword arg", function()
assert.are.same({
"def something(",
" a:,",
" b:,",
" c:",
")",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(",
" a:,",
" b:,",
" c:",
")",
" puts a + b + c",
"end",
},
Helper:call({
"def something(a:, b:, c:)",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("expands block", function()
assert.are.same({
"def something(",
" &block",
")",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(",
" &block",
")",
" puts a + b + c",
"end",
},
Helper:call({
"def something(&block)",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("expands keyword arg with default value", function()
assert.are.same({
"def something(",
" a: 1,",
" b: 2",
")",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(",
" a: 1,",
" b: 2",
")",
" puts a + b + c",
"end",
},
Helper:call({
"def something(a: 1, b: 2)",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("expands positional arg with default value", function()
assert.are.same({
"def something(",
" a = 1,",
" b = 2",
")",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(",
" a = 1,",
" b = 2",
")",
" puts a + b + c",
"end",
},
Helper:call({
"def something(a = 1, b = 2)",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("collapses positional argument", function()
assert.are.same({
"def something(a, b, c)",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(a, b, c)",
" puts a + b + c",
"end",
},
Helper:call({
"def something(",
" a,",
@ -719,16 +688,18 @@ describe("method_parameters", function()
" c",
")",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("collapses keyword arg", function()
assert.are.same({
"def something(a:, b:, c:)",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(a:, b:, c:)",
" puts a + b + c",
"end",
},
Helper:call({
"def something(",
" a:,",
@ -736,55 +707,62 @@ describe("method_parameters", function()
" c:",
")",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("collapses block", function()
assert.are.same({
"def something(&block)",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(&block)",
" puts a + b + c",
"end",
},
Helper:call({
"def something(",
" &block",
")",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("collapses keyword arg with default value", function()
assert.are.same({
"def something(a: 1, b: 2)",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(a: 1, b: 2)",
" puts a + b + c",
"end",
},
Helper:call({
"def something(",
" a: 1,",
" b: 2",
")",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
it("collapses positional arg with default value", function()
assert.are.same({
"def something(a = 1, b = 2)",
" puts a + b + c",
"end"
},
assert.are.same(
{
"def something(a = 1, b = 2)",
" puts a + b + c",
"end",
},
Helper:call({
"def something(",
" a = 1,",
" b = 2",
")",
" puts a + b + c",
"end"
}, { 1, 14 }))
"end",
}, { 1, 14 })
)
end)
end)

View file

@ -4,121 +4,158 @@ local Helper = SpecHelper.new("sql", { shiftwidth = 2 })
describe("boolean", function()
it("turns 'true' into 'false'", function()
assert.are.same({ "select true" }, Helper:call({ "select false" }, { 1, 8 }))
assert.are.same(
{ "select true" },
Helper:call({ "select false" }, { 1, 8 })
)
end)
it("turns 'false' into 'true'", function()
assert.are.same({ "select false" }, Helper:call({ "select true" }, { 1, 8 }))
assert.are.same(
{ "select false" },
Helper:call({ "select true" }, { 1, 8 })
)
end)
it("turns 'TRUE' into 'FALSE'", function()
assert.are.same({ "select TRUE" }, Helper:call({ "select FALSE" }, { 1, 8 }))
assert.are.same(
{ "select TRUE" },
Helper:call({ "select FALSE" }, { 1, 8 })
)
end)
it("turns 'FALSE' into 'true'", function()
assert.are.same({ "select FALSE" }, Helper:call({ "select TRUE" }, { 1, 8 }))
assert.are.same(
{ "select FALSE" },
Helper:call({ "select TRUE" }, { 1, 8 })
)
end)
end)
describe("operators", function()
it("turns 'and' into 'or'", function()
assert.are.same({ "select a from b where a < 5 and a > 1" },
Helper:call({ "select a from b where a < 5 or a > 1" }, { 1, 29 }))
assert.are.same(
{ "select a from b where a < 5 and a > 1" },
Helper:call({ "select a from b where a < 5 or a > 1" }, { 1, 29 })
)
end)
it("turns '=' into '!='", function()
assert.are.same({ "select a from b where a = 1" },
Helper:call({ "select a from b where a != 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a = 1" },
Helper:call({ "select a from b where a != 1" }, { 1, 25 })
)
end)
it("turns '!=' into '='", function()
assert.are.same({ "select a from b where a != 1" },
Helper:call({ "select a from b where a = 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a != 1" },
Helper:call({ "select a from b where a = 1" }, { 1, 25 })
)
end)
it("turns '<' into '>'", function()
assert.are.same({ "select a from b where a < 1" },
Helper:call({ "select a from b where a > 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a < 1" },
Helper:call({ "select a from b where a > 1" }, { 1, 25 })
)
end)
it("turns '>' into '<'", function()
assert.are.same({ "select a from b where a > 1" },
Helper:call({ "select a from b where a < 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a > 1" },
Helper:call({ "select a from b where a < 1" }, { 1, 25 })
)
end)
it("turns '<=' into '>='", function()
assert.are.same({ "select a from b where a <= 1" },
Helper:call({ "select a from b where a >= 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a <= 1" },
Helper:call({ "select a from b where a >= 1" }, { 1, 25 })
)
end)
it("turns '>=' into '<='", function()
assert.are.same({ "select a from b where a >= 1" },
Helper:call({ "select a from b where a <= 1" }, { 1, 25 }))
assert.are.same(
{ "select a from b where a >= 1" },
Helper:call({ "select a from b where a <= 1" }, { 1, 25 })
)
end)
it("turns '+' into '-'", function()
assert.are.same({ "select a + 1" },
Helper:call({ "select a - 1" }, { 1, 10 }))
assert.are.same(
{ "select a + 1" },
Helper:call({ "select a - 1" }, { 1, 10 })
)
end)
it("turns '-' into '+'", function()
assert.are.same({ "select a - 1" },
Helper:call({ "select a + 1" }, { 1, 10 }))
assert.are.same(
{ "select a - 1" },
Helper:call({ "select a + 1" }, { 1, 10 })
)
end)
it("turns '*' into '/'", function()
assert.are.same({ "select a * 1" },
Helper:call({ "select a / 1" }, { 1, 10 }))
assert.are.same(
{ "select a * 1" },
Helper:call({ "select a / 1" }, { 1, 10 })
)
end)
it("turns '/' into '*'", function()
assert.are.same({ "select a / 1" },
Helper:call({ "select a * 1" }, { 1, 10 }))
assert.are.same(
{ "select a / 1" },
Helper:call({ "select a * 1" }, { 1, 10 })
)
end)
end)
describe("expands and collapses: ", function()
it("Expands select_expression", function()
assert.are.same({ "select a as c1, b as c2" },
assert.are.same(
{ "select a as c1, b as c2" },
Helper:call({
"select a as c1,",
"b as c2"
}, { 1, 15 }))
"b as c2",
}, { 1, 15 })
)
end)
it("Collapses select_expression", function()
assert.are.same({
"select a as c1,",
"b as c2" },
Helper:call({ "select a as c1, b as c2" },
{ 1, 15 }))
"b as c2",
}, Helper:call({ "select a as c1, b as c2" }, { 1, 15 }))
end)
it("Expands select_expression with subquery", function()
assert.are.same({ "select a as c1, (select 1) as sq" },
assert.are.same(
{ "select a as c1, (select 1) as sq" },
Helper:call({
"select a as c1,",
"(select 1) as sq"
}, { 1, 15 }))
"(select 1) as sq",
}, { 1, 15 })
)
end)
it("Collapses select_expression", function()
assert.are.same({
"select a as c1,",
"(select 1) as sq" },
Helper:call({ "select a as c1, (select 1) as sq" },
{ 1, 15 }))
"(select 1) as sq",
}, Helper:call({ "select a as c1, (select 1) as sq" }, { 1, 15 }))
end)
it("Expands column_definition in create table statement", function()
assert.are.same({ "create table tab (a int, b float)" },
assert.are.same(
{ "create table tab (a int, b float)" },
Helper:call({
"create table tab (",
"a int,",
"b float",
")",
}, { 1, 18 }))
}, { 1, 18 })
)
end)
it("Collapses column_definition in create table statement", function()
@ -127,9 +164,6 @@ describe("expands and collapses: ", function()
" a int,",
" b float",
")",
},
Helper:call(
{ "create table tab (a int, b float)" },
{ 1, 18 }))
}, Helper:call({ "create table tab (a int, b float)" }, { 1, 18 }))
end)
end)

View file

@ -3,11 +3,11 @@ dofile("spec/spec_helper.lua")
local Helper = SpecHelper.new("yaml", { shiftwidth = 2 })
describe("boolean", function()
it("turns 'true' into 'false'", function()
assert.are.same({ "key: false" }, Helper:call({ "key: true" }, { 1, 6 }))
end)
it("turns 'true' into 'false'", function()
assert.are.same({ "key: false" }, Helper:call({ "key: true" }, { 1, 6 }))
end)
it("turns 'false' into 'true'", function()
assert.are.same({ "key: true" }, Helper:call({ "key: false" }, { 1, 6 }))
end)
it("turns 'false' into 'true'", function()
assert.are.same({ "key: true" }, Helper:call({ "key: false" }, { 1, 6 }))
end)
end)

View file

@ -1,12 +1,17 @@
local function ensure_installed(repo)
local name = repo:match(".+/(.+)$")
local name = repo:match(".+/(.+)$")
local install_path = "spec/support/" .. name
vim.opt.rtp:prepend(install_path)
if not vim.loop.fs_stat(install_path) then
print("* Downloading " .. name .. " to '" .. install_path .. "/'")
vim.fn.system({ "git", "clone", "git@github.com:" .. repo .. ".git", install_path })
vim.fn.system({
"git",
"clone",
"git@github.com:" .. repo .. ".git",
install_path,
})
end
end
@ -18,5 +23,5 @@ if os.getenv("CI") then
else
ensure_installed("nvim-lua/plenary.nvim")
ensure_installed("nvim-treesitter/nvim-treesitter")
require('plenary.test_harness').test_directory('spec/filetypes')
require("plenary.test_harness").test_directory("spec/filetypes")
end

View file

@ -12,14 +12,10 @@ local Buffer = {}
function Buffer.new(lang, buf_opts)
local instance = {
lang = lang,
opts = vim.tbl_extend(
"keep",
{
filetype = lang,
indentexpr = "nvim_treesitter#indent()",
},
buf_opts or {}
)
opts = vim.tbl_extend("keep", {
filetype = lang,
indentexpr = "nvim_treesitter#indent()",
}, buf_opts or {}),
}
setmetatable(instance, { __index = Buffer })
@ -46,10 +42,11 @@ function Buffer:set_cursor(pos)
local row = pos[1] - 1
local col = pos[2] - 1
local fake_get_node = function()
local node = vim.treesitter.get_parser(self.handle, self.lang)
:parse()[1]
:root()
:named_descendant_for_range(row, col, row, col)
local node = vim.treesitter
.get_parser(self.handle, self.lang)
:parse()[1]
:root()
:named_descendant_for_range(row, col, row, col)
return node, self.lang
end
@ -99,8 +96,8 @@ _G.SpecHelper = SpecHelper
--- @return SpecHelper
function SpecHelper.new(lang, buf_opts)
local instance = {
lang = lang,
buf_opts = buf_opts or {}
lang = lang,
buf_opts = buf_opts or {},
}
setmetatable(instance, { __index = SpecHelper })
@ -116,11 +113,8 @@ end
--- @return table
function SpecHelper:call(text, pos)
local buffer = Buffer.new(self.lang, self.buf_opts)
local result = buffer:setup()
:set_cursor(pos or { 1, 1 })
:write(text)
:run_action()
:read()
local result =
buffer:setup():set_cursor(pos or { 1, 1 }):write(text):run_action():read()
buffer:teardown()