Skip to content

Commit

Permalink
Merge pull request #17 from joshuadanpeterson/dev
Browse files Browse the repository at this point in the history
Preserve Column Position When Moving Between Lines of Different Lengths
  • Loading branch information
joshuadanpeterson authored Jul 29, 2024
2 parents 34e444b + 6b89bca commit 9c27157
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Changelog

## [v0.6.5](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.6.5) (2024-07-29)
- feat(autocommands): Preserve column position when moving between lines of different lengths
- docs: update CHANGELOG.md for v0.6.4 and remove duplicate entries

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

## [v0.6.4](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.6.4) (2024-07-21)
- docs: update CHANGELOG.md for v0.6.3 and remove duplicate entries

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

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

[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.6.2...v0.6.3)
[Full Changelog](https://github.com/joshuadanpeterson/typewriter.nvim/compare/v0.6.4...v0.6.3)

## [v0.6.2](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.6.2) (2024-07-21)
- Merge branch 'main' into dev
Expand Down
36 changes: 36 additions & 0 deletions lua/typewriter/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ function M.autocmd_setup()
commands.move_to_bottom_of_block()
end, { desc = "Move the bottom of the current code block to the bottom of the screen" })

-- Preserve column position when moving between lines of different lengths
local target_col = nil
local last_line = nil

vim.api.nvim_create_autocmd("CursorMoved", {
pattern = "*",
callback = function()
local current_line, current_col = unpack(vim.api.nvim_win_get_cursor(0))
local line_length = vim.fn.col('$') - 1 -- Get the actual length of the current line

-- If we've moved to a new line
if last_line ~= current_line then
-- If target_col is not set, use the current column
if target_col == nil then
target_col = current_col
end

-- If the current line is shorter than target_col, move to the end of the line
if line_length < target_col then
vim.api.nvim_win_set_cursor(0, { current_line, line_length })
-- If the current line is long enough, move to the target column
elseif current_col ~= target_col then
vim.api.nvim_win_set_cursor(0, { current_line, target_col })
end
else
-- If we're on the same line, update the target column
target_col = current_col
end

last_line = current_line

-- Center the cursor
commands.center_cursor()
end
})

-- Autocommands for ZenMode integration
if config.config.enable_with_zen_mode then
vim.api.nvim_create_autocmd("User", {
Expand Down

0 comments on commit 9c27157

Please sign in to comment.