Skip to content

Commit

Permalink
Merge pull request #11 from joshuadanpeterson/dev
Browse files Browse the repository at this point in the history
Fix for 'attempt to call global 'get_expand_root' (a nil value)' Error
  • Loading branch information
joshuadanpeterson authored Jul 19, 2024
2 parents 8556d63 + af06296 commit 062ace3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Changelog

## [v0.4.22](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.22) (2024-07-19)
- fix(commands): Move helper functions outside specific functions to avoid nil error
- docs: update CHANGELOG.md for v0.4.21 and remove duplicate entries

[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.21...v0.4.22)

## [v0.4.21](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.21) (2024-07-18)
- Merge pull request #10 from joshuadanpeterson/dev
- docs: update CHANGELOG.md for v0.4.20 and remove duplicate entries

[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.20...v0.4.21)
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.22...v0.4.21)

## [v0.4.20](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.20) (2024-07-18)
- Merge branch dev of https://github.com/joshuadanpeterson/typewriter.nvim into dev
Expand Down
39 changes: 18 additions & 21 deletions lua/typewriter/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ local center_block_config = require("typewriter.utils.center_block_config")
local M = {}
local typewriter_active = false

--- Helper function to determine if a node is a significant block
local function is_significant_block(node)
local node_type = node:type()
return center_block_config.expand[node_type] == true
end

--- Helper function to get the root of the expandable block
local function get_expand_root(node)
while node do
if is_significant_block(node) then
return node
end
node = node:parent()
end
return nil
end

--- Center the cursor on the screen
---
--- This function moves the view so that the cursor is centered vertically
Expand Down Expand Up @@ -76,30 +93,14 @@ function M.toggle_typewriter_mode()
M.enable_typewriter_mode()
end
end

--- Center the current code block and cursor
---
--- This function centers both the current code block and the cursor on the screen.
--- It's useful for focusing on a specific block of code.
---
--- @usage require("typewriter.commands").center_block_and_cursor()
function M.center_block_and_cursor()
-- Helper function to determine if a node is a significant block
local function is_significant_block(node)
local node_type = node:type()
return center_block_config.expand[node_type] == true
end

-- Helper function to get the root of the expandable block
local function get_expand_root(node)
while node do
if is_significant_block(node) then
return node
end
node = node:parent()
end
return nil
end

local node = ts_utils.get_node_at_cursor()
if not node then
return
Expand Down Expand Up @@ -131,8 +132,6 @@ end
---
--- @usage require("typewriter.commands").move_to_top_of_block()
function M.move_to_top_of_block()
-- Helper functions (is_significant_block and get_expand_root) are the same as in center_block_and_cursor

local node = ts_utils.get_node_at_cursor()
if not node then
return
Expand Down Expand Up @@ -171,8 +170,6 @@ end
---
--- @usage require("typewriter.commands").move_to_bottom_of_block()
function M.move_to_bottom_of_block()
-- Helper functions (is_significant_block and get_expand_root) are the same as in center_block_and_cursor

local node = ts_utils.get_node_at_cursor()
if not node then
return
Expand Down

0 comments on commit 062ace3

Please sign in to comment.