Skip to content

Commit

Permalink
feat: Add lua language server to pre-commit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fidgetingbits committed Jul 30, 2024
1 parent a3982a9 commit 5b1abfc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
pre-commit:
name: Pre-commit
runs-on: ubuntu-latest
env:
CURSORLESS_REPO_ROOT: ${{ github.workspace }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand All @@ -26,6 +28,11 @@ jobs:
- run: pnpm --color install
- uses: leafo/gh-actions-lua@v9
- uses: leafo/gh-actions-luarocks@v4
- uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: nix profile install --quiet --accept-flake-config nixpkgs#lua-language-server --override-input nixpkgs path:$PWD/patches/lua-language-server.nix
shell: bash
- uses: pre-commit/[email protected]
- uses: pre-commit-ci/[email protected]
if: always()
6 changes: 6 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"runtime.version": "Lua 5.1",
"diagnostics.ignoredFiles": "Disable",
"diagnostics.globals": ["vim", "talon", "it", "describe"],
"workspace.ignoreDir": ["data/playground/lua/"]
}
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,10 @@ repos:
hooks:
- id: stylua
exclude: ^data/playground/lua/.*\.lua$
- repo: local
hooks:
- id: lua-language-server
name: lua-language-server
files: \.(lua|busted)$
entry: scripts/lint-lua.sh
language: script
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"typescript.tsdk": "node_modules/typescript/lib",
"eslint.workingDirectories": [{ "pattern": "packages/*/" }],
"Lua.runtime.version": "Lua 5.1",
"Lua.diagnostics.globals": ["vim", "talon", "it", "describe"],
"Lua.diagnostics.ignoredFiles": "Disable",
"Lua.workspace.ignoreDir": ["data/playground/lua/"],
"[lua]": {
"editor.defaultFormatter": "JohnnyMorganz.stylua"
},
Expand Down
6 changes: 4 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
pkgs = import nixpkgs {
inherit system;
overlays = [
(import ./patches/lua-language-server.nix)
# https://github.com/NixOS/nixpkgs/pull/317333
(final: prev: {
nodePackages = prev.nodePackages // {
Expand All @@ -45,7 +46,6 @@
};
};
neovim = prev.neovim.override { withNodeJs = true; };

})

];
Expand All @@ -69,19 +69,21 @@
[
pkgs.corepack
pkgs.vsce
pkgs.nodejs

# https://github.com/NixOS/nixpkgs/pull/251418
(pkgs.pre-commit.overrideAttrs (previousAttrs: {
makeWrapperArgs = ''
--set PYTHONPATH $PYTHONPATH
'';
}))
python
pkgs.lua-language-server # language server used by pre-commit hooks

pkgs.neovim
pkgs.luajitPackages.busted # for lua testing
pkgs.luarocks # pre-commit doesn't auto-install luarocks
pkgs.ps
pkgs.nodejs
];
# To prevent weird broken non-interactive bash terminal
buildInputs = [ pkgs.bashInteractive ];
Expand Down
17 changes: 17 additions & 0 deletions patches/lua-language-server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(final: prev: {
# There is a recent bug that prevents cli invocation:
# See: https://github.com/LuaLS/lua-language-server/pull/2775
lua-language-server = prev.lua-language-server.overrideAttrs {
postPatch =
let
patch = prev.fetchurl {
url = "https://github.com/LuaLS/lua-language-server/pull/2775.patch";
sha256 = "sha256-5hjuNzBHLp9kiD6O8jTL5YlvaqR8IuJPHchIZE2/p/Q=";
};
in
''
patch -p1 < ${patch}
''
+ prev.lua-language-server.postPatch;
};
})
26 changes: 26 additions & 0 deletions scripts/lint-lua.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -euo pipefail

# lua-language-server should be installed automatically by the flake.nix dev shell
# or .github/workflows/pre-commit.yml
if ! type lua-language-server &>/dev/null; then
echo "ERROR: lua-ls is not installed. Please run 'nix develop' or install it manually."
exit 1
fi

if [ ! -e "${CURSORLESS_REPO_ROOT-nonexistent}" ]; then
CURSORLESS_REPO_ROOT=$(git rev-parse --show-toplevel)
fi

logpath="$(mktemp -d)"
rm -rf "$logpath/check.json"
result=$(lua-language-server --check "${CURSORLESS_REPO_ROOT}" \
--checklevel="Warning" \
--configpath="${CURSORLESS_REPO_ROOT}/.luarc.json" \
--logpath="$logpath")
if [[ ! "$result" == *"no problems found"* ]]; then
cat "$logpath/check.json"
exit 1
fi

exit 0

0 comments on commit 5b1abfc

Please sign in to comment.