Skip to content

Instantly share code, notes, and snippets.

@greydot
Last active June 9, 2020 13:49
Show Gist options
  • Save greydot/4e8b05a699437623fb0e53e2e92aff3c to your computer and use it in GitHub Desktop.
Save greydot/4e8b05a699437623fb0e53e2e92aff3c to your computer and use it in GitHub Desktop.
Wraps around Haskell LSP servers and runs one in nix-shell if available. Requires yq if hie.yaml is present.
#!/usr/bin/env bash
SERVERS=( haskell-language-server-wrapper hie-wrapper haskell-language-server hie ghcide )
SERVER=
SERVER_PATH=
NIX_FILES=( shell.nix default.nix nix/shell.nix nix/default.nix )
NIX_CMD=
HIE_CRADLE=
for srv in ${SERVERS[@]}; do
if hash "$srv"; then
SERVER="$srv"
SERVER_PATH="$(which "$srv")"
SERVER_PATH="$(dirname "$SERVER_PATH")"
break
fi
done
if [ -z "$SERVER" ]; then
echo "Unable to locate any Haskell LSP servers. Exiting."
exit 1
fi
for f in ${NIX_FILES[@]}; do
if test -f "$f"; then
NIX_CMD="nix-shell $f --run bash"
break
fi
done
SHELL_CMD=$(cat <<- EOF
export PATH="$SERVER_PATH:$PATH"
$SERVER $@
EOF
)
if test -f hie.yaml; then
HIE_CRADLE=$(cat hie.yaml | yq ".cradle | keys | .[0]" | tr -d '"')
fi
# Stack doesn't work inside nix-shell, so we don't wanna run it that way.
# Hence, if nix-shell is available, run anything but stack there.
# Otherwise run without nix-shell.
if [ "$HIE_CRADLE" != "stack" ] && [ ! -z "$NIX_CMD" ]; then
$NIX_CMD <<< "$SHELL_CMD"
else
$SERVER $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment