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

🐛 Refactor denops#plugin module and fix denops#cache#update() to use registered plugins instead #367

Merged
merged 5 commits into from
Jun 18, 2024

Conversation

lambdalisue
Copy link
Member

@lambdalisue lambdalisue commented Jun 17, 2024

Close #360. Additionally denops#plugin#check_type also discover/find plugins as like denops#cache#update thus this PR fixed the behavior to use registered plugins instead.

Summary by CodeRabbit

  • New Features

    • Improved plugin management with new states and event handlers for better reliability and efficiency.
  • Refactor

    • Revised existing plugin management functions to support new state handling and callbacks.
    • Enhanced plugin loading logic for more efficient operation.
  • Bug Fixes

    • Addressed issues in plugin loading and reloading processes to prevent errors and improve performance.

Copy link

coderabbitai bot commented Jun 17, 2024

Walkthrough

Significant updates to the denops plugin management system include: adding plugin states, restructuring functions, and incorporating event handlers. Adjustments to denops#cache#update() enhance plugin registration, removing reliance on plugins with main.ts, and improving plugin loading reliability and efficiency.

Changes

File Change Summary
autoload/denops/_internal/plugin.vim Introduced plugin states, restructured management functions, and added event handlers.
autoload/denops/cache.vim Converted variables to constants, introduced new constants, and optimized plugin list usage in denops#cache#update().
autoload/denops/plugin.vim Refactored plugin loading logic, updated state management, revised loading/waiting functions, and deprecated old functions.

Sequence Diagram(s)

sequenceDiagram
    participant Vim as Vim
    participant Denops as Denops Plugin System
    participant Plugin as Plugin

    Note over Vim, Denops: Plugin Loading Process
    Vim->>Denops: load(plugin_name, script)
    Denops->>Plugin: Set plugin state to LOADING
    Plugin->>Denops: Script execution result
    alt Successfully loaded
      Denops->>Plugin: Set state to LOADED
      Denops->>Vim: Notify success
    else Failed to load
      Denops->>Plugin: Set state to FAILED
      Denops->>Vim: Notify failure
    end
Loading

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Use registered plugins instead of discover in denops#cache#update() (#360)

Poem

In the world of code at night,
Plugins dance, a treat to the site,
States are new and handlers bloom,
Denops hums, dispelling gloom.
Cache now swift, no longer slow,
With these changes, watch it grow! 🌙🐇


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

codecov bot commented Jun 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.23%. Comparing base (6e14ad1) to head (2f30e6b).

Current head 2f30e6b differs from pull request most recent head c616af0

Please upload reports for the commit c616af0 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #367   +/-   ##
=======================================
  Coverage   90.23%   90.23%           
=======================================
  Files           9        9           
  Lines         676      676           
  Branches       64       64           
=======================================
  Hits          610      610           
  Misses         66       66           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lambdalisue lambdalisue requested a review from Shougo June 17, 2024 15:55
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6e14ad1 and 6ce7704.

Files selected for processing (3)
  • autoload/denops/_internal/plugin.vim (1 hunks)
  • autoload/denops/cache.vim (1 hunks)
  • autoload/denops/plugin.vim (3 hunks)
Additional comments not posted (19)
autoload/denops/cache.vim (5)

1-2: The introduction of constants s:root and s:mod is a good practice for values that do not change, enhancing maintainability and preventing accidental reassignments.


6-8: The use of const for local variables within denops#cache#update ensures that these do not get re-assigned inadvertently, which is a good practice for clarity and error prevention.


Line range hint 6-30: The refactoring of the cache update mechanism to utilize dynamic plugin and entry file lists is a significant improvement. It allows for more flexible cache management and can adapt to changes in the plugin environment more responsively.


Line range hint 6-30: Error handling in s:on_stderr and job cleanup in s:on_exit are well implemented. These are crucial for robustness, especially in asynchronous operations like handling external jobs.


6-8: Ensure that the l:plugins and l:entryfiles variables are correctly populated as expected. This new logic integrates plugin management directly with cache updates, which should improve the coherence of state management across the system.

autoload/denops/_internal/plugin.vim (6)

1-4: Defining plugin states as constants is a best practice that enhances readability and maintainability of the code by avoiding magic strings.


8-13: The update to denops#_internal#plugin#get to include plugin state and callbacks is crucial for the new event-driven plugin system. This change enables better state management and event handling.


15-17: The modification to denops#_internal#plugin#list to return a mapped list of plugin values ensures that the function does not leak references to its internal state, which is a good security practice.


19-36: The refactor of denops#_internal#plugin#load and the introduction of denops#_internal#plugin#reload are well-structured to handle the new plugin states and improve the reliability of plugin loading and reloading processes.


38-61: The introduction of s:DenopsSystemPluginPre, s:DenopsSystemPluginPost, and s:DenopsSystemPluginFail functions is a solid design choice. These functions facilitate a more modular and event-driven approach to plugin management.


63-69: The autocmd group setup for handling plugin events (DenopsSystemPluginPre, DenopsSystemPluginPost, DenopsSystemPluginFail) is correctly implemented. This setup ensures that plugin events are handled in an organized manner.

autoload/denops/plugin.vim (8)

2-2: The update to denops#plugin#is_loaded to check the plugin state using a regex pattern is a straightforward and effective way to determine if a plugin is loaded or failed.


21-38: The refactor of denops#plugin#wait to handle plugin loading states more robustly, including better timeout and error handling, is a significant improvement. This change enhances the reliability of the plugin waiting mechanism.


42-49: The changes in denops#plugin#wait_async to handle callbacks based on plugin state are well thought out. This ensures that callbacks are only called when appropriate, which is crucial for event-driven programming.


53-57: The delegation of functionality from denops#plugin#load and denops#plugin#reload to their counterparts in the internal plugin module simplifies the external API and ensures consistency in plugin handling.


61-71: The refactor of denops#plugin#discover to use a pattern-based approach for discovering plugins is efficient and reduces complexity. This method ensures that all relevant plugins are considered, and the counter provides a simple way to track the number of plugins loaded.


76-77: The changes to denops#plugin#check_type to utilize the updated plugin listing function reflect good integration with the new plugin management system. This ensures that type checking is performed against the current state of plugins.


93-106: The deprecation of denops#plugin#register in favor of denops#plugin#load is clearly communicated. Including backward compatibility notes and providing a clear migration path are best practices for API changes.


109-118: The introduction of s:find_script to abstract the script finding logic is a clean way to encapsulate this functionality. The error handling with a throw statement ensures that issues are caught early.

Copy link
Contributor

@Shougo Shougo left a comment

Choose a reason for hiding this comment

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

I have found one bug.

autoload/denops/cache.vim Outdated Show resolved Hide resolved
@Shougo
Copy link
Contributor

Shougo commented Jun 17, 2024

:call denops#cache#update()

[denops] Update cache of the following files. Call `denops#cache#update(#{reload: v:true})` to forcibly update.
[denops] /home/shougo/work/denops.vim/denops/@denops-private/mod.ts
[denops] /home/shougo/work/skkeleton/denops/skkeleton/main.ts
[denops] /home/shougo/work/ddc.vim/denops/ddc/app.ts
[denops] /home/shougo/work/ddu.vim/denops/ddu/app.ts
[denops] It tooks more than 5000 ms. Use Ctrl-C to cancel.
[denops] Deno cache is updated.

It is too slow. Why?

@Shougo
Copy link
Contributor

Shougo commented Jun 17, 2024

And denops plugins are loaded but does not work when the branch is used.

Fixed.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6ce7704 and 379c574.

Files selected for processing (2)
  • autoload/denops/_internal/plugin.vim (1 hunks)
  • autoload/denops/cache.vim (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • autoload/denops/cache.vim
Additional comments not posted (7)
autoload/denops/_internal/plugin.vim (7)

1-4: Define plugin states as constants for clearer and more maintainable code.

This change improves code readability and maintainability by using descriptive constants instead of magic strings throughout the code.


63-69: Review the new autocmd group denops_autoload_plugin_internal for correct event registration and cleanup.

The autocmd group setup correctly handles event registration and ensures that the plugin state is reset when the Denops system is closed. This is a good practice for avoiding memory leaks or stale data.


19-27: Validate the updated plugin loading mechanism in denops#_internal#plugin#load.

The function now explicitly updates plugin state and script details, which is crucial for correct plugin lifecycle management.

Verification successful

The updated plugin loading mechanism in denops#_internal#plugin#load correctly updates the plugin state and interacts with the server as intended.

  • The function denops#_internal#plugin#load sets the plugin state to s:STATE_LOADING and updates the script details.
  • It then notifies the server with the invoke command and the appropriate arguments.

Locations in the codebase:

  • autoload/denops/_internal/plugin.vim:
    • Line setting plugin state to s:STATE_LOADING: Line 5
    • Server notification: Line 9
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure that the new loading mechanism correctly updates plugin states and interacts with the server.

# Test: Simulate plugin loading to observe state changes and server notifications.
ast-grep --lang vim --pattern $'function! denops#_internal#plugin#load($_, $_) {
  $$$
}'

Length of output: 197



Script:

#!/bin/bash
# Find the function definition for denops#_internal#plugin#load
rg 'function! denops#_internal#plugin#load' -A 10

# List all occurrences of state updates and server notifications within the plugin.vim file
rg 'let l:plugin.state' autoload/denops/_internal/plugin.vim
rg 'call denops#_internal#server#chan#notify' autoload/denops/_internal/plugin.vim

Length of output: 1369


38-41: Ensure that new event handling functions (s:DenopsSystemPluginPre, s:DenopsSystemPluginPost, s:DenopsSystemPluginFail) work as expected.

These functions are designed to handle specific plugin lifecycle events. It's crucial to confirm that they trigger the appropriate system responses without causing unintended side effects.

Also applies to: 43-52, 55-60

Verification successful

The functions s:DenopsSystemPluginPre, s:DenopsSystemPluginPost, and s:DenopsSystemPluginFail are defined and called within the autoload/denops/_internal/plugin.vim file. The calls are triggered by corresponding User autocommands, ensuring that these functions are executed during specific plugin lifecycle events.

  • s:DenopsSystemPluginPre: Defined at line 38 and called by autocmd User DenopsSystemPluginPre:*.
  • s:DenopsSystemPluginPost: Defined at line 43 and called by autocmd User DenopsSystemPluginPost:*.
  • s:DenopsSystemPluginFail: Defined at line 55 and called by autocmd User DenopsSystemPluginFail:*.

These functions are correctly set up to handle their respective events.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct execution and side effects of new event handling functions.

# Test: Monitor the system's behavior when these events are triggered.
ast-grep --lang vim --pattern $'function! s:DenopsSystemPluginPre() {
  $$$
}'
ast-grep --lang vim --pattern $'function! s:DenopsSystemPluginPost() {
  $$$
}'
ast-grep --lang vim --pattern $'function! s:DenopsSystemPluginFail() {
  $$$
}'

Length of output: 562



Script:

#!/bin/bash
# Description: Verify the correct execution and side effects of new event handling functions.

# Search for the definition of the functions
rg 'function! s:DenopsSystemPluginPre'
rg 'function! s:DenopsSystemPluginPost'
rg 'function! s:DenopsSystemPluginFail'

# Search for the usage of these functions
rg 'call s:DenopsSystemPluginPre'
rg 'call s:DenopsSystemPluginPost'
rg 'call s:DenopsSystemPluginFail'

Length of output: 825


8-13: Ensure compatibility and functionality of the modified denops#_internal#plugin#get function.

This function now includes plugin state and callbacks, which could affect how plugins are managed and interacted with. Ensure that this change integrates well with the rest of the system.


15-17: Review the implementation of denops#_internal#plugin#list for potential performance impacts.

This function now returns a mapped list of plugin values, potentially increasing computational overhead depending on the number of plugins and their complexity.


30-36: Confirm the correct functionality of the denops#_internal#plugin#reload function with the new reloading mechanism.

This function now handles plugin reloading, which is a critical operation for maintaining plugin functionality during runtime updates.

Verification successful

The denops#_internal#plugin#reload function correctly handles plugin reloading by retrieving the plugin, logging a debug message, and sending a notification. The implementations of the dependent functions denops#_internal#plugin#get, denops#_internal#echo#debug, and denops#_internal#server#chan#notify have been verified and found to be correct.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Test the reliability and correctness of the plugin reloading process.

# Test: Trigger plugin reloads under various conditions to ensure robustness.
ast-grep --lang vim --pattern $'function! denops#_internal#plugin#reload($_) {
  $$$
}'

Length of output: 195



Script:

#!/bin/bash
# Description: Search for the definition of the `denops#_internal#plugin#reload` function.

# Search for the function definition in the codebase.
rg 'function! denops#_internal#plugin#reload'

Length of output: 137



Script:

#!/bin/bash
# Description: Extract the full implementation of the `denops#_internal#plugin#reload` function.

# Extract the function implementation from the file.
sed -n '/function! denops#_internal#plugin#reload/,/endfunction/p' autoload/denops/_internal/plugin.vim

Length of output: 433



Script:

#!/bin/bash
# Description: Search for the definition of the `denops#_internal#plugin#get` function.

# Search for the function definition in the codebase.
rg 'function! denops#_internal#plugin#get'

Length of output: 131



Script:

#!/bin/bash
# Description: Extract the full implementation of the `denops#_internal#plugin#get` function.

# Extract the function implementation from the file.
sed -n '/function! denops#_internal#plugin#get/,/endfunction/p' autoload/denops/_internal/plugin.vim

Length of output: 309



Script:

#!/bin/bash
# Description: Search for the definition of the `denops#_internal#echo#debug` function.

# Search for the function definition in the codebase.
rg 'function! denops#_internal#echo#debug'

Length of output: 128



Script:

#!/bin/bash
# Description: Extract the full implementation of the `denops#_internal#echo#debug` function.

# Extract the function implementation from the file.
sed -n '/function! denops#_internal#echo#debug/,/endfunction/p' autoload/denops/_internal/echo.vim

Length of output: 236



Script:

#!/bin/bash
# Description: Search for the definition of the `denops#_internal#server#chan#notify` function.

# Search for the function definition in the codebase.
rg 'function! denops#_internal#server#chan#notify'

Length of output: 162



Script:

#!/bin/bash
# Description: Extract the full implementation of the `denops#_internal#server#chan#notify` function.

# Extract the function implementation from the file.
sed -n '/function! denops#_internal#server#chan#notify/,/endfunction/p' autoload/denops/_internal/server/chan.vim

Length of output: 359

@Shougo
Copy link
Contributor

Shougo commented Jun 18, 2024

It works now.

1. Move most of core functions of `denops#plugin` into the internal module
2. Track plugin state in `denops#_internal#plugin` before it is loaded
@lambdalisue
Copy link
Member Author

#367 (comment)

What do you mean "too"? I think it's common to take time for reloading all cache.

@lambdalisue lambdalisue requested a review from Shougo June 18, 2024 04:50
@lambdalisue
Copy link
Member Author

Thanks for review & fix. I rebased to tidy commit history so rerequest a review in case @Shougo

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

Outside diff range and nitpick comments (1)
autoload/denops/_internal/plugin.vim (1)

1-4: Add comprehensive documentation for the new constants.

It's essential to document the purpose and usage of these constants to help future developers understand the different states a plugin can be in.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 379c574 and c616af0.

Files selected for processing (3)
  • autoload/denops/_internal/plugin.vim (1 hunks)
  • autoload/denops/cache.vim (1 hunks)
  • autoload/denops/plugin.vim (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • autoload/denops/cache.vim
  • autoload/denops/plugin.vim
Additional comments not posted (2)
autoload/denops/_internal/plugin.vim (2)

15-17: Looks good as it effectively retrieves all plugins.


55-61: Properly handles plugin failures by setting the state and clearing callbacks.

autoload/denops/_internal/plugin.vim Show resolved Hide resolved
autoload/denops/_internal/plugin.vim Show resolved Hide resolved
autoload/denops/_internal/plugin.vim Show resolved Hide resolved
autoload/denops/_internal/plugin.vim Show resolved Hide resolved
autoload/denops/_internal/plugin.vim Show resolved Hide resolved
@Shougo
Copy link
Contributor

Shougo commented Jun 18, 2024

What do you mean "too"? I think it's common to take time for reloading all cache.

It is normal behavior?

@Shougo
Copy link
Contributor

Shougo commented Jun 18, 2024

I have fixed for rabbit review.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c616af0 and 2f30e6b.

Files selected for processing (1)
  • autoload/denops/_internal/plugin.vim (1 hunks)
Additional comments not posted (5)
autoload/denops/_internal/plugin.vim (5)

1-4: Introduced constants for plugin states enhance readability and maintainability.

Nice use of constants to define plugin states, which makes the code easier to understand and maintain.


22-30: Optimize external call error handling in denops#_internal#plugin#load.

Consider adding error handling around external calls to denops#_internal#echo#debug and denops#_internal#server#chan#notify as previously suggested to enhance robustness.


33-39: Replicate error handling improvements in denops#_internal#plugin#reload.

Similar to the load function, it's crucial to handle potential errors from denops#_internal#echo#debug and denops#_internal#server#chan#notify to prevent issues during plugin reloading.


41-44: Refactor event handling functions to streamline plugin lifecycle management.

The addition of s:DenopsSystemPluginPre, s:DenopsSystemPluginPost, and s:DenopsSystemPluginFail functions along with corresponding autocmds effectively manage plugin states and callbacks, enhancing the plugin lifecycle management.

Also applies to: 46-55, 58-63


66-72: Autocmd group for handling plugin events ensures clean state management.

The use of an autocmd group to handle plugin-specific events and to reset the plugins state upon DenopsClosed is a good practice, ensuring that the plugin states are managed cleanly.

autoload/denops/_internal/plugin.vim Outdated Show resolved Hide resolved
@lambdalisue
Copy link
Member Author

lambdalisue commented Jun 18, 2024

I have fixed for rabbit review.

I'm sorry but the rabbit review didn't make sense so we should ignore. I removed that commits and force pushed.

@lambdalisue lambdalisue merged commit c6fa7c6 into main Jun 18, 2024
16 checks passed
@lambdalisue lambdalisue deleted the fix-cache-update branch June 18, 2024 11:15
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.

Use registered plugins instead of discover in denops#cache#update()
2 participants