Skip to content

Commit a5fe434

Browse files
committed
show_inherited option
1 parent 372fa85 commit a5fe434

4 files changed

Lines changed: 114 additions & 14 deletions

File tree

doc/clapi.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ The clapi picker accepts the following options:
9999
- `bufnr`: Buffer number (defaults to current buffer)
100100
- `path_display`: How to display paths
101101
- `entry_maker`: Custom entry maker function
102+
- `show_inherited`: Whether to show inherited members from parent classes,
103+
traits, and interfaces (defaults to true)
102104

103105
Example configuration: >lua
104106
require('telescope').setup {
105107
extensions = {
106108
clapi = {
107109
-- Default configuration options
110+
show_inherited = true, -- Set to false to only show members defined in the current class
108111
},
109112
},
110113
}
@@ -128,6 +131,9 @@ Parameters:
128131
- `bufnr` (number, optional): Buffer number, defaults to current buffer (0)
129132
- `path_display` (table|string, optional): How to display paths
130133
- `entry_maker` (function, optional): Custom entry maker function
134+
- `show_inherited` (boolean, optional): Show inherited members from parent classes,
135+
traits, and interfaces. Defaults to true. Set to false to only show members
136+
defined in the current class.
131137

132138
Returns: nil
133139

@@ -150,7 +156,8 @@ Basic usage: >lua
150156
Advanced usage with custom options: >lua
151157
-- Open the API picker with specific options
152158
require('clapi').builtin({
153-
path_display = { "smart" }
159+
path_display = { "smart" },
160+
show_inherited = false -- Only show members defined in the current class
154161
})
155162
<
156163

lua/clapi/finder.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ local M = {}
1313
---@param opts.bufnr? integer Buffer number, defaults to current buffer (0)
1414
---@param opts.path_display? table|string How to display paths
1515
---@param opts.entry_maker? function Custom entry maker function
16+
---@param opts.show_inherited? boolean Show inherited members, defaults to true
1617
---@return nil
1718
M.builtin = function(opts)
1819
opts = opts or {}
1920
opts.bufnr = opts.bufnr or 0
2021
opts.path_display = { "hidden" }
22+
23+
-- Get extension configuration
24+
local telescope_config = require("telescope.config").values
25+
local ext_config = telescope_config.extensions and telescope_config.extensions.clapi or {}
26+
27+
-- Set show_inherited default value (true if not specified)
28+
opts.show_inherited = vim.F.if_nil(opts.show_inherited, ext_config.show_inherited, true)
29+
2130
async.run(function()
2231
local results = parser.parse_file(opts)
2332
if not results then

lua/clapi/parser/init.lua

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ end
4141
---Get parent class file and parse its definitions
4242
---@param opts table Options for parsing
4343
---@param opts.bufnr? integer Buffer number, defaults to current buffer (0)
44+
---@param opts.show_inherited? boolean Whether to include inherited members from grandparents, defaults to true
4445
---@return table|nil definitions List of definitions from parent class or nil if error
4546
local get_parent_file = async.wrap(function(opts, callback)
4647
opts = opts or {}
4748
opts.bufnr = opts.bufnr or 0
49+
opts.show_inherited = vim.F.if_nil(opts.show_inherited, true)
4850

4951
local filetype = tsparsers.get_buf_lang(opts.bufnr)
5052
local parser = vim.treesitter.get_parser(opts.bufnr, filetype)
@@ -108,7 +110,12 @@ local get_parent_file = async.wrap(function(opts, callback)
108110
-- error already printed in get_file_from_position
109111
callback(nil)
110112
end
111-
local defs = M.parse_file({ filename = filepath, class_name = class_name })
113+
-- Pass the show_inherited option to parse_file for recursive parent parsing
114+
local defs = M.parse_file({
115+
filename = filepath,
116+
class_name = class_name,
117+
show_inherited = opts.show_inherited
118+
})
112119
for _, value in pairs(defs) do
113120
if value["visibility"] ~= "private" then
114121
table.insert(result, value)
@@ -127,10 +134,12 @@ end, 2)
127134
---@param opts.class_name? string Name of the class to filter results
128135
---@param opts.filetype? string Force filetype instead of detecting from extension
129136
---@param opts.query_str? string Custom query string instead of loading from queries
137+
---@param opts.show_inherited? boolean Whether to include inherited members, defaults to true
130138
---@return table|nil results List of symbols found or nil if error
131139
M.parse_file = async.wrap(function(opts, callback)
132140
opts = opts or {}
133141
opts.class_name = opts.class_name and string.format("%s::", opts.class_name) or ""
142+
opts.show_inherited = vim.F.if_nil(opts.show_inherited, true)
134143

135144
if opts.filename and opts.bufnr then
136145
utils.notify("parse_file", {
@@ -258,19 +267,27 @@ M.parse_file = async.wrap(function(opts, callback)
258267
end
259268
end
260269

261-
async.run(function()
262-
local parent_defs = get_parent_file({ bufnr = opts.bufnr })
263-
if not parent_defs then
264-
-- error already printed somewhere
265-
callback(result)
266-
return
267-
end
268-
for _, value in pairs(parent_defs) do
269-
table.insert(result, value)
270-
end
270+
-- Only include inherited members if show_inherited is true
271+
if opts.show_inherited then
272+
async.run(function()
273+
local parent_defs = get_parent_file({
274+
bufnr = opts.bufnr,
275+
show_inherited = opts.show_inherited
276+
})
277+
if not parent_defs then
278+
-- error already printed somewhere
279+
callback(result)
280+
return
281+
end
282+
for _, value in pairs(parent_defs) do
283+
table.insert(result, value)
284+
end
271285

286+
callback(result)
287+
end)
288+
else
272289
callback(result)
273-
end)
290+
end
274291
end, 2)
275292

276-
return M
293+
return M
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
local parser = require("clapi.parser")
2+
local lsp = require("clapi.lsp")
3+
4+
-- Mock functions to avoid actual LSP call dependency
5+
local old_get_file = lsp.get_file_from_position
6+
lsp.get_file_from_position = function()
7+
return "/home/markel/estudio/lua/clapi.nvim/tests/clapi/functional/resources/code/java/example/src/com/example/shared/AggregateRoot.java"
8+
end
9+
10+
-- This test verifies that the show_inherited option works correctly
11+
local function test_show_inherited_option()
12+
-- Test with default (show_inherited = true)
13+
local course_file = "/home/markel/estudio/lua/clapi.nvim/tests/clapi/functional/resources/code/java/example/src/com/example/course/Course.java"
14+
local results_with_inherited = parser.parse_file({ filename = course_file })
15+
16+
-- Results should contain items from both Course.java and AggregateRoot.java
17+
local found_course_method = false
18+
local found_aggregate_method = false
19+
20+
for _, item in ipairs(results_with_inherited) do
21+
if item.name == "getTitle" then
22+
found_course_method = true
23+
elseif item.name == "getId" then
24+
found_aggregate_method = true
25+
end
26+
end
27+
28+
print("Test with show_inherited = true (default):")
29+
print("Found Course.getTitle: " .. tostring(found_course_method))
30+
print("Found AggregateRoot.getId: " .. tostring(found_aggregate_method))
31+
print("")
32+
33+
-- Test with show_inherited = false
34+
local results_without_inherited = parser.parse_file({
35+
filename = course_file,
36+
show_inherited = false
37+
})
38+
39+
-- Results should only contain items from Course.java
40+
found_course_method = false
41+
found_aggregate_method = false
42+
43+
for _, item in ipairs(results_without_inherited) do
44+
if item.name == "getTitle" then
45+
found_course_method = true
46+
elseif item.name == "getId" then
47+
found_aggregate_method = true
48+
end
49+
end
50+
51+
print("Test with show_inherited = false:")
52+
print("Found Course.getTitle: " .. tostring(found_course_method))
53+
print("Found AggregateRoot.getId: " .. tostring(found_aggregate_method))
54+
print("")
55+
56+
-- Restore original function
57+
lsp.get_file_from_position = old_get_file
58+
59+
return (found_course_method and found_aggregate_method) ~= (found_course_method and not found_aggregate_method)
60+
end
61+
62+
print("\n---------- Testing show_inherited option ----------")
63+
local success = test_show_inherited_option()
64+
print("Test " .. (success and "PASSED" or "FAILED"))
65+
print("---------------------------------------------------\n")
66+
67+
return success

0 commit comments

Comments
 (0)