Skip to content

Commit

Permalink
refactor: extend
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed May 28, 2024
1 parent e9307df commit e7d0aab
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions lua/regexplainer/renderers/narrative/narrative.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
local descriptions = require 'regexplainer.component.descriptions'
local comp = require 'regexplainer.component'
local D = require'regexplainer.component.descriptions'
local P = require'regexplainer.component.predicates'
local U = require'regexplainer.utils'

local extend = vim.tbl_extend
local extend = function(a, b) return vim.tbl_extend('force', a, b) end

local M = {}

---@class RegexplainerNarrativeRendererOptions
---@field indentation_string string|fun(component:RegexplainerComponent):string # clause separator

---@class RegexplainerNarrativeRendererState : RegexplainerRendererState
---@field lookbehind_found boolean # see https://github.com/tree-sitter/tree-sitter-regex/issues/13
---@field first boolean # is it first in the term?
---@field last boolean # is it last in the term?
---@field parent RegexplainerComponent the parent component
Expand Down Expand Up @@ -110,9 +108,9 @@ local function get_infix(component, options, state)
return '`' .. component.text .. '`'
else
local sep = get_indent_string(component, options)
local line_sep = P.is_alternation(state.parent) and '' or '\n'
local sublines = M.recurse(component.children, options, state)
local contents = table.concat(sublines, '\n')
local line_sep = P.is_alternation(state.parent) and '' or '\n'
return ''
.. get_quantifier(component)
.. line_sep
Expand All @@ -130,7 +128,7 @@ local function get_infix(component, options, state)
local oxford = i == #component.children and 'or ' or ''
local first_in_alt = i == 1
local last_in_alt = i == #component.children
local next_state = extend('force', state, {
local next_state = extend(state, {
first = first_in_alt,
last = last_in_alt,
parent = component
Expand All @@ -147,7 +145,9 @@ local function get_infix(component, options, state)

if P.is_capture_group(component) then
local indent = get_indent_string(component, options)
local sublines = M.recurse(component.children, options, state)
local sublines = M.recurse(component.children, options, extend(state, {
parent = component
}))
local contents = table.concat(sublines, '\n' .. indent)
local name = component.group_name and ('`' .. component.group_name .. '`') or ''
local header = ''
Expand All @@ -168,7 +168,7 @@ local function get_infix(component, options, state)
end

if P.is_character_class(component) then
return '\n' .. descriptions.describe_character_class(component)
return '\n' .. D.describe_character_class(component)
end

if P.is_escape(component) then
Expand All @@ -179,14 +179,14 @@ local function get_infix(component, options, state)
if escaped_text == ' ' then escaped_text = '(space)' end
return '`' .. escaped_text .. '`'
elseif P.is_decimal_escape(component) then
return '`' .. descriptions.describe_escape(component.text) .. '`'
return '`' .. D.describe_escape(component.text) .. '`'
else
return '**' .. descriptions.describe_escape(component.text) .. '**'
return '**' .. D.describe_escape(component.text) .. '**'
end
end

if P.is_character_class_escape(component) then
return '**' .. descriptions.describe_escape(component.text) .. '**'
return '**' .. D.describe_escape(component.text) .. '**'
end

if P.is_pattern_character(component) then
Expand All @@ -195,7 +195,9 @@ local function get_infix(component, options, state)

if P.is_lookaround_assertion(component) then
local indent = get_indent_string(component, options)
local sublines = M.recurse(component.children, options, state)
local sublines = M.recurse(component.children, options, extend(state, {
parent = component
}))
local contents = table.concat(sublines, '\n'..indent)

local negation = (component.negative and 'NOT ' or '')
Expand All @@ -218,7 +220,7 @@ local function get_infix(component, options, state)

if P.is_special_character(component) then
local infix = ''
infix = infix .. '**' .. descriptions.describe_character(component) .. '**'
infix = infix .. '**' .. D.describe_character(component) .. '**'
if P.is_start_assertion(component) then
infix = infix .. '\n'
end
Expand Down Expand Up @@ -277,8 +279,6 @@ end
---@param state RegexplainerNarrativeRendererState
---@return string[] lines, RegexplainerNarrativeRendererState state
function M.recurse(components, options, state)
state = state or { }
state.parent = state.parent or { type = 'root' }
local clauses = {}
for i, component in ipairs(components) do
local first = i == 1
Expand All @@ -289,21 +289,18 @@ function M.recurse(components, options, state)
return get_error_message(error, state), state
end

local next_state = extend('force', state, {
local next_state = extend(state, {
first = first,
last = last,
parent = state.parent or { type = 'root' }
})

local clause = ''
.. get_prefix(component, options, next_state)
.. get_infix(component, options, next_state)
.. get_suffix(component, options, next_state)

-- if P.is_lookaround_assertion(component) and clauses[#clauses] then
-- clauses[#clauses] = clauses[#clauses] .. '' .. clause
-- else
table.insert(clauses, clause)
-- end
table.insert(clauses, clause)
end

local lines = vim.iter(clauses)
Expand Down

0 comments on commit e7d0aab

Please sign in to comment.