towolf / vim-helm

vim syntax for helm templates (yaml + gotmpl + sprig + custom)
Other
213 stars 22 forks source link

How do you deal with LSP YAML detection? #15

Closed rafi closed 1 year ago

rafi commented 2 years ago

Helm files are handled as YAML and produces much LSP errors. I've got this in my on_attach function:

local on_attach = function(client, bufnr)
    -- Short-circuit for Helm template files
    if vim.bo[bufnr].buftype ~= '' or vim.bo[bufnr].filetype == 'helm' then
        vim.diagnostic.disable(bufnr)
        vim.defer_fn(function()
            vim.diagnostic.reset(nil, bufnr)
        end, 1000)
        return
    end
...

It's not perfect (defer_fn), the statusline still reports error summary sometimes, but it's the best I came up with. Anybody has better workarounds?

phortonssf commented 2 years ago

I am trying to figure out the same thing currently, I have the autocomplete working for the Kubernetes yaml file but the helm charts blow up the LSP diagnostics. Is neovim able to tell a helm file from a regular yaml file? If so what do I need for that part? This reddit post here has the same direction but I dont know how you get a helm filetype. helm charts neovim

I also found kubelinter Thank you.

dannylongeuay commented 2 years ago

My hacky solution to detach the yamlls from helm templates.

local function detach_yamlls()
    local clients = vim.lsp.get_active_clients()
    for client_id, client in pairs(clients) do
        if client.name == "yamlls" then
            vim.lsp.buf_detach_client(0, client_id)
        end
    end
end

local gotmpl_group = vim.api.nvim_create_augroup("_gotmpl", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
    group = gotmpl_group,
    pattern = "yaml",
    callback = function()
        vim.schedule(function()
            local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
            for _, line in ipairs(lines) do
                if string.match(line, "{{.+}}") then
                    vim.defer_fn(detach_yamlls, 500)
                    return
                end
            end
        end)
    end,
})

The main idea here is to do a quick pattern check on the yaml file, {{.+}}. If the pattern match is true then we wait an arbitrary amount of time (500 ms seems to work well for me) and detach the yamlls from the buffer.

dminca commented 2 years ago

There was this suggestion on reddit from u/jemag

if vim.bo[bufnr].buftype ~= "" or vim.bo[bufnr].filetype == "helm" then
      vim.diagnostic.disable(bufnr)
      vim.defer_fn(function()
        vim.diagnostic.reset(nil, bufnr)
      end, 1000)
    end
Vanderscycle commented 1 year ago

My hacky solution to detach the yamlls from helm templates.

local function detach_yamlls()
  local clients = vim.lsp.get_active_clients()
  for client_id, client in pairs(clients) do
      if client.name == "yamlls" then
          vim.lsp.buf_detach_client(0, client_id)
      end
  end
end

local gotmpl_group = vim.api.nvim_create_augroup("_gotmpl", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
  group = gotmpl_group,
  pattern = "yaml",
  callback = function()
      vim.schedule(function()
          local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
          for _, line in ipairs(lines) do
              if string.match(line, "{{.+}}") then
                  vim.defer_fn(detach_yamlls, 500)
                  return
              end
          end
      end)
  end,
})

The main idea here is to do a quick pattern check on the yaml file, {{.+}}. If the pattern match is true then we wait an arbitrary amount of time (500 ms seems to work well for me) and detach the yamlls from the buffer.

This actually worked the best for me. Thank you for sharing.

rafi commented 1 year ago

Use https://github.com/mrjosh/helm-ls

dami2 commented 1 year ago

My hacky solution to detach the yamlls from helm templates.

local function detach_yamlls()
  local clients = vim.lsp.get_active_clients()
  for client_id, client in pairs(clients) do
      if client.name == "yamlls" then
          vim.lsp.buf_detach_client(0, client_id)
      end
  end
end

local gotmpl_group = vim.api.nvim_create_augroup("_gotmpl", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
  group = gotmpl_group,
  pattern = "yaml",
  callback = function()
      vim.schedule(function()
          local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
          for _, line in ipairs(lines) do
              if string.match(line, "{{.+}}") then
                  vim.defer_fn(detach_yamlls, 500)
                  return
              end
          end
      end)
  end,
})

The main idea here is to do a quick pattern check on the yaml file, {{.+}}. If the pattern match is true then we wait an arbitrary amount of time (500 ms seems to work well for me) and detach the yamlls from the buffer.

In case syntax is not working for you, I had to do the following:

if client.name == "yamlls" then
       vim.lsp.buf_detach_client(0, client_id)
       vim.cmd "set syntax=helm"
end
AMacedoP commented 1 year ago

In case anyone is using nvim-lua/kickstart I was able to solve this issue by setting the ft property in the plugin declaration:

require('lazy').setup({
  ...
  {
    'towolf/vim-helm',
    ft = 'helm',
  },
  ...
}, {})

I'm not sure why this works, can someone explain it?