Skip to content

Commit 85a752a

Browse files
committed
Fixed errors when LSP didn't finished loading + updated README
1 parent 6b97ea3 commit 85a752a

3 files changed

Lines changed: 63 additions & 48 deletions

File tree

README.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ The [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) pickers t
55
The plugin analyzes the entire class/module hierarchy, including parent classes, traits, interfaces, and other inherited elements, giving you a complete view of the API surface.
66
![demo.gif](https://github.com/user-attachments/assets/e9ddda56-912d-4475-b7d6-94c573939db6)
77

8-
## Features
9-
10-
- **Complete API view**: Display all class/module members including inherited elements
11-
- **Visibility indicators**: Shows access modifiers (public, private, protected) for each member
12-
- **Hierarchical analysis**: Understands inheritance from parent classes, traits, and interfaces
13-
- **Fast navigation**: Quickly jump to any symbol definition
14-
- **Integration with LSP**: Works alongside your language server for accurate symbol information
15-
- **Telescope integration**: Familiar fuzzy-finding interface for efficient filtering
16-
178
## Supported Languages
189

1910
- PHP
@@ -30,37 +21,37 @@ The plugin analyzes the entire class/module hierarchy, including parent classes,
3021
## Installation
3122
Using [lazy.nvim](https://github.com/folke/lazy.nvim)
3223
```lua
33-
{
24+
{ -- Add the clapi plugin with its dependencies
3425
'markelca/clapi.nvim',
35-
-- Dev Mode (Clone the repo, update the `dir` value and uncomment the two lines below)
36-
-- dir = '~/<dir-where-you-cloned>/clapi.nvim/',
37-
-- name = 'clapi',
3826
dependencies = {
3927
'nvim-telescope/telescope.nvim',
4028
'nvim-treesitter/nvim-treesitter',
4129
'nvim-lua/plenary.nvim',
4230
},
31+
},
32+
{ -- Add the following to your telescope configuration
33+
'nvim-telescope/telescope.nvim',
4334
config = function()
44-
-- Enable the clapi extension adding the following line to your telescope configuration:
45-
pcall(require('telescope').load_extension 'clapi')
46-
47-
-- Optionally you can set up a keymap to run the picker
48-
vim.keymap.set('n', '<leader>sa', require('clapi').builtin, { desc = '[S]earch [A]pi' })
49-
5035
-- Configurations for the clapi picker
5136
require('telescope').setup {
5237
extensions = {
5338
clapi = {
5439
-- Additional options can be configured here
55-
-- show_inherited = true, -- Set to false to only show members defined in the current class
56-
-- default_visibility = "public", -- Filter by default visibility (public, protected, private)
40+
show_inherited = true, -- Set to false to only show members defined in the current class
41+
default_visibility = "public", -- Filter by default visibility (public, protected, private)
5742
},
5843
},
5944
}
45+
-- Enable the clapi extension adding the following line to your telescope configuration:
46+
pcall(require('telescope').load_extension 'clapi')
47+
-- Optionally you can set up a keymap to run the picker
48+
vim.keymap.set('n', '<leader>sa', require('clapi').builtin, { desc = '[S]earch [A]pi' })
6049
end,
6150
}
6251
```
63-
Full example in my nvim config repository: [nvim](https://github.com/MarkelCA/nvim/blob/master/lua/plugins/clapi.lua)
52+
Full example in my [nvim](https://github.com/markelca/nvim) config repository:
53+
- [clapi](https://github.com/markelca/nvim/blob/master/lua/plugins/clapi.lua)
54+
- [telescope](https://github.com/markelca/nvim/blob/master/lua/plugins/telescope.lua)
6455

6556
## Usage
6657

@@ -129,4 +120,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
129120
4. Push to the branch
130121
5. Submit a pull request
131122

132-
For adding support for a new language, check the `lua/clapi/parser/` directory for examples.
123+
For adding support for a new language, check the `lua/clapi/parser/` directory for examples.

lua/clapi/lsp.lua

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ M.get_file_from_position = async.wrap(function(opts, callback)
2424
return
2525
end
2626

27+
local clients = vim.lsp.get_clients({ bufnr = opts.bufnr })
28+
if #clients == 0 then
29+
utils.notify("get_file_from_position", {
30+
msg = "Lsp is not attached",
31+
level = "ERROR",
32+
})
33+
callback(nil)
34+
return
35+
end
36+
2737
local callback_called = false
2838

2939
---Safe callback to prevent multiple callbacks
3040
---@param result string|nil The file path or nil
41+
---FIX: Accumulate results on multiple callbacks instead of ignoring them
3142
local function safe_callback(result)
3243
if not callback_called then
3344
callback_called = true
@@ -40,27 +51,38 @@ M.get_file_from_position = async.wrap(function(opts, callback)
4051
end
4152
end
4253

43-
vim.lsp.buf_request(opts.bufnr, "textDocument/definition", {
54+
vim.lsp.buf_request_all(opts.bufnr, "textDocument/definition", {
4455
position = opts.position,
4556
textDocument = {
4657
uri = string.format("file://%s", vim.api.nvim_buf_get_name(opts.bufnr)),
4758
},
48-
}, function(err, result, _, _)
49-
if err or not result then
50-
utils.notify("get_parent_file", {
51-
msg = "Couldn't get the file for the parent class",
52-
level = "ERROR",
53-
})
54-
callback(nil)
55-
return
56-
end
59+
}, function(results)
60+
for _, result_entry in pairs(results) do
61+
if result_entry.error then
62+
utils.notify("get_parent_file", {
63+
msg = "Couldn't get the file for the parent class: " .. result_entry.error.message,
64+
level = "ERROR",
65+
})
66+
callback(nil)
67+
return
68+
else
69+
if result_entry.result == nil then
70+
utils.notify("get_parent_file", {
71+
msg = "Couldn't get the file for the parent class",
72+
level = "WARN",
73+
})
74+
else
75+
local result = result_entry.result
5776

58-
local uri = result.uri or result[1].uri or result[1].targetUri
77+
local uri = result.uri or result[1].uri or result[1].targetUri
5978

60-
if uri then
61-
safe_callback(vim.uri_to_fname(uri))
62-
else
63-
safe_callback(nil)
79+
if uri then
80+
safe_callback(vim.uri_to_fname(uri))
81+
else
82+
safe_callback(nil)
83+
end
84+
end
85+
end
6486
end
6587
end)
6688
end, 2)

lua/clapi/parser/init.lua

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ end
4646
local get_parent_file = async.wrap(function(opts, callback)
4747
opts = opts or {}
4848
opts.bufnr = opts.bufnr or 0
49-
-- Use explicit check for boolean values since vim.F.if_nil doesn't handle false properly
50-
if opts.show_inherited ~= nil then
51-
-- Keep the provided value (could be true or false)
52-
opts.show_inherited = opts.show_inherited
53-
else
54-
-- Default to true if not specified
55-
opts.show_inherited = true
56-
end
5749

5850
local filetype = tsparsers.get_buf_lang(opts.bufnr)
5951
local parser = vim.treesitter.get_parser(opts.bufnr, filetype)
@@ -63,6 +55,7 @@ local get_parent_file = async.wrap(function(opts, callback)
6355
level = "ERROR",
6456
})
6557
callback(nil)
58+
return
6659
end
6760

6861
-- Parse the query
@@ -74,6 +67,7 @@ local get_parent_file = async.wrap(function(opts, callback)
7467
level = "ERROR",
7568
})
7669
callback(nil)
70+
return
7771
end
7872

7973
local query = vim.treesitter.query.parse(filetype, query_str)
@@ -84,6 +78,7 @@ local get_parent_file = async.wrap(function(opts, callback)
8478
level = "ERROR",
8579
})
8680
callback(nil)
81+
return
8782
end
8883

8984
-- Parse the content
@@ -95,6 +90,7 @@ local get_parent_file = async.wrap(function(opts, callback)
9590
level = "ERROR",
9691
})
9792
callback(nil)
93+
return
9894
end
9995

10096
tree = tree[1]
@@ -116,14 +112,16 @@ local get_parent_file = async.wrap(function(opts, callback)
116112
if not filepath or filepath == "" then
117113
-- error already printed in get_file_from_position
118114
callback(nil)
115+
return
119116
end
117+
vim.print("fp", filepath)
120118
-- Pass the show_inherited option to parse_file for recursive parent parsing
121119
local defs = M.parse_file({
122120
filename = filepath,
123121
class_name = class_name,
124122
show_inherited = opts.show_inherited,
125123
})
126-
for _, value in pairs(defs) do
124+
for _, value in pairs(defs or {}) do
127125
if value["visibility"] ~= "private" then
128126
table.insert(result, value)
129127
end
@@ -165,6 +163,11 @@ M.parse_file = async.wrap(function(opts, callback)
165163
end
166164

167165
if not opts.filetype then
166+
if not opts.filename then
167+
callback(nil)
168+
return
169+
end
170+
168171
local filetype = utils.get_file_extension(opts.filename)
169172
if not filetype then
170173
utils.notify("parse_file", {
@@ -297,4 +300,3 @@ M.parse_file = async.wrap(function(opts, callback)
297300
end, 2)
298301

299302
return M
300-

0 commit comments

Comments
 (0)