-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathgh_actions_ls.lua
More file actions
91 lines (86 loc) · 2.83 KB
/
gh_actions_ls.lua
File metadata and controls
91 lines (86 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---@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
--- ```
--- Session token used to perform authenticated API requests
---@return string? token
local function token()
local result = vim.system({ 'gh', 'auth', 'token', '-h', 'github.com' }):wait()
return result.code == 0 and vim.trim(assert(result.stdout)) or nil
end
--- Repository metadata needed for full features
---@param root_dir string
---@return table? metadata about the repository
local function repos(root_dir)
local result = vim.system({ 'gh', 'repo', 'view', '--json', 'owner,name,isInOrganization' }):wait()
if result.code == 0 then
local data = vim.json.decode(assert(result.stdout))
return {
{
owner = data.owner.login --[[@as string]],
name = data.name --[[@as string]],
organizationOwned = data.isInOrganization --[[@as boolean]],
workspaceUri = vim.uri_from_fname(root_dir),
},
}
end
return nil
end
---@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
before_init = function(params, config)
if config.root_dir and vim.fn.executable('gh') == 1 then
params.initializationOptions = {
sessionToken = token(),
repos = repos(config.root_dir),
}
end
end,
capabilities = {
workspace = {
didChangeWorkspaceFolders = {
dynamicRegistration = true,
},
},
},
}