nvim-lspconfig/lsp/gh_actions_ls.lua
Julio García 7e585e3f37
feat(gh_actions_ls): handle "actions/readFile" request #4046
Fixes https://github.com/lttb/gh-actions-language-server/issues/5

Implement a custom handler for the "actions/readFile" request in the
gh_actions_ls config. This handler reads the requested file from disk and
returns its contents if the file is readable. This improves integration
with the GitHub Actions language server by supporting file content
requests.

Here is how the vscode github actions extension sets the handler:
https://github.com/github/vscode-github-actions/blob/main/src/workflow/languageServer.ts#L68

TODO:
We could also provide an implementation for populating the init_options
properly, docs currently suggest an empty table. For the lsp to work properly,
it should be populated with this "shape":

    init_options = {
      sessionToken = session_token,
      repos = {
        {
          id = 1008200293,
          owner = org_name,
          name = repo_name,
          workspaceUri = "file://" .. vim.fn.getcwd(),
          organizationOwned = true,
        },
      },
    },
2025-09-01 10:40:46 -07:00

58 lines
1.7 KiB
Lua

---@brief
--- https://github.com/lttb/gh-actions-language-server
---
--- Language server for GitHub Actions.
---
--- The projects [forgejo](https://forgejo.org/) and [gitea](https://about.gitea.com/)
--- design their actions to be as compatible to github as possible
--- with only [a few differences](https://docs.gitea.com/usage/actions/comparison#unsupported-workflows-syntax) between the systems.
--- The `gh_actions_ls` is therefore enabled for those `yaml` files as well.
---
--- The `gh-actions-language-server` can be installed via `npm`:
---
--- ```sh
--- npm install -g gh-actions-language-server
--- ```
---@type vim.lsp.Config
return {
cmd = { 'gh-actions-language-server', '--stdio' },
filetypes = { 'yaml' },
-- `root_dir` ensures that the LSP does not attach to all yaml files
root_dir = function(bufnr, on_dir)
local parent = vim.fs.dirname(vim.api.nvim_buf_get_name(bufnr))
if
vim.endswith(parent, '/.github/workflows')
or vim.endswith(parent, '/.forgejo/workflows')
or vim.endswith(parent, '/.gitea/workflows')
then
on_dir(parent)
end
end,
handlers = {
['actions/readFile'] = function(_, result)
if type(result.path) ~= 'string' then
return nil, nil
end
local file_path = vim.uri_to_fname(result.path)
if vim.fn.filereadable(file_path) == 1 then
local f = assert(io.open(file_path, 'r'))
local text = f:read('*a')
f:close()
return text, nil
end
return nil, nil
end,
},
init_options = {}, -- needs to be present https://github.com/neovim/nvim-lspconfig/pull/3713#issuecomment-2857394868
capabilities = {
workspace = {
didChangeWorkspaceFolders = {
dynamicRegistration = true,
},
},
},
}