Skip to content
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

feat(nvim): nvm-default package #49

Merged
merged 2 commits into from
Dec 12, 2024
Merged

feat(nvim): nvm-default package #49

merged 2 commits into from
Dec 12, 2024

Conversation

ivuorinen
Copy link
Owner

@ivuorinen ivuorinen commented Dec 12, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new Neovim plugin that automatically fetches and sets the default Node.js version from NVM.
    • Added a setup function with customizable options for the plugin.
  • Bug Fixes

    • Improved error handling for missing Node.js installations, with notifications for users.

Copy link

coderabbitai bot commented Dec 12, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new Lua module named nvm-default to the Neovim configuration. This module is designed to fetch the default Node.js version from the Node Version Manager (NVM) and set the appropriate environment variables for Neovim. The module includes a setup() function that allows for optional configuration, ensuring compatibility with the user's NVM setup. The existing configuration structure remains intact, with no alterations to other modules or exported entities.

Changes

File Change Summary
config/nvim/init.lua Added the nvm-default module with a require('nvm-default').setup() call.
config/nvim/lua/nvm-default/init.lua Introduced a new plugin that fetches the default Node.js version from NVM and sets environment variables. Added M.setup(opts), M.name, M.version, M.defaults, and NvmDefaultOptions.

Poem

In the land of code where rabbits play,
A new module hops in, brightening the day.
With Node.js ready, the setup's a breeze,
NVM's magic brings us to our knees.
So let's cheer for changes, both nifty and neat,
In our Neovim burrow, life is sweet! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (3)
config/nvim/lua/nvm-default/init.lua (3)

1-7: Add comprehensive module documentation.

Consider adding more detailed documentation including:

  • Configuration options
  • Requirements (NVM installation)
  • Example usage
 -- Get nvm default version and use it in node_host_prog
 -- and g.copilot_node_command.
+--
+-- This module automatically configures Neovim to use the default Node.js version
+-- from NVM. It requires a working NVM installation.
+--
+-- Usage:
+--   require('nvm-default').setup({
+--     nvm_path = "~/.nvm",      -- optional: custom NVM installation path
+--     notify_level = "info"     -- optional: notification level
+--   })
 local M = {}

40-43: Normalize paths in options.

Consider normalizing paths to handle different formats (e.g., "~", relative paths) consistently.

 M.defaults = {
-  nvm_path = os.getenv("NVM_DIR") or os.getenv("HOME") .. "/.nvm",
+  nvm_path = vim.fn.expand(os.getenv("NVM_DIR") or "~/.nvm"),
   notify_level = vim.g.nvm_default_notify_level or "info",
 }

79-79: Consider PATH modification implications.

Modifying PATH could affect other plugins. Consider using absolute paths for binaries instead.

-    vim.env.PATH = node_dir_bin .. ":" .. vim.env.PATH
+    -- Remove PATH modification and use absolute paths in node_host_prog and copilot_node_command
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ece4656 and d7b1286.

📒 Files selected for processing (2)
  • config/nvim/init.lua (1 hunks)
  • config/nvim/lua/nvm-default/init.lua (1 hunks)
🔇 Additional comments (1)
config/nvim/init.lua (1)

61-61: LGTM! Verify setup order dependencies.

The placement of the nvm-default setup is logically sound, occurring after plugin initialization but before keymaps.

Let's verify if any plugins depend on Node.js being available:

✅ Verification successful

Setup order is correct and necessary for Copilot functionality

The placement of nvm-default setup is not only logical but necessary as it configures node_host_prog and copilot_node_command which are required by the Copilot plugin. The code shows that:

  • Copilot is configured in lua/plugins/blink.lua
  • nvm-default properly sets up Node.js paths before they're needed
  • The setup occurs after plugin initialization which is the correct order
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for plugins that might require Node.js
rg -l "node_host_prog|copilot_node_command|require.*node" config/nvim/lua/plugins/

Length of output: 84


Script:

#!/bin/bash
# Let's check for any Node.js related dependencies in the entire config directory
rg -i "node|npm|nvm" config/nvim/

# Also check if there are any copilot configurations since it's a common Node.js dependent plugin
rg -i "copilot" config/nvim/

Length of output: 4691

config/nvim/lua/nvm-default/init.lua Outdated Show resolved Hide resolved
config/nvim/lua/nvm-default/init.lua Outdated Show resolved Hide resolved
config/nvim/lua/nvm-default/init.lua Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
config/nvim/lua/nvm-default/init.lua (2)

100-102: Consider more robust PATH modification.

The current PATH modification might lead to duplicate entries if setup is called multiple times. Consider checking if the path already exists before adding it.

 if options.add_to_path then
-  vim.env.PATH = current_nvm_node_bin_path .. ":" .. vim.env.PATH
+  if not vim.env.PATH:find(vim.fn.escape(current_nvm_node_bin_path, "%-%."), 1, true) then
+    vim.env.PATH = current_nvm_node_bin_path .. ":" .. vim.env.PATH
+  end
 end

94-95: Enhance error message with installation instructions.

The error message could be more helpful by including instructions for installing missing components.

-      n("Missing required files:\n- " .. table.concat(missing, "\n- "), "error")
+      n("Missing required files:\n- " .. table.concat(missing, "\n- ") .. 
+        "\n\nPlease ensure Node.js is installed via NVM and run:\n" ..
+        "npm install -g neovim", "error")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7b1286 and 9ad430b.

📒 Files selected for processing (1)
  • config/nvim/lua/nvm-default/init.lua (1 hunks)
🔇 Additional comments (5)
config/nvim/lua/nvm-default/init.lua (5)

1-16: LGTM! Well-documented module header.

The documentation clearly explains the purpose, requirements, and usage of the module. Installation instructions and configuration options are well-documented.


18-21: LGTM! Clean module initialization.

The module initialization follows Lua best practices, and the version string is properly formatted for release automation.


23-44: LGTM! Well-implemented helper functions.

The helper functions are well-implemented with proper type annotations and error handling. The run_command function uses the safe vim.fn.system API as recommended.


46-57: LGTM! Well-structured type definitions and defaults.

The type definitions are clear and the defaults are properly configured with appropriate fallbacks using environment variables and global settings.


64-67: 🛠️ Refactor suggestion

Verify NVM installation before usage.

The code should verify that the NVM installation (nvm.sh) exists before attempting to use it.

 local nvm_path = options.nvm_path
+ local nvm_sh = nvm_path .. "/nvm.sh"
+ 
+ if not vim.fn.filereadable(nvm_sh) then
+   n("NVM installation not found at " .. nvm_sh, "error")
+   return
+ end
+ 
 local node_version =
-    run_command(string.format(". %s/nvm.sh && nvm version default", nvm_path)) or
-    run_command(string.format(". %s/nvm.sh && nvm version node", nvm_path))
+    run_command(string.format(". %s && nvm version default", nvm_sh)) or
+    run_command(string.format(". %s && nvm version node", nvm_sh))
✅ Verification successful

Based on the code analysis, I can now generate the final response:

The NVM path verification suggestion is valid but needs refinement

The code already handles the NVM path intelligently by:

  1. Using NVM_DIR environment variable as the primary source
  2. Falling back to ~/.nvm as a default
  3. Expanding the path using vim.fn.expand()

However, the review suggestion to verify the existence of nvm.sh is still valuable, but should be implemented with these considerations in mind.

local nvm_path = options.nvm_path
local nvm_sh = nvm_path .. "/nvm.sh"

if vim.fn.filereadable(vim.fn.expand(nvm_sh)) ~= 1 then
  n("NVM installation not found at " .. nvm_sh, "error")
  return
end

local node_version =
    run_command(string.format(". %s && nvm version default", nvm_sh)) or
    run_command(string.format(". %s && nvm version node", nvm_sh))
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if nvm.sh exists in common installation paths

# Common NVM installation paths
paths=(
  "$HOME/.nvm/nvm.sh"
  "$HOME/.config/nvm/nvm.sh"
  "/usr/local/nvm/nvm.sh"
)

for path in "${paths[@]}"; do
  echo "Checking $path"
  [ -f "$path" ] && echo "Found nvm.sh at $path"
done

Length of output: 533


Script:

#!/bin/bash
# Let's check how nvm_path is being set and used in the codebase
rg -A 5 "nvm_path" --type lua

Length of output: 2570

@ivuorinen ivuorinen merged commit b314c0b into main Dec 12, 2024
9 checks passed
@ivuorinen ivuorinen deleted the feat/nvim-nvm-default branch December 12, 2024 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant