From 8f2160cf3cf8f9daf066c05400b3ce480e6a2155 Mon Sep 17 00:00:00 2001 From: Keenan Johns Date: Tue, 6 Jun 2023 15:03:27 -0500 Subject: [PATCH 1/5] refactor --- lua/bionic-reading/cmds.lua | 31 ++++++++++++++++++------------- lua/bionic-reading/highlight.lua | 29 ++++++++++++----------------- lua/bionic-reading/init.lua | 13 ++++++++----- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/lua/bionic-reading/cmds.lua b/lua/bionic-reading/cmds.lua index 7b59fbb..1b27cdf 100644 --- a/lua/bionic-reading/cmds.lua +++ b/lua/bionic-reading/cmds.lua @@ -31,13 +31,14 @@ function CMDS:_setup() callback = function(args) local Buffers = require("bionic-reading.buffers") local Config = require("bionic-reading.config") + local bufnr = args.buf -- if buffer is active, we have already highlighted the file - if Buffers:check_active_buf(args.buf) or not Utils.check_file_types() or not Config.opts.auto_highlight then + if Buffers:check_active_buf(bufnr) or not Utils.check_file_types() or not Config.opts.auto_highlight then return end - Highlight:highlight(0, -1) + Highlight:highlight(0, -1, bufnr) end, }) @@ -47,8 +48,9 @@ function CMDS:_setup() callback = function(args) local Buffers = require("bionic-reading.buffers") local Config = require("bionic-reading.config") + local bufnr = args.buf - if not Buffers:check_active_buf(args.buf) or not Utils.check_file_types() then + if not Buffers:check_active_buf(bufnr) or not Utils.check_file_types() then return end @@ -63,7 +65,7 @@ function CMDS:_setup() line_end = -1 end - Highlight:highlight(line_start, line_end) + Highlight:highlight(line_start, line_end, bufnr) end, }) @@ -73,11 +75,12 @@ function CMDS:_setup() callback = function(args) local Config = require("bionic-reading.config") local Buffers = require("bionic-reading.buffers") + local bufnr = args.buf if - not Buffers:check_active_buf(args.buf) - or not Config.opts.update_in_insert_mode - or not Utils.check_file_types() + not Buffers:check_active_buf(bufnr) + or not Config.opts.update_in_insert_mode + or not Utils.check_file_types() then return end @@ -93,14 +96,16 @@ function CMDS:_setup() line_end = -1 end - Highlight:highlight(line_start, line_end) + Highlight:highlight(line_start, line_end, bufnr) end, }) - create_user_command("BRToggle", function() + create_user_command("BRToggle", function(opts) local Config = require("bionic-reading.config") local Buffers = require("bionic-reading.buffers") local bufnr = get_current_buf() + local line_start = opts.line1 or 0 + local line_end = opts.line2 or -1 -- check if file type is in config if not Utils.check_file_types() then @@ -128,10 +133,10 @@ function CMDS:_setup() if Buffers:check_active_buf(bufnr) then Utils.notify("BionicReading disabled", "info", "") - Highlight:clear() + Highlight:clear(line_start, line_end, bufnr) else Utils.notify("BionicReading enabled", "info", "") - Highlight:highlight(0, -1) + Highlight:highlight(line_start, line_end, bufnr) end end, {}) @@ -143,7 +148,7 @@ function CMDS:_setup() if success then Utils.notify("Syllable algorithm is now " .. (new_value and "enabled" or "disabled"), "info", "") - Highlight:highlight(0, -1) + Highlight:highlight(0, -1, 0) end end, {}) @@ -161,7 +166,7 @@ function CMDS:_setup() if success then Utils.notify("Saccade cadence is now " .. new_value, "info", "") - Highlight:highlight(0, -1) + Highlight:highlight(0, -1, 0) end end, { nargs = 1 }) diff --git a/lua/bionic-reading/highlight.lua b/lua/bionic-reading/highlight.lua index 0565976..185fbc5 100644 --- a/lua/bionic-reading/highlight.lua +++ b/lua/bionic-reading/highlight.lua @@ -14,16 +14,14 @@ local Highlight = { --- Clear all highlights in current buffer by clearing namespace --- @param line_start number --- @param line_end number +--- @param bufnr number --- @return nil -function Highlight:clear(line_start, line_end) - -- default to clear highlighting for all lines - if not line_start or not line_end then - line_start = 0 - line_end = -1 - end - +function Highlight:clear(line_start, line_end, bufnr) local Buffers = require("bionic-reading.buffers") - local bufnr = api.nvim_get_current_buf() + + bufnr = bufnr or api.nvim_get_current_buf() + line_start = line_start or 0 + line_end = line_end or -1 Buffers:deactivate_buf(bufnr) api.nvim_buf_clear_namespace(bufnr, self.namespace, line_start, line_end) @@ -32,21 +30,18 @@ end --- Highlight lines in current buffer --- @param line_start number --- @param line_end number +--- @param bufnr number --- @return nil -function Highlight:highlight(line_start, line_end) +function Highlight:highlight(line_start, line_end, bufnr) local Buffers = require("bionic-reading.buffers") local Config = require("bionic-reading.config") local saccade_cadence = Config.opts.saccade_cadence - -- default to highlight all lines - if not line_start or not line_end then - line_start = 0 - line_end = -1 - end - - Highlight:clear(line_start, line_end) + bufnr = bufnr or api.nvim_get_current_buf() + line_start = line_start or 0 + line_end = line_end or -1 - local bufnr = api.nvim_get_current_buf() + Highlight:clear(line_start, line_end, bufnr) Buffers:activate_buf(bufnr) diff --git a/lua/bionic-reading/init.lua b/lua/bionic-reading/init.lua index 3b749a7..d99855b 100644 --- a/lua/bionic-reading/init.lua +++ b/lua/bionic-reading/init.lua @@ -4,16 +4,19 @@ local initialized = false --- Highlight lines in current buffer --- @param line_start number --- @param line_end number ---- @param override boolean @clear all highlights before highlighting +--- @param bufnr number --- @return nil -function M.highlight(line_start, line_end, override) - require("bionic-reading.highlight"):highlight(line_start, line_end, override) +function M.highlight(line_start, line_end, bufnr) + require("bionic-reading.highlight"):highlight(line_start, line_end, bufnr) end --- Clear all highlights in current buffer by clearing namespace +--- @param line_start number +--- @param line_end number +--- @param bufnr number --- @return nil -function M.clear() - require("bionic-reading.highlight"):clear() +function M.clear(line_start, line_end, bufnr) + require("bionic-reading.highlight"):clear(line_start, line_end, bufnr) end --- Setup bionic-reading.nvim From aa76ecb0272f0e8c2f6a0f467f6838450b150493 Mon Sep 17 00:00:00 2001 From: Keenan Johns Date: Fri, 9 Jun 2023 15:10:59 -0500 Subject: [PATCH 2/5] refactor: Rewrote using treesitter --- .DS_Store | Bin 0 -> 6148 bytes lua/.DS_Store | Bin 0 -> 6148 bytes lua/bionic-reading/cmds.lua | 277 ++++++++++++------------------- lua/bionic-reading/config.lua | 27 ++- lua/bionic-reading/highlight.lua | 148 ++++++++++------- lua/bionic-reading/init.lua | 16 +- lua/bionic-reading/test.lua | 2 + lua/bionic-reading/utils.lua | 25 ++- plugin/bionic-reading.vim | 14 ++ 9 files changed, 256 insertions(+), 253 deletions(-) create mode 100644 .DS_Store create mode 100644 lua/.DS_Store create mode 100644 lua/bionic-reading/test.lua diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b3496066ab2902e8d17b6828e16c663467abef7a GIT binary patch literal 6148 zcmeHK!EVz)5Ph43#A&IN14z9fS>jry0ji3Mi<>5gN`*s>$^lTY>nJsKy;1CtLlntp z_!zEy3IEa)%^PCoC4S>Y@jo0a2NI8Nd$?{t2PO11W=-f$Yurt{gqkTX9I@@du$ zCLg)=UdklA@q_Rz9?k}>Cr2{PgE$>dR6!gKDdp3LIF012E2n9csajumI8CQHXl>8u zuU~d{+`aw1#g02aQ2wj^#iHpveZKqVba0tmrSiMt0D@LKC0hn>;0HNo&fok=n#goS z5zc;$F|mA(OR^(kx{a^IxJ0_b4Ue$Qcb)2(!x7@O-0PLxCQj+O9HNJ}gl$|QDvw^_ zyICHcnpPex1Lj}sY(7{9?8GetmVvt%;Pt^JW%LXd8r9N4MjZhNYZO*OK7R;sjcd>| zSZJgk5}`{8b*V5%457;@?wUN$V4+c$Lzu&dFjp4lgd)_{X?$0uLwFjkwG3DW$_!L> zv&H*=|L*gDxyZIG1D1h%#ek^w{9c!)WcJpz$?;z6l71m&;k-hlDj{RGW99HxyiKZv Z`doGhJ%fctijde30Y!r~mVtlDz+bK=l(7H+ literal 0 HcmV?d00001 diff --git a/lua/.DS_Store b/lua/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..812fd2ccd2616392593dde3ef13099e6acf638a1 GIT binary patch literal 6148 zcmeHKO>fgc5S>j!UHri;;#o4APrJ$FEnmGhrbyR*lD^4@i+ Date: Fri, 9 Jun 2023 15:12:11 -0500 Subject: [PATCH 3/5] removed DS files --- .DS_Store | Bin 6148 -> 0 bytes lua/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store delete mode 100644 lua/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index b3496066ab2902e8d17b6828e16c663467abef7a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!EVz)5Ph43#A&IN14z9fS>jry0ji3Mi<>5gN`*s>$^lTY>nJsKy;1CtLlntp z_!zEy3IEa)%^PCoC4S>Y@jo0a2NI8Nd$?{t2PO11W=-f$Yurt{gqkTX9I@@du$ zCLg)=UdklA@q_Rz9?k}>Cr2{PgE$>dR6!gKDdp3LIF012E2n9csajumI8CQHXl>8u zuU~d{+`aw1#g02aQ2wj^#iHpveZKqVba0tmrSiMt0D@LKC0hn>;0HNo&fok=n#goS z5zc;$F|mA(OR^(kx{a^IxJ0_b4Ue$Qcb)2(!x7@O-0PLxCQj+O9HNJ}gl$|QDvw^_ zyICHcnpPex1Lj}sY(7{9?8GetmVvt%;Pt^JW%LXd8r9N4MjZhNYZO*OK7R;sjcd>| zSZJgk5}`{8b*V5%457;@?wUN$V4+c$Lzu&dFjp4lgd)_{X?$0uLwFjkwG3DW$_!L> zv&H*=|L*gDxyZIG1D1h%#ek^w{9c!)WcJpz$?;z6l71m&;k-hlDj{RGW99HxyiKZv Z`doGhJ%fctijde30Y!r~mVtlDz+bK=l(7H+ diff --git a/lua/.DS_Store b/lua/.DS_Store deleted file mode 100644 index 812fd2ccd2616392593dde3ef13099e6acf638a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO>fgc5S>j!UHri;;#o4APrJ$FEnmGhrbyR*lD^4@i+ Date: Fri, 9 Jun 2023 15:15:56 -0500 Subject: [PATCH 4/5] refactor(test): removed --- lua/bionic-reading/test.lua | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 lua/bionic-reading/test.lua diff --git a/lua/bionic-reading/test.lua b/lua/bionic-reading/test.lua deleted file mode 100644 index af6eb79..0000000 --- a/lua/bionic-reading/test.lua +++ /dev/null @@ -1,2 +0,0 @@ ---- Clear all highlights in current buffer by clearing namespace ---- Apply highlighting to buffer From f27d5a112a536cdf757488d8e68dbb51a3d41101 Mon Sep 17 00:00:00 2001 From: Keenan Johns Date: Fri, 9 Jun 2023 15:30:03 -0500 Subject: [PATCH 5/5] refactor(README): updated to reflect treesitter changes --- README.md | 78 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 2ee48dc..accf6ae 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,17 @@ /____/ ``` -Togglable and customizable bionic reading for Neovim using syllable based highlighting! +Togglable and customizable bionic reading for Neovim using syllable based highlighting +and TreeSitter! ## What is BionicReading? `bionic-reading.nvim` is a simple but powerful Neovim plugin designed to improve reading and writing efficiency. By highlighting the first syllable of each word, -it aims to aid comprehension and to highlight on logical sections of the code +it aims to aid comprehension and to highlight on logical sections of the code. By +using the power of TreeSitter you have complete control over what is highlighted +in your files. Just highlight comments? Sure! Strings? Of course! Function names? +Why not! Or just highlight the whole file! The choice is yours ### Syllable Algorithm @@ -44,7 +48,7 @@ Font is [VictorMono](https://rubjo.github.io/victor-mono/) and my NeoVim setup u ## Features - No dependencies! - - Custom highlighting amounts and highlighting style + - Custom highlighting style - Toggable update while in insert mode - File types restricted - Highlighting stays after colorscheme changes @@ -52,9 +56,8 @@ Font is [VictorMono](https://rubjo.github.io/victor-mono/) and my NeoVim setup u - Uses [nvim-notify](https://github.com/rcarriga/nvim-notify) for notifications if available - Now prompts to highlight file if file type is not in your config - Can enable/disable prompting ^ - - *NEW*: Added saccade_cadence to control how often words are highlighted - - *NEW*: Added user command to set saccade_cadence - - *NEW*: Can now use a syllable algorithm to highlight the first syllable in a word + - *NEW*: Now use a syllable algorithm to highlight the first syllable in a word + - *NEW*: You can now highlight on node type thanks to treesitter! ## Getting Started @@ -117,45 +120,52 @@ Example using lazy.nvim -- determines if the file types below will be -- automatically highlighted on buffer open auto_highlight = true, - -- the file types you want to highlight - file_types = { 'text' }, + -- the file types you want to highlight with + -- the node types you would like to target + -- using treesitter + file_types = { + ["text"] = { + "any", -- highlight any node + }, + -- EX: only highlights comments in lua files + ["lua"] = { + "comment", + }, + }, -- the highlighting styles applied -- IMPORTANT - if link is present, no other -- styles are applied hl_group_value = { link = "Bold", }, - -- dictates the characters highlighted based - -- off of word length. key is word length and - -- value is the number of characters highlighted - -- note used if syllable_algorithm is on - hl_offsets = { - ['1'] = 1, - ['2'] = 1, - ['3'] = 2, - ['4'] = 2, - ['default'] = 0.4, -- defaults to 40% of the word - }, -- Flag used to control if the user is prompted -- if BRToggle is called on a file type that is not -- explicitly defined above prompt_user = true, - -- The cadence of highlight word. Defaults to ever - -- word. Example: 2 would be every other word - saccade_cadence = 1, - -- Flag used to control if the highlighting is - -- applied while typing + -- Enable or disable the use of treesitter + treesitter = true, + -- Flag used to control if highlighting is applied as + -- you type update_in_insert_mode = true, - -- Flag used to dicate if the syllable_algorithm - -- is used. Highlights on syllables instead of - -- characters based on word length. Disables the - -- use of hl_offset if on - syllable_algorithm = true, }) end, } ``` +### TreeSitter + +TreeSitter is used to target node types listed in your config above. This +allows for BionicReading to highlight as much code or as little as you would +like. For instance, the default config highlights only comments for Lua files. +This allows you to tweak what is highlighted on a filetype bases. + +NOTE: you need to have a the language parser installed for TreeSitter to work + +```lua +-- example +:TSInstall lua +``` + ## Mappings No mapping are added. Feel free to map as you would like @@ -176,12 +186,6 @@ Please see autocmd code [here](lua/bionic-reading/cmds.lua) " Toggles the auto_highlight flag :BRToggleAutoHighlight - -" Used to set the saccade_cadence -:BRSaccadeCadence - -" Toggles the syllable_algorithm flag -:BRToggleSyllableAlgorithm ``` ## TODO @@ -189,9 +193,7 @@ Please see autocmd code [here](lua/bionic-reading/cmds.lua) - [x] Add support for nvim-notify - [x] Prompt user to highlight file IF file type is not in config - [x] Add ability to toggle user prompt -- [ ] Investigate treesitter highlighting -- [x] Add saccade cadence -- [x] Add user command to set saccade cadence +- [x] Investigate treesitter highlighting - [x] Add syllable algorithm - [x] Expose highlight and clear - [ ] ????