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

fix: improve center_block_and_cursor function to correctly center code blocks for PHP files #12

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3252614
feat(commands): Correctly center code block even when near bottom of …
joshuadanpeterson Jul 19, 2024
d43df69
fix: Improve center_block_and_cursor function for better PHP block ha…
joshuadanpeterson Jul 19, 2024
2ff8311
fix: Improve center_block_and_cursor function for better PHP block ha…
joshuadanpeterson Jul 19, 2024
b42196f
fix: Improve get_expand_root logic for better PHP block handling
joshuadanpeterson Jul 19, 2024
04ef13a
feat(center_block_config): Add 'declaration_list' to node list
joshuadanpeterson Jul 19, 2024
34170e9
refactor(center_block_and_cursor): Add new nodes
joshuadanpeterson Jul 19, 2024
6b17d24
feat(center_block_config): Add new node
joshuadanpeterson Jul 19, 2024
73b1fb0
style(commands): Remove debug print statements
joshuadanpeterson Jul 19, 2024
da09d20
docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries
actions-user Jul 19, 2024
45e17bf
refactor(commands): Remove 'stop traversal' statement
joshuadanpeterson Jul 19, 2024
25ca088
Merge branch 'dev' of https://github.com/joshuadanpeterson/typewriter…
joshuadanpeterson Jul 19, 2024
5dc8592
Merge branch 'main' into dev
joshuadanpeterson Jul 20, 2024
8670b5c
feat(commands): Correctly center code block even when near bottom of …
joshuadanpeterson Jul 19, 2024
0d09411
fix: Improve center_block_and_cursor function for better PHP block ha…
joshuadanpeterson Jul 19, 2024
b853eb3
fix: Improve center_block_and_cursor function for better PHP block ha…
joshuadanpeterson Jul 19, 2024
6a63cde
fix: Improve get_expand_root logic for better PHP block handling
joshuadanpeterson Jul 19, 2024
d9377e1
feat(center_block_config): Add 'declaration_list' to node list
joshuadanpeterson Jul 19, 2024
bdcae40
refactor(center_block_and_cursor): Add new nodes
joshuadanpeterson Jul 19, 2024
1256ddc
feat(center_block_config): Add new node
joshuadanpeterson Jul 19, 2024
6b81dc6
style(commands): Remove debug print statements
joshuadanpeterson Jul 19, 2024
bf71692
refactor(commands): Remove 'stop traversal' statement
joshuadanpeterson Jul 19, 2024
4790f89
docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries
actions-user Jul 19, 2024
470f237
Merge branch 'dev' of https://github.com/joshuadanpeterson/typewriter…
joshuadanpeterson Jul 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,87 @@
# Changelog

# ||||||| c2862a9

## [v0.4.24](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.24) (2024-07-19)

- style(commands): Remove debug print statements
- feat(center_block_config): Add new node
- refactor(center_block_and_cursor): Add new nodes
- feat(center_block_config): Add declaration_list to node list
- fix: Improve get_expand_root logic for better PHP block handling
- fix: Improve center_block_and_cursor function for better PHP block handling
- feat(commands): Correctly center code block even when near bottom of the file

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

> > > > > > > 5dc8592017c9708d356747d77de7303c39bf2eb1

## [v0.4.23](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.23) (2024-07-19)

- Merge pull request #11 from joshuadanpeterson/dev
- docs: update CHANGELOG.md for v0.4.22 and remove duplicate entries

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

# ||||||| parent of da09d20 (docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries)

## [v0.4.24](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.24) (2024-07-19)

- style(commands): Remove debug print statements
- feat(center_block_config): Add new node
- refactor(center_block_and_cursor): Add new nodes
- feat(center_block_config): Add declaration_list to node list
- fix: Improve get_expand_root logic for better PHP block handling
- fix: Improve center_block_and_cursor function for better PHP block handling
- feat(commands): Correctly center code block even when near bottom of the file

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

> > > > > > > da09d20 (docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries)

## [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

<<<<<<< HEAD
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.23...v0.4.22)
||||||| parent of da09d20 (docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries)
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.21...v0.4.22)

> > > > > > > da09d20 (docs: update CHANGELOG.md for v0.4.24 and remove duplicate entries)

## [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.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
- feat(docs): Update CHANGELOG and TODO in help docs
- docs: update CHANGELOG.md for v0.4.19 and remove duplicate entries

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

## [v0.4.19](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.19) (2024-07-18)

- feat(docs): Add top-level tag to typewriter help file
- docs: update CHANGELOG.md for v0.4.18 and remove duplicate entries

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

## [v0.4.18](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.18) (2024-07-07)

- Merge pull request #6 from joshuadanpeterson/dev
- docs: update CHANGELOG.md for v0.4.17 and remove duplicate entries

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

## [v0.4.17](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.17) (2024-07-07)

- Merge branch main into dev
- docs: update CHANGELOG.md for v0.4.16 and remove duplicate entries
- Merge branch dev of https://github.com/joshuadanpeterson/typewriter.nvim into dev Update local repo with remote changes
Expand All @@ -51,6 +95,7 @@
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.18...v0.4.17)

## [v0.4.16](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.16) (2024-07-07)

- Merge branch dev of https://github.com/joshuadanpeterson/typewriter.nvim into dev Update local repo with remote changes
- refactor(ci): improve CHANGELOG.md generation to prevent duplicate entries
- docs: update CHANGELOG.md for v0.4.16 and remove duplicate entries
Expand All @@ -65,6 +110,7 @@
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.17...v0.4.16)

## [v0.4.15](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.15) (2024-07-06)

- chore(ci): Update push branches
- docs(help): improve and expand plugin documentation
- refactor(init): lazy-load dependencies and simplify setup
Expand All @@ -76,25 +122,29 @@
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.4.16...v0.4.15)

## [v0.4.14](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.14) (2024-07-05)

- Merge branch main of https://github.com/joshuadanpeterson/typewriter.nvim Update local repo with remote changes
- fix(ci): resolve duplicate entries in CHANGELOG generation
- docs: update CHANGELOG.md for v0.4.13 and standardize all entries

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

## [v0.4.13](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.13) (2024-07-05)

- refactor(ci): Enhance CHANGELOG management to remove duplicate entries
- docs: update CHANGELOG.md for v0.4.12 and standardize all entries

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

## [v0.4.12](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.12) (2024-07-05)

- Update CHANGELOG.md
- docs: update CHANGELOG.md for v0.4.11 and standardize all entries

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

## [v0.4.11](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.11) (2024-07-05)

- Merge pull request #4 from joshuadanpeterson/center-horizontally
- docs: update CHANGELOG.md for v0.4.10 and standardize all entries

Expand Down Expand Up @@ -169,116 +219,99 @@
## [v0.4.1](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.1) (2024-07-05)

## [Unreleased](https://github.com/joshuadanpeterson/typewriter.nvim/tree/HEAD)

**Fixed bugs:**

- Fix `$` Command Issue in Normal Mode [\#2](https://github.com/joshuadanpeterson/typewriter.nvim/pull/2) ([joshuadanpeterson](https://github.com/joshuadanpeterson))

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

## [v0.4.0](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.4.0) (2024-07-05)

**Fixed bugs:**

- In normal mode with plugin installed using command $ - end of line not reached. [\#1](https://github.com/joshuadanpeterson/typewriter.nvim/issues/1)

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

## [v0.3.2](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.3.2) (2024-07-04)


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

## [v0.3.0](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.3.0) (2024-07-01)


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

## [v0.3.1](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.3.1) (2024-07-01)


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

## [v0.2.0](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.2.0) (2024-06-30)


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

## [v0.1.5](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.5) (2024-06-16)


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

## [v0.1.4](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.4) (2024-06-16)


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

## [v0.1.3](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.3) (2024-06-16)


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

## [v0.1.2](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.2) (2024-06-16)


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

## [v0.1.1](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.1) (2024-06-16)


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

## [v0.1.0](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.1.0) (2024-06-16)


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

## [v0.0.9](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.9) (2024-06-15)


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

## [v0.0.8](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.8) (2024-06-15)


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

## [v0.0.7](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.7) (2024-06-15)


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

## [v0.0.4](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.4) (2024-06-15)


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

## [v0.0.5](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.5) (2024-06-15)


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

## [v0.0.6](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.6) (2024-06-15)


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

## [v0.0.3](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.3) (2024-06-15)


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

## [v0.0.2](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.2) (2024-06-15)


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

## [v0.0.1](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.0.1) (2024-06-15)


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

## [v1.0.0](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v1.0.0) (2024-06-15)

\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
\* _This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)_

[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.0.1...v1.0.0)

29 changes: 24 additions & 5 deletions lua/typewriter/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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
return center_block_config.should_expand(node_type)
end

--- Helper function to get the root of the expandable block
Expand Down Expand Up @@ -100,6 +100,7 @@ end
--- It's useful for focusing on a specific block of code.
---
--- @usage require("typewriter.commands").center_block_and_cursor()
--- Center the current code block and cursor
function M.center_block_and_cursor()
local node = ts_utils.get_node_at_cursor()
if not node then
Expand All @@ -114,12 +115,30 @@ function M.center_block_and_cursor()
local start_row, _, end_row, _ = node:range()
local middle_line = math.floor((start_row + end_row) / 2)

-- Check for edge cases
local line_count = vim.api.nvim_buf_line_count(0)
if middle_line < 0 then middle_line = 0 end
if middle_line >= line_count then middle_line = line_count - 1 end

-- Determine the visible window range
local win_height = vim.api.nvim_win_get_height(0)
local top_visible_line = math.max(middle_line - math.floor(win_height / 2), 0)
local bottom_visible_line = math.min(middle_line + math.floor(win_height / 2), line_count - 1)

if config.config.keep_cursor_position then
local cursor = api.nvim_win_get_cursor(0)
vim.cmd("normal! zz")
api.nvim_win_set_cursor(0, cursor)
local cursor = vim.api.nvim_win_get_cursor(0)
-- Save current cursor position
local cursor_row = cursor[1]
local cursor_col = cursor[2]

-- Move cursor to the middle line of the block and center the view
vim.cmd(string.format("normal! %dGzz", middle_line + 1))

-- Restore original cursor position
vim.api.nvim_win_set_cursor(0, { cursor_row, cursor_col })
else
vim.cmd("normal! zz")
-- Move cursor to the middle line of the block and center the view
vim.cmd(string.format("normal! %dGzz", middle_line + 1))
end

utils.notify("Code block centered")
Expand Down
5 changes: 5 additions & 0 deletions lua/typewriter/utils/center_block_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ local M = {}
--- -- To disable centering for a specific node type:
--- center_block_config.expand["function"] = false
M.expand = {
["comment"] = true,
["function"] = true,
["body"] = true,
["method"] = true,
Expand Down Expand Up @@ -78,6 +79,10 @@ M.expand = {
["return_statement"] = true,
["throw_statement"] = true,
["await_expression"] = true,
["declaration_list"] = true,
["compound_statement"] = true,
["method_declaration"] = true,
["class_declaration"] = true,
}

--- Get the expansion status for a given node type
Expand Down
Loading