|
| 1 | +local Util = require "easycomplete.util" |
| 2 | +local log = Util.log |
| 3 | +local console = Util.console |
| 4 | +local normal_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789#$_" |
| 5 | +local M = {} |
| 6 | + |
| 7 | +function pum_complete() |
| 8 | + print(get_typing_word()) |
| 9 | + local word = get_typing_word() |
| 10 | + local t = { |
| 11 | + { |
| 12 | + word = "s", |
| 13 | + abbr = word, |
| 14 | + kind = "x", |
| 15 | + menu = "x" |
| 16 | + }, |
| 17 | + { |
| 18 | + word = "abcd", |
| 19 | + abbr = "abcd", |
| 20 | + kind = "x", |
| 21 | + menu = "x" |
| 22 | + } |
| 23 | + } |
| 24 | + local start_col = vim.fn.getcmdpos() - calculate_sign_and_linenr_width() - #word |
| 25 | + vim.fn["easycomplete#pum#complete"](start_col, t) |
| 26 | +end |
| 27 | + |
| 28 | +function pum_close() |
| 29 | + vim.fn["easycomplete#pum#close"]() |
| 30 | +end |
| 31 | + |
| 32 | +function get_typing_word() |
| 33 | + -- 获取当前行的文本 |
| 34 | + local current_line = string.sub(vim.fn.getcmdline(),1,vim.fn.getcmdpos()) |
| 35 | + |
| 36 | + -- 使用 gmatch 迭代所有单词(假设单词由空格分隔) |
| 37 | + local last_word = "" |
| 38 | + for word in string.gmatch(current_line, "[%w%-]+") do |
| 39 | + last_word = word |
| 40 | + end |
| 41 | + |
| 42 | + return last_word |
| 43 | +end |
| 44 | + |
| 45 | +function calculate_sign_and_linenr_width() |
| 46 | + local width = 0 |
| 47 | + |
| 48 | + -- 检查是否有 sign 列 |
| 49 | + local signcolumn = vim.api.nvim_win_get_option(0, "signcolumn") |
| 50 | + if signcolumn == "yes" or signcolumn == "auto" or signcolumn == "number" then |
| 51 | + width = width + 2 -- sign 列通常占据 2 个字符宽度 |
| 52 | + end |
| 53 | + |
| 54 | + -- 检查是否显示行号 |
| 55 | + if vim.wo.number or vim.wo.relativenumber then |
| 56 | + -- 计算行号的最大宽度 |
| 57 | + local max_num_width = #tostring(vim.fn.line("$")) |
| 58 | + width = width + max_num_width + 1 -- 加 1 是为了考虑空隙或者额外的字符 |
| 59 | + end |
| 60 | + |
| 61 | + return width |
| 62 | +end |
| 63 | + |
| 64 | +local function bind_cmdline_event() |
| 65 | + vim.on_key(function(keys, _) |
| 66 | + -- 这里不论是否是插入模式,都会触发,需要过滤掉 |
| 67 | + if vim.api.nvim_get_mode().mode ~= "c" then |
| 68 | + return |
| 69 | + end |
| 70 | + -- 将按键字节序列转换为字符串 |
| 71 | + local key_str = vim.api.nvim_replace_termcodes(keys, true, false, true) |
| 72 | + |
| 73 | + vim.defer_fn(function() |
| 74 | + pum_complete() |
| 75 | + console(string.byte(key_str)) |
| 76 | + if string.byte(key_str) == 9 then |
| 77 | + console("Tab 键被按下") |
| 78 | + elseif string.byte(key_str) == 32 then |
| 79 | + console("空格键被按下") |
| 80 | + pum_close() |
| 81 | + elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then |
| 82 | + console("退格键被按下") |
| 83 | + elseif string.byte(key_str) == 13 then |
| 84 | + console("回车键被按下") |
| 85 | + else |
| 86 | + console("其他键被按下: " .. keys) |
| 87 | + end |
| 88 | + -- console(key_str) |
| 89 | + vim.cmd("redraw") |
| 90 | + -- vim.fn["easycomplete#CmdlineChanged"](key_str) |
| 91 | + end, 10) |
| 92 | + end) |
| 93 | +end |
| 94 | + |
| 95 | + |
| 96 | +function M.init_once() |
| 97 | + -- TODO here ----------------------------- |
| 98 | + if true then return end |
| 99 | + -- TODO here ----------------------------- |
| 100 | + if vim.g.easycomplete_cmdline_loaded == 1 then |
| 101 | + return |
| 102 | + end |
| 103 | + vim.g.easycomplete_cmdline_loaded = 1 |
| 104 | + bind_cmdline_event() |
| 105 | +end |
| 106 | + |
| 107 | + |
| 108 | +return M |
0 commit comments