feat: perfect the hover view ui
This commit is contained in:
		
							
								
								
									
										21
									
								
								lua/Trans/core/action.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lua/Trans/core/action.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
|  | ||||
| local function feedkey(mode, key) | ||||
|     vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), mode, false) | ||||
| end | ||||
|  | ||||
|  | ||||
| local M = { | ||||
|     pageup = function (bufnr) | ||||
|         return function () | ||||
|             vim.api.nvim_buf_call(bufnr, function () | ||||
|                 -- TODO : | ||||
|             end) | ||||
|         end | ||||
|     end, | ||||
|     pagedown = function () | ||||
|         feedkey('n', '<C-d>') | ||||
|     end | ||||
| } | ||||
|  | ||||
|  | ||||
| return M | ||||
| @@ -1,7 +1,7 @@ | ||||
| local M = {} | ||||
| M.__index = M | ||||
|  | ||||
| local get_width = vim.fn.strdisplaywidth | ||||
| M.get_width = vim.fn.strdisplaywidth | ||||
| -- local get_width = vim.fn.strwidth | ||||
| -- local get_width = vim.api.nvim_strwidth | ||||
|  | ||||
| @@ -37,7 +37,7 @@ function M:alloc_block(s_row, s_col, height, width) | ||||
|  | ||||
|         __newindex = function(_, key, value) | ||||
|             assert(0 < key and key <= height) | ||||
|             local wid = get_width(value) | ||||
|             local wid = self.get_width(value) | ||||
|             if wid > width then | ||||
|                 error('check out the str width: Max ->' .. self.width .. ' str ->' .. wid) | ||||
|             else | ||||
| @@ -50,20 +50,20 @@ function M:alloc_block(s_row, s_col, height, width) | ||||
|     }) | ||||
| end | ||||
|  | ||||
| function M:alloc_newline() | ||||
|     self.len = self.len + 1 | ||||
| function M:alloc_items() | ||||
|     local items = {} | ||||
|     local len = 0 | ||||
|     local size = 0 | ||||
|     return { | ||||
|         add_item = function(item, highlight) | ||||
|             size = size + 1 | ||||
|             local wid = get_width(item) | ||||
|             local wid = self.get_width(item) | ||||
|             items[size] = { item, highlight } | ||||
|             len = len + wid | ||||
|         end, | ||||
|  | ||||
|         load_line = function() | ||||
|         load = function() | ||||
|             self.len = self.len + 1 | ||||
|             local space = math.floor((self.width - len) / (size - 1)) | ||||
|             assert(space > 0) | ||||
|             local interval = (' '):rep(space) | ||||
| @@ -71,7 +71,7 @@ function M:alloc_newline() | ||||
|             local function load_item(index) | ||||
|                 if items[index][2] then | ||||
|                     local _start = #value | ||||
|                     local _end = _start + #items[index][2] | ||||
|                     local _end = _start + #items[index][1] | ||||
|                     table.insert(self.highlights[self.len], { | ||||
|                         name = items[index][2], | ||||
|                         _start = _start, | ||||
| @@ -92,12 +92,35 @@ function M:alloc_newline() | ||||
|     } | ||||
| end | ||||
|  | ||||
| function M:alloc_text() | ||||
|     local value = '' | ||||
|     return { | ||||
|         add_text = function(text, highlight) | ||||
|             if highlight then | ||||
|                 local _start = #value | ||||
|                 local _end = _start + #text | ||||
|                 table.insert(self.highlights[self.len + 1], { | ||||
|                     name = highlight, | ||||
|                     _start = _start, | ||||
|                     _end = _end, | ||||
|                 }) | ||||
|             end | ||||
|             value = value .. text | ||||
|         end, | ||||
|         load = function () | ||||
|             self.len = self.len + 1 | ||||
|             self.lines[self.len] = value | ||||
|         end | ||||
|     } | ||||
| end | ||||
|  | ||||
|  | ||||
| function M:addline(text, highlight) | ||||
|     self.len = self.len + 1 | ||||
|     if highlight then | ||||
|         table.insert(self.highlights[self.len], { | ||||
|             name = highlight, | ||||
|             _start = 1, | ||||
|             _start = 0, | ||||
|             _end = -1 | ||||
|         }) | ||||
|     end | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| local M = {} | ||||
| local icon = require('Trans').conf.icon | ||||
|  | ||||
| -- local components = { | ||||
| @@ -53,21 +54,32 @@ local function exist(res) | ||||
|     return res and res ~= '' | ||||
| end | ||||
|  | ||||
| M = { | ||||
| local function expl(c, text) | ||||
|     local t = c:alloc_text() | ||||
|     t.add_text('', 'TransTitleRound') | ||||
|     t.add_text(text, 'TransTitle') | ||||
|     t.add_text('', 'TransTitleRound') | ||||
|     t.load() | ||||
| end | ||||
|  | ||||
| local indent = '    ' | ||||
|  | ||||
| M.hover = { | ||||
|     title = function(result, content) | ||||
|         local line = content:alloc_newline() | ||||
|         local line = content:alloc_items() | ||||
|         line.add_item(result.word, 'TransWord') | ||||
|         local pho = ('[' .. (exist(result.phonetic) and result.phonetic or icon.notfound) .. ']') | ||||
|         -- line.add_item(pho, 'TransPhonetic', #pho) | ||||
|         line.add_item(pho, 'TransPhonetic') | ||||
|         line.add_item((exist(result.collins) and icon.star:rep(result.collins) or icon.notfound)) | ||||
|         line.add_item((exist(result.oxford) and icon.yes or icon.no)) | ||||
|         line.load_line() | ||||
|         line.add_item((exist(result.collins) and icon.star:rep(result.collins) or icon.notfound), 'TransCollins') | ||||
|         line.add_item((result.oxford == 1 and icon.yes or icon.no)) | ||||
|         line.load() | ||||
|     end, | ||||
|  | ||||
|     tag = function(result, content) | ||||
|         if exist(result.tag) then | ||||
|             content:addline('标签:', 'TransRef') | ||||
|             expl(content, '标签') | ||||
|  | ||||
|             local tags = vim.tbl_map(function(tag) | ||||
|                 return tag_map[tag] | ||||
|             end, vim.split(result.tag, ' ', { plain = true, trimempry = true })) | ||||
| @@ -75,7 +87,7 @@ M = { | ||||
|             local size = #tags | ||||
|             local i = 1 | ||||
|             while i <= size do | ||||
|                 content:addline('    ' .. tags[i] .. '    ' .. (tags[i + 1] or '') .. '    ' .. (tags[i + 2] or ''), | ||||
|                 content:addline(indent .. tags[i] .. '    ' .. (tags[i + 1] or '') .. '    ' .. (tags[i + 2] or ''), | ||||
|                     'TransTag') | ||||
|                 i = i + 3 | ||||
|             end | ||||
| @@ -85,9 +97,9 @@ M = { | ||||
|  | ||||
|     pos = function(result, content) | ||||
|         if exist(result.pos) then | ||||
|             content:addline('词性:', 'TransRef') | ||||
|             expl(content, '词性') | ||||
|             vim.tbl_map(function(pos) | ||||
|                 content:addline('    ' .. pos_map[pos:sub(1, 1)] .. pos:sub(3) .. '%', 'TransPos') | ||||
|                 content:addline(indent .. pos_map[pos:sub(1, 1)] .. pos:sub(3) .. '%', 'TransPos') | ||||
|             end, vim.split(result.pos, '/', { plain = true, trimempry = true })) | ||||
|  | ||||
|             content:addline('') | ||||
| @@ -96,10 +108,10 @@ M = { | ||||
|  | ||||
|     exchange = function(result, content) | ||||
|         if exist(result.exchange) then | ||||
|             content:addline('词性变化:', 'TransRef') | ||||
|             expl(content, '词形变化') | ||||
|  | ||||
|             vim.tbl_map(function(exc) | ||||
|                 content:addline('    ' .. exchange_map[exc:sub(1, 1)] .. '    ' .. exc:sub(3), 'TransExchange') | ||||
|                 -- content:addline('    ' .. exchange_map[exc:sub(1, 1)] .. exc:sub(2), 'TransExchange') | ||||
|                 content:addline(indent .. exchange_map[exc:sub(1, 1)] .. '    ' .. exc:sub(3), 'TransExchange') | ||||
|             end, vim.split(result.exchange, '/', { plain = true, trimempry = true })) | ||||
|  | ||||
|             content:addline('') | ||||
| @@ -107,46 +119,30 @@ M = { | ||||
|     end, | ||||
|  | ||||
|     translation = function(result, content) | ||||
|         if result.translation and result.translation ~= '' then | ||||
|             local ref = { | ||||
|                 { '中文翻译:', 'TransRef' } | ||||
|             } | ||||
|         expl(content, '中文翻译') | ||||
|  | ||||
|             local translations = { | ||||
|                 highlight = 'TransTranslation', | ||||
|                 indent = 4, | ||||
|                 emptyline = true, | ||||
|             } | ||||
|             for trans in vim.gsplit(result.translation, '\n', true) do | ||||
|                 table.insert(translations, trans) | ||||
|             end | ||||
|         vim.tbl_map(function(trs) | ||||
|             content:addline(indent .. trs, 'TransTranslation') | ||||
|         end, vim.split(result.translation, '\n', { plain = true, trimempry = true })) | ||||
|  | ||||
|             return { ref, translations } | ||||
|         end | ||||
|         content:addline('') | ||||
|     end, | ||||
|  | ||||
|  | ||||
|     definition = function(result, content) | ||||
|         if result.definition and result.definition ~= '' then | ||||
|             local ref = { | ||||
|                 { '英文注释:', 'TransRef' } | ||||
|             } | ||||
|         if exist(result.definition) then | ||||
|             expl(content, '英文注释') | ||||
|  | ||||
|             local definitions = { | ||||
|                 highlight = 'TransDefinition', | ||||
|                 indent = 4, | ||||
|                 emptyline = true, | ||||
|             } | ||||
|             vim.tbl_map(function(def) | ||||
|                 content:addline(def, 'TransDefinition') | ||||
|             end, vim.split(indent .. result.definition, '\n', { plain = true, trimempry = true })) | ||||
|  | ||||
|             for defin in vim.gsplit(result.definition, '\n', true) do | ||||
|                 if defin ~= '' then | ||||
|                     table.insert(definitions, defin) | ||||
|                 end | ||||
|             end | ||||
|  | ||||
|             return { ref, definitions } | ||||
|             content:addline('') | ||||
|         end | ||||
|     end | ||||
|     end, | ||||
|     failed = function(content) | ||||
|         content:addline(icon.notfound .. indent .. '没有找到相关的翻译') | ||||
|     end, | ||||
| } | ||||
|  | ||||
| return M | ||||
|   | ||||
| @@ -36,23 +36,18 @@ M.translate = function(method, view) | ||||
|  | ||||
|     win.init(view) | ||||
|     local result = api.query('offline', word) | ||||
|     local content = c:new(win.width) | ||||
|     local hd = handler[view] | ||||
|  | ||||
|     if result then | ||||
|         local content = c:new(win.width) | ||||
|  | ||||
|         for i = 1, #conf.order do | ||||
|             handler[conf.order[i]](result, content) | ||||
|             hd[conf.order[i]](result, content) | ||||
|         end | ||||
|  | ||||
|         win.draw(content) | ||||
|     else | ||||
|         local line = { '⚠️   本地没有找到相应的结果' } | ||||
|         vim.api.nvim_buf_set_lines(win.bufnr, 0, -1, false, line) | ||||
|         local wid = vim.fn.strdisplaywidth(line[1]) | ||||
|         vim.api.nvim_win_set_width(win.id, wid) | ||||
|         vim.api.nvim_win_set_height(win.id, #line) | ||||
|         win.auto_close() | ||||
|         hd.failed(content) | ||||
|     end | ||||
|     win.draw(content) | ||||
| end | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| local M    = {} | ||||
| local api  = vim.api | ||||
| local conf = require('Trans').conf | ||||
| local action = require('Trans.core.action') | ||||
|  | ||||
| M.id       = 0 | ||||
| M.bufnr    = 0 | ||||
| M.ns       = api.nvim_create_namespace('Trans') | ||||
| @@ -22,7 +24,11 @@ function M.init(view) | ||||
|         height    = M.height, | ||||
|         style     = 'minimal', | ||||
|         border    = conf.window.border, | ||||
|         title     = 'Trans', | ||||
|         title     = { | ||||
|             {'', 'TransTitleRound'}, | ||||
|             {'Trans', 'TransTitle'}, | ||||
|             {'', 'TransTitleRound'}, | ||||
|         }, | ||||
|         title_pos = 'center', | ||||
|         focusable = true, | ||||
|         zindex    = 100, | ||||
| @@ -53,14 +59,17 @@ M.draw = function(content) | ||||
|  | ||||
|     local len = #content.lines | ||||
|     if M.height > len then | ||||
|         api.nvim_win_set_height(M.id, len + 1) | ||||
|         api.nvim_win_set_height(M.id, len) | ||||
|     end | ||||
|     if len == 1 then | ||||
|         api.nvim_win_set_width(M.id, content.get_width(content.lines[1])) | ||||
|     end | ||||
|  | ||||
|  | ||||
|     api.nvim_buf_set_option(M.bufnr, 'modifiable', false) | ||||
|     api.nvim_buf_set_option(M.bufnr, 'filetype', 'Trans') | ||||
|     api.nvim_win_set_option(M.id, 'wrap', not M.float) | ||||
|     api.nvim_win_set_option(M.id, 'winhl', 'Normal:TransCursorWin,FloatBorder:TransCursorBorder') | ||||
|     api.nvim_win_set_option(M.id, 'winhl', ('Normal:Trans%sWin,FloatBorder:Trans%sBorder'):format(M.view, M.view)) | ||||
|  | ||||
|     if M.float then | ||||
|         vim.keymap.set('n', 'q', function() | ||||
| @@ -91,4 +100,15 @@ M.auto_close = function() | ||||
| end | ||||
|  | ||||
|  | ||||
| -- M.load_keymap = function (once) | ||||
| --     local keymap = conf.keymap[M.view] | ||||
| --     local function warp(func) | ||||
| --         return func or function () | ||||
| --             vim.api.nvim_get_keymap(' th') | ||||
| --         end | ||||
| --     end | ||||
| -- | ||||
| -- end | ||||
|  | ||||
|  | ||||
| return M | ||||
|   | ||||
| @@ -24,19 +24,24 @@ M.conf = { | ||||
|         'tag', | ||||
|         'pos', | ||||
|         'exchange', | ||||
|         -- 'translation', | ||||
|         'translation', | ||||
|         -- NOTE :如果你想限制某个组件的行数,可以设置max_size | ||||
|         -- { 'Definition', max_size = 4 }, | ||||
|         'definition', | ||||
|         -- }, | ||||
|         -- online = { | ||||
|         --     -- TODO | ||||
|         -- }, | ||||
|     }, | ||||
|     icon = { | ||||
|         star = '⭐', | ||||
|         notfound = '❔', | ||||
|         yes = '✔️', | ||||
|         no = '❌' | ||||
|         star = '', | ||||
|         notfound = '', | ||||
|         yes = '', | ||||
|         no = '' | ||||
|         -- star = '⭐', | ||||
|         -- notfound = '❔', | ||||
|         -- yes = '✔️', | ||||
|         -- no = '❌' | ||||
|     }, | ||||
|     db_path = '$HOME/.vim/dict/ultimate.db', | ||||
|     -- TODO : | ||||
| @@ -44,9 +49,13 @@ M.conf = { | ||||
|     --     -- TODO | ||||
|     --     'offline', | ||||
|     -- } | ||||
|     -- map = { | ||||
|     --     -- TODO | ||||
|     -- }, | ||||
|     map = { | ||||
|         -- TODO | ||||
|         hover = { | ||||
|             pageup = '<C-u>', | ||||
|             pagedown = '<C-d>', | ||||
|         }, | ||||
|     }, | ||||
|     -- history = { | ||||
|     --     -- TOOD | ||||
|     -- } | ||||
|   | ||||
| @@ -21,9 +21,12 @@ local highlights = { | ||||
|     TransPhonetic = { | ||||
|         link = 'Linenr' | ||||
|     }, | ||||
|     TransRef = { | ||||
|     TransTitle = { | ||||
|         fg = '#0f0f15', | ||||
|         bg = '#75beff', | ||||
|     }, | ||||
|     TransTitleRound = { | ||||
|         fg = '#75beff', | ||||
|         bold = true, | ||||
|     }, | ||||
|     TransTag = { | ||||
|         fg = '#e5c07b', | ||||
| @@ -38,16 +41,18 @@ local highlights = { | ||||
|         link = 'TransWord', | ||||
|     }, | ||||
|     TransDefinition = { | ||||
|         -- fg = '#bc8cff', | ||||
|         link = 'Moremsg', | ||||
|     }, | ||||
|     TransCursorWin = { | ||||
|     TransHoverWin = { | ||||
|         link = 'Normal', | ||||
|     }, | ||||
|  | ||||
|     TransCursorBorder = { | ||||
|     TransHoverBorder = { | ||||
|         link = 'FloatBorder', | ||||
|     } | ||||
|     }, | ||||
|     TransCollins = { | ||||
|         fg = '#faf743', | ||||
|         bold = true, | ||||
|     }, | ||||
| } | ||||
|  | ||||
| -- TODO | ||||
|   | ||||
		Reference in New Issue
	
	Block a user