Skip to content

Commit

Permalink
chore(deps): update commons library (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Apr 18, 2024
1 parent b6f0c06 commit feaf610
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 355 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
if: ${{ github.ref != 'refs/heads/main' }}
with:
commit_message: "chore(pr): auto-commit"
push_options: "--force"
unit_test:
name: Unit Test
needs:
Expand Down
14 changes: 9 additions & 5 deletions lua/gentags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ M.setup = function(opts)
-- cache dir
logger:ensure(
vim.fn.filereadable(cfg.cache_dir) <= 0,
"%s (cache_dir) already exist but not a directory!",
vim.inspect(cfg.cache_dir)
string.format(
"%s (cache_dir) already exist but not a directory!",
vim.inspect(cfg.cache_dir)
)
)
vim.fn.mkdir(cfg.cache_dir, "p")

Expand All @@ -35,7 +37,7 @@ M.setup = function(opts)
callback = function(event)
logging
.get("gentags")
:debug("|setup| enter buffer:%s", vim.inspect(event))
:debug(string.format("|setup| enter buffer:%s", vim.inspect(event)))
local dispatcher = require("gentags.dispatcher")
if dispatcher.enabled() then
dispatcher.load()
Expand All @@ -53,7 +55,7 @@ M.setup = function(opts)
callback = function(event)
logging
.get("gentags")
:debug("|setup| write buffer:%s", vim.inspect(event))
:debug(string.format("|setup| write buffer:%s", vim.inspect(event)))
local dispatcher = require("gentags.dispatcher")
if dispatcher.enabled() then
dispatcher.update()
Expand All @@ -64,7 +66,9 @@ M.setup = function(opts)
-- terminate before leaving vim
vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
callback = function(event)
logging.get("gentags"):debug("|setup| leave vim:%s", vim.inspect(event))
logging
.get("gentags")
:debug(string.format("|setup| leave vim:%s", vim.inspect(event)))
require("gentags.dispatcher").terminate()
end,
})
Expand Down
1 change: 1 addition & 0 deletions lua/gentags/commons/_system.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@diagnostic disable
local uv = vim.uv or vim.loop

--- @class vim.SystemOpts
Expand Down
6 changes: 0 additions & 6 deletions lua/gentags/commons/_system_types.lua

This file was deleted.

130 changes: 130 additions & 0 deletions lua/gentags/commons/_uri.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---@diagnostic disable
-- TODO: This is implemented only for files currently.
-- https://tools.ietf.org/html/rfc3986
-- https://tools.ietf.org/html/rfc2732
-- https://tools.ietf.org/html/rfc2396

local M = {}
local sbyte = string.byte
local schar = string.char
local tohex = require('bit').tohex
local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*'
local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*'
local PATTERNS = {
-- RFC 2396
-- https://tools.ietf.org/html/rfc2396#section-2.2
rfc2396 = "^A-Za-z0-9%-_.!~*'()",
-- RFC 2732
-- https://tools.ietf.org/html/rfc2732
rfc2732 = "^A-Za-z0-9%-_.!~*'()[]",
-- RFC 3986
-- https://tools.ietf.org/html/rfc3986#section-2.2
rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/",
}

---Converts hex to char
---@param hex string
---@return string
local function hex_to_char(hex)
return schar(tonumber(hex, 16))
end

---@param char string
---@return string
local function percent_encode_char(char)
return '%' .. tohex(sbyte(char), 2)
end

---@param uri string
---@return boolean
local function is_windows_file_uri(uri)
return uri:match('^file:/+[a-zA-Z]:') ~= nil
end

---URI-encodes a string using percent escapes.
---@param str string string to encode
---@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil
---@return string encoded string
function M.uri_encode(str, rfc)
local pattern = PATTERNS[rfc] or PATTERNS.rfc3986
return (str:gsub('([' .. pattern .. '])', percent_encode_char)) -- clamped to 1 retval with ()
end

---URI-decodes a string containing percent escapes.
---@param str string string to decode
---@return string decoded string
function M.uri_decode(str)
return (str:gsub('%%([a-fA-F0-9][a-fA-F0-9])', hex_to_char)) -- clamped to 1 retval with ()
end

---Gets a URI from a file path.
---@param path string Path to file
---@return string URI
function M.uri_from_fname(path)
local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') ---@type string?
local is_windows = volume_path ~= nil
if is_windows then
path = volume_path .. M.uri_encode(fname:gsub('\\', '/'))
else
path = M.uri_encode(path)
end
local uri_parts = { 'file://' }
if is_windows then
table.insert(uri_parts, '/')
end
table.insert(uri_parts, path)
return table.concat(uri_parts)
end

---Gets a URI from a bufnr.
---@param bufnr integer
---@return string URI
function M.uri_from_bufnr(bufnr)
local fname = vim.api.nvim_buf_get_name(bufnr)
local volume_path = fname:match('^([a-zA-Z]:).*')
local is_windows = volume_path ~= nil
local scheme ---@type string?
if is_windows then
fname = fname:gsub('\\', '/')
scheme = fname:match(WINDOWS_URI_SCHEME_PATTERN)
else
scheme = fname:match(URI_SCHEME_PATTERN)
end
if scheme then
return fname
else
return M.uri_from_fname(fname)
end
end

---Gets a filename from a URI.
---@param uri string
---@return string filename or unchanged URI for non-file URIs
function M.uri_to_fname(uri)
local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
if scheme ~= 'file' then
return uri
end
local fragment_index = uri:find('#')
if fragment_index ~= nil then
uri = uri:sub(1, fragment_index - 1)
end
uri = M.uri_decode(uri)
--TODO improve this.
if is_windows_file_uri(uri) then
uri = uri:gsub('^file:/+', ''):gsub('/', '\\')
else
uri = uri:gsub('^file:/+', '/') ---@type string
end
return uri
end

---Gets the buffer for a uri.
---Creates a new unloaded buffer if no buffer for the uri already exists.
---@param uri string
---@return integer bufnr
function M.uri_to_bufnr(uri)
return vim.fn.bufadd(M.uri_to_fname(uri))
end

return M
2 changes: 1 addition & 1 deletion lua/gentags/commons/color/hsl.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@diagnostic disable: lowercase-global
---@diagnostic disable
-----------------------------------------------------------------------------
-- Provides support for color manipulation in HSL color space.
--
Expand Down
12 changes: 10 additions & 2 deletions lua/gentags/commons/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ M.encode = function(t)
if t == nil then
return nil
end
return require("gentags.commons._json").encode(t)
if vim.json ~= nil and vim.is_callable(vim.json.encode) then
return vim.json.encode(t)
else
return require("gentags.commons._json").encode(t)
end
end

--- @param j string?
Expand All @@ -15,7 +19,11 @@ M.decode = function(j)
if j == nil then
return nil
end
return require("gentags.commons._json").decode(j)
if vim.json ~= nil and vim.is_callable(vim.json.decode) then
return vim.json.decode(j)
else
return require("gentags.commons._json").decode(j)
end
end

return M
Loading

0 comments on commit feaf610

Please sign in to comment.