Skip to content

Commit

Permalink
chore(commons): upgrade 'commons' (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Mar 6, 2024
1 parent 6fac13b commit 38b508e
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 42 deletions.
7 changes: 3 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

## Functions

- [ ] Use `GitLink` to copy git link.
- [ ] Use `GitLink!` to open git link in browser.
- [ ] Use `GitLink blame` to copy the `/blame` git link.
- [ ] Use `GitLink! blame` to open the `/blame` git link in browser.
- [ ] Use `GitLink(!)` to copy git link (or open in browser).
- [ ] Use `GitLink(!) blame` to copy the `/blame` link (or open in browser).
- [ ] Use `GitLink(!) default_branch` to open the `/main`/`/master` link in browser (or open in browser).
- [ ] Copy git link in a symlink directory of git repo.
- [ ] Copy git link in an un-pushed git branch, and receive an expected error.
- [ ] Copy git link in a pushed git branch but edited file, and receive a warning says the git link could be wrong.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ concurrency:
group: ${{ github.ref }}-ci
cancel-in-progress: true
jobs:
pr_conventional_commit:
name: Conventional Commit
commits:
name: Commits
if: ${{ github.ref != 'refs/heads/master' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ytanikin/PRConventionalCommits@1.1.0
with:
task_types: '["feat","fix","docs","test","ci","refactor","perf","chore","revert","break"]'
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
lint:
name: Lint
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ require('gitlinker').setup(opts)

The `opts` is an optional lua table that override the default options.

For complete default options, please see [configs.lua](https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker/configs.lua).
For complete default options, please see `Defaults` in [configs.lua](https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker/configs.lua).

### Customize Urls

Expand All @@ -181,7 +181,7 @@ For complete default options, please see [configs.lua](https://github.com/linron

> [!NOTE]
>
> Please refer to the `router` field in [configs.lua](https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker/configs.lua) for more examples about string template.
> Please refer to `Defaults.router` in [configs.lua](https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker/configs.lua) for more examples about string template.
To create customized urls for other git hosts, please bind the target git host name with a new router.

Expand Down
10 changes: 5 additions & 5 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local str = require("gitlinker.commons.str")
local async = require("gitlinker.commons.async")
local strings = require("gitlinker.commons.strings")
local LogLevels = require("gitlinker.commons.logging").LogLevels
local logging = require("gitlinker.commons.logging")

Expand Down Expand Up @@ -27,13 +27,13 @@ local function _url_template_engine(lk, template)
local i = 1
local n = string.len(template)
while i <= n do
local open_pos = strings.find(template, OPEN_BRACE, i)
local open_pos = str.find(template, OPEN_BRACE, i)
if not open_pos then
table.insert(exprs, { plain = true, body = string.sub(template, i) })
break
end
table.insert(exprs, { plain = true, body = string.sub(template, i, open_pos - 1) })
local close_pos = strings.find(template, CLOSE_BRACE, open_pos + string.len(OPEN_BRACE))
local close_pos = str.find(template, CLOSE_BRACE, open_pos + string.len(OPEN_BRACE))
assert(
type(close_pos) == "number" and close_pos > open_pos,
string.format(
Expand Down Expand Up @@ -72,7 +72,7 @@ local function _url_template_engine(lk, template)
PORT = lk.port or "",
USER = lk.user or "",
ORG = lk.org or "",
REPO = strings.endswith(lk.repo, ".git") and lk.repo:sub(1, #lk.repo - 4) or lk.repo,
REPO = str.endswith(lk.repo, ".git") and lk.repo:sub(1, #lk.repo - 4) or lk.repo,
REV = lk.rev,
FILE = lk.file,
LSTART = lk.lstart,
Expand Down Expand Up @@ -265,7 +265,7 @@ local function _parse_args(args)
local args_splits = vim.split(args, " ", { plain = true, trimempty = true })
for _, a in ipairs(args_splits) do
if string.len(a) > 0 then
if strings.startswith(a, "remote=") then
if str.startswith(a, "remote=") then
remote = a:sub(8)
else
router_type = a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local NVIM_VERSION_0_8 = false
local NVIM_VERSION_0_9 = false

do
NVIM_VERSION_0_8 = require("gitlinker.commons.versions").ge({ 0, 8 })
NVIM_VERSION_0_9 = require("gitlinker.commons.versions").ge({ 0, 9 })
NVIM_VERSION_0_8 = require("gitlinker.commons.version").ge({ 0, 8 })
NVIM_VERSION_0_9 = require("gitlinker.commons.version").ge({ 0, 9 })
end

local M = {}
Expand Down Expand Up @@ -69,10 +69,12 @@ M.get_hl = function(hl)
if NVIM_VERSION_0_9 then
return vim.api.nvim_get_hl(0, { name = hl, link = false })
else
---@diagnostic disable-next-line: undefined-field
local ok1, rgb_value = pcall(vim.api.nvim_get_hl_by_name, hl, true)
if not ok1 then
return vim.empty_dict()
end
---@diagnostic disable-next-line: undefined-field
local ok2, cterm_value = pcall(vim.api.nvim_get_hl_by_name, hl, false)
if not ok2 then
return vim.empty_dict()
Expand Down
1 change: 1 addition & 0 deletions lua/gitlinker/commons/async.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@diagnostic disable: luadoc-miss-module-name, undefined-doc-name
--- Small async library for Neovim plugins
--- @module async

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function FileLineReader:open(filename, batchsize)
if type(handler) ~= "number" then
error(
string.format(
"|commons.fileios - FileLineReader:open| failed to fs_open file: %s",
"|commons.fileio - FileLineReader:open| failed to fs_open file: %s",
vim.inspect(filename)
)
)
Expand All @@ -30,7 +30,7 @@ function FileLineReader:open(filename, batchsize)
if type(fstat) ~= "table" then
error(
string.format(
"|commons.fileios - FileLineReader:open| failed to fs_fstat file: %s",
"|commons.fileio - FileLineReader:open| failed to fs_fstat file: %s",
vim.inspect(filename)
)
)
Expand Down Expand Up @@ -67,7 +67,7 @@ function FileLineReader:_read_chunk()
if read_err then
error(
string.format(
"|commons.fileios - FileLineReader:_read_chunk| failed to fs_read file: %s, read_error:%s, read_name:%s",
"|commons.fileio - FileLineReader:_read_chunk| failed to fs_read file: %s, read_error:%s, read_name:%s",
vim.inspect(self.filename),
vim.inspect(read_err),
vim.inspect(read_name)
Expand All @@ -91,12 +91,12 @@ end
function FileLineReader:next()
--- @return string?
local function impl()
local strings = require("gitlinker.commons.strings")
local str = require("gitlinker.commons.str")
if self.buffer == nil then
return nil
end
self.buffer = self.buffer:gsub("\r\n", "\n")
local nextpos = strings.find(self.buffer, "\n")
local nextpos = str.find(self.buffer, "\n")
if nextpos then
local line = self.buffer:sub(1, nextpos - 1)
self.buffer = self.buffer:sub(nextpos + 1)
Expand Down Expand Up @@ -293,10 +293,13 @@ end
M.asyncreadlines = function(filename, opts)
assert(type(opts) == "table")
assert(type(opts.on_line) == "function")
---@diagnostic disable-next-line: undefined-field
local batchsize = opts.batchsize or 4096

local function _handle_error(err, msg)
---@diagnostic disable-next-line: undefined-field
if type(opts.on_error) == "function" then
---@diagnostic disable-next-line: undefined-field
opts.on_error(err)
else
error(
Expand Down Expand Up @@ -333,11 +336,11 @@ M.asyncreadlines = function(filename, opts)
local buffer = nil

local function _process(buf, fn_line_processor)
local strings = require("gitlinker.commons.strings")
local str = require("gitlinker.commons.str")

local i = 1
while i <= #buf do
local newline_pos = strings.find(buf, "\n", i)
local newline_pos = str.find(buf, "\n", i)
if not newline_pos then
break
end
Expand Down Expand Up @@ -388,7 +391,9 @@ M.asyncreadlines = function(filename, opts)
if close_complete_err then
_handle_error(close_complete_err, "fs_close complete")
end
---@diagnostic disable-next-line: undefined-field
if type(opts.on_complete) == "function" then
---@diagnostic disable-next-line: undefined-field
opts.on_complete(fsize)
end
end
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions lua/gitlinker/commons/logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ local FORMATTING_TAGS = {
--- @param meta table<string,any>
--- @return string
function Formatter:format(meta)
local strings = require("gitlinker.commons.strings")
local str = require("gitlinker.commons.str")

local n = string.len(self.fmt)

Expand All @@ -94,7 +94,7 @@ function Formatter:format(meta)
return false
end

return strings.startswith(string.sub(self.fmt, idx, endpos), FORMATTING_TAGS[tag])
return str.startswith(string.sub(self.fmt, idx, endpos), FORMATTING_TAGS[tag])
end
return impl
end
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ M.normalize = function(p, opts)
-- )
end

return result
return M._normalize_slash(result, opts)
end

--- @param ... any
Expand Down Expand Up @@ -209,8 +209,8 @@ end
M.parent = function(p)
p = p or vim.fn.getcwd()

local strings = require("gitlinker.commons.strings")
if strings.endswith(p, "/") or strings.endswith(p, "\\") then
local str = require("gitlinker.commons.str")
if str.endswith(p, "/") or str.endswith(p, "\\") then
p = string.sub(p, 1, #p - 1)
end

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions lua/gitlinker/commons/spawn.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local NVIM_VERSION_0_10 = false

do
NVIM_VERSION_0_10 = require("gitlinker.commons.versions").ge({ 0, 10 })
NVIM_VERSION_0_10 = require("gitlinker.commons.version").ge({ 0, 10 })
end

local M = {}
Expand All @@ -27,11 +27,11 @@ M.run = function(cmd, opts, on_exit)
--- @param fn_line_processor commons.SpawnLineProcessor
--- @return integer
local function _process(buffer, fn_line_processor)
local strings = require("gitlinker.commons.strings")
local str = require("gitlinker.commons.str")

local i = 1
while i <= #buffer do
local newline_pos = strings.find(buffer, "\n", i)
local newline_pos = str.find(buffer, "\n", i)
if not newline_pos then
break
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ M.setchar = function(s, pos, ch)
assert(string.len(ch) == 1)

local n = string.len(s)
pos = require("gitlinker.commons.tables").list_index(pos, n)
pos = require("gitlinker.commons.tbl").list_index(pos, n)

local buffer = ""
if pos > 1 then
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lua/gitlinker/commons/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.1.0
11.0.0
6 changes: 3 additions & 3 deletions lua/gitlinker/path.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
--- @param cwd string?
--- @return string?
local function buffer_relpath(cwd)
local paths = require("gitlinker.commons.paths")
local path = require("gitlinker.commons.path")

cwd = cwd or vim.fn.getcwd()
cwd = vim.fn.resolve(cwd)
cwd = paths.normalize(cwd, { double_backslash = true, expand = true })
cwd = path.normalize(cwd, { double_backslash = true, expand = true })

local bufpath = vim.api.nvim_buf_get_name(0)
bufpath = vim.fn.resolve(bufpath)
bufpath = paths.normalize(bufpath, { double_backslash = true, expand = true })
bufpath = path.normalize(bufpath, { double_backslash = true, expand = true })

-- logger.debug(
-- "|path.buffer_relpath| enter, cwd:%s, bufpath:%s",
Expand Down
9 changes: 5 additions & 4 deletions lua/gitlinker/routers.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local strings = require("gitlinker.commons.strings")
local range = require("gitlinker.range")
local str = require("gitlinker.commons.str")
local logging = require("gitlinker.commons.logging")

local range = require("gitlinker.range")

--- @class gitlinker.Builder
--- @field domain string?
--- @field org string?
Expand Down Expand Up @@ -80,11 +81,11 @@ function Builder:new(lk, range_maker)
local o = {
domain = string.format("https://%s", lk.host),
org = lk.org,
repo = strings.endswith(lk.repo, ".git") and lk.repo:sub(1, #lk.repo - 4) or lk.repo,
repo = str.endswith(lk.repo, ".git") and lk.repo:sub(1, #lk.repo - 4) or lk.repo,
rev = lk.rev,
location = string.format(
"%s%s",
lk.file .. (strings.endswith(lk.file, ".md", { ignorecase = true }) and "?plain=1" or ""),
lk.file .. (str.endswith(lk.file, ".md", { ignorecase = true }) and "?plain=1" or ""),
type(r) == "string" and r or ""
),
}
Expand Down
File renamed without changes.

0 comments on commit 38b508e

Please sign in to comment.