|
| 1 | +local config = require('blink.cmp.config').accept.auto_brackets |
| 2 | +local utils = require('blink.cmp.accept.brackets.utils') |
| 3 | + |
| 4 | +local semantic = {} |
| 5 | + |
| 6 | +--- Asynchronously use semantic tokens to determine if brackets should be added |
| 7 | +--- @param filetype string |
| 8 | +--- @param item blink.cmp.CompletionItem |
| 9 | +--- @param callback fun() |
| 10 | +function semantic.add_brackets_via_semantic_token(filetype, item, callback) |
| 11 | + if not utils.should_run_resolution(filetype, 'semantic_token') then return callback() end |
| 12 | + |
| 13 | + local text_edit = item.textEdit |
| 14 | + assert(text_edit ~= nil, 'Got nil text edit while adding brackets via semantic tokens') |
| 15 | + local client = vim.lsp.get_client_by_id(item.client_id) |
| 16 | + if client == nil then return callback() end |
| 17 | + |
| 18 | + local capabilities = client.server_capabilities.semanticTokensProvider |
| 19 | + if not capabilities or not capabilities.legend or (not capabilities.range and not capabilities.full) then |
| 20 | + return callback() |
| 21 | + end |
| 22 | + |
| 23 | + local token_types = client.server_capabilities.semanticTokensProvider.legend.tokenTypes |
| 24 | + local params = { |
| 25 | + textDocument = vim.lsp.util.make_text_document_params(), |
| 26 | + range = capabilities.range and { |
| 27 | + start = { line = text_edit.range.start.line, character = text_edit.range.start.character }, |
| 28 | + ['end'] = { line = text_edit.range.start.line + 1, character = 0 }, |
| 29 | + } or nil, |
| 30 | + } |
| 31 | + |
| 32 | + local cursor_before_call = vim.api.nvim_win_get_cursor(0) |
| 33 | + |
| 34 | + local start_time = vim.uv.hrtime() |
| 35 | + client.request( |
| 36 | + capabilities.range and 'textDocument/semanticTokens/range' or 'textDocument/semanticTokens/full', |
| 37 | + params, |
| 38 | + function(err, result) |
| 39 | + if err ~= nil or result == nil or #result.data == 0 then return callback() end |
| 40 | + |
| 41 | + -- cancel if it's been too long, or if the cursor moved |
| 42 | + local ms_since_call = (vim.uv.hrtime() - start_time) / 1000000 |
| 43 | + local cursor_after_call = vim.api.nvim_win_get_cursor(0) |
| 44 | + if |
| 45 | + ms_since_call > config.semantic_token_resolution.timeout_ms |
| 46 | + or cursor_before_call[1] ~= cursor_after_call[1] |
| 47 | + or cursor_before_call[2] ~= cursor_after_call[2] |
| 48 | + then |
| 49 | + return callback() |
| 50 | + end |
| 51 | + |
| 52 | + for _, token in ipairs(semantic.process_semantic_token_data(result.data, token_types)) do |
| 53 | + if |
| 54 | + cursor_after_call[1] == token.line |
| 55 | + and cursor_after_call[2] >= token.start_col |
| 56 | + and cursor_after_call[2] <= token.end_col |
| 57 | + then |
| 58 | + -- add the brackets |
| 59 | + local brackets_for_filetype = utils.get_for_filetype(filetype, item) |
| 60 | + local line = vim.api.nvim_get_current_line() |
| 61 | + local start_col = text_edit.range.start.character + #text_edit.newText |
| 62 | + local new_line = line:sub(1, start_col) |
| 63 | + .. brackets_for_filetype[1] |
| 64 | + .. brackets_for_filetype[2] |
| 65 | + .. line:sub(start_col + 1) |
| 66 | + vim.api.nvim_set_current_line(new_line) |
| 67 | + vim.api.nvim_win_set_cursor(0, { cursor_after_call[1], start_col + #brackets_for_filetype[1] }) |
| 68 | + callback() |
| 69 | + return |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + callback() |
| 74 | + end |
| 75 | + ) |
| 76 | +end |
| 77 | + |
| 78 | +function semantic.process_semantic_token_data(data, token_types) |
| 79 | + local tokens = {} |
| 80 | + local idx = 0 |
| 81 | + local token_line = 0 |
| 82 | + local token_start_col = 0 |
| 83 | + |
| 84 | + while (idx + 1) * 5 <= #data do |
| 85 | + local delta_token_line = data[idx * 5 + 1] |
| 86 | + local delta_token_start_col = data[idx * 5 + 2] |
| 87 | + local delta_token_length = data[idx * 5 + 3] |
| 88 | + local type = token_types[data[idx * 5 + 4] + 1] |
| 89 | + |
| 90 | + if delta_token_line > 0 then token_start_col = 0 end |
| 91 | + token_line = token_line + delta_token_line |
| 92 | + token_start_col = token_start_col + delta_token_start_col |
| 93 | + |
| 94 | + table.insert(tokens, { |
| 95 | + line = token_line + 1, |
| 96 | + start_col = token_start_col, |
| 97 | + end_col = token_start_col + delta_token_length, |
| 98 | + type = type, |
| 99 | + }) |
| 100 | + |
| 101 | + token_start_col = token_start_col + delta_token_length |
| 102 | + idx = idx + 1 |
| 103 | + end |
| 104 | + |
| 105 | + return tokens |
| 106 | +end |
| 107 | + |
| 108 | +return semantic.add_brackets_via_semantic_token |
0 commit comments