Skip to content
Merged

test #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ luac.out
*.hex

templates/*
TODO.md
15 changes: 6 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 5 additions & 29 deletions lua/headers/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
2 changes: 1 addition & 1 deletion lua/headers/comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<!--", middle = "--", tail = "-->" }),
comment:new({ "html", "md" }, { head = "<!--", middle = "--", tail = "-->" }),
comment:new({ "cbl", "cob", "cpy" }, { middle = " *" }),
comment:new({ "clj", "cljs", "cljr", "cljc", "cljd", "edn" }, { middle = ";;" }),
comment:new({ "lisp", "lsp", "l", "cl", "fasl" }, { middle = ";" }),
Expand Down
4 changes: 2 additions & 2 deletions lua/headers/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions lua/headers/gui.lua

This file was deleted.

11 changes: 6 additions & 5 deletions lua/headers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 11 additions & 2 deletions lua/headers/patterns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
102 changes: 76 additions & 26 deletions lua/headers/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<variant>
local template = {}
template.__index = template
---@field variations table<TemplateVariation>
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()
Expand All @@ -65,35 +67,36 @@ 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
new_template.text = text
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
Expand All @@ -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
Expand All @@ -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<template>
---@type table<Template>
M.list = {}

---@param name string
---@return number, template|nil
---@return number, Template|nil
M.find = function(name)
for i, templ in ipairs(M.list) do
if templ.name == name then
Expand All @@ -150,7 +153,7 @@ M.scan = function()
local list = scan.scan_dir(storage_path, { depth = 1 })

for _, file in pairs(list) do
local t = template:scan(file)
local t = _template:scan(file)

if t ~= nil then
table.insert(M.list, t)
Expand All @@ -169,7 +172,7 @@ M.add = function(name, text, opts)
M.remove(i)
end

local t = template:new(name, text, opts)
local t = _template:new(name, text, opts)
table.insert(M.list, t)

return true
Expand All @@ -191,7 +194,7 @@ M.remove = function(idx)
return true
end

---@return template|nil
---@return Template|nil
M.getSelected = function()
for _, templ in ipairs(M.list) do
if templ.is_selected then
Expand Down Expand Up @@ -242,4 +245,51 @@ 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 ---@type table<string>|nil
if opts.generalize == true then
template_split = patterns.generalize(formatted, extension, opts)
if template_split == nil then
print("Error while generalizing pattern.")
return
end
else
template_split = vim.split(formatted, "\n")
end

local success = patterns.insert(template_split)
if success == false then
print("Could not insert header.")
end
end

M.get_names = function()
local names = {} ---@type table<string>

for _, templ in ipairs(M.list) do
table.insert(names, templ.name)
end

return names
end

return M
Loading
Loading