-
Notifications
You must be signed in to change notification settings - Fork 19
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 Object Browser and Add Support for Custom Key Mappings #237
Conversation
Replace multiple `vim.api.nvim_set_option_value` calls with a single options table and iterate over it to set buffer options. This reduces code repetition, improves readability, and simplifies future maintenance.
Replace multiple `vim.api.nvim_buf_set_keymap` calls with `vim.keymap.set`, including the `buffer = true` option. This change utilizes the more modern Neovim keymap API.
…al statements. 1. **Convert anonymous function assignments to named function declarations** - From: ```lua local add_backticks = function(word, esc_reserved) M.start = function(_) -- etc ``` To: ```lua local function add_backticks(word, esc_reserved) function M.start(_) -- etc ``` 2. **Fix typos** 3. **Simplify conditional statements** - Original nested `if` statements: ```lua if isutf8 then if idx == 12 then -- Code end elseif idx == 8 then -- Code end ``` - Simplified combined conditions: ```lua if isutf8 and idx == 12 then -- Code elseif not isutf8 and idx == 8 then -- Code end ```
- Introduce `config.objbr_mappings` to allow users to define custom key-command pairs. - Update `set_buf_options` to iterate over `objbr_mappings` and set corresponding key mappings. - Implement `M.run_custom_command` to execute the specified command on the selected object. - Provide a default mapping (`s` key to `summary` command) in the configuration.
Thanks! And welcome back! I will look at this on the weekend. |
I can define arbitrary mapping in my {
'she3o/tmp-R-Nvim',
lazy = false,
branch = 'clean-object-browser',
opts = {
auto_start = 'on startup',
R_args = { '--quiet', '--no-save' },
objbr_mappings = {
g = 'dplyr::glimpse',
dd = 'rm',
m = 'object.size',
},
-- Rest on init.lua But the I also wished to re-implement the keymapping for |
This now works. 3d89282 objbr_mappings = {
c = 'class({object})',
dd = 'rm',
g = 'dplyr::glimpse',
h = 'head({object}, 10)', -- Command with arguments
l = 'length({object})',
m = 'object.size',
n = 'names', -- Command without placeholder, object name will be appended.
p = 'plot({object})',
s = 'str({object})',
ss = 'summary({object})',
},
|
In the edge case that the objbr_placeholder = "<<<>>>",
objbr_mapping ={
plot = "plot(<<<>>>)"
} I can't think of a character sequence that is extremely readable, extremely short, and guaranteed to not overlap with lua/R syntax. so I shift this dilemma to the user. |
This commit allows users to define functions and bind them to keys in the object browser window. it also adds a new default keymap (i.e., 'v') to toggle between .GlobalEnv and libraries views. This showcases the new feature added in this commit.
Commit e2ce3c9 added the default v = function()
require('r.browser').toggle_view()
end,
It didn't work but, it works if I define it in my |
Potential use... -- init.lua
local function view_object_details()
local browser = require 'r.browser'
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local curline = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]
local object_name = browser.get_name(lnum, curline)
if object_name == '' then
require('r').warn 'No object selected.'
return
end
-- Open a split window and show the object's details
vim.cmd 'split'
vim.cmd 'enew'
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'Details of ' .. object_name })
-- More logic here to fetch and display details
end
-- more config
-- Bind e to view_object_details
objbr_mappings = {
c = 'class({object})',
dd = 'rm',
e = view_object_details,
g = 'dplyr::glimpse',
h = 'head({object}, 10)', -- Command with arguments
l = 'length({object})',
m = 'object.size',
n = 'names', -- Command without placeholder, object name will be appended.
p = 'plot({object})',
s = 'str({object})',
ss = 'summary({object})',
v = function()
require('r.browser').toggle_view()
end,
},
|
This reverts commit 2f1805e.
Would it be appropriate to add something in https://github.com/R-nvim/R.nvim/blob/main/doc/R.nvim.txt? |
Looking at this pull request now... On the one hand, since the Object Browser is not supposed to be edited, with this pull request, we can easily create custom commands with single-letter shortcuts in Normal mode that would normally clash with default Vim edit commands, and that's good. On the other hand, the more cumbersome keybindings starting with With the example given in a comment above, I'm getting these warnings on startup:
Yes, when the development of the feature is finished or immediately to allow us to know how it should work. |
objbr_mappings = {
['<localleader>gg'] = 'head', -- Use <localleader> to define keymaps
c = 'class',
dd = 'rm',
e = view_object_details,
g = 'dplyr::glimpse',
h = 'head({object}, 10)', -- Command with arguments
l = 'length',
m = 'object.size',
n = 'names', -- Command without placeholder, object name will be appended.
p = 'plot',
s = 'str',
ss = 'summary',
v = function()
require('r.browser').toggle_view()
end,
},
I just noticed that I get the same warnings. |
Great! When the function is documented, it will be good to include an example similar to this. |
What are your opinions on encapsulating parts of the config in separate subtables @jalvesaq @PMassicotte , like this - objbr_allnames = false,
- objbr_auto_start = false,
- objbr_h = 10,
- objbr_opendf = true,
- objbr_openlist = false,
- objbr_place = "script,right",
- objbr_w = 40,
- objbr_mappings = {
- s = "summary",
- p = "plot",
- },
- objbr_placeholder = "{object}",
+ object_browser = {
+ allnames = false,
+ auto_start = false,
+ height = 10,
+ open_data_frame = true,
+ open_list = false,
+ place = "script,right",
+ width = 40,
+ mappings = {
+ s = "summary",
+ p = "plot",
+ },
+ placeholder = "{object}",
+ },
|
It looks more organized. It reminds me of going from C (global variables) to C++ (classes). But it's important to hear from more people because this would be a disruptive change requiring users to read the documentation and update their config files. It would not introduce any new functionality or improve how the plugin works. |
I will try and make it so that old configuration style also works |
I agree. Maybe we could support both for a while, with an upcoming deprecation message. |
I like subtables! Regarding breaking changes, in my opinion R.nvim is probably young enough that the inconvenience for existing users would be okay. However I do think some concessions should be made to support anyone affected. Namely:
|
Maybe we could follow the path suggested by @wurli. Keeping old code alive is extra work for us. It's easier to add tags, although I have never created tags (except when releasing a new version because Github creates them automatically). When do you think we should add them? After every commit or only before making breaking changes? I tried to maintain a stable branch on Nvim-R. Now, I think it would be better to create a branch corresponding to a released version (not calling it "stable") and port bug fixes to this branch for a while. But this also means extra work that I'm not willing to have. |
- Group related variables (`curview`, `ob_win`, `ob_buf`, `upobcnt`, `running_objbr`, `auto_starting`, `hasbrowsermenu`) into a unified `state` table. - Update all function implementations to reference `state` fields instead of individual variables. - Enhance code readability and maintainability by centralizing state management. - Prepare the codebase for easier future modifications and potential feature additions.
I'd suggest that a good rule of thumb might be to bump the minor version either when enough new features have been added that users would probably find the hassle of updating and resolving any unforeseen issues worthwhile, or in case of a clear breaking change. Refactors, optimisations, bug fixes etc would probably all be good reasons to bump the patch version number; I don't think it's necessary for every commit. Perhaps we could use GitHub milestones to plan out when to bump the version too...
My possibly controversial opinion is that we probably don't need to worry about backporting bug fixes; I think it's sufficient to advise that users upgrade. My two main reasons for this are that (1) I think truly critical bugs, i.e. things which cause significant system crashes or security risks, are very unlikely to occur because of the nature of the project, and (2) most R packages also follow a similar system, so users will be unlikely to see it as unreasonable/burdensome. NB, one advantage of this system is that we wouldn't need to create a new branch for each release and clutter the working tree, just a new tag on In practice I would imagine this would mean continuing to use -- Would use the latest commit to main
{ "R-nvim/R.nvim", branch = "main" }
-- Would use the latest stable version
{ "R-nvim/R.nvim", version = "*" }
-- Would use any version compatible with 1.2.3, e.g. 1.2.4, 1.2.5, but not 1.3.0 or 1.2.2
{ "R-nvim/R.nvim", version = "~1.2.3" } |
Using versioning is a good idea. We should follow https://semver.org/ |
Agreed. 2 points from semver which are particularly relevant:
|
I agree too. We need a roadmap to define what we expect to reach in each future version. The only feature that I still plan to develop is better YAML completion for Quarto:
This can be either before or after the 1.0.0. |
Glad you agree too! So, here's my suggested roadmap to getting this PR merged, breaking changes and all:
|
I agree. I'm reading the semver.org document now... So, just to clarify: what we can consider our public "API" is our config table, right? |
Yep :) |
Actually, I guess it would also include Lua functions, since these are accessible to users... Maybe we need some notion of 'public' so users know which functions they can rely on. Or maybe we should say they're currently all subject to change without notice until 1.0.0? |
We can declare in |
I might be able to find time for a PR tomorrow to add docs about versioning, fine by me if anyone else wants to do it before then though :) |
Thanks! |
Versioning docs are complete and 0.1.0 released :) This should now be okay to merge I think! |
The warnings at startup have to be fixed before merging the pull request. Also, the option was added to the config table following a different style. The change below is a way of fixing both issues: diff --git a/lua/r/config.lua b/lua/r/config.lua
index 947f931..38ae045 100644
--- a/lua/r/config.lua
+++ b/lua/r/config.lua
@@ -62,11 +62,11 @@ local config = {
objbr_openlist = false,
objbr_place = "script,right",
objbr_w = 40,
- objbr_mappings = {
- s = "summary",
- p = "plot",
- },
- objbr_placeholder = "{object}",
+ objbr_mappings = {
+ s = "summary",
+ p = "plot",
+ },
+ objbr_placeholder = "{object}",
open_example = true,
open_html = "open and focus",
open_pdf = "open and focus",
@@ -270,7 +270,7 @@ local apply_user_opts = function()
-----------------------------------------------------------------------
-- 4. If the option is a dictionary, check each value individually
-----------------------------------------------------------------------
- if type(user_opt) == "table" then
+ if type(user_opt) == "table" and key_name ~= "objbr_mappings" then
for k, v in pairs(user_opt) do
if type(k) == "string" then
local next_key = {} |
Also: the new feature has to be documented... |
I will do this in a couple of hours |
This adds PR R-nvim#237 to this branch so that doc can be subsequently modified without introducing conflicts.
Document objbr_mappings in README and help files to explain how to configure custom key mappings for running R commands or Lua functions in the object browser. Added notes on using placeholders (e.g., {object}) and the new objbr_placeholder option for flexibility in command customization.
Thank you! The option isn't available on version 0.1.0 which was already released. We are currently developing version 0.2.0. But I already fixed this detail in the documentation and will merge the pull request now. |
This pull request introduces several enhancements to the Object Browser module in R.nvim, focusing on code refactoring, improved readability, and added flexibility through custom key mappings.
Key Changes
1. Code Refactoring and Cleanup
Use Options Table in set_buf_options: Replaced multiple vim.api.nvim_set_option_value calls with a single options table and a loop to set buffer options. This reduces code repetition and simplifies future maintenance.
afe2e60
Update Key Mappings to Use vim.keymap.set: Switched from vim.api.nvim_buf_set_keymap to vim.keymap.set with the buffer = true option, utilizing the more modern Neovim keymap API. afe2e60
Convert Anonymous Functions to Named Declarations: Changed anonymous function assignments to named function declarations for better readability and stack traces. e3ac35c
Simplify Conditional Statements: Streamlined nested if statements to reduce complexity. e3ac35c
Add Module Documentation
0b39c6e
Support for Custom Key Mappings in Object Browser
69002a6 168e901