Skip to content
Open
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
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,46 @@ jobs:

- name: run tests
run: ./run test

stylua:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: format
uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --check lua tests

lint:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
- uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: nightly

- name: install lua-language-server
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd
gh release download -R sumneko/lua-language-server -p '*-linux-x64.tar.gz' -D lua-language-server
tar xzf lua-language-server/* -C lua-language-server
echo "${PWD}/lua-language-server/bin" >> $GITHUB_PATH
export PATH="${PWD}/lua-language-server/bin:${PATH}"
lua-language-server --version

- name: install luacheck
run: |
sudo apt-get update
sudo apt-get install -y luarocks
sudo luarocks install luacheck

- name: lint
run: ./run lint
19 changes: 19 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
read_globals = { "vim" }

max_comment_line_length = false
codes = true

exclude_files = {}

ignore = {
"111", -- setting non-standard global variable
"113", -- accessing undefined variable
"211", -- unused function
"212", -- unused self
"311", -- unused variable
"431", -- shadow upvalue self
"542", -- empty if branch
"631", -- line is too long
}

read_globals = { "vim" }
32 changes: 32 additions & 0 deletions .luarc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
"runtime": { "version": "LuaJIT" },
"workspace": {
"library": [
"$VIMRUNTIME",
"${3rd}/busted/library",
"${3rd}/luassert/library",
"./lua"
]
},
"diagnostics": {
"libraryFiles": "Disable",
"groupFileStatus": {
"strict": "Opened",
"strong": "Opened",
"ambiguity": "Opened",
"duplicate": "Opened",
"global": "Opened",
"luadoc": "Opened",
"redefined": "Opened",
"type-check": "Opened",
"unbalanced": "Opened",
"unused": "Opened"
},
"groupSeverity": {
"strong": "Warning",
"strict": "Warning"
},
"unusedLocalExclude": ["_*"]
}
}
49 changes: 24 additions & 25 deletions lua/dial/augend/case.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ local util = require "dial.util"
local M = {}

---@alias casetype '"PascalCase"' | '"camelCase"' | '"snake_case"' | '"kebab-case"' | '"SCREAMING_SNAKE_CASE"'
---@alias extractf fun(word: string) -> string[] | nil
---@alias constractf fun(terms: string[]) -> string
---@alias extractf fun(word: string): string[]|nil
---@alias constractf fun(terms: string[]): string
---@alias casepattern { word_regex: string, extract: extractf, constract: constractf }

---@class AugendCase
---@implement Augend
---@class AugendCase: Augend
---@field config { types: casetype[], cyclic: boolean }
---@field patterns casepattern[]
local AugendCase = {}
Expand All @@ -27,7 +26,7 @@ M.case_patterns["camelCase"] = {
local ptr = 1
for i = 1, word:len(), 1 do
local char = word:sub(i, i)
if not (char == char:lower()) then
if char ~= char:lower() then
-- i 番目の文字が大文字の場合は直前で切る
-- 小文字や数字などは切らない
if i == 1 then
Expand All @@ -39,9 +38,7 @@ M.case_patterns["camelCase"] = {
end
end
table.insert(subwords, word:sub(ptr, word:len()))
return vim.tbl_map(function(s)
return s:lower()
end, subwords)
return vim.tbl_map(string.lower, subwords)
end,

---@param terms string[]
Expand Down Expand Up @@ -70,17 +67,15 @@ M.case_patterns["PascalCase"] = {
local ptr = 1
for i = 2, word:len(), 1 do
local char = word:sub(i, i)
if not (char == char:lower()) then
if char ~= char:lower() then
-- i 番目の文字が大文字の場合は直前で切る
-- 小文字や数字などは切らない
table.insert(subwords, word:sub(ptr, i - 1))
ptr = i
end
end
table.insert(subwords, word:sub(ptr, word:len()))
return vim.tbl_map(function(s)
return s:lower()
end, subwords)
return vim.tbl_map(string.lower, subwords)
end,

---@param terms string[]
Expand Down Expand Up @@ -163,9 +158,7 @@ M.case_patterns["SCREAMING_SNAKE_CASE"] = {
end
end
table.insert(subwords, word:sub(ptr, word:len()))
return vim.tbl_map(function(s)
return s:lower()
end, subwords)
return vim.tbl_map(string.lower, subwords)
end,

---@param terms string[]
Expand All @@ -176,7 +169,7 @@ M.case_patterns["SCREAMING_SNAKE_CASE"] = {
}

---@param config { types: casetype[], cyclic?: boolean }
---@return Augend
---@return AugendCase
function M.new(config)
vim.validate("cyclic", config.cyclic, "boolean", true)

Expand All @@ -195,10 +188,16 @@ function M.new(config)
end
return false
end)
local patterns = vim.tbl_map(function(type)
return M.case_patterns[type]
end, config.types)

local patterns = vim.tbl_map(
---@overload fun(type: casetype): casepattern
function(type)
return M.case_patterns[type]
end,
config.types
)

-- luacheck: ignore
-- local query = prefix .. util.if_expr(natural, "", "-?") .. "[" .. radix_to_query_character(radix) .. delimiter .. "]+"
return setmetatable({
patterns = patterns,
Expand All @@ -214,7 +213,7 @@ function AugendCase:find(line, cursor)
local most_front_range = nil

for _, caseptn in ipairs(self.patterns) do
---@type textrange
---@type textrange?
local range = common.find_pattern_regex(caseptn.word_regex)(line, cursor)
if range ~= nil then
if most_front_range == nil or range.from < most_front_range.from then
Expand All @@ -227,9 +226,9 @@ end

---@param text string
---@param addend integer
---@param cursor? integer
---@return { text?: string, cursor?: integer }
function AugendCase:add(text, addend, cursor)
---@param _cursor? integer
---@return addresult
function AugendCase:add(text, addend, _cursor)
local len_patterns = #self.patterns
---@type integer
local index
Expand All @@ -243,7 +242,7 @@ function AugendCase:add(text, addend, cursor)

local terms = self.patterns[index].extract(text)

local new_index
local new_index ---@type integer
if self.config.cyclic then
new_index = (len_patterns + (index - 1 + addend) % len_patterns) % len_patterns + 1
else
Expand All @@ -258,7 +257,7 @@ function AugendCase:add(text, addend, cursor)
if new_index == index then
return { cursor = text:len() }
end
text = self.patterns[new_index].constract(terms)
text = self.patterns[new_index].constract(assert(terms))
return { text = text, cursor = text:len() }
end

Expand Down
5 changes: 3 additions & 2 deletions lua/dial/augend/common.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
-- augend で共通して用いられる関数。

local util = require "dial.util"

local M = {}

---augend の find field を簡単に実装する。
Expand Down Expand Up @@ -55,7 +53,9 @@ function M.find_pattern_regex(ptn, allow_match_before_cursor)
local s, e = vim.regex(ptn):match_str(line:sub(idx_start))

if s then
---@type integer
s = s + idx_start -- 上で得られた s は相対位置なので
---@type integer
e = e + idx_start - 1 -- 上で得られた s は相対位置なので

-- 検索結果があったら
Expand All @@ -81,6 +81,7 @@ function M.find_pattern_regex(ptn, allow_match_before_cursor)
end

---@param elems string[]
---@return string
function M.enum_to_regex(elems)
return table.concat(elems, [[\|]])
end
Expand Down
26 changes: 15 additions & 11 deletions lua/dial/augend/constant.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ local common = require "dial.augend.common"

---@alias AugendConstantConfig { elements: string[], cyclic: boolean, pattern_regexp: string, preserve_case: boolean, match_before_cursor: boolean }

---@class AugendConstant
---@implement Augend
---@class AugendConstant: Augend
---@field config AugendConstantConfig
local AugendConstant = {}

Expand Down Expand Up @@ -34,7 +33,7 @@ local function preserve_case(word)
end

---@param config { elements: string[], word?: boolean, cyclic?: boolean, pattern_regexp?: string, preserve_case?: boolean, match_before_cursor?: boolean }
---@return Augend
---@return AugendConstant
function M.new(config)
util.validate_list("config.elements", config.elements, "string")

Expand Down Expand Up @@ -69,22 +68,28 @@ end
---@param cursor? integer
---@return textrange?
function AugendConstant:find(line, cursor)
local escaped_elements = vim.tbl_map(function(e)
return vim.fn.escape(e, [[/\]])
end, self.config.elements)
local escaped_elements = vim.tbl_map(
---@param e string
---@return string
function(e)
return vim.fn.escape(e, [[/\]])
end,
self.config.elements
)
local vim_regex_ptn = self.config.pattern_regexp:format(table.concat(escaped_elements, [[\|]]))
return common.find_pattern_regex(vim_regex_ptn, self.config.match_before_cursor)(line, cursor)
end

---@param text string
---@param addend integer
---@param cursor? integer
---@return { text?: string, cursor?: integer }
function AugendConstant:add(text, addend, cursor)
---@param _cursor? integer
---@return addresult
function AugendConstant:add(text, addend, _cursor)
local elements = self.config.elements
local n_patterns = #elements
local n = 1

---@type fun(elem: string): boolean
local query
if self.config.preserve_case then
query = function(elem)
Expand Down Expand Up @@ -128,8 +133,7 @@ function AugendConstant:add(text, addend, cursor)
text = new_text
end

cursor = #text
return { text = text, cursor = cursor }
return { text = text, cursor = #text }
end

M.alias = {
Expand Down
Loading