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: make VirtualIndent dynamically attach/detach based on vim.b.org_indent_mode #658

Merged

Conversation

PriceHiller
Copy link
Contributor

@PriceHiller PriceHiller commented Jan 28, 2024

Following on from #627, this adds the capability to toggle virtual indentation within an orgmode buffer. This allows me to check one thing off my list here.

See the short gif below:
dynamic-org-indent-mode

Bullet list of changes:

  • Refactored VirtualIndent to allow multiple instances to easily track attachment to different buffers
  • Added capability to VirtualIndent to enable/disable based on vim.b.org_indent_mode using a timer set to check the variable every 50ms
  • Added tests for new functionality
  • Updated docs to reflect this new functionality (org_startup_indented is where I placed them)

Do I need to add a user command like OrgToggleIndentMode or a keybind for this? Or is using the buffer variable, vim.b.org_indent_mode good enough to expose this functionality?

@PriceHiller PriceHiller force-pushed the virtual-indent-dynamic-attach branch 2 times, most recently from f787f8d to 7c1195c Compare January 28, 2024 09:16
@PriceHiller PriceHiller marked this pull request as draft January 28, 2024 09:19
@PriceHiller
Copy link
Contributor Author

Found a bug with scheduling and the current code. When setting a schedule the org_indent_mode variable stills shows as enabled, but the virtual indents are removed.

@PriceHiller PriceHiller force-pushed the virtual-indent-dynamic-attach branch from 7c1195c to 7e364c7 Compare January 28, 2024 09:22
@PriceHiller
Copy link
Contributor Author

Solved this... dumb mistake.

I'm going to leave this as a draft for now and drive this PR's branch when I get up later to see if anything else crops up. There might be other minor screwups, I am rather tired and it does in fact happen to be 3:23am which I am sure is negatively impacting my code 💀 .

If this is looking good on my side by EOD, or sometime on Monday then I'll mark it "Ready for Review".

@PriceHiller
Copy link
Contributor Author

Thus far it's been ok for me, opening this up for review 🙂

@PriceHiller PriceHiller marked this pull request as ready for review January 29, 2024 23:42
Copy link
Member

@kristijanhusak kristijanhusak left a comment

Choose a reason for hiding this comment

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

I didn't give this a test yet, just checked the code.
Lets do 2 things here:

  1. Rebase this from nightly branch and set target to nightly branch.
  2. Add the dict watcher I mentioned in below comment

lua/orgmode/ui/virtual_indent.lua Outdated Show resolved Hide resolved
This ensures we can call `start_watch_org_indent` as much as we want
without starting a bunch of timers in the background.

This enforces the use of `start_watch_org_indent` and `stop_watch_org_indent` for managing the timer.
@PriceHiller PriceHiller force-pushed the virtual-indent-dynamic-attach branch from 84a8c6f to ac27fa2 Compare February 7, 2024 20:54
@PriceHiller PriceHiller changed the base branch from master to nightly February 7, 2024 21:07
@PriceHiller
Copy link
Contributor Author

PriceHiller commented Feb 8, 2024

I didn't give this a test yet, just checked the code. Lets do 2 things here:

1. Rebase this from `nightly` branch and set target to `nightly` branch.

2. Add the dict watcher I mentioned in below comment

1.) Done
2.) Done

On #2 though, there's a slight catch. dictwatcheradd doesn't return any context of which buffer the variable was changed in. This means we have to loop all of the VirtualIndent instances to determine which one had its variable changed.

Still way more efficient than my timer approach. The efficiency hit from having to do the loop is negligible imo, just feels nasty. Shame I couldn't find a way to extract the buffer in which the variable was updated in.

See this for how that was done and the note around it.

@kristijanhusak
Copy link
Member

On #2 though, there's a slight catch. dictwatcheradd doesn't return any context of which buffer the variable was changed in. This means we have to loop all of the VirtualIndent instances to determine which one had its variable changed.

We could just store the buffer number as a buffer variable, and read that from the 3rd param of the watcher callback.

  1. In the ftplugin/org.lua add something like vim.b.org_bufnr = vim.fn.bufnr().
  2. In the watch_buffer_variable callback, grab the 3rd param which is the whole vim.b value for the buffer. Take the buffer number from org_bufnr.

@PriceHiller PriceHiller force-pushed the virtual-indent-dynamic-attach branch from 68705b7 to 1d256cf Compare February 9, 2024 06:03
@PriceHiller
Copy link
Contributor Author

@kristijanhusak Done as part of the latest force push, pretty smart idea you had there. I definitely wouldn't have thought of that.

As an aside, sorry for the back and forth. I'd like to respond and nail these problems within the hour of your responses, but I'm getting tied up in things outside of github. I'm hoping it's not too much a bother. 😅

Copy link
Member

@kristijanhusak kristijanhusak left a comment

Choose a reason for hiding this comment

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

Ok looks good now, thanks!

Sorry for delaying it a bit, I was busy working on improvements for markup highlighter.

As an aside, sorry for the back and forth. I'd like to respond and nail these problems within the hour of your responses, but I'm getting tied up in things outside of github. I'm hoping it's not too much a bother

Don't worry about it. There's no need to rush. This is FOSS, no one is expecting you to be available :)

@kristijanhusak kristijanhusak merged commit aaedd5c into nvim-orgmode:nightly Feb 14, 2024
@PriceHiller
Copy link
Contributor Author

Don't worry about it. There's no need to rush. This is FOSS, no one is expecting you to be available :)

Good point.

Sorry for delaying it a bit, I was busy working on improvements for markup highlighter.

No worry about the delays, nobody is paying you to work on this. I just run straight from the branch until it gets merged, a literal one line change in my Neovim config. 😄

@PriceHiller PriceHiller deleted the virtual-indent-dynamic-attach branch February 14, 2024 16:21
kristijanhusak added a commit that referenced this pull request Feb 14, 2024
* refactor!: Rewrite file management to use tree-sitter

* feat!: Deprecate org_mappings.handle_return in favor of org_mappings.meta_return

If you did not use this mapping directly everything is ok. This is just for users that mapped it directly

* feat: Add org_return_uses_meta_return option

* fix: Allow modifications of plan date range end date

* fix: Properly show plan date ranges in agenda

* feat(api): Add get_closest_headline and refile to api

* fix: Update minimal init to work with Windows

* tests: Add tests for file and fix edge case with set text

* fix(cron): Instantiate orgmode before running cron

* refactor: Stars highlighter

* refactor: Todo faces highlighter

* refactor: Markup highlighter

* fix: Do not rely on node:has_changes()

* feat: make VirtualIndent dynamically attach/detach based on `vim.b.org_indent_mode` (#658)

* feat: make VirtualIndent react to changes in `vim.b.org_indent_mode`

* test: add test to validate VirtualIndent dynamic attach functionality

* docs: update docs to reflect dynamic virtual indent attach

* refactor: make VirtualIndent `start_watch_org_indent` idempotent

This ensures we can call `start_watch_org_indent` as much as we want
without starting a bunch of timers in the background.

This enforces the use of `start_watch_org_indent` and `stop_watch_org_indent` for managing the timer.

* feat: add dict_watcher utility

* refactor: use `dict_watcher` to monitor `vim.b.org_indent_mode`

* ci: Run tests when creating PR to nightly

* chore: Reformat files

* refactor: remove sync call to set indent for virtual indents (#663)

On the current version of nightly (at the time of writing this is NVIM
v0.10.0-dev-2361+ga376d979b) the synchronous call doesn't seem to have
any value.

Using only a scheduled call is seemingly smooth now -- perhaps when I
made the original commit I had something wrong with my configuration or
perhaps upstream Neovim fixed whatever was causing some choppiness?

Another note is that `on_lines` in `nvim_buf_attach` can have `textlock`
restrictions and more applied, thus we really should just use
`vim.schedule` only anyhow.

* feat: add `org-indent-mode-turns-on-hiding-stars` equivalent (#659)

* feat: add equivalent of `org-indent-mode-turns-on-hiding-stars`

* docs: add documentation for `org_indent_mode_turns_on_hiding_stars`

* docs: add reference to `org_indent_mode`  for hiding stars

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

* fix: ensure hl group for hiding stars is applied

See
#659 (review)

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

---------

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

* fix: Hide leading stars on foldtext

* ci: Run tests only on nightly

* fix: run Org init as part of setup (#664)

This ensures that notifications do not error on startup. Prior to this
commit the `OrgFiles` object wasn't loaded in time for notifications to
work.

* fix: Statusline function for clock

* ci: Remove nightly branch from CI triggers

---------

Co-authored-by: Price Hiller <price@orion-technologies.io>
SlayerOfTheBad pushed a commit to SlayerOfTheBad/orgmode that referenced this pull request Aug 16, 2024
* refactor!: Rewrite file management to use tree-sitter

* feat!: Deprecate org_mappings.handle_return in favor of org_mappings.meta_return

If you did not use this mapping directly everything is ok. This is just for users that mapped it directly

* feat: Add org_return_uses_meta_return option

* fix: Allow modifications of plan date range end date

* fix: Properly show plan date ranges in agenda

* feat(api): Add get_closest_headline and refile to api

* fix: Update minimal init to work with Windows

* tests: Add tests for file and fix edge case with set text

* fix(cron): Instantiate orgmode before running cron

* refactor: Stars highlighter

* refactor: Todo faces highlighter

* refactor: Markup highlighter

* fix: Do not rely on node:has_changes()

* feat: make VirtualIndent dynamically attach/detach based on `vim.b.org_indent_mode` (nvim-orgmode#658)

* feat: make VirtualIndent react to changes in `vim.b.org_indent_mode`

* test: add test to validate VirtualIndent dynamic attach functionality

* docs: update docs to reflect dynamic virtual indent attach

* refactor: make VirtualIndent `start_watch_org_indent` idempotent

This ensures we can call `start_watch_org_indent` as much as we want
without starting a bunch of timers in the background.

This enforces the use of `start_watch_org_indent` and `stop_watch_org_indent` for managing the timer.

* feat: add dict_watcher utility

* refactor: use `dict_watcher` to monitor `vim.b.org_indent_mode`

* ci: Run tests when creating PR to nightly

* chore: Reformat files

* refactor: remove sync call to set indent for virtual indents (nvim-orgmode#663)

On the current version of nightly (at the time of writing this is NVIM
v0.10.0-dev-2361+ga376d979b) the synchronous call doesn't seem to have
any value.

Using only a scheduled call is seemingly smooth now -- perhaps when I
made the original commit I had something wrong with my configuration or
perhaps upstream Neovim fixed whatever was causing some choppiness?

Another note is that `on_lines` in `nvim_buf_attach` can have `textlock`
restrictions and more applied, thus we really should just use
`vim.schedule` only anyhow.

* feat: add `org-indent-mode-turns-on-hiding-stars` equivalent (nvim-orgmode#659)

* feat: add equivalent of `org-indent-mode-turns-on-hiding-stars`

* docs: add documentation for `org_indent_mode_turns_on_hiding_stars`

* docs: add reference to `org_indent_mode`  for hiding stars

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

* fix: ensure hl group for hiding stars is applied

See
nvim-orgmode#659 (review)

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

---------

Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>

* fix: Hide leading stars on foldtext

* ci: Run tests only on nightly

* fix: run Org init as part of setup (nvim-orgmode#664)

This ensures that notifications do not error on startup. Prior to this
commit the `OrgFiles` object wasn't loaded in time for notifications to
work.

* fix: Statusline function for clock

* ci: Remove nightly branch from CI triggers

---------

Co-authored-by: Price Hiller <price@orion-technologies.io>
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