-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mentions
- Loading branch information
Showing
6 changed files
with
158 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
local utils = require("tabnine.utils") | ||
local M = {} | ||
|
||
M.SYMBOL_KIND = { FUNCTION = 12, CLASS = 5, METHOD = 6 } | ||
|
||
local function is_not_source(symbol_path) | ||
local dirs = { "node_modules", "dist", "build", "target", "out" } | ||
for i, dir in ipairs(dirs) do | ||
if string.sub(symbol_path, 1, string.len(dir)) == dir then return true end | ||
end | ||
return false | ||
end | ||
|
||
local function flatten_symbols(symbols, result) | ||
result = result or {} | ||
|
||
for _, symbol in ipairs(symbols) do | ||
table.insert(result, symbol) | ||
|
||
if symbol.children then flatten_symbols(symbol.children, result) end | ||
end | ||
|
||
return result | ||
end | ||
|
||
function M.get_workspace_symbols(query, callback) | ||
local params = { query = query } | ||
|
||
return vim.lsp.buf_request_all(0, "workspace/symbol", params, function(responses) | ||
local results = {} | ||
for _, response in ipairs(responses) do | ||
if response.result then | ||
for _, result in ipairs(flatten_symbols(response.result)) do | ||
result.location.uri = utils.remove_matching_prefix(result.location.uri, "file://") | ||
if | ||
( | ||
result.kind == M.SYMBOL_KIND.CLASS | ||
or result.kind == M.SYMBOL_KIND.METHOD | ||
or result.kind == M.SYMBOL_KIND.FUNCTION | ||
) | ||
and utils.starts_with(result.location.uri, vim.fn.getcwd()) | ||
and not is_not_source(result.location.uri) | ||
then | ||
table.insert(results, result) | ||
end | ||
end | ||
end | ||
end | ||
callback(results) | ||
end) | ||
end | ||
|
||
function M.get_document_symbols(query, callback) | ||
local params = { | ||
textDocument = vim.lsp.util.make_text_document_params(), | ||
} | ||
return vim.lsp.buf_request_all(0, "textDocument/documentSymbol", params, function(responses) | ||
local results = {} | ||
for _, response in ipairs(responses) do | ||
if response.result then | ||
for _, result in ipairs(flatten_symbols(response.result)) do | ||
if | ||
( | ||
result.kind == M.SYMBOL_KIND.CLASS | ||
or result.kind == M.SYMBOL_KIND.METHOD | ||
or result.kind == M.SYMBOL_KIND.FUNCTION | ||
) and utils.starts_with(result.name, query) | ||
then | ||
table.insert(results, result) | ||
end | ||
end | ||
end | ||
end | ||
callback(results) | ||
end) | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters