Skip to content

Commit b836963

Browse files
authored
feat: change contest file naming
* Update filesystem.lua Add helper function inside Filesystem:save to convert the filename from PascalCase to Capitalised_Snake_Case * Update filesystem.lua Fix to_snake_case function to handle consecutive capital letters * Update filesystem.lua Fix `to_snake_case` function to handle spaces and numbers * Update filesystem.lua * Fix the formatting using stylua lua --config-path=./.stylua.toml
1 parent ffcd313 commit b836963

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lua/assistant/core/filesystem.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,32 @@ end
8383

8484
---@param chunk string
8585
function FileSystem:save(chunk)
86+
-- Helper function to convert to snake_case
87+
local function to_snake_case(str)
88+
-- Convert spaces to underscores and handle camel-case and consecutive capital letters
89+
local result = str:gsub("(%s)", "_"):gsub("(%u%l)", "_%1"):gsub("(%l)(%u)", "%1_%2"):gsub("^_", "")
90+
return result
91+
:gsub("^%s*(.-)%s*$", "%1")
92+
:gsub("%s+", "_")
93+
:gsub("(%l)(%u)", "%1_%2")
94+
:gsub("(%l)(%d)", "%1_%2")
95+
:gsub("(%d)(%l)", "%1_%2")
96+
:gsub("(%d)(%u)", "%1_%2")
97+
end
98+
8699
self.__init__()
87100
chunk = string.match(chunk, "^.+\r\n(.+)$")
88101
local data = vim.json.decode(chunk)
89102

90103
if data.languages.java.taskClass then
91104
vim.schedule(function()
92105
local filtered_data = self.filter(chunk)
93-
local filepath = string.format("%s/.ast/%s.json", vim.fn.expand("%:p:h"), data.languages.java.taskClass)
106+
local task_class_snake = to_snake_case(data.languages.java.taskClass) -- Convert to snake_case
107+
local filepath = string.format("%s/.ast/%s.json", vim.fn.expand("%:p:h"), task_class_snake)
94108

95109
if filtered_data then
96110
self:write(filepath, vim.json.encode(filtered_data))
97-
self.create(data.languages.java.taskClass)
111+
self.create(task_class_snake)
98112
end
99113
end)
100114
end

0 commit comments

Comments
 (0)