Skip to content

Commit a296ab7

Browse files
committed
Merge branch 'gitpushjoe-main' into main
2 parents 7f76286 + f9cd4ac commit a296ab7

File tree

16 files changed

+61
-42
lines changed

16 files changed

+61
-42
lines changed

core/config.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Config.__name = "Config"
7575
Config.errors = {
7676

7777
--- @param list table
78-
--- @return string
78+
--- @return string errmsg
7979
missing_item_in_note_schema_list = function(list, idx)
8080
return "Missing "
8181
.. (#list <= 1 and "open-tag" or "close-tag")
@@ -84,7 +84,7 @@ Config.errors = {
8484
.. "]."
8585
end,
8686

87-
--- @return string
87+
--- @return string errmsg
8888
root_reserved = function()
8989
return "Cannot use "
9090
.. "ROOT"
@@ -98,7 +98,8 @@ Config.errors = {
9898
}
9999

100100
--- @param config_table PartialConfigTable
101-
--- @return Config?, string?
101+
--- @return Config? config
102+
--- @return string? errmsg
102103
function Config:new(config_table)
103104
self = {}
104105
--- @cast self Config

core/context.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ Context.errors = {
9090
---@param text_stream Stream
9191
---@param preserve boolean
9292
---@param ansi_enabled boolean
93-
---@return Context?, string?
93+
---@return Context? ctx
94+
---@return string? errmsg
9495
function Context:new(
9596
config,
9697
src_path,

core/fold.lua

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ M.__name = "fold"
1414
M.errors = {
1515

1616
---@param section Section?
17-
---@return string
17+
---@return string errmsg
1818
unterminated_section = function(section)
1919
return "Unterminated section: " .. (tostring(section) or "nil")
2020
end,
2121

2222
---@param line number
23-
---@return string
23+
---@return string errmsg
2424
inconsistent_indent = function(line)
2525
return "Inconsistent indent on line " .. line
2626
end,
2727

2828
---@param retry_count number
2929
---@param section Section?
3030
---@param is_local boolean?
31-
---@return string
31+
---@return string errmsg
3232
maximum_retry_count = function(retry_count, section, is_local)
3333
return "Maximum "
3434
.. (is_local and "local " or "")
@@ -39,7 +39,7 @@ M.errors = {
3939
end,
4040

4141
---@param path Path
42-
---@return string
42+
---@return string errmsg
4343
path_should_not_be_directory = function(path)
4444
return "`config.resolve_path` returned directory-type Path ("
4545
.. path:escaped()
@@ -48,14 +48,14 @@ M.errors = {
4848

4949
---@param path string
5050
---@param section Section?
51-
---@return string
51+
---@return string errmsg
5252
cannot_write = function(path, section)
5353
return "Cannot write to path " .. path .. "\n" .. tostring(section)
5454
end,
5555

5656
---@param command string
5757
---@param err string?
58-
---@return string
58+
---@return string errmsg
5959
command_failed = function(command, err)
6060
return "Failed to execute command: "
6161
.. command
@@ -64,14 +64,15 @@ M.errors = {
6464
end,
6565

6666
---@param path string
67-
---@return string
67+
---@return string errmsg
6868
expected_file_to_be_writable = function(path)
6969
return "Expected file " .. tostring(path) .. " to be writable"
7070
end,
7171
}
7272

7373
---@param ctx Context
74-
---@return Section?, string?
74+
---@return Section? section
75+
---@return string? errmsg
7576
M.parse = function(ctx)
7677
local curr_section, err = Section:new(
7778
0,
@@ -111,8 +112,13 @@ M.parse = function(ctx)
111112
line = string.sub(line, indent_chars + 1)
112113

113114
for note_idx, note_type in ipairs(note_schema) do
114-
local prefix = note_type[2]
115-
if str.starts_with(line, prefix) then
115+
local open_tag = note_type[2]
116+
local do_not_open_only_close = line == open_tag
117+
and curr_section
118+
and open_tag == curr_section:close_tag()
119+
if
120+
str.starts_with(line, open_tag) and not do_not_open_only_close
121+
then
116122
local new_section
117123
new_section, err = Section:new(
118124
id,
@@ -134,10 +140,14 @@ M.parse = function(ctx)
134140
end
135141
end
136142
-- TODO(gitpushjoe): maybe still check if a suffix for another note type slipped through?
143+
local do_not_close_only_open = line == curr_section:open_tag()
144+
and line == curr_section:close_tag()
145+
and curr_section.start_line == i
137146
if
138147
curr_section
139-
and curr_section.type[1] ~= "ROOT"
148+
and not curr_section:is_root()
140149
and str.ends_with(line, curr_section:close_tag())
150+
and not do_not_close_only_open
141151
then
142152
curr_section.end_line = i
143153
curr_section = curr_section.parent
@@ -151,13 +161,14 @@ end
151161

152162
---@param section_root Section
153163
---@param ctx Context
154-
---@return nil, string?
164+
---@return nil
165+
---@return string? errmsg
155166
M.prepare = function(section_root, ctx)
156167
---@type { [string]: Section }
157168
local created_files = {}
158169

159170
local _, err = traverse.preorder(section_root, function(section)
160-
if section.type[1] == "ROOT" then
171+
if section:is_root() then
161172
section.path = ctx.dest_path
162173
return
163174
end
@@ -210,7 +221,7 @@ M.prepare = function(section_root, ctx)
210221
end
211222

212223
_, err = traverse.postorder(section_root, function(section)
213-
if section.type[1] == "ROOT" then
224+
if section:is_root() then
214225
section.lines = ctx.lines
215226
return
216227
end
@@ -256,7 +267,8 @@ end
256267
---@param section_root Section
257268
---@param ctx Context
258269
---@param is_dry_run boolean?
259-
---@return Plan?, string?
270+
---@return Plan? plan
271+
---@return string? errmsg
260272
M.execute = function(section_root, ctx, is_dry_run)
261273
is_dry_run = is_dry_run or false
262274
local io = ctx.io
@@ -299,7 +311,7 @@ M.execute = function(section_root, ctx, is_dry_run)
299311
---@param lines string[]
300312
---@param path Path
301313
---@param is_overwrite boolean?
302-
---@return string?
314+
---@return string? errmsg
303315
local function write(handle, lines, path, is_overwrite)
304316
is_overwrite = is_overwrite or false
305317
created_or_modified_paths[tostring(path)] = 1
@@ -316,7 +328,7 @@ M.execute = function(section_root, ctx, is_dry_run)
316328
end
317329

318330
---@param directory Path
319-
---@return string?
331+
---@return string? errmsg
320332
local function mkdir(directory)
321333
created_or_modified_paths[tostring(directory)] = 1
322334
if not is_dry_run then
@@ -350,7 +362,7 @@ M.execute = function(section_root, ctx, is_dry_run)
350362
end
351363

352364
while true do
353-
if section.type[1] == "ROOT" then
365+
if section:is_root() then
354366
is_overwrite = ctx.src_path == ctx.dest_path
355367
break
356368
end
@@ -379,7 +391,7 @@ M.execute = function(section_root, ctx, is_dry_run)
379391
full_path = tostring(section.path) or ""
380392
end
381393

382-
if section.type[1] == "ROOT" and ctx.preserve then
394+
if section:is_root() and ctx.preserve then
383395
return
384396
end
385397

core/parser.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local validate = require("core.validate")
22

3-
---@alias Equivalences { [string]: EquivalenceClass|string|nil }
3+
---@alias Equivalences table<string, EquivalenceClass|string|nil>
44

55
---@class (exact) EquivalenceClass
66
---@field equivalences string[]

core/path.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ end
3535
---@param path string|string[]
3636
---`allow_relative` IS INTENDED FOR INTERNAL USE ONLY.
3737
---@param allow_relative boolean?
38-
---@return Path?, string?
38+
---@return Path? path
39+
---@return string? errmsg
3940
function Path:new(path, allow_relative)
4041
self = {}
4142
setmetatable(self, Path)
@@ -137,7 +138,7 @@ end
137138
--- assert(path:pop_directory() == "baz")
138139
--- assert(path == Path:new("/foo/"))
139140
--- ```
140-
--- @return string?
141+
--- @return string? errmsg
141142
function Path:pop_directory()
142143
handle_is_void(self)
143144
if #self.parts <= 2 then
@@ -200,7 +201,7 @@ end
200201
--- assert(path == Path:new("/foo/"))
201202
--- ```
202203
--- @param filename string
203-
--- @return string
204+
--- @return string old_filename
204205
function Path:set_filename(filename)
205206
handle_is_void(self)
206207
if #self.parts < 1 then
@@ -252,7 +253,7 @@ end
252253
--- assert(Path:new("/foo/bar"):join("./baz") == Path:new("/foo/baz"))
253254
--- ```
254255
--- @param path string
255-
--- @return Path?
256+
--- @return Path? new_path
256257
--- @return string? errmsg
257258
function Path:join(path)
258259
local err = validate.types("Path:join", { { path, "string", "path" } })

core/section.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ Section.ROOT = "ROOT"
5555
---@param children Section[]
5656
---@param parent Section?
5757
---@param indent string?
58-
---@return Section?, string?
58+
---@return Section? section
59+
---@return string? errmsg
5960
function Section:new(
6061
id,
6162
type,
@@ -205,7 +206,7 @@ function Section:type_name_is(name)
205206
return name == self:type_name()
206207
end
207208

208-
---@return string?
209+
---@return string
209210
function Section:__tostring()
210211
return "Section {"
211212
.. '\n\ttype = {"'

core/validate.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ M.RETVAL = "RETVAL"
99
---@param expected_type string
1010
---@param actual string
1111
---@overload fun(function_name: string, triplet: {[1]: any, [2]: string, [3]: string?}): string
12-
---@return string
12+
---@return string errmsg
1313
M.invalid_type_error = function(function_name, arg_name, expected_type, actual)
1414
arg_name = arg_name or M.RETVAL
1515
if type(arg_name) == type({}) then
@@ -36,6 +36,7 @@ end
3636
---@param expected_class table
3737
---@param actual any
3838
---@overload fun(function_name: string, triplet: {[1]: any, [2]: table, [3]: string?}): string
39+
---@return string
3940
M.invalid_instance_error = function(
4041
function_name,
4142
arg_name,
@@ -72,7 +73,7 @@ end
7273

7374
---@param function_name string
7475
---@param data {[1]: any, [2]: string, [3]: string?}[]
75-
---@return string?
76+
---@return string? errmsg
7677
M.types = function(function_name, data)
7778
for _, item in ipairs(data) do
7879
local elem = item[1]
@@ -105,7 +106,7 @@ end
105106
---@param list any[]
106107
---@param arg_name string
107108
---@param expected_type string
108-
---@return string?
109+
---@return string? errmsg
109110
M.types_in_list = function(function_name, list, arg_name, expected_type)
110111
arg_name = arg_name or M.RETVAL
111112
local err = M.types(function_name, { { list, "table", arg_name } })
@@ -131,7 +132,7 @@ end
131132

132133
---@param function_name string
133134
---@param data {[1]: any, [2]: table, [3]: string?}[]
134-
---@return string?
135+
---@return string? errmsg
135136
M.are_instances = function(function_name, data)
136137
for _, item in ipairs(data) do
137138
if not item[1] then

core/version.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return "β0.1.0"

examples/bible/configs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local Path = require("core.path")
22
local utils = require("core.utils")
33

4-
---@type { [string]: PartialConfigTable }
4+
---@type table<string, PartialConfigTable>
55
local configs = {
66

77
bible = {

examples/chess/configs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local Path = require("core.path")
22

3-
---@type { [string]: PartialConfigTable }
3+
---@type table<string, PartialConfigTable>
44
local configs = {
55

66
chess = {

0 commit comments

Comments
 (0)