document_color_spec.lua (8031B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 local t_lsp = require('test.functional.plugin.lsp.testutil') 4 local Screen = require('test.functional.ui.screen') 5 6 local dedent = t.dedent 7 local eq = t.eq 8 9 local api = n.api 10 local exec_lua = n.exec_lua 11 local insert = n.insert 12 13 local clear_notrace = t_lsp.clear_notrace 14 local create_server_definition = t_lsp.create_server_definition 15 16 describe('vim.lsp.document_color', function() 17 local text = dedent([[ 18 body { 19 color: #FFF; 20 background-color: rgb(0, 255, 255); 21 } 22 ]]) 23 24 local grid_without_colors = [[ 25 body { | 26 color: #FFF; | 27 background-color: rgb(0, 255, 255); | 28 } | 29 ^ | 30 {1:~ }|*8 31 | 32 ]] 33 34 local grid_with_colors = [[ 35 body { | 36 color: {2:#FFF}; | 37 background-color: {3:rgb(0, 255, 255)}; | 38 } | 39 ^ | 40 {1:~ }|*8 41 | 42 ]] 43 44 --- @type test.functional.ui.screen 45 local screen 46 47 --- @type integer 48 local client_id 49 50 --- @type integer 51 local bufnr 52 53 before_each(function() 54 clear_notrace() 55 exec_lua(create_server_definition) 56 57 screen = Screen.new() 58 screen:set_default_attr_ids { 59 [1] = { bold = true, foreground = Screen.colors.Blue1 }, 60 [2] = { background = Screen.colors.Gray100, foreground = Screen.colors.Gray0 }, 61 [3] = { background = Screen.colors.Cyan1, foreground = Screen.colors.Gray0 }, 62 [4] = { foreground = Screen.colors.Grey100 }, 63 [5] = { foreground = Screen.colors.Cyan1 }, 64 } 65 66 bufnr = n.api.nvim_get_current_buf() 67 client_id = exec_lua(function() 68 _G.server = _G._create_server({ 69 capabilities = { 70 colorProvider = true, 71 }, 72 handlers = { 73 ['textDocument/documentColor'] = function(_, _, callback) 74 callback(nil, { 75 { 76 range = { 77 start = { line = 1, character = 9 }, 78 ['end'] = { line = 1, character = 13 }, 79 }, 80 color = { red = 1, green = 1, blue = 1 }, 81 }, 82 { 83 range = { 84 start = { line = 2, character = 20 }, 85 ['end'] = { line = 2, character = 36 }, 86 }, 87 color = { red = 0, green = 1, blue = 1 }, 88 }, 89 }) 90 end, 91 }, 92 }) 93 94 return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }) 95 end) 96 97 insert(text) 98 99 exec_lua(function() 100 vim.lsp.document_color.enable(true, bufnr) 101 end) 102 103 screen:expect({ grid = grid_with_colors }) 104 end) 105 106 after_each(function() 107 api.nvim_exec_autocmds('VimLeavePre', { modeline = false }) 108 end) 109 110 it('clears document colors when sole client detaches', function() 111 exec_lua(function() 112 vim.lsp.get_client_by_id(client_id):stop() 113 end) 114 115 screen:expect({ grid = grid_without_colors }) 116 end) 117 118 it('supports dynamic registration', function() 119 local grid_with_dynamic_highlights = [[ 120 body { | 121 {2:color}: {2:#FFF}; | 122 background-color: {3:rgb(0, 255, 255)}; | 123 } | 124 ^ | 125 {1:~ }|*8 126 | 127 ]] 128 129 exec_lua(function() 130 _G.server2 = _G._create_server({ 131 colorProvider = { 132 documentSelector = vim.NIL, 133 }, 134 handlers = { 135 ['textDocument/documentColor'] = function(_, _, callback) 136 callback(nil, { 137 { 138 range = { 139 start = { line = 1, character = 2 }, 140 ['end'] = { line = 1, character = 7 }, 141 }, 142 color = { red = 1, green = 1, blue = 1 }, 143 }, 144 }) 145 end, 146 }, 147 }) 148 149 local client_id2 = assert(vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd })) 150 151 vim.lsp.handlers['client/registerCapability'](nil, { 152 registrations = { 153 { id = 'documentColor', method = 'textDocument/documentColor' }, 154 }, 155 }, { client_id = client_id2, method = 'client/registerCapability' }) 156 end) 157 158 screen:expect({ grid = grid_with_dynamic_highlights }) 159 end) 160 161 it('does not clear document colors when one of several clients detaches', function() 162 local client_id2 = exec_lua(function() 163 _G.server2 = _G._create_server({ 164 capabilities = { 165 colorProvider = true, 166 }, 167 handlers = { 168 ['textDocument/documentColor'] = function(_, _, callback) 169 callback(nil, {}) 170 end, 171 }, 172 }) 173 local client_id2 = vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd }) 174 vim.lsp.document_color.enable(true, bufnr) 175 return client_id2 176 end) 177 178 exec_lua(function() 179 vim.lsp.get_client_by_id(client_id2):stop() 180 end) 181 182 screen:expect({ grid = grid_with_colors, unchanged = true }) 183 end) 184 185 describe('is_enabled()', function() 186 it('returns true when document colors is enabled', function() 187 eq( 188 true, 189 exec_lua(function() 190 return vim.lsp.document_color.is_enabled(bufnr) 191 end) 192 ) 193 194 exec_lua(function() 195 vim.lsp.get_client_by_id(client_id):stop() 196 end) 197 198 eq( 199 false, 200 exec_lua(function() 201 return vim.lsp.document_color.is_enabled(bufnr) 202 end) 203 ) 204 end) 205 206 it('does not error when called on a new unattached buffer', function() 207 eq( 208 false, 209 exec_lua(function() 210 return vim.lsp.document_color.is_enabled(vim.api.nvim_create_buf(false, true)) 211 end) 212 ) 213 end) 214 end) 215 216 describe('enable()', function() 217 it('supports foreground styling', function() 218 local grid_with_fg_colors = [[ 219 body { | 220 color: {4:#FFF}; | 221 background-color: {5:rgb(0, 255, 255)}; | 222 } | 223 ^ | 224 {1:~ }|*8 225 | 226 ]] 227 228 exec_lua(function() 229 vim.lsp.document_color.enable(true, bufnr, { style = 'foreground' }) 230 end) 231 232 screen:expect({ grid = grid_with_fg_colors }) 233 end) 234 235 it('supports custom swatch text', function() 236 local grid_with_swatches = [[ 237 body { | 238 color: {4: :) }#FFF; | 239 background-color: {5: :) }rgb(0, 255, 255); | 240 } | 241 ^ | 242 {1:~ }|*8 243 | 244 ]] 245 246 exec_lua(function() 247 vim.lsp.document_color.enable(true, bufnr, { style = ' :) ' }) 248 end) 249 250 screen:expect({ grid = grid_with_swatches }) 251 end) 252 253 it('will not create highlights with custom style function', function() 254 exec_lua(function() 255 vim.lsp.document_color.enable(true, bufnr, { 256 style = function() end, 257 }) 258 end) 259 260 screen:expect({ grid = grid_without_colors }) 261 end) 262 end) 263 end)