pbnj

snippet

local handlers = {}

handlers["initialize"] = function(_, callback)
	callback(nil, {
		capabilities = {
			hoverProvider = true,
		},
		serverInfo = {
			name = "testlsp",
			version = "0.0.1",
		},
	})
end

handlers["textDocument/hover"] = function(_, callback)
	local serial = vim.fn.expand("<cword>") -- cheating!
	local command_format = "gxctl --config=/home/danieln/.gxctl/config.yaml --profile='gridx' get device -S %s -o json"
	vim.system(
		{
			"fish",
			"-c",
			command_format:format(serial),
		},
		vim.schedule_wrap(function(out)
			local contents
			if out.code ~= 0 then
				contents = "fetch failed"
			else
				local ok, decoded = pcall(vim.json.decode, out.stdout)
				if ok and decoded and decoded[1] then
					contents = decoded[1].status.info.publicIP .. ", last seen: " .. decoded[1].status.lastHeartbeat
				else
					contents = "parse failed"
				end
			end
			callback(nil, { contents = contents })
		end)
	)
end

vim.api.nvim_create_autocmd("FileType", {
	pattern = { "markdown" },
	callback = function(ev)
		vim.lsp.start({
			name = "testlsp",
			cmd = function()
				return {
					request = function(method, params, callback)
						if handlers[method] then
							handlers[method](params, callback)
						end
					end,
					notify = function() end,
					is_closing = function() end,
					terminate = function() end,
				}
			end,
		}, { bufnr = ev.buf, silent = false })
	end,
})