Skip to content

Commit

Permalink
chore: add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Dec 8, 2023
1 parent 00561b6 commit 1ed963f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lua/img-clip/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local util = require("img-clip.util")

local M = {}

---@return string | nil
M.get_clip_cmd = function()
-- Linux (X11)
if os.getenv("DISPLAY") then
Expand Down Expand Up @@ -46,6 +47,8 @@ M.get_clip_cmd = function()
end
end

---@param cmd string
---@return boolean
M.check_if_content_is_image = function(cmd)
-- Linux (X11)
if cmd == "xclip" then
Expand Down Expand Up @@ -73,8 +76,13 @@ M.check_if_content_is_image = function(cmd)
-- TODO: Implement clipboard check for Windows
return false
end

return false
end

---@param cmd string
---@param file_path string
---@return boolean
M.save_clipboard_image = function(cmd, file_path)
-- Linux (X11)
if cmd == "xclip" then
Expand All @@ -97,6 +105,8 @@ M.save_clipboard_image = function(cmd, file_path)
-- TODO: Implement clipboard check for Windows
return false
end

return false
end

return M
3 changes: 3 additions & 0 deletions lua/img-clip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local defaults = {

M.options = {}

---@param key string
---@param opts? table the options passed to pasteImage function
---@return string | nil
M.get_option = function(key, opts)
local ft = vim.bo.filetype
local val
Expand Down
12 changes: 12 additions & 0 deletions lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ local util = require("img-clip.util")

local M = {}

---@type string
M.sep = package.config:sub(1, 1)

---@param path string
---@return string
M.normalize_path = function(path)
return vim.fn.simplify(path):gsub(M.sep .. "$", "") .. M.sep
end

---@param str string
---@param ext string
---@return string
M.add_file_ext = function(str, ext)
str = vim.fn.fnamemodify(str, ":r")
return str .. "." .. ext
end

---@param ext string
---@param opts? table
---@return string
M.get_file_path = function(ext, opts)
local config_dir_path = config.get_option("dir_path", opts)
local config_filename = os.date(config.get_option("filename", opts))
Expand Down Expand Up @@ -56,6 +65,9 @@ M.get_file_path = function(ext, opts)
return file_path
end

---@param dir string
---@param mode number
---@return boolean
M.mkdirp = function(dir, mode)
dir = vim.fn.resolve(dir)
mode = mode or 493
Expand Down
1 change: 1 addition & 0 deletions lua/img-clip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ end

local clip_cmd = nil

---@param opts? table
M.pasteImage = function(opts)
if not clip_cmd then
clip_cmd = clipboard.get_clip_cmd()
Expand Down
26 changes: 19 additions & 7 deletions lua/img-clip/markup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local config = require("img-clip.config")

local M = {}

---@param template string
---@return string[]
function M.split_lines(template)
local lines = vim.split(template, "\n")

Expand All @@ -16,7 +18,12 @@ function M.split_lines(template)
return lines
end

function M.get_new_row(row, lines)
---@param row number
---@param lines string[]
---@return number new_row The new cursor row pos
---@return string matched_line The matched line in the tempalte (or the last line if no match)
---@return number matched_line_index The index of the matched line
function M.get_new_cursor_row(row, lines)
for i, line in ipairs(lines) do
if line:match("$CURSOR") then
return row + i, line, i
Expand All @@ -26,7 +33,9 @@ function M.get_new_row(row, lines)
return row + #lines, lines[#lines], #lines
end

function M.get_new_col(line)
---@param line string
---@return number new_col The new cursor col pos
function M.get_new_cursor_col(line)
local cursor_pos = line:find("$CURSOR")
if cursor_pos then
return cursor_pos - 1
Expand All @@ -35,23 +44,26 @@ function M.get_new_col(line)
return string.len(line) - 1
end

function M.insert_markup(filepath, opts)
---@param file_path string
---@param opts? table
---@return boolean
function M.insert_markup(file_path, opts)
local template = config.get_option("template", opts)
if not template then
return false
end

local filename = vim.fn.fnamemodify(filepath, ":t")
local filename = vim.fn.fnamemodify(file_path, ":t")

template = template:gsub("$FILEPATH", filepath)
template = template:gsub("$FILEPATH", file_path)
template = template:gsub("$FILENAME", filename)
local lines = M.split_lines(template)

local cur_pos = vim.api.nvim_win_get_cursor(0)
local cur_row = cur_pos[1]

local new_row, line, index = M.get_new_row(cur_row, lines)
local new_col = M.get_new_col(line)
local new_row, line, index = M.get_new_cursor_row(cur_row, lines)
local new_col = M.get_new_cursor_col(line)

lines[index] = line:gsub("$CURSOR", "")

Expand Down

0 comments on commit 1ed963f

Please sign in to comment.