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(mappings): show leaf node in preview window #518

Merged
merged 1 commit into from
Jan 26, 2025

Conversation

brandon1024
Copy link
Contributor

@brandon1024 brandon1024 commented Jan 6, 2025

Overview

This revision introduces new plugin mappings for showing the current
leaf node in a preview window, through the 'preview' and
'preview:jump' actions available for the 'file://' scheme.

Documentation has been updated accordingly.

This was originally implemented in my personal dotfiles but I figured these features might be useful for the wider community.

Screenshots

image
image

Summary by CodeRabbit

  • New Features
    • Added preview functionality to the fern file management plugin.
    • Introduced new mappings for various preview positions (left, right, top, bottom).
    • Updated existing mapping for clarity on its alias to the open:edit action.
    • Enhanced file browsing experience with improved preview capabilities.

Copy link

coderabbitai bot commented Jan 6, 2025

Walkthrough

The pull request introduces new mappings to the fern Vim plugin documentation, specifically for enhancing the preview functionality. Several mappings are added to allow users to preview nodes in different positions, along with a clarification of an existing mapping as an alias for the edit action. Additionally, a new function is implemented to manage the previewing of files, ensuring that the cursor node is valid and visible before executing the preview command. These changes collectively enhance the user experience within the file management interface.

Changes

File Change Summary
doc/fern.txt Added documentation for new preview mappings *<Plug>(fern-action-preview)*, *<Plug>(fern-action-preview:left)*, *<Plug>(fern-action-preview:right)*, *<Plug>(fern-action-preview:top)*, and *<Plug>(fern-action-preview:bottom)*. Clarified *<Plug>(fern-action-open)* as an alias for open:edit.
autoload/fern/scheme/file/mapping.vim Introduced s:map_preview(helper, prefix) function to manage file/directory previewing with error handling and support for directional preview mappings.

Sequence Diagram

sequenceDiagram
    participant User
    participant FernPlugin
    participant PreviewBuffer
    
    User->>FernPlugin: Trigger preview mapping
    FernPlugin->>PreviewBuffer: Open file in preview window
    alt Optional directional preview
        FernPlugin->>PreviewBuffer: Position preview window
    end
Loading

Poem

🐰 In the forest of files, a rabbit hops light,
Previewing nodes with magical might!
A window that peeks, without leaving the tree,
New mappings that set the file browser free 🌿
Hop, preview, jump - what a delightful sight! 🔍


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 04d0158 and 354126a.

📒 Files selected for processing (2)
  • autoload/fern/scheme/file/mapping.vim (2 hunks)
  • doc/fern.txt (5 hunks)
🔇 Additional comments (4)
autoload/fern/scheme/file/mapping.vim (3)

16-22: Well-structured preview action mappings!

The implementation provides a good set of preview options with different window positions (left, right, top, bottom) and includes a sensible default alias mapping.


215-230: Robust implementation with proper error handling!

The preview function implementation:

  • Validates cursor node visibility and status
  • Uses proper error handling with try/catch
  • Safely escapes file paths
  • Returns promises consistently

Line range hint 1040-1049: Clear documentation for open action aliases!

The documentation clearly explains the relationship between open actions and provides helpful examples for customization.

doc/fern.txt (1)

1377-1389: Comprehensive and well-formatted documentation!

The documentation:

  • Clearly describes all preview variants
  • Provides a helpful example for jumping to preview window
  • Follows Vim's help file conventions

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. (Beta)
  • @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.

@brandon1024 brandon1024 changed the title ggfeat(mappings): show leaf node in preview window feat(mappings): show leaf node in preview window Jan 6, 2025
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

🧹 Nitpick comments (6)
autoload/fern/mapping/open.vim (3)

22-23: Ensure consistent naming and add usage hints.
These lines add two new mappings for previewing nodes. It might be helpful to emphasize in accompanying documentation how these differ from other open actions. Consider providing a usage hint or including a short comment to describe their intended behavior.


97-101: Validate the scheme or show a warning.
Before checking that the scheme is 'file', consider logging or displaying a brief warning if it does not match. This helps users understand why the preview isn't available.


105-107: Consider an explicit check for preview-window availability.
When the user wants to jump to the preview window (wincmd P), ensure it is valid. In some edge cases, a preview window might be unavailable or used by another buffer, leading to confusion or unexpected results.

doc/fern.txt (3)

1032-1036: Clarify behavior on non-leaf nodes.
These newly documented mappings focus on showing a leaf node in a preview window. If a user invokes them on a directory or other non-leaf node, clarify what happens (e.g., no action, error message, etc.). A short explanation can reduce confusion.


1045-1045: Confirm consistency with the new preview mappings.
Since <Plug>(fern-action-open) is labeled as open:edit, confirm in the docs whether users can safely override it to use open:preview. This ensures clarity for those who might want to unify their mappings.


1054-1054: Add short code snippet for user reference (optional).
If desired, provide a minimal usage example in the documentation, illustrating how to map user-defined keys to <Plug>(fern-action-open:preview) or <Plug>(fern-action-open:preview-jump). This helps users quickly adopt the feature.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 56f7c0d and 8db35ef.

📒 Files selected for processing (2)
  • autoload/fern/mapping/open.vim (2 hunks)
  • doc/fern.txt (3 hunks)
🔇 Additional comments (1)
autoload/fern/mapping/open.vim (1)

102-104: Handle directory paths gracefully.
For directories, pedit might fail or behave unexpectedly. Consider verifying if l:path points to a file and handle directory previews separately.

Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

  1. Please implement the mapping in https://github.com/lambdalisue/vim-fern/tree/main/autoload/fern/scheme/file/mapping if the mapping is schema specific
  2. Please follow the indentation rules around the code
  3. Please add more error handlings

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: 1

🧹 Nitpick comments (3)
autoload/fern/scheme/file/mapping.vim (3)

16-17: LGTM! Consider documenting the recommended key mappings.

The new preview mappings are well-implemented and follow the existing patterns. While it's good that they're not added to default mappings, consider documenting recommended key mappings in the plugin documentation to help users get started.


210-230: Consider enhancing the preview window management.

The current implementation could be improved in several ways:

  1. Handle existing preview windows (close or reuse)
  2. Clean up preview windows when previewing new files
  3. Add window-local settings for preview window (e.g., nomodifiable)

Here's a suggested enhancement:

 function! s:map_preview(helper, mode) abort
   let node = a:helper.sync.get_cursor_node()
   if node is# v:null
     return s:Promise.reject('cursor node is not visible')
   endif
   if node.status isnot# a:helper.STATUS_NONE
     return s:Promise.reject('cannot preview a directory')
   endif

+  " Close existing preview window if any
+  pclose
+
   try
-    execute "noautocmd bo pedit " .. node._path
+    execute "bo pedit " .. fnameescape(node._path)
+
+    " Set preview window options
+    wincmd P
+    setlocal nomodifiable
+    setlocal nonumber
+    setlocal norelativenumber
+    wincmd p
 
     if a:mode ==# 'jump'
       wincmd P
     endif
 
     return s:Promise.resolve()
   catch
     return s:Promise.reject(v:exception)
   endtry
 endfunction

212-217: Improve error messages for better user experience.

The current error messages could be more user-friendly and descriptive.

Apply this diff:

   if node is# v:null
-    return s:Promise.reject('cursor node is not visible')
+    return s:Promise.reject('No file selected. Please move the cursor to a file.')
   endif
   if node.status isnot# a:helper.STATUS_NONE
-    return s:Promise.reject('cannot preview a directory')
+    return s:Promise.reject('Preview is only available for files, not directories.')
   endif
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8db35ef and d677908.

📒 Files selected for processing (2)
  • autoload/fern/scheme/file/mapping.vim (2 hunks)
  • doc/fern.txt (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • doc/fern.txt
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test (macos-latest, nightly)
🔇 Additional comments (1)
autoload/fern/scheme/file/mapping.vim (1)

220-220: Consider the implications of using noautocmd.

Using noautocmd with pedit skips buffer-local autocmds which might be important for proper buffer setup. Consider if this is really necessary, or if certain autocmds should be preserved.

@brandon1024
Copy link
Contributor Author

  1. Please implement the mapping in https://github.com/lambdalisue/vim-fern/tree/main/autoload/fern/scheme/file/mapping if the mapping is schema specific

Done :-)

2. Please follow the indentation rules around the code

My bad, I should have checked this. Thanks!

3. Please add more error handlings

I wrapped the sensitive parts in a try/catch block, and I also added some checks on the cursor node to make sure it's actually valid (could be v:null if the cursor isn't visible) and that it's a leaf node (file).

@brandon1024
Copy link
Contributor Author

This should be ready for a second review @lambdalisue :-)

Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

Sorry for late. Please check my requests.

return s:Promise.reject('cursor node is not visible')
endif
if node.status isnot# a:helper.STATUS_NONE
return s:Promise.reject('cannot preview a directory')
Copy link
Owner

@lambdalisue lambdalisue Jan 23, 2025

Choose a reason for hiding this comment

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

It's a bit annoying, so let's just return a void promise in this case so that user can use this mapping like

nnoremap j j<Plug>(fern-action-preview)
nnoremap k k<Plug>(fern-action-preview)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, very clever idea. Will do!

endif

try
execute "noautocmd bo pedit " .. node._path
Copy link
Owner

@lambdalisue lambdalisue Jan 23, 2025

Choose a reason for hiding this comment

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

Please make it (bo) configurable so that we can define the followings to control the preview window location.

  • fern-action-preview:left (vertical topleft)
  • fern-action-preview:right (vertical botright)
  • fern-action-preview:top (topleft)
  • fern-action-preview:bottom (botright)
  • fern-action-preview aliases to fern-action-preview:bottom (or one of the aboves)

Additionally, please use full name (like botright) instead of short name (like bo)

Copy link
Owner

Choose a reason for hiding this comment

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

Please use fnameescape to escape node._path like

execute printf('noautocmd %s pedit %s', a:prefix, fnameescape(node._path))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestions, will do :-)

Comment on lines 222 to 224
if a:mode ==# 'jump'
wincmd P
endif
Copy link
Owner

@lambdalisue lambdalisue Jan 23, 2025

Choose a reason for hiding this comment

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

I'm not 100% sure but I think we don't need this feature while we can write mapping like below to jump (please confirm if it's true.)

nnoremap P <Plug>(fern-action-preview)<C-w>p

It's nice if this tips is written in the help.

This revision introduces new plugin mappings for showing the current
leaf node in a preview window, through the 'preview' and
'preview:*' actions available for the 'file://' scheme.

Documentation has been updated accordingly.
Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

LGTM

@lambdalisue lambdalisue merged commit cb5be74 into lambdalisue:main Jan 26, 2025
13 checks passed
@brandon1024
Copy link
Contributor Author

Thanks @lambdalisue :-)

@brandon1024 brandon1024 deleted the node-preview-mapping branch January 26, 2025 07:17
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.

2 participants