Skip to content

Commit

Permalink
Fix user_data.help_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Jul 9, 2023
1 parent 81bcd2d commit b009b7f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
35 changes: 13 additions & 22 deletions denops/@ddc-sources/nvim-lua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import {
Denops,
fn,
GatherArguments,
Item,
Item as LackItem,
LineContext,
linePatch,
OnCompleteDoneArguments,
} from "./lib/deps.ts";
import { escapeString } from "./lib/strings.ts";

type SomeRequired<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;

type Item<T> = SomeRequired<LackItem<T>, "kind" | "user_data">;

// :h luaref-type()
const LuaType = {
nil: "nil",
number: "number",
Expand Down Expand Up @@ -49,34 +54,18 @@ export class Source extends BaseSource<Params> {
if (path === undefined) {
return [];
}
const items = await this.getItems(denops, path);

return items.map((item) => {
const help_tag = path.startsWith("vim")
? /vim\.(?:api|fn)/.test(path)
? item.word
: [...path, item.word].join(".")
: "";
return {
...item,
user_data: {
...item.user_data,
name: item.word,
help_tag,
},
};
});
return await this.getItems(denops, path);
}

private async getItems(
denops: Denops,
path: string,
): Promise<Item<Pick<UserData, "key_type">>[]> {
): Promise<Item<UserData>[]> {
if (path.startsWith("vim.fn.")) {
const funcName = path.slice(7);
const input = path.slice(7);
const functions = await fn.getcompletion(
denops,
funcName,
input,
"function",
) as string[];
return functions
Expand All @@ -86,6 +75,8 @@ export class Source extends BaseSource<Params> {
word: func,
kind: "function",
user_data: {
name: func,
help_tag: func,
key_type: "string",
},
}));
Expand All @@ -96,7 +87,7 @@ export class Source extends BaseSource<Params> {
"luaeval",
`require("ddc-source-nvim-lua").items(_A)`,
parent,
) as Item<Pick<UserData, "key_type">>[];
) as Item<UserData>[];
}
}

Expand Down
27 changes: 22 additions & 5 deletions lua/ddc-source-nvim-lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}
---@field menu? string
---@field info? string
---@field kind? string
---@field user_data? unknown
---@field user_data? UserData
---@field highlights? PumHighlight[]
---@field columns? table<string, string>
---@field [1] string
Expand All @@ -18,6 +18,11 @@ local M = {}
---@field col number
---@field width number

---@class UserData
---@field name string same as item.word
---@field help_tag string
---@field key_type string

---@generic T
---@param dst T[]
---@param src T[]
Expand All @@ -40,13 +45,15 @@ function M.items(parent)
end
end

local parent_s = table.concat(parent, ".")

---@type DdcItem[], DdcItem[]
local before, after = {}, {}
for _, key in ipairs(target_keys) do
if type(key) == "string" and key:find("^%a[%a_]*$") then
table.insert(before, M.item(key, target[key]))
table.insert(before, M.item(key, target[key], parent_s))
else
table.insert(after, M.item(key, target[key]))
table.insert(after, M.item(key, target[key], parent_s))
end
end

Expand All @@ -55,12 +62,22 @@ end

---@param key unknown
---@param value unknown
---@param parent string
---@return DdcItem
function M.item(key, value)
function M.item(key, value, parent)
local word = tostring(key)
local help_tag = ""
if parent == "vim.api" then
help_tag = word
elseif vim.startswith(parent, "vim") then
help_tag = parent .. "." .. word
end
return {
word = tostring(key),
word = word,
kind = type(value),
user_data = {
name = word,
help_tag = help_tag,
key_type = type(key),
},
}
Expand Down

0 comments on commit b009b7f

Please sign in to comment.