Skip to content

Commit

Permalink
chore: types (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: tris203 <admin@snappeh.com>
  • Loading branch information
tris203 and tris203 authored Dec 30, 2023
1 parent 3a9def0 commit ce2a3f9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
7 changes: 6 additions & 1 deletion lua/hawtkeys/duplicates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ local M = {}
local utils = require("hawtkeys.utils")
local tsSearch = require("hawtkeys.ts")

---@return table
---@class HawtkeysDuplicatesData
---@field key string
---@field file1 HawtkeysKeyMapData
---@field file2 HawtkeysKeyMapData

---@return HawtkeysDuplicatesData[]
function M.show_duplicates()
local allKeys = tsSearch.get_all_keymaps()
local duplicates = utils.find_duplicates(allKeys)
Expand Down
7 changes: 5 additions & 2 deletions lua/hawtkeys/score.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ end

-- Function to generate all possible two-character combinations
---@param str string
---@return table
---@return string[]
local function generate_combos(str)
str = str:gsub(hawtkeys.config.leader, "")
local pairs = {}
Expand All @@ -104,8 +104,11 @@ local function generate_combos(str)
return pairs
end

---@class HawtkeysScoreData
---@field [string] integer

---@param str string
---@return table
---@return HawtkeysScoreData[]
local function find_matches(str)
local combinations = generate_combos(str)
local scores = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/hawtkeys/show_all.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}
local tsSearch = require("hawtkeys.ts")

---@return table
---@return HawtkeysKeyMapData
function M.show_all()
return tsSearch.get_all_keymaps()
end
Expand Down
28 changes: 19 additions & 9 deletions lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ local tsQuery = require("nvim-treesitter.query")

---@alias VimModes 'n' | 'x' | 'v' | 'i'

---@class HawtkeysKeyMapData
---@field lhs string
---@field rhs string
---@field mode VimModes
---@field from_file string

---@alias WhichKeyMethods 'which_key'

---@alias LazyMethods 'lazy'
Expand Down Expand Up @@ -57,7 +63,7 @@ local function build_dot_index_expression_query(mapDefs)
return string.format(query, build_args(mapDefs, "dot_index_expression"))
end

---@param mapDefs TSKeyMapArgs[]
---@param mapDefs WhichKeyMapargs[]
---@return string
local function build_which_key_query(mapDefs)
local query = [[
Expand All @@ -80,7 +86,7 @@ local function build_function_call_query(mapDefs)
end

---@param node TSNode
---@param indexData TSKeyMapArgs | WhichKeyMapargs
---@param indexData TSKeyMapArgs
---@param targetData string
---@param fileContent string
---@return string
Expand Down Expand Up @@ -112,7 +118,7 @@ local function return_field_data(node, indexData, targetData, fileContent)
end

---@param dir string
---@return table
---@return string[]
local function find_files(dir)
-- print("Scanning dir" .. dir)
local dirScan = dir or vim.fn.stdpath("config")
Expand All @@ -121,7 +127,7 @@ local function find_files(dir)
end

---@param filePath string
---@return table
---@return HawtkeysKeyMapData[]
local function find_maps_in_file(filePath)
if scannedFiles[filePath] then
-- already scanned
Expand Down Expand Up @@ -157,7 +163,8 @@ local function find_maps_in_file(filePath)
node.node:parent():child(0),
fileContent
)
local mapDef = hawtkeys.config.keyMapSet[parent]
--@type TSKeyMapArgs
local mapDef = hawtkeys.config.keyMapSet[parent] --[[@as TSKeyMapArgs]]
---@type string
local mode = return_field_data(
node.node,
Expand Down Expand Up @@ -195,6 +202,7 @@ local function find_maps_in_file(filePath)
end

if not bufLocal then
---@type HawtkeysKeyMapData
local map = {
mode = mode,
lhs = lhs,
Expand Down Expand Up @@ -243,7 +251,7 @@ local function find_maps_in_file(filePath)
node.node:parent():child(0),
fileContent
)
local mapDef = hawtkeys.config.keyMapSet[parent]
local mapDef = hawtkeys.config.keyMapSet[parent] --[[@as TSKeyMapArgs]]
---@type string
local mode = return_field_data(
node.node,
Expand Down Expand Up @@ -281,6 +289,7 @@ local function find_maps_in_file(filePath)
end

if not bufLocal then
---@type HawtkeysKeyMapData
local map = {
mode = mode,
lhs = lhs,
Expand Down Expand Up @@ -358,7 +367,7 @@ local function find_maps_in_file(filePath)
return tsKeymaps
end

---@return table
---@return HawtkeysKeyMapData[]
local function get_keymaps_from_lazy()
local lazyKeyMaps = {}
for _, args in pairs(hawtkeys.config.keyMapSet) do
Expand Down Expand Up @@ -400,7 +409,7 @@ local function get_keymaps_from_lazy()
return lazyKeyMaps
end

---@return table
---@return HawtkeysKeyMapData[]
local function get_keymaps_from_vim()
local vimKeymaps = {}

Expand Down Expand Up @@ -437,12 +446,13 @@ local function get_runtime_path()
return vim.api.nvim_list_runtime_paths()
end

---@return table
---@return HawtkeysKeyMapData[]
function M.get_all_keymaps()
local returnKeymaps
--[[ if next(returnKeymaps) ~= nil then
return returnKeymaps
end ]]
---@type HawtkeysKeyMapData[]
local keymaps = {}

if M._testing then
Expand Down
8 changes: 5 additions & 3 deletions lua/hawtkeys/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function M.top5(scores_table)
return top_list
end

---@param t1 table
---@param t2 table
---@return table
---@param t1 HawtkeysKeyMapData[]
---@param t2 HawtkeysKeyMapData[]
---@return HawtkeysKeyMapData[]
function M.merge_tables(t1, t2)
local t3 = {}

Expand Down Expand Up @@ -68,6 +68,8 @@ function M.merge_tables(t1, t2)
return t3
end

---@param keymaps HawtkeysKeyMapData[]
---@return { [string]: HawtkeysKeyMapData[] }
function M.find_duplicates(keymaps)
local duplicates = {}
for _, v in pairs(keymaps) do
Expand Down
2 changes: 0 additions & 2 deletions tests/hawtkeys/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local utils = require("hawtkeys.utils")
---@diagnostic disable-next-line: undefined-field
local eq = assert.are.same
---@diagnostic disable-next-line: undefined-field
local falsy = assert.falsy

describe("utils functionality", function()
it("can find duplicate lhs in table", function()
Expand Down

0 comments on commit ce2a3f9

Please sign in to comment.