Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
diagnostics: convert columns to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
name-snrl committed May 28, 2023
1 parent 77e53bc commit 28aec7d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lua/null-ls/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,29 @@ local postprocess = function(diagnostic, _, generator)
diagnostic.message = formatted
end

local function column_to_byte(line, col, offset_encoding)
local ok, result = pcall(vim.str_byteindex, line, col, offset_encoding == "utf-16")
if ok then
return result
end

return col
end

local function columns_to_bytes(bufnr, diagnostics)
local offset_encoding = "utf-16"
return vim.tbl_map(function(diagnostic)
local start_line = vim.api.nvim_buf_get_lines(bufnr, diagnostic.lnum, diagnostic.lnum + 1, false)[1]
local end_line = vim.api.nvim_buf_get_lines(bufnr, diagnostic.end_lnum, diagnostic.end_lnum + 1, false)[1]
return vim.tbl_deep_extend("force", diagnostic, {
col = column_to_byte(start_line, diagnostic.col, offset_encoding),
end_col = column_to_byte(end_line, diagnostic.end_col, offset_encoding),
})
end, diagnostics)
end

local handle_single_file_diagnostics = function(namespace, diagnostics, bufnr)
vim.diagnostic.set(namespace, bufnr, diagnostics)
vim.diagnostic.set(namespace, bufnr, columns_to_bytes(bufnr, diagnostics))
end

local handle_multiple_file_diagnostics = function(namespace, diagnostics)
Expand All @@ -110,7 +131,7 @@ local handle_multiple_file_diagnostics = function(namespace, diagnostics)
end

for bufnr, bufnr_diagnostics in pairs(by_bufnr) do
vim.diagnostic.set(namespace, bufnr, bufnr_diagnostics)
vim.diagnostic.set(namespace, bufnr, columns_to_bytes(bufnr, bufnr_diagnostics))
end
end

Expand Down

0 comments on commit 28aec7d

Please sign in to comment.