Skip to content

Commit f314db8

Browse files
authored
Merge pull request #12 from nullromo/issue-5
Add `alignOffsetLines` option.
2 parents 16feef1 + 61c01d0 commit f314db8

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,21 @@ Go-Up provides the following functions that you can use.
8989
respectScrolloff = false,
9090
-- limit number of virtual lines. See options table
9191
goUpLimit = nil,
92+
-- number of offset lines to use when aligning
93+
alignOffsetLines = { top = 0, bottom = 0 },
9294
}
9395
```
9496

9597
### Options Table
9698

97-
| Option | Data Type | Default | Description |
98-
| ------------------ | --------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
99-
| `mapZZ` | boolean | `true` | Go-Up remaps `zz` by default. Set `mapZZ` to `false` to prevent this. |
100-
| `respectScrolloff` | boolean | `false` | Go-Up works best when `scrolloff` is set to `0`[<sup>[1]</sup>](https://github.com/nullromo/go-up.nvim/issues/3), so it does that by default. Set `respectScrolloff` to `true` to prevent Go-Up from modifying `scrolloff`. |
101-
| `respectSplitkeep` | boolean | `false` | Go-Up works best when `splitkeep` is set to `'topline'`[<sup>[2]</sup>](https://github.com/nullromo/go-up.nvim/issues/4), so it does that by default. Set `respectSplitkeep` to `true` to prevent Go-Up from modifying `splitkeep`. |
102-
| `goUpLimit` | nil or number or `'center'` | `nil` | By default, Go-Up will allow you to scroll up until line 1 of your buffer hits the bottom of your window. This can be limited to a number of lines, or set to `'center'` to only allow scrolling until line 1 is centered in the window. |
99+
| Option | Data Type | Default | Description |
100+
| ------------------------- | --------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
101+
| `mapZZ` | boolean | `true` | Go-Up remaps `zz` by default. Set `mapZZ` to `false` to prevent this. |
102+
| `respectScrolloff` | boolean | `false` | Go-Up works best when `scrolloff` is set to `0`[<sup>[ref]</sup>](https://github.com/nullromo/go-up.nvim/issues/3), so it does that by default. Set `respectScrolloff` to `true` to prevent Go-Up from modifying `scrolloff`. |
103+
| `respectSplitkeep` | boolean | `false` | Go-Up works best when `splitkeep` is set to `'topline'`[<sup>[ref]</sup>](https://github.com/nullromo/go-up.nvim/issues/4), so it does that by default. Set `respectSplitkeep` to `true` to prevent Go-Up from modifying `splitkeep`. |
104+
| `goUpLimit` | nil or number or `'center'` | `nil` | By default, Go-Up will allow you to scroll up until line 1 of your buffer hits the bottom of your window. This can be limited to a number of lines, or set to `'center'` to only allow scrolling until line 1 is centered in the window. |
105+
| `alignOffsetLines.top` | number | `0` | Some other plugins may insert virtual lines to display various GUI elements[<sup>[ref]</sup>](https://github.com/nullromo/go-up.nvim/issues/5). This option can help overcome the resulting incompatibility by modifying the align behavior. |
106+
| `alignOffsetLines.bottom` | number | `0` | See `alignOffsetLines.top`. |
103107

104108
## 📈 Other Tips
105109

lua/go-up/internals.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ M.alignTop = function()
6666
local windowID = vim.fn.win_getid()
6767
local windowScreenPosition = vim.fn.win_screenpos(windowID)[1]
6868
local firstLineScreenPosition = vim.fn.screenpos(windowID, 1, 1).row
69-
local offset = firstLineScreenPosition - windowScreenPosition
69+
local offset = firstLineScreenPosition
70+
- windowScreenPosition
71+
- options.opts.alignOffsetLines.top
7072
if offset <= 0 then
7173
return
7274
end
@@ -86,6 +88,7 @@ M.alignBottom = function()
8688
+ windowHeight
8789
- lastLineScreenPosition
8890
- 1
91+
- options.opts.alignOffsetLines.bottom
8992
if lastLineScreenPosition == 0 or offset <= 0 then
9093
return
9194
end
@@ -100,7 +103,10 @@ M.align = function()
100103
vim.fn.screenpos(windowID, lastLineNumber, 1).row
101104
local windowScreenPosition = vim.fn.win_screenpos(windowID)[1]
102105

103-
if firstLineScreenPosition > windowScreenPosition then
106+
if
107+
firstLineScreenPosition
108+
> windowScreenPosition + options.opts.alignOffsetLines.top
109+
then
104110
M.alignTop()
105111
elseif lastLineScreenPosition > 0 then
106112
M.alignBottom()

lua/go-up/options.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ options.defaultOptions = {
1515
respectScrolloff = false,
1616
-- limit number of virtual lines. See options table
1717
goUpLimit = nil,
18+
-- number of offset lines to use when aligning
19+
alignOffsetLines = { top = 0, bottom = 0 },
1820
}
1921

2022
options.validateOptions = function(opts)
@@ -35,6 +37,30 @@ options.validateOptions = function(opts)
3537
'goUpLimit must be nil, a number, or "center" for Go-Up.nvim'
3638
)
3739
end
40+
elseif key == 'alignOffsetLines' then
41+
for key2, value2 in pairs(value) do
42+
if key2 == 'top' then
43+
checkType(value2, 'opts.alignOffsetLines.top', 'number')
44+
if value2 < 0 then
45+
error(
46+
'opts.alignOffsetLines.top must be positive for Go-Up.nvim'
47+
)
48+
end
49+
elseif key2 == 'bottom' then
50+
checkType(value2, 'opts.alignOffsetLines.bottom', 'number')
51+
if value2 < 0 then
52+
error(
53+
'opts.alignOffsetLines.bottom must be positive for Go-Up.nvim'
54+
)
55+
end
56+
else
57+
error(
58+
'"opts.alignOffsetLines.'
59+
.. key2
60+
.. '" is not a valid option for Go-Up.nvim'
61+
)
62+
end
63+
end
3864
else
3965
error('"opts.' .. key .. '" is not a valid option for Go-Up.nvim')
4066
end

0 commit comments

Comments
 (0)