Skip to content

Commit 94e298d

Browse files
committed
cmdline add help tags support
1 parent a5baf8b commit 94e298d

File tree

2 files changed

+97
-35
lines changed

2 files changed

+97
-35
lines changed

lua/easycomplete/cmdline.lua

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,69 @@ this.REG_CMP_HANDLER = {
426426
end
427427
end
428428
},
429+
{
430+
-- help
431+
pattern = {
432+
"^h%s[%w_]+$",
433+
"^help%s[%w_]+$"
434+
},
435+
get_cmp_items = function()
436+
if this.insearch() then
437+
return this.get_normal_search_cmp()
438+
else
439+
local all_tags = this.get_help_completions()
440+
local typing_word = this.get_typing_word()
441+
local guide_char = string.sub(typing_word, 1, 1)
442+
local result = util.filter(all_tags, function(item)
443+
if string.find(string.lower(string.sub(item, 1, 2)), string.lower(guide_char)) and
444+
not string.find(item, "[%s<>%+%-]") then
445+
return true
446+
else
447+
return false
448+
end
449+
end)
450+
return result
451+
end
452+
end
453+
},
454+
{
455+
-- highlight colors name
456+
pattern = {
457+
"^hi%s.*gui[fb]g=$",
458+
"^hi%s.*gui[fb]g=%w+$",
459+
"^highlight%s.*gui[fb]g=$",
460+
"^highlight%s.*gui[fb]g=%w+$",
461+
},
462+
get_cmp_items = function()
463+
if this.insearch() then
464+
return this.get_normal_search_cmp()
465+
else
466+
local colors = this.native_colors
467+
return colors
468+
end
469+
end
470+
},
471+
{
472+
-- highlight color groups
473+
pattern = {
474+
"^hi%s+[%w@]+$",
475+
"^highlight%s+[%w@]+$", -- 几个常见的属性 TODO
476+
"^hi%s+link%s+$",
477+
"^hi%s+link%s+[%w@]+$",
478+
"^hi%s+link%s+[%w@]+%s+$",
479+
"^hi%s+link%s+[%w@]+%s+[%w@]+$"
480+
},
481+
get_cmp_items = function()
482+
if this.insearch() then
483+
return this.get_normal_search_cmp()
484+
else
485+
local typing_word = this.get_typing_word()
486+
local guide_str = string.sub(typing_word, 1, 1)
487+
local result = vim.fn.getcompletion(guide_str, "highlight")
488+
return result
489+
end
490+
end
491+
},
429492
{
430493
pattern = {
431494
"^[a-zA-Z0-9_]+%s$", -- 命令输入完毕,并敲击空格
@@ -475,40 +538,6 @@ this.REG_CMP_HANDLER = {
475538
end
476539
end
477540
},
478-
{
479-
-- highlight colors name
480-
pattern = {
481-
"^hi%s.*gui[fb]g=$",
482-
"^hi%s.*gui[fb]g=%w+$",
483-
"^highlight%s.*gui[fb]g=$",
484-
"^highlight%s.*gui[fb]g=%w+$",
485-
},
486-
get_cmp_items = function()
487-
if this.insearch() then
488-
return this.get_normal_search_cmp()
489-
else
490-
local colors = this.native_colors
491-
return colors
492-
end
493-
end
494-
},
495-
{
496-
-- highlight color groups
497-
pattern = {
498-
"^hi%s.*$",
499-
"^highlight%s.*$",
500-
},
501-
get_cmp_items = function()
502-
if this.insearch() then
503-
return this.get_normal_search_cmp()
504-
else
505-
local typing_word = this.get_typing_word()
506-
local guide_str = string.sub(typing_word, 1, 1)
507-
local result = vim.fn.getcompletion(guide_str, "highlight")
508-
return result
509-
end
510-
end
511-
},
512541
{
513542
-- 输入路径
514543
pattern = {
@@ -682,6 +711,23 @@ function this.cmd_match(rgx)
682711
end
683712
end
684713

714+
-- 得到所有的文档中的tags
715+
local help_all_tags = nil
716+
function this.get_help_completions()
717+
if help_all_tags ~= nil then
718+
return help_all_tags
719+
else
720+
local help_files = vim.api.nvim_get_runtime_file('doc/tags', true)
721+
local all_tags = {}
722+
for _, filename in ipairs(help_files) do
723+
local tags = util.get_file_tags(filename)
724+
for _, v in ipairs(tags) do table.insert(all_tags, v) end
725+
end
726+
help_all_tags = all_tags
727+
return all_tags
728+
end
729+
end
730+
685731
this.cmd_type = {
686732
-- File completion
687733
file = {
@@ -703,7 +749,7 @@ this.cmd_type = {
703749
diff_buffer = { 'diffthis', 'diffoff', 'diffupdate' },
704750
command = { 'command', 'delcommand' },
705751
option = { 'set', 'setlocal', 'setglobal' },
706-
help = { 'help' },
752+
-- help = { 'help' },
707753
expression = {
708754
'substitute', 'global', 'vglobal', 'let',
709755
'echo', 'echom', 'echon',

lua/easycomplete/util.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ function util.get_server()
101101
return Server
102102
end
103103

104+
function util.get_file_tags(filename)
105+
local lines = {}
106+
local tags = {}
107+
local file, err = io.open(filename, "r") -- 打开文件为只读模式
108+
if not file then
109+
print("打开文件失败: " .. err)
110+
return {}
111+
end
112+
for line in file:lines() do -- 使用 :lines() 迭代器逐行读取
113+
local tag = line:match('^([^\t]+)')
114+
if tag then table.insert(tags, tag) end
115+
end
116+
file:close() -- 关闭文件
117+
return tags
118+
end
119+
104120
function util.complete_menu_filter(matching_res, word)
105121
local fullmatch_result = {} -- 完全匹配
106122
local firstchar_result = {} -- 首字母匹配

0 commit comments

Comments
 (0)