diff --git a/.gitignore b/.gitignore index 06d217c..07bda0a 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ luac.out *.hex templates/* +TODO.md diff --git a/Makefile b/Makefile index 54fcab2..8461bb0 100644 --- a/Makefile +++ b/Makefile @@ -5,15 +5,12 @@ ## Makefile ## -# WARN: This Makefile is for the convenience of running tests -# DO NOT RUN WITHOUT CHANGING THE PROPER TEMPLATES DIR IN EACH FILE +all: tests -all: test_all +tests: + nvim --headless -c "PlenaryBustedDirectory spec/" +qa -test_all: - nvim --headless -c "PlenaryBustedDirectory spec/" +utils: + nvim --headless -c "PlenaryBustedFile spec/utils_spec.lua" +qa -test_templates: - nvim --headless -c "PlenaryBustedFile spec/templates_spec.lua" - -.PHONY: test_all +.PHONY: tests diff --git a/lua/headers/commands.lua b/lua/headers/commands.lua index 45a6664..c75a5ad 100644 --- a/lua/headers/commands.lua +++ b/lua/headers/commands.lua @@ -5,39 +5,15 @@ -- commands.lua -- -local patterns = require("headers.patterns") local templates = require("headers.templates") -local utils = require("headers.utils") +local ui = require("headers.ui") local cmd = vim.api.nvim_create_user_command cmd("InsertSelectedHeader", function() - local template = templates.getSelected() - if template == nil then - print("You don't have a selected header.") - return - end - - local extension = utils.get_extension() - local template_string, opts = template:get_info() - if template_string == nil then - print("Template string not found.") - return - end - - local formatted = patterns.format(template_string) - if formatted == nil then - print("Error while parsing patterns.") - return - end - local template_split = patterns.generalize(formatted, extension, opts) - if template_split == nil then - print("Error while generalizing pattern.") - return - end + templates.insert() +end, {}) - local success = patterns.insert(template_split) - if success == false then - print("Could not insert header.") - end +cmd("Headers", function() + ui:toggle() end, {}) diff --git a/lua/headers/comments.lua b/lua/headers/comments.lua index 0f4a298..5fdc33f 100644 --- a/lua/headers/comments.lua +++ b/lua/headers/comments.lua @@ -55,7 +55,7 @@ local __comments = { comment:new({ "erl", "hrl" }, { middle = "%" }), comment:new({ "hs", "lhs" }, { head = "{-", middle = "--", tail = "-}" }), comment:new({ "jsx" }, { head = "{/*", middle = "**", tail = "*/}" }), - comment:new({ "html" }, { head = "" }), + comment:new({ "html", "md" }, { head = "" }), comment:new({ "cbl", "cob", "cpy" }, { middle = " *" }), comment:new({ "clj", "cljs", "cljr", "cljc", "cljd", "edn" }, { middle = ";;" }), comment:new({ "lisp", "lsp", "l", "cl", "fasl" }, { middle = ";" }), diff --git a/lua/headers/config.lua b/lua/headers/config.lua index b6581b4..575cab6 100644 --- a/lua/headers/config.lua +++ b/lua/headers/config.lua @@ -34,7 +34,7 @@ local function merge_table(t1, t2) return t1 end ----@param UserConfig table +---@param UserConfig table | nil function HConfig:merge(UserConfig) - HConfig = merge_table(HConfig, UserConfig) + HConfig = merge_table(HConfig, UserConfig or {}) end diff --git a/lua/headers/gui.lua b/lua/headers/gui.lua deleted file mode 100644 index 57148df..0000000 --- a/lua/headers/gui.lua +++ /dev/null @@ -1,8 +0,0 @@ --- --- EPITECH PROJECT, 2024 --- headers.nvim --- File description: --- gui.lua --- - --- TODO: Interface diff --git a/lua/headers/init.lua b/lua/headers/init.lua index 7e844c9..c481149 100644 --- a/lua/headers/init.lua +++ b/lua/headers/init.lua @@ -19,17 +19,18 @@ local M = { ---@param opts table M.setup = function(opts) local hPath = path:new(vim.fn.stdpath("data") .. "/headers") + local templates = require("headers.templates") + local ui = require("headers.ui") + require("headers.config") + require("headers.commands") + if hPath:is_dir() == false then hPath:mkdir() end - require("headers.config") HConfig:merge(opts) - - local templates = require("headers.templates") templates.scan() - - require("headers.commands") + ui:setup() end return M diff --git a/lua/headers/patterns.lua b/lua/headers/patterns.lua index ac3d791..f87e8a7 100644 --- a/lua/headers/patterns.lua +++ b/lua/headers/patterns.lua @@ -213,6 +213,7 @@ end ---@param opts table ---@return table|nil M.generalize = function(pattern, ft, opts) + opts = opts or {} ft = string.lower(ft) local CommentType = comments.find(ft) @@ -250,9 +251,17 @@ M.generalize = function(pattern, ft, opts) end ---@param pattern_split table +---@param buff? integer ---@return boolean -M.insert = function(pattern_split) - return pcall(vim.api.nvim_buf_set_lines, 0, 0, 0, true, pattern_split) +M.insert = function(pattern_split, buff) + return pcall( + vim.api.nvim_buf_set_lines, + buff or 0, + 0, + 0, + true, + pattern_split + ) end return M diff --git a/lua/headers/templates.lua b/lua/headers/templates.lua index 011850a..a5793d2 100644 --- a/lua/headers/templates.lua +++ b/lua/headers/templates.lua @@ -8,46 +8,48 @@ local path = require("plenary.path") local scan = require("plenary.scandir") local utils = require("headers.utils") +local patterns = require("headers.patterns") local storage_path = vim.fn.stdpath("data") .. "/headers" local M = {} ----@class variant +---@class TemplateVariation ---@field extension string ---@field replacement string ---@field opts table -local variant = {} -variant.__index = variant +local _variant = {} +_variant.__index = _variant ---@param extension string ---@param replacement string ---@param opts table ----@return variant -function variant:new(extension, replacement, opts) +---@return TemplateVariation +function _variant:new(extension, replacement, opts) local new_variant = {} new_variant.extension = extension new_variant.replacement = replacement new_variant.opts = opts or {} + new_variant.opts.generalize = new_variant.opts.generalize or true - setmetatable(new_variant, variant) + setmetatable(new_variant, _variant --[[@as table]]) return new_variant end ----@class template +---@class Template ---@field name string ---@field text string ---@field is_selected boolean ---@field path string ---@field opts table ----@field variations table -local template = {} -template.__index = template +---@field variations table +local _template = {} +_template.__index = _template ---@param file string ----@return template|nil -function template:scan(file) +---@return Template|nil +function _template:scan(file) local p = path:new(file) local contents = p:read() @@ -65,15 +67,15 @@ function template:scan(file) return nil end - setmetatable(t, template) + setmetatable(t, _template --[[@as table]]) return t end ---@param name string ---@param text string ---@param opts table ----@return template -function template:new(name, text, opts) +---@return Template +function _template:new(name, text, opts) local new_template = {} new_template.name = name @@ -81,19 +83,20 @@ function template:new(name, text, opts) new_template.is_selected = false new_template.variations = {} new_template.opts = opts or {} + new_template.opts.generalize = new_template.opts.generalize or true local p = path:new(storage_path):joinpath(utils.hash_string(name) .. ".txt") new_template.path = p.filename local contents = vim.json.encode(new_template) p:write(contents, "w") - setmetatable(new_template, template) + setmetatable(new_template, _template --[[@as table]]) return new_template end ---@return string, table -function template:get_info() +function _template:get_info() local extension = string.lower(utils.get_extension()) for _, var in ipairs(self.variations) do if var.extension == extension then @@ -107,7 +110,7 @@ end ---@param extension string ---@param text string ---@param opts table -function template:add_variant(extension, text, opts) +function _template:add_variant(extension, text, opts) extension = string.lower(extension) for _, var in ipairs(self.variations) do if var.extension == extension then @@ -119,23 +122,23 @@ function template:add_variant(extension, text, opts) end end - table.insert(self.variations, variant:new(extension, text, opts)) + table.insert(self.variations, _variant:new(extension, text, opts)) self:save() end -function template:save() +function _template:save() local p = path:new(self.path) local contents = vim.json.encode(setmetatable(self, {})) - setmetatable(self, template) + setmetatable(self, _template --[[@as table]]) p:write(contents, "w") end ----@type table