-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
neovim-require-check-hook: auto discover modules to require #352277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 125 additions & 13 deletions
138
pkgs/applications/editors/vim/plugins/neovim-require-check-hook.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,136 @@ | ||
| #shellcheck shell=bash | ||
| # Setup hook for checking whether Python imports succeed | ||
| # Setup hook for checking whether Lua imports succeed | ||
khaneliman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| echo "Sourcing neovim-require-check-hook.sh" | ||
|
|
||
| neovimRequireCheckHook () { | ||
| echo "Executing neovimRequireCheckHook" | ||
| # Discover modules automatically if nvimRequireCheck is not set | ||
| discover_modules() { | ||
| echo "Running module discovery in source directory..." | ||
|
|
||
| # Create unique lists so we can organize later | ||
| modules=() | ||
|
|
||
| if [ -n "$nvimRequireCheck" ]; then | ||
| echo "Check whether the following module can be imported: $nvimRequireCheck" | ||
| while IFS= read -r lua_file; do | ||
| # Ignore certain infra directories | ||
| if [[ "$lua_file" =~ debug/|scripts?/|tests?/|spec/ || "$lua_file" =~ .*\meta.lua ]]; then | ||
| continue | ||
| # Ignore optional telescope and lualine modules | ||
| elif [[ "$lua_file" =~ ^lua/telescope/_extensions/(.+)\.lua || "$lua_file" =~ ^lua/lualine/(.+)\.lua ]]; then | ||
| continue | ||
| # Grab main module names | ||
| elif [[ "$lua_file" =~ ^lua/([^/]+)/init.lua$ ]]; then | ||
| echo "$lua_file" | ||
| modules+=("${BASH_REMATCH[1]}") | ||
| # Check other lua files | ||
| elif [[ "$lua_file" =~ ^lua/(.*)\.lua$ ]]; then | ||
| echo "$lua_file" | ||
| # Replace slashes with dots to form the module name | ||
| module_name="${BASH_REMATCH[1]//\//.}" | ||
| modules+=("$module_name") | ||
| elif [[ "$lua_file" =~ ^([^/.][^/]*)\.lua$ ]]; then | ||
| echo "$lua_file" | ||
| modules+=("${BASH_REMATCH[1]}") | ||
| fi | ||
| done < <(find "$src" -name '*.lua' | xargs -n 1 realpath --relative-to="$src") | ||
|
|
||
| # editorconfig-checker-disable | ||
| export HOME="$TMPDIR" | ||
| nvimRequireCheck=("${modules[@]}") | ||
| echo "Discovered modules: ${nvimRequireCheck[*]}" | ||
|
|
||
| local deps="${dependencies[*]}" | ||
| @nvimBinary@ -es --headless -n -u NONE -i NONE --clean -V1 \ | ||
| --cmd "set rtp+=$out,${deps// /,}" \ | ||
| --cmd "lua require('$nvimRequireCheck')" | ||
| if [ "${#nvimRequireCheck[@]}" -eq 0 ]; then | ||
| echo "No valid Lua modules found; skipping check" | ||
| return 1 | ||
| fi | ||
| return 0 | ||
| } | ||
|
|
||
| echo "Using neovimRequireCheckHook" | ||
| appendToVar preDistPhases neovimRequireCheckHook | ||
| # Run require checks on each module in nvimRequireCheck | ||
| run_require_checks() { | ||
| echo "Starting require checks" | ||
| check_passed=false | ||
| failed_modules=() | ||
| successful_modules=() | ||
|
|
||
| export HOME="$TMPDIR" | ||
| local deps="${dependencies[*]}" | ||
| local checks="${nativeBuildInputs[*]}" | ||
| set +e | ||
| for name in "${nvimRequireCheck[@]}"; do | ||
| local skip=false | ||
| for module in "${nvimSkipModule[@]}"; do | ||
| if [[ "$module" == "$name" ]]; then | ||
| echo "$name is in list of modules to not check. Skipping..." | ||
| skip=true | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if [ "$skip" = false ]; then | ||
| echo "Attempting to require module: $name" | ||
| if @nvimBinary@ -es --headless -n -u NONE -i NONE --clean -V1 \ | ||
| --cmd "set rtp+=$out,${deps// /,}" \ | ||
| --cmd "set rtp+=$out,${checks// /,}" \ | ||
| --cmd "lua require('$name')"; then | ||
| check_passed=true | ||
| successful_modules+=("$name") | ||
| echo "Successfully required module: $name" | ||
| else | ||
| echo "Failed to require module: $name" | ||
| failed_modules+=("$name") | ||
| fi | ||
| fi | ||
| done | ||
| set -e | ||
| } | ||
|
|
||
| # Define color codes | ||
| GREEN="\033[0;32m" | ||
| RED="\033[0;31m" | ||
| NC="\033[0m" # No Color | ||
|
|
||
| # Print summary of the require checks | ||
| print_summary() { | ||
| echo -e "\n======================================================" | ||
| if [[ "$check_passed" == "true" ]]; then | ||
| echo -e "${GREEN}Require check succeeded for the following modules:${NC}" | ||
| for module in "${successful_modules[@]}"; do | ||
| echo -e " ${GREEN}- $module${NC}" | ||
| done | ||
| echo "All lua modules were checked." | ||
| else | ||
| echo -e "${RED}No successful require checks.${NC}" | ||
| fi | ||
|
|
||
| # Print any modules that failed with improved formatting and color | ||
| if [ "${#failed_modules[@]}" -gt 0 ]; then | ||
| echo -e "\n${RED}Require check failed for the following modules:${NC}" | ||
| for module in "${failed_modules[@]}"; do | ||
| echo -e " ${RED}- $module${NC}" | ||
| done | ||
| fi | ||
| echo "======================================================" | ||
|
|
||
| if [ "${#failed_modules[@]}" -gt 0 ]; then | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| # Main entry point: orchestrates discovery, require checks, and summary | ||
| neovimRequireCheckHook() { | ||
| echo "Executing neovimRequireCheckHook" | ||
|
|
||
| if [ "${nvimRequireCheck[*]}" = "" ]; then | ||
| echo "nvimRequireCheck is empty; entering discovery mode" | ||
| # Auto-discovery mode | ||
| if ! discover_modules; then | ||
| echo "No modules found during discovery; exiting hook" | ||
| return | ||
| fi | ||
| else | ||
| echo "nvimRequireCheck is pre-populated; entering manual check mode" | ||
| fi | ||
|
|
||
| run_require_checks | ||
| print_summary | ||
| } | ||
|
|
||
| echo "Using neovimRequireCheckHook" | ||
| appendToVar preDistPhases neovimRequireCheckHook | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My personal preference would have been to accept juste one format to avoid surprising the user "it's a list here but not there ?", plus it makes implementation easier. Like always accept a list and mark it plural:
nvimRequireChecks = []