Created
December 22, 2023 23:07
-
-
Save shirts/513fb1dd1eec3fdd01f48b3b948a4c6e to your computer and use it in GitHub Desktop.
C# Dotnet show virtual text warnings/errors in vim virtual text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Call DotnetRunAndShowVirtualText on file open & save | |
autocmd BufReadPost,BufWritePost *.cs | call DotnetRunAndShowVirtualText() | |
" Delete virtual text on a buffer when it is modified | |
autocmd TextChanged,TextChangedI *.cs lua vim.api.nvim_buf_clear_namespace(0, -1, vim.fn.line(".") - 1, vim.fn.line(".")) | |
highlight MyWarningVirtualText guifg=orange | |
highlight MyErrorVirtualText guifg=red | |
function! DotnetRunAndShowVirtualText() | |
lua << EOF | |
-- Get the name of the currently open file | |
local target_file = vim.api.nvim_buf_get_name(0) | |
-- Clear the existing virtual text from every line | |
vim.api.nvim_buf_clear_namespace(0, -1, 0, -1) | |
-- Define a handler for the job output | |
local on_stdout = function(_, data, _) | |
-- Parse the output | |
for _, line in ipairs(data) do | |
-- Split the line into parts | |
local parts = {} | |
for part in line:gmatch("%S+") do | |
table.insert(parts, part) | |
end | |
-- Extract the file path, line number and message | |
local file_and_line = parts[1] | |
local message = table.concat(parts, " ", 3) | |
-- Remove the file name from the end of the message | |
message = message:gsub(" %[.*%]$", "") | |
-- Extract the file path and line number from the file and line part | |
local file_path, line_number = file_and_line:match("^(.-)%((%d+),") | |
if file_path == target_file and line_number then | |
-- Convert the line number to a number | |
line_number = tonumber(line_number) | |
local highlight_group = line:find("error CS") and "MyErrorVirtualText" or "MyWarningVirtualText" | |
-- Add virtual text to the line with the error or warning, with spaces added for padding | |
vim.api.nvim_buf_set_virtual_text(0, -1, line_number - 1, {{" "..message, highlight_group}}, {}) | |
end | |
end | |
end | |
-- Start the job | |
vim.fn.jobstart("dotnet run", {on_stdout = on_stdout}) | |
EOF | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment