Skip to content

Commit 5b1abfc

Browse files
committed
feat: Add lua language server to pre-commit
Fixes cursorless-dev#2576. Adds lua-language-server linter to pre-commit. There is no existing pre-commit hook, and also no Ubuntu package for this linter. I install it using nix package manager, as nixpkgs is one of the only places with a package. However, there is also a recent bug that prevents --check invocation of the cli. I have patched this in the lua-language-server using a nix overlay, until the PR is merged. This also moves the linting configuration to a single source of true, that vscode, pre-commit, CI can all use.
1 parent a3982a9 commit 5b1abfc

File tree

7 files changed

+67
-6
lines changed

7 files changed

+67
-6
lines changed

.github/workflows/pre-commit.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
pre-commit:
1414
name: Pre-commit
1515
runs-on: ubuntu-latest
16+
env:
17+
CURSORLESS_REPO_ROOT: ${{ github.workspace }}
1618
steps:
1719
- uses: actions/checkout@v4
1820
- uses: actions/setup-python@v5
@@ -26,6 +28,11 @@ jobs:
2628
- run: pnpm --color install
2729
- uses: leafo/gh-actions-lua@v9
2830
- uses: leafo/gh-actions-luarocks@v4
31+
- uses: cachix/install-nix-action@v27
32+
with:
33+
nix_path: nixpkgs=channel:nixos-unstable
34+
- run: nix profile install --quiet --accept-flake-config nixpkgs#lua-language-server --override-input nixpkgs path:$PWD/patches/lua-language-server.nix
35+
shell: bash
2936
- uses: pre-commit/[email protected]
3037
- uses: pre-commit-ci/[email protected]
3138
if: always()

.luarc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"runtime.version": "Lua 5.1",
3+
"diagnostics.ignoredFiles": "Disable",
4+
"diagnostics.globals": ["vim", "talon", "it", "describe"],
5+
"workspace.ignoreDir": ["data/playground/lua/"]
6+
}

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ repos:
9191
hooks:
9292
- id: stylua
9393
exclude: ^data/playground/lua/.*\.lua$
94+
- repo: local
95+
hooks:
96+
- id: lua-language-server
97+
name: lua-language-server
98+
files: \.(lua|busted)$
99+
entry: scripts/lint-lua.sh
100+
language: script

.vscode/settings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
"typescript.enablePromptUseWorkspaceTsdk": true,
2727
"typescript.tsdk": "node_modules/typescript/lib",
2828
"eslint.workingDirectories": [{ "pattern": "packages/*/" }],
29-
"Lua.runtime.version": "Lua 5.1",
30-
"Lua.diagnostics.globals": ["vim", "talon", "it", "describe"],
31-
"Lua.diagnostics.ignoredFiles": "Disable",
32-
"Lua.workspace.ignoreDir": ["data/playground/lua/"],
3329
"[lua]": {
3430
"editor.defaultFormatter": "JohnnyMorganz.stylua"
3531
},

flake.nix

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
pkgs = import nixpkgs {
2323
inherit system;
2424
overlays = [
25+
(import ./patches/lua-language-server.nix)
2526
# https://github.com/NixOS/nixpkgs/pull/317333
2627
(final: prev: {
2728
nodePackages = prev.nodePackages // {
@@ -45,7 +46,6 @@
4546
};
4647
};
4748
neovim = prev.neovim.override { withNodeJs = true; };
48-
4949
})
5050

5151
];
@@ -69,19 +69,21 @@
6969
[
7070
pkgs.corepack
7171
pkgs.vsce
72+
pkgs.nodejs
73+
7274
# https://github.com/NixOS/nixpkgs/pull/251418
7375
(pkgs.pre-commit.overrideAttrs (previousAttrs: {
7476
makeWrapperArgs = ''
7577
--set PYTHONPATH $PYTHONPATH
7678
'';
7779
}))
7880
python
81+
pkgs.lua-language-server # language server used by pre-commit hooks
7982

8083
pkgs.neovim
8184
pkgs.luajitPackages.busted # for lua testing
8285
pkgs.luarocks # pre-commit doesn't auto-install luarocks
8386
pkgs.ps
84-
pkgs.nodejs
8587
];
8688
# To prevent weird broken non-interactive bash terminal
8789
buildInputs = [ pkgs.bashInteractive ];

patches/lua-language-server.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(final: prev: {
2+
# There is a recent bug that prevents cli invocation:
3+
# See: https://github.com/LuaLS/lua-language-server/pull/2775
4+
lua-language-server = prev.lua-language-server.overrideAttrs {
5+
postPatch =
6+
let
7+
patch = prev.fetchurl {
8+
url = "https://github.com/LuaLS/lua-language-server/pull/2775.patch";
9+
sha256 = "sha256-5hjuNzBHLp9kiD6O8jTL5YlvaqR8IuJPHchIZE2/p/Q=";
10+
};
11+
in
12+
''
13+
patch -p1 < ${patch}
14+
''
15+
+ prev.lua-language-server.postPatch;
16+
};
17+
})

scripts/lint-lua.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# lua-language-server should be installed automatically by the flake.nix dev shell
5+
# or .github/workflows/pre-commit.yml
6+
if ! type lua-language-server &>/dev/null; then
7+
echo "ERROR: lua-ls is not installed. Please run 'nix develop' or install it manually."
8+
exit 1
9+
fi
10+
11+
if [ ! -e "${CURSORLESS_REPO_ROOT-nonexistent}" ]; then
12+
CURSORLESS_REPO_ROOT=$(git rev-parse --show-toplevel)
13+
fi
14+
15+
logpath="$(mktemp -d)"
16+
rm -rf "$logpath/check.json"
17+
result=$(lua-language-server --check "${CURSORLESS_REPO_ROOT}" \
18+
--checklevel="Warning" \
19+
--configpath="${CURSORLESS_REPO_ROOT}/.luarc.json" \
20+
--logpath="$logpath")
21+
if [[ ! "$result" == *"no problems found"* ]]; then
22+
cat "$logpath/check.json"
23+
exit 1
24+
fi
25+
26+
exit 0

0 commit comments

Comments
 (0)