From d65c36cccd2368f60addd627220c2e40aad3dd90 Mon Sep 17 00:00:00 2001 From: lg-epitech Date: Tue, 11 Jun 2024 23:29:50 +0200 Subject: [PATCH 01/10] [ADD] popup --- lua/headers/commands.lua | 34 ++---------- lua/headers/config.lua | 2 + lua/headers/gui.lua | 8 --- lua/headers/init.lua | 11 ++-- lua/headers/patterns.lua | 1 + lua/headers/templates.lua | 42 +++++++++++++++ lua/headers/ui.lua | 107 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 42 deletions(-) delete mode 100644 lua/headers/gui.lua create mode 100644 lua/headers/ui.lua 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/config.lua b/lua/headers/config.lua index b6581b4..7ba86ff 100644 --- a/lua/headers/config.lua +++ b/lua/headers/config.lua @@ -9,6 +9,8 @@ local comments = require("headers.comments") ---@class HConfig ---@field merge function +---@field width number +---@field height number ---@field email string|nil ---@field username string|nil ---@field comments table 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..c7d9287 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) diff --git a/lua/headers/templates.lua b/lua/headers/templates.lua index 011850a..c8f5d9e 100644 --- a/lua/headers/templates.lua +++ b/lua/headers/templates.lua @@ -8,6 +8,7 @@ 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" @@ -242,4 +243,45 @@ M.add_variant = function(template_name, extension, text, opts) return true end +M.insert = function() + local templ = M.getSelected() + if templ == nil then + print("You don't have a selected header.") + return + end + + local extension = utils.get_extension() + local template_string, opts = templ: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 + + local success = patterns.insert(template_split) + if success == false then + print("Could not insert header.") + end +end + +M.getNames = function() + local names = {} ---@type table + + for _, templ in ipairs(M.list) do + table.insert(names, templ.name) + end + + return names +end + return M diff --git a/lua/headers/ui.lua b/lua/headers/ui.lua new file mode 100644 index 0000000..efa5d25 --- /dev/null +++ b/lua/headers/ui.lua @@ -0,0 +1,107 @@ +-- +-- EPITECH PROJECT, 2024 +-- headers.nvim +-- File description: +-- ui.lua +-- + +local templates = require("headers.templates") +local popup = require("plenary.popup") + +-- TODO: Interface + +---@class HeadersUI +---@field bufHandle? integer +---@field winnr? integer +---@field win? table +local UI = {} + +function UI:setup() + -- Auto commands + vim.api.nvim_create_autocmd("BufLeave", { + pattern = "Headers", + callback = function() + self:close() + end, + nested = true, + }) +end + +function UI:configure() + -- Options + local option = vim.api.nvim_set_option_value + + vim.api.nvim_buf_set_name(self.bufHandle, "Headers") + option("number", true, { win = self.winnr }) + option("winhl", "Normal:Headers", { win = self.winnr }) + option("filetype", "headers", { buf = self.bufHandle }) + option("buftype", "acwrite", { buf = self.bufHandle }) + option("bufhidden", "delete", { buf = self.bufHandle }) + + -- User commands + vim.api.nvim_buf_set_keymap( + self.bufHandle, + "n", + "q", + "lua require('headers.ui'):toggle()", + { silent = true } + ) + vim.api.nvim_buf_set_keymap( + self.bufHandle, + "n", + "", + "lua require('headers.ui'):toggle()", + { silent = true } + ) +end + +function UI:window() + local borders = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } + local width = HConfig.width or 60 + local height = HConfig.height or 20 + + self.bufHandle = vim.api.nvim_create_buf(false, false) + self.winnr, self.win = popup.create(self.bufHandle, { + title = "Headers.nvim", + highlight = "Headers", + line = math.floor(((vim.o.lines - height) / 2) - 1), + col = math.floor(((vim.o.columns - width) / 2)), + minwidth = width, + minheight = height, + borderchars = borders, + number = true, + }) +end + +function UI:open() + self:window() + self:configure() + + local names = templates.getNames() + + vim.api.nvim_buf_set_lines(self.bufHandle, 0, #names, false, names) +end + +function UI:close() + if self.win == nil then + return + end + + self.win = nil + self.bufHandle = nil + + vim.api.nvim_win_close(self.winnr, true) + + self.winnr = nil + +end + +function UI:toggle() + if self.win == nil or not vim.api.nvim_win_is_valid(self.winnr) then + self:open() + else + self:close() + end +end + +return UI From a238c63a1f6ed9a29750540e05fa67e6b0c6463a Mon Sep 17 00:00:00 2001 From: lg-epitech Date: Tue, 11 Jun 2024 23:32:00 +0200 Subject: [PATCH 02/10] [FIX] stylua --- lua/headers/ui.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/headers/ui.lua b/lua/headers/ui.lua index efa5d25..3685e05 100644 --- a/lua/headers/ui.lua +++ b/lua/headers/ui.lua @@ -93,7 +93,6 @@ function UI:close() vim.api.nvim_win_close(self.winnr, true) self.winnr = nil - end function UI:toggle() From a88d6c1a514f1f371e20ba2ec1906776a57f6e49 Mon Sep 17 00:00:00 2001 From: lg-epitech Date: Wed, 12 Jun 2024 00:00:18 +0200 Subject: [PATCH 03/10] [FIX] lsp type errors and local logic --- lua/headers/templates.lua | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/lua/headers/templates.lua b/lua/headers/templates.lua index c8f5d9e..b646359 100644 --- a/lua/headers/templates.lua +++ b/lua/headers/templates.lua @@ -14,41 +14,41 @@ 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 {} - 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() @@ -66,15 +66,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 @@ -88,13 +88,13 @@ function template:new(name, text, opts) 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 @@ -108,7 +108,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 @@ -120,23 +120,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