Skip to content

Commit 17eb242

Browse files
committed
Added type annotations (to be reviewed)
1 parent 6dc763c commit 17eb242

10 files changed

Lines changed: 132 additions & 44 deletions

File tree

lua/clapi/finder.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ local make_entry = require("clapi.make_entry")
55
local parser = require("clapi.parser.init")
66
local async = require("plenary.async")
77

8+
---@class FinderModule
89
local M = {}
910

11+
---Build and display a telescope picker with module interface
12+
---@param opts? table Configuration options
13+
---@param opts.bufnr? integer Buffer number, defaults to current buffer (0)
14+
---@param opts.path_display? table|string How to display paths
15+
---@param opts.entry_maker? function Custom entry maker function
16+
---@return nil
1017
M.builtin = function(opts)
1118
opts = opts or {}
1219
opts.bufnr = opts.bufnr or 0

lua/clapi/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local finder = require("clapi.finder")
22

3+
---@class CLAPI
4+
---@field builtin fun(opts?: table): nil
35
local M = {}
46

57
-- TODO: Unit test the whole plugin

lua/clapi/lsp.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
local utils = require("clapi.utils")
22
local async = require("plenary.async")
33

4+
---@class LSPModule
45
local M = {}
56

6-
---@param opts table
7+
---Gets a file path from a position using LSP
8+
---@param opts table Options for the LSP definition request
9+
---@param opts.bufnr? integer Buffer number, defaults to current buffer (0)
10+
---@param opts.position table The position in the buffer {line, character}
11+
---@param opts.position.line integer Line number (0-indexed)
12+
---@param opts.position.character integer Character/column position (0-indexed)
13+
---@return string|nil filepath The file path or nil if not found
714
M.get_file_from_position = async.wrap(function(opts, callback)
815
opts = opts or {}
916
opts.bufnr = opts.bufnr or 0
@@ -19,6 +26,8 @@ M.get_file_from_position = async.wrap(function(opts, callback)
1926

2027
local callback_called = false
2128

29+
---Safe callback to prevent multiple callbacks
30+
---@param result string|nil The file path or nil
2231
local function safe_callback(result)
2332
if not callback_called then
2433
callback_called = true

lua/clapi/make_entry.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ local make_entry = require("telescope.make_entry")
55
local utils = require("telescope.utils")
66
local entry_display = require("telescope.pickers.entry_display")
77

8+
---@class MakeEntry
89
local M = {}
910

11+
---@type table<string, string>
1012
local lsp_type_highlight = {
1113
["Class"] = "TelescopeResultsClass",
1214
["Constant"] = "TelescopeResultsConstant",
@@ -18,8 +20,12 @@ local lsp_type_highlight = {
1820
["Variable"] = "TelescopeResultsVariable",
1921
}
2022

23+
---Get the filename function with caching
24+
---@return function
2125
local get_filename_fn = function()
2226
local bufnr_name_cache = {}
27+
---@param bufnr? integer Buffer number
28+
---@return string filename
2329
return function(bufnr)
2430
bufnr = vim.F.if_nil(bufnr, 0)
2531
local c = bufnr_name_cache[bufnr]
@@ -33,6 +39,15 @@ local get_filename_fn = function()
3339
end
3440
end
3541

42+
---Generate an entry function for LSP symbols with visibility
43+
---@param opts? table Options for customizing entry display
44+
---@param opts.bufnr? integer Buffer number
45+
---@param opts.symbol_width? integer Width for symbol columns
46+
---@param opts.symbol_type_width? integer Width for symbol type column
47+
---@param opts.fname_width? integer Width for filename column
48+
---@param opts.show_line? boolean Whether to show the line content
49+
---@param opts.symbol_highlights? table<string, string> Custom highlights for symbol types
50+
---@return function entry_maker Function to create entries
3651
function M.gen_from_lsp_symbols(opts)
3752
opts = opts or {}
3853

@@ -64,6 +79,9 @@ function M.gen_from_lsp_symbols(opts)
6479
})
6580
local type_highlight = vim.F.if_nil(opts.symbol_highlights or lsp_type_highlight)
6681

82+
---Create display for an entry
83+
---@param entry table The entry to display
84+
---@return string
6785
local make_display = function(entry)
6886
local msg
6987

@@ -96,6 +114,8 @@ function M.gen_from_lsp_symbols(opts)
96114
end
97115

98116
local get_filename = get_filename_fn()
117+
---@param entry table Raw entry from finder
118+
---@return table Processed entry
99119
return function(entry)
100120
local filename = vim.F.if_nil(entry.filename, get_filename(entry.bufnr))
101121
local symbol_msg = entry.text

lua/clapi/parser/__get_query.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local utils = require("clapi.utils")
22

3-
---@param lang string
4-
---@param query_group string
3+
---Get a treesitter query for a specific language and query group
4+
---@param lang string The language to get the query for
5+
---@param query_group string The query group name (e.g., "locals", "parent")
6+
---@return string|nil query The query string or nil if not found
57
return function(lang, query_group)
68
-- TODO: nil check
79
local results = vim.api.nvim_get_runtime_file(string.format("queries/%s/%s.scm", lang, query_group), true)

lua/clapi/parser/__parser.lua

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
---@class Parser
2+
---@field private __index any
23
local Parser = {}
34

5+
---Create a new parser instance
6+
---@param o? table Optional initial object
7+
---@return Parser
48
function Parser:new(o)
59
o = o or {}
610
setmetatable(o, self)
711
self.__index = self
812
return o
913
end
1014

15+
---Get visibility modifier of a node
1116
---@param node TSNode The treesitter node of the visibility modifier
1217
---@param bufnr integer The buffer number of the source file
1318
---@return string visibility The visibility modifier of the node
1419
function Parser.get_visibility(node, bufnr)
1520
error("Not implemented!")
1621
end
1722

18-
---@param node TSNode
19-
---@param start_col integer
20-
---@param start_row integer
21-
---@param opts table
23+
---Parse a method node
24+
---@param node TSNode The treesitter node of the method
25+
---@param start_col integer Starting column of the node
26+
---@param start_row integer Starting row of the node
27+
---@param opts table Additional options
28+
---@return table Method information
2229
function Parser.parse_method(node, start_col, start_row, opts)
2330
error("Not implemented!")
2431
end
2532

26-
---@param node TSNode
27-
---@param start_col integer
28-
---@param start_row integer
29-
---@param opts table
33+
---Parse a constant node
34+
---@param node TSNode The treesitter node of the constant
35+
---@param start_col integer Starting column of the node
36+
---@param start_row integer Starting row of the node
37+
---@param opts table Additional options
38+
---@return table Constant information
3039
function Parser.parse_constant(node, start_col, start_row, opts)
3140
error("Not implemented!")
3241
end
3342

34-
---@param node TSNode
35-
---@param start_col integer
36-
---@param start_row integer
37-
---@param opts table
43+
---Parse a property node
44+
---@param node TSNode The treesitter node of the property
45+
---@param start_col integer Starting column of the node
46+
---@param start_row integer Starting row of the node
47+
---@param opts table Additional options
48+
---@return table Property information
3849
function Parser.parse_property(node, start_col, start_row, opts)
3950
error("Not implemented!")
4051
end

lua/clapi/parser/__parser_java.lua

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
local Parser = require("clapi.parser.__parser")
22

3+
---@class JavaParser : Parser
34
local JavaParser = Parser:new()
45

5-
---@param node TSNode The treesitter node of the visibility modifier
6+
---Extract visibility from Java modifiers
7+
---@param node TSNode The treesitter node containing modifiers
68
---@param bufnr integer The buffer number of the source file
79
---@return string visibility The visibility modifier of the node
810
local function _get_visibility_from_modifiers(node, bufnr)
@@ -21,6 +23,7 @@ local function _get_visibility_from_modifiers(node, bufnr)
2123
return "private"
2224
end
2325

26+
---Get visibility modifier of a node
2427
---@param node TSNode The treesitter node of the visibility modifier
2528
---@param bufnr integer The buffer number of the source file
2629
---@return string visibility The visibility modifier of the node
@@ -34,10 +37,12 @@ function JavaParser.get_visibility(node, bufnr)
3437
return "private"
3538
end
3639

37-
---@param node TSNode
38-
---@param start_col integer
39-
---@param start_row integer
40-
---@param opts table
40+
---Parse a property node
41+
---@param node TSNode The treesitter node of the property
42+
---@param start_col integer Starting column of the node
43+
---@param start_row integer Starting row of the node
44+
---@param opts table Additional options
45+
---@return table PropertyInfo Property information
4146
function JavaParser.parse_property(node, start_col, start_row, opts)
4247
local visibility = JavaParser.get_visibility(node:parent():parent(), opts.bufnr)
4348
local text = vim.treesitter.get_node_text(node, opts.bufnr)
@@ -51,10 +56,12 @@ function JavaParser.parse_property(node, start_col, start_row, opts)
5156
}
5257
end
5358

54-
---@param node TSNode
55-
---@param start_col integer
56-
---@param start_row integer
57-
---@param opts table
59+
---Parse a method node
60+
---@param node TSNode The treesitter node of the method
61+
---@param start_col integer Starting column of the node
62+
---@param start_row integer Starting row of the node
63+
---@param opts table Additional options
64+
---@return table MethodInfo Method information
5865
function JavaParser.parse_method(node, start_col, start_row, opts)
5966
local visibility = JavaParser.get_visibility(node:parent(), opts.bufnr)
6067
local text = vim.treesitter.get_node_text(node, opts.bufnr)

lua/clapi/parser/__parser_php.lua

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local Parser = require("clapi.parser.__parser")
22

3+
---@class PhpParser : Parser
34
local PhpParser = Parser:new()
45

6+
---Get visibility modifier of a node
57
---@param node TSNode The treesitter node of the visibility modifier
68
---@param bufnr integer The buffer number of the source file
79
---@return string visibility The visibility modifier of the node
@@ -15,10 +17,12 @@ function PhpParser.get_visibility(node, bufnr)
1517
return "public" -- Public by default (TODO: review this wih more languages)
1618
end
1719

18-
---@param node TSNode
19-
---@param start_col integer
20-
---@param start_row integer
21-
---@param opts table
20+
---Parse a method node
21+
---@param node TSNode The treesitter node of the method
22+
---@param start_col integer Starting column of the node
23+
---@param start_row integer Starting row of the node
24+
---@param opts table Additional options
25+
---@return table MethodInfo Method information
2226
function PhpParser.parse_method(node, start_col, start_row, opts)
2327
local visibility = PhpParser.get_visibility(node:parent(), opts.bufnr)
2428
local text = vim.treesitter.get_node_text(node, opts.bufnr)
@@ -32,10 +36,12 @@ function PhpParser.parse_method(node, start_col, start_row, opts)
3236
}
3337
end
3438

35-
---@param node TSNode
36-
---@param start_col integer
37-
---@param start_row integer
38-
---@param opts table
39+
---Parse a constant node
40+
---@param node TSNode The treesitter node of the constant
41+
---@param start_col integer Starting column of the node
42+
---@param start_row integer Starting row of the node
43+
---@param opts table Additional options
44+
---@return table ConstantInfo Constant information
3945
function PhpParser.parse_constant(node, start_col, start_row, opts)
4046
local text = vim.treesitter.get_node_text(node, opts.bufnr)
4147
local visibility = PhpParser.get_visibility(node:parent():parent(), opts.bufnr)
@@ -49,10 +55,12 @@ function PhpParser.parse_constant(node, start_col, start_row, opts)
4955
}
5056
end
5157

52-
---@param node TSNode
53-
---@param start_col integer
54-
---@param start_row integer
55-
---@param opts table
58+
---Parse a property node
59+
---@param node TSNode The treesitter node of the property
60+
---@param start_col integer Starting column of the node
61+
---@param start_row integer Starting row of the node
62+
---@param opts table Additional options
63+
---@return table PropertyInfo Property information
5664
function PhpParser.parse_property(node, start_col, start_row, opts)
5765
local parent = node:parent()
5866
local prop_parent = parent

lua/clapi/parser/init.lua

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ local tsparsers = require("nvim-treesitter.parsers")
44
local async = require("plenary.async")
55
local lsp = require("clapi.lsp")
66

7+
---@class ParserModule
78
local M = {}
89

9-
---@param lang string
10+
---Get a parser for a specific language
11+
---@param lang string Language name
1012
---@return Parser|nil
1113
local function get_parser(lang)
1214
if not lang then
@@ -17,6 +19,7 @@ local function get_parser(lang)
1719
return
1820
end
1921

22+
---@type table<string, Parser>
2023
local parsers = {
2124
["php"] = require("clapi.parser.__parser_php"),
2225
["java"] = require("clapi.parser.__parser_java"),
@@ -34,8 +37,11 @@ local function get_parser(lang)
3437

3538
return parser:new()
3639
end
37-
---
38-
---@param opts table
40+
41+
---Get parent class file and parse its definitions
42+
---@param opts table Options for parsing
43+
---@param opts.bufnr? integer Buffer number, defaults to current buffer (0)
44+
---@return table|nil definitions List of definitions from parent class or nil if error
3945
local get_parent_file = async.wrap(function(opts, callback)
4046
opts = opts or {}
4147
opts.bufnr = opts.bufnr or 0
@@ -89,7 +95,7 @@ local get_parent_file = async.wrap(function(opts, callback)
8995
local result = {}
9096

9197
async.run(function()
92-
for id, node, metadata in query:iter_captures(root, opts.bufnr) do
98+
for id, node, _ in query:iter_captures(root, opts.bufnr) do
9399
local capture_name = query.captures[id]
94100
if capture_name == "parent" then
95101
local line, char = node:start()
@@ -114,7 +120,14 @@ local get_parent_file = async.wrap(function(opts, callback)
114120
end)
115121
end, 2)
116122

117-
---@param opts table
123+
---Parse a file for LSP symbols
124+
---@param opts table Options for parsing
125+
---@param opts.filename? string Path to the file (cannot be used with bufnr)
126+
---@param opts.bufnr? integer Buffer number (cannot be used with filename)
127+
---@param opts.class_name? string Name of the class to filter results
128+
---@param opts.filetype? string Force filetype instead of detecting from extension
129+
---@param opts.query_str? string Custom query string instead of loading from queries
130+
---@return table|nil results List of symbols found or nil if error
118131
M.parse_file = async.wrap(function(opts, callback)
119132
opts = opts or {}
120133
opts.class_name = opts.class_name and string.format("%s::", opts.class_name) or ""

lua/clapi/utils.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
---@class Utils
12
local utils = {}
23

34
--- Clapi Wrapper around vim.notify
4-
---@param funname string: name of the function that will be
5-
---@param opts table: opts.level string, opts.msg string, opts.once bool
5+
---@param funname string Name of the function that will be logged
6+
---@param opts table Options for notification
7+
---@param opts.level string The log level (ERROR, WARN, INFO, DEBUG, TRACE)
8+
---@param opts.msg string The message to display
9+
---@param opts.once? boolean Whether to only notify once, defaults to false
610
function utils.notify(funname, opts)
711
opts.once = vim.F.if_nil(opts.once, false)
812
local level = vim.log.levels[opts.level]
@@ -15,6 +19,9 @@ function utils.notify(funname, opts)
1519
})
1620
end
1721

22+
---Read file contents from a path
23+
---@param path string The path to the file
24+
---@return string|nil content The file content or nil if file doesn't exist
1825
function utils.read_file(path)
1926
-- Check if the file exists
2027
if vim.fn.filereadable(path) == 0 then
@@ -29,7 +36,9 @@ function utils.read_file(path)
2936
return content
3037
end
3138

32-
---@param filepath string
39+
---Get the file extension from a filepath
40+
---@param filepath string The path to the file
41+
---@return string|nil extension The file extension or nil if not found
3342
function utils.get_file_extension(filepath)
3443
-- Find the last dot position
3544
local lastDotIndex = filepath:match("^.+()%.%w+$")

0 commit comments

Comments
 (0)