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

Support for simultaneous lean3/lean4 (in separate windows/tabs) #35

Closed
wants to merge 75 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
d7e0685
Implement lean3 vs. lean4 filetype detection; remove lean.vim depende…
rish987 Jun 1, 2021
1de6b3b
Fix infoview.
rish987 Jun 1, 2021
cd18592
Fix 4-second lean4 updatetime.
rish987 Jun 2, 2021
d6ea036
Implement multi-tab infoview.
rish987 Jun 2, 2021
70dca5d
Fix infoview toggle.
rish987 Jun 2, 2021
5976501
Index infoviews by windows rather than buffers.
rish987 Jun 2, 2021
23522d4
Update README and add infoview server startup grace period.
rish987 Jun 2, 2021
f15f610
Merge remote-tracking branch 'upstream/main' into main
rish987 Jun 2, 2021
c2db67d
More README notes and add all remaining lean.vim files.
rish987 Jun 2, 2021
f9ac13a
README formatting.
rish987 Jun 2, 2021
edeaa70
Bring syntax files up-to-date, duplicate plugin files from lean to le…
rish987 Jun 3, 2021
81ab591
Remove some lean3 switch mappings from lean4.
rish987 Jun 3, 2021
e6c4656
Update indent file for lean4.
rish987 Jun 3, 2021
50953f9
Remove lean4 executable path stuff from README.
rish987 Jun 3, 2021
7a90e25
Implement dynamically configurable per-tab/per-window option.
rish987 Jun 3, 2021
b9e0576
Slight refactor of infoview.lua.
rish987 Jun 3, 2021
d3ebca1
Fix default one_per_tab setting.
rish987 Jun 3, 2021
0a9a20f
Merge remote-tracking branch 'upstream/main' into main
rish987 Jun 3, 2021
bcef288
Fix some remaining instances of indexing solely by nvim_get_current_w…
rish987 Jun 3, 2021
126690c
Rename src_winnr -> src_idx (readability).
rish987 Jun 3, 2021
5d45699
Get seemingly working auto-infoview-close functionality, but throws e…
rish987 Jun 4, 2021
adf970d
Fix closing single source with multiple source windows in tab in per-…
rish987 Jun 4, 2021
d039630
Refactor autocmds.
rish987 Jun 4, 2021
333c84a
Remove debug print, minor formatting.
rish987 Jun 4, 2021
12e14bd
Fix E855 (per-tab mode error mentioned above) by making an autocmd di…
rish987 Jun 4, 2021
8ade70b
Put back <afile> expansion on WinClosed to avoid any timing issues.
rish987 Jun 5, 2021
5138c58
Remove more begin/ends from Lean 4.
Julian Jun 5, 2021
ef93fa1
pre-commit
Julian Jun 5, 2021
5d1e7a9
Make robust to manual infoview close by user.
rish987 Jun 5, 2021
27dda16
Relocate some refresh_infos() calls.
rish987 Jun 5, 2021
5e024be
Specify whether the infoview is already closed to close_win_wrapper().
rish987 Jun 5, 2021
ba3b226
Make 'lean' filetype refer to Lean 4 and 'lean3' refer to Lean 3.
rish987 Jun 5, 2021
fec293f
Make suggested keymappings buffer-local; make user commands for setti…
rish987 Jun 6, 2021
8e15a8f
Describe suggested mappings in README.
rish987 Jun 6, 2021
6eecae6
Fix abbreviations for lean3/lean4.
rish987 Jun 6, 2021
04d4f60
Revert snippets setting to previous format (accidentally overrode exi…
rish987 Jun 6, 2021
2836a3e
'Fix' compe abbreviations (not sure how to test this).
rish987 Jun 6, 2021
61ff2b6
Fix a minor typo in the README.
Julian Jun 6, 2021
6fca73c
Document the current mappings.
Julian Jun 6, 2021
ff94cd4
Remove the note on LeanPlainGoal.
Julian Jun 6, 2021
28b62d8
Fix automated tests.
rish987 Jun 6, 2021
3265d1b
Merge remote-tracking branch 'upstream/main' into main
rish987 Jun 6, 2021
0c29f2d
Pre-commit
rish987 Jun 6, 2021
bbf811a
Fix README typos.
rish987 Jun 6, 2021
1250a0a
Fix error message when closing infoview after closing source with CTR…
rish987 Jun 6, 2021
1689187
Fix autocmd use of '<buffer=0>'.
rish987 Jun 6, 2021
9f5b875
Add more details to README about new features.
rish987 Jun 6, 2021
22b873b
pre-commit
rish987 Jun 6, 2021
14fbe51
Fix bug where in per-tab mode, closing the 'original' window would ca…
rish987 Jun 6, 2021
7ec6249
Add source comments.
rish987 Jun 6, 2021
0e5d621
Support buffer-local suggested mapping application.
Julian Jun 7, 2021
37be218
Add back the infoview mode-toggling mappings.
Julian Jun 7, 2021
40666a2
Merge branch 'main' into rish
Julian Jun 7, 2021
8885da7
Merge branch 'main' into rish
Julian Jun 7, 2021
e25e07e
Minor readme revert.
Julian Jun 7, 2021
ae3f0bc
Remove some less-relevant notes with the infoview.
Julian Jun 7, 2021
c0b6c0e
Merge branch 'main' into rish
Julian Jun 7, 2021
01f7e35
Merge branch 'main' into rish
Julian Jun 8, 2021
7540117
Minor ternary style, which Lua people seem to like.
Julian Jun 8, 2021
beef5e4
Merge remote-tracking branch 'upstream/main' into main
rish987 Jun 10, 2021
24a4137
Revert a couple ternary refactors and minor close_win_wrapper() fix.
rish987 Jun 10, 2021
d48c9f8
For now, always erase infoviews.
Julian Jun 10, 2021
e73cc6d
Pull in some upstream changes.
Julian Jun 10, 2021
c63b1eb
Remove dead code.
Julian Jun 11, 2021
f97b0a0
Remove some sections from the README.
Julian Jun 11, 2021
1033ed2
Pull in the max_width change.
Julian Jun 11, 2021
0e3e2f6
Pull in the augroup helper
Julian Jun 11, 2021
01d6f99
Use is_lean3_project in _update to detect lean3 projects.
Julian Jun 11, 2021
e3b1063
Guard against lean3ls not existing yet until the PR is merged.
Julian Jun 11, 2021
8604975
Partially pull in teardown.
Julian Jun 11, 2021
ac92804
Remove the note about Lean3 being removed in the future.
Julian Jun 11, 2021
6cb1154
Use lspconfig.util to find leanpkg.toml
Julian Jun 11, 2021
58da940
Get rid of lean3.detect if we already have ft.detect.
Julian Jun 11, 2021
3cdad44
Properly detect leanpkg.toml relative to the lean file, not cwd.
Julian Jun 11, 2021
eb4cd45
Fix infoview closing.
rish987 Jun 11, 2021
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
72 changes: 40 additions & 32 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ Prerequisites
``lean.nvim`` currently requires `neovim 0.5 HEAD / nightly
<https://github.com/neovim/neovim/releases/tag/nightly>`_.

For syntax highlighting and basic language support, you should either:
NOTE: ``lean.nvim`` is incompatible with `lean.vim <https://github.com/leanprover/lean.vim>`_,
as it implements its own kind filetype detection.
You should NOT have ``lean.vim`` installed if using ``lean.nvim``.

* Install the normal `lean.vim <https://github.com/leanprover/lean.vim>`_.
For lean4 syntax highlighting and basic language support, you should
try the experimental support present via `tree-sitter-lean
<https://github.com/Julian/tree-sitter-lean>`_ by installing
`nvim-treesitter <https://github.com/nvim-treesitter/nvim-treesitter>`_

* or try the experimental support present via `tree-sitter-lean
<https://github.com/Julian/tree-sitter-lean>`_ by installing
`nvim-treesitter <https://github.com/nvim-treesitter/nvim-treesitter>`_

Note that many simple syntactical things are not yet implemented
(help is of course welcome), and that ``tree-sitter-lean`` is lean
4-only.
Note that many simple syntactical things are not yet implemented
(help is of course welcome), and that ``tree-sitter-lean`` is lean
4-only.

Installation
------------
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove line 37 here suggesting installing lean.vim I suppose

Expand Down Expand Up @@ -88,12 +89,32 @@ Configuration & Usage
In e.g. your ``init.lua``:

.. code-block:: lua
on_attach = function(client, bufnr)
-- See https://github.com/neovim/nvim-lspconfig#keybindings-and-completion
-- for detailed examples of what you may want to do here.
--
-- Mapping a key (typically K) to `vim.lsp.buf.hover()`
-- is highly recommended for Lean, since the hover LSP command
-- is where you'll see the current goal state.
--
-- You may furthermore want to add an `autocmd` to run it on
-- `CursorHoldI`, which will show the goal state any time the
-- cursor is unmoved in insert mode.
--
-- In the future, this plugin may offer a recommended "complete
-- setup" for easy enabling of the above.
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', {noremap = true})
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', {noremap = true})
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
end

require('lean').setup{
-- Enable abbreviation support?
--
-- false to disable, otherwise a table of options described below
abbreviations = {,
abbreviations = {
extra = {
-- Add a \wknight abbreviation to insert ♘
--
Expand All @@ -102,7 +123,7 @@ In e.g. your ``init.lua``:
-- this if so desired.
wknight = '♘',
},
}
},
-- Enable suggested mappings?
--
-- false by default, true to enable
Expand All @@ -112,35 +133,22 @@ In e.g. your ``init.lua``:
-- Clip the infoview to a maximum width
max_width = 79,
},
-- Enable the Lean language server?
-- Enable the Lean3/Lean4 language servers?
--
-- false to disable, otherwise should be a table of options to pass to
-- `leanls`. See https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#leanls
-- for details though lean-language-server actually doesn't support all
-- the options mentioned there yet.
lsp = {
on_attach = function(client, bufnr)
-- See https://github.com/neovim/nvim-lspconfig#keybindings-and-completion
-- for detailed examples of what you may want to do here.
--
-- Mapping a key (typically K) to `vim.lsp.buf.hover()`
-- is highly recommended for Lean, since the hover LSP command
-- is where you'll see the current goal state.
--
-- You may furthermore want to add an `autocmd` to run it on
-- `CursorHoldI`, which will show the goal state any time the
-- cursor is unmoved in insert mode.
--
-- In the future, this plugin may offer a recommended "complete
-- setup" for easy enabling of the above.
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', {noremap = true})
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', {noremap = true})
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
end,
on_attach = on_attach,
cmd = {"lean-language-server", "--stdio", '--', "-M", "4096"},
}

-- replace the path below with a path to your Lean4 executable
lsp4 = {
on_attach = on_attach,
cmd = {"/path/to/lean4/executable/lean", "--server"},
}
}

If you're using an ``init.vim``-only configuration setup, simply surround the
Expand Down
2 changes: 1 addition & 1 deletion ftdetect/lean.vim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
autocmd BufRead,BufNewFile *.lean lua require'lean.lean3'.detect()
autocmd BufRead,BufNewFile *.lean lua require'lean.ft'.detect(); require'lean.lean3'.detect()
31 changes: 31 additions & 0 deletions ftplugin/lean/lean.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if exists('b:did_ftplugin')
finish
endif
let b:did_ftplugin = 1

set wildignore+=*.olean

setlocal iskeyword=@,48-57,_,-,!,#,$,%

setlocal comments=s0:/-,mb:\ ,ex:-/,:--
setlocal commentstring=/-\ %s\ -/

setlocal expandtab
setlocal shiftwidth=2
setlocal softtabstop=2

function! lean#dotted2path(fname)
return substitute(a:fname, '\.', '/', 'g') . '.lean'
endfunction
setlocal includeexpr=lean#dotted2path(v:fname)

setlocal matchpairs+=⟨:⟩

" Matchit support
if exists('loaded_matchit') && !exists('b:match_words')
let b:match_ignorecase = 0

let b:match_words =
\ '\<begin\>:\<end$' .
\ ',\<\%(namespace\|section\)\s\+\(.\{-}\)\>:\<end\s\+\1\>'
endif
2 changes: 2 additions & 0 deletions ftplugin/lean4/infoview.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" required for infoview
setlocal updatetime=100
39 changes: 39 additions & 0 deletions ftplugin/lean4/switch.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
" This possibly belongs in lean.vim or switch.vim itself but putting it here
" for now.

if !exists("g:loaded_switch")
finish
endif

let b:switch_definitions = [
\ g:switch_builtins.true_false,
\ ["#check", "#eval", "#reduce"],
\ ["tidy", "suggest", "hint", "linarith", "library_search"],
\ ["rw", "simp", "simp?"],
\ ["cases", "rcases", "obtain"],
\ ["norm_cast", "push_cast"],
\ ["inl", "inr"],
\ ["tt", "ff"],
\ ["=", "≠"],
\ ["∈", "∉"],
\ ["∪", "∩"],
\ ["⋃", "⋂"],
\ ["⊆", "⊂", "⊃", "⊇"],
\ ["Σ", "∑"],
\ ["∀", "∃"],
\ ["∧", "∨"],
\ ["⊔", "⊓"],
\ ["⊥", "⊤"],
\ ["⋀", "⋁"],
\ ["×", "→"],
\ ["0", "₀", "⁰"],
\ ["1", "₁", "¹"],
\ ["2", "₂", "²"],
\ ["3", "₃", "³"],
\ ["4", "₄", "⁴"],
\ ["5", "₅", "⁵"],
\ ["6", "₆", "⁶"],
\ ["7", "₇", "⁷"],
\ ["8", "₈", "⁸"],
\ ["9", "₉", "⁹"],
\ ]
6 changes: 6 additions & 0 deletions indent/lean.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if exists("b:did_indent")
finish
endif
let b:did_indent = 1

setlocal indentkeys+==end,==begin
21 changes: 21 additions & 0 deletions lua/lean/ft.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local util = require'lean.util'

-- Ideally this obviously would use a TOML parser but yeah choosing to
-- do nasty things and not add the dependency for now.
local _MARKER = '.*lean_version.*\".*lean4.*'

local M = {}

-- If a TOML file with a lean4 version string is found, use filetype "lean4".
-- Otherwise use "lean" (lean3).
function M.detect()
local toml_file = util.find_toml(vim.api.nvim_buf_get_name(0))
if not toml_file then vim.bo.ft = "lean" return end
local _, result = pcall(vim.fn.readfile, toml_file)
for _, line in ipairs(result) do
if line:match(_MARKER) then vim.bo.ft = "lean4" return end
end
vim.bo.ft = "lean"
end

return M
120 changes: 63 additions & 57 deletions lua/lean/infoview.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local lean3 = require('lean.lean3')

local M = {_infoview = nil, _opts = {}}
local M = {_infoviews = {}, _infoviews_open = {}, _opts = {}}
Copy link
Owner

@Julian Julian Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for still working through things here, but one question -- can you elaborate on your thinking for having separate _infoviews_open here, which I think relates to functionality that persists infoview windows after they're semi-closed, right?

What benefit is there in supporting that extra state / how do you see it being used?

I know there's a difference between :close and :quit for normal windows, but ISTM that given that the infoview windows are non-modifiable and basically easily repopulatable from the server response that there isn't a huge functional difference, but let me know if I'm missing something.

Copy link
Collaborator Author

@rish987 rish987 Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - it may be wise to refactor this to instead let _infoviews[key] have two fields, data: { win: ..., buf: ...} and open, and remove _infoviews_open - let me know if you'd like this in this PR.

If we didn't have a state indicating closure, I'm not sure how we would distinguish between an infoview simply not having been created yet (in which case _infoviews[key] = nil, and we would want to open it) and having been explicitly closed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, after the refactor above, we may be able to indicate this by setting _infoviews[key].data = nil when closed and _infoviews[key] = nil when erased - though doing this might be a little bit less readable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see where you might have been confused here - I moved the infoview opening code to the M.update() method because I thought it was necessary for this, but looking back it should be fine to move it back to M.open(), and add a config setting infoview_open_default to control whether new source windows automatically open it. In this case, I should be able to use whether or not _infoviews[key] = nil as an indicator of whether it is open. Let me know if you'd like this in this PR.

Copy link
Owner

@Julian Julian Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right something like that is I think exactly what's confusing me yeah. I think probably it's reasonable to do on top of what's here, yeah, since I'm still trying to wrap my head around things, so I suspect doing it that way will actually simplify things a bit and make them more similar to the original diff, so worth doing I suspect. Let me first just push a simple style commit but then think go for it! (EDIT: Done)

Copy link
Collaborator Author

@rish987 rish987 Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok actually here are my updated thoughts on this - it may be better just to do the initial refactor I mentioned above, because we're eventually going to have to add persistent state between open/close to infoviews anyways when we implement e.g. pinnable/pausable messages. At that point we would be forced to have a new state field indicating open/closed. Thoughts?

Copy link
Collaborator Author

@rish987 rish987 Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the refactor I've done of this anyways, if you're curious what it would have looked like: https://github.com/rish987/lean.nvim/tree/infoview_close_refactor

Copy link
Owner

@Julian Julian Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I follow what you mean when you're talking about persistent state yet -- I still don't understand why we don't always just want to completely get rid of an infoview when it's closed -- for pinnable messages it seems to me all you do is temporarily/permanently unhook calling update on a window, and then it will stay showing the contents it has. Maybe I'm missing something still though.

But I want to get these set of incremental changes merged from this PR before doing more functionality, unless the implementation simplifies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it more similar to VSCode, where you can close the webview without having your set of pinned messages deleted.

This will be one of the aspects of this that seems "necessary" to make this a practical tool. For example, say you're editing Lean and LaTeX side-by side. The infoview is useful for getting an idea of how to interpret the Lean code, but once you understand it you'll probably want to get rid of the infoview for more screen space as you work on your LaTeX code. Now if there's a detail that you missed, you'll want to open the infoview again to see your pinned messages without them having been deleted.

Again, the code we have now should be good for a merge, but I'm pretty certain we'll want to implement persistence in the future.

Copy link
Owner

@Julian Julian Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's take things step by step, if/when we get there we can make the implementation more complex, but for now I think we should stick to the simplest thing that solves the current desired functionality, which is:

all of which are in this PR (and which I'm trying to merge one by one essentially).

If after that we need a more complex implementation we can evolve into it.

EDIT: Other things that are here:

Current issues / items to check:

  • calling require'lean.infoview'.close() re-opens the window immediately (on update). The reason is because all the open logic is in update, instead of being done on WinNew or thereabouts I think.
  • support :close in addition to :quit
  • I don't follow the test changes (other than just increasing the latencies)
  • it'd be nice to somehow name infoview buffers by their associated lean buffers rather than putting buffer numbers in them to make them unique
  • does toggle() now toggle only the current lean window's infoview, or all?
  • switching from per-tab to per-window or vice versa doesn't perform the switch immediately until an update


local _INFOVIEW_BUF_NAME = 'lean://infoview'
local _DEFAULT_BUF_OPTIONS = {
Expand All @@ -24,8 +24,51 @@ local _SEVERITY = {
[4] = "hint",
}

function M.update(infoview_bufnr)
local _update = vim.b.lean3 and lean3.update_infoview or function(set_lines)
function M.update(src_winnr)
-- grace period for server startup (prevents initial handler error for lean3 files)
local succeeded, _ = vim.wait(5000, vim.lsp.buf.server_ready)
if not succeeded then return end

if M._infoviews_open[src_winnr] == false then
return
end

local infoview_bufnr
local infoview = M._infoviews[src_winnr]
if not infoview then
M._infoviews[src_winnr] = {}

infoview_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(infoview_bufnr, _INFOVIEW_BUF_NAME .. infoview_bufnr)
for name, value in pairs(_DEFAULT_BUF_OPTIONS) do
vim.api.nvim_buf_set_option(infoview_bufnr, name, value)
end

local current_window = vim.api.nvim_get_current_win()

vim.cmd "botright vsplit"
vim.cmd(string.format("buffer %d", infoview_bufnr))

local window = vim.api.nvim_get_current_win()

for name, value in pairs(_DEFAULT_WIN_OPTIONS) do
vim.api.nvim_win_set_option(window, name, value)
end
vim.api.nvim_set_current_win(current_window)

local max_width = M._opts.max_width or 79
if vim.api.nvim_win_get_width(window) > max_width then
vim.api.nvim_win_set_width(window, max_width)
end

M._infoviews[src_winnr].buf = infoview_bufnr
M._infoviews[src_winnr].win = window

else
infoview_bufnr = infoview.buf
end

local _update = vim.bo.ft == "lean" and lean3.update_infoview or function(set_lines)
local current_buffer = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
local params = vim.lsp.util.make_position_params()
Expand Down Expand Up @@ -73,73 +116,36 @@ end
function M.enable(opts)
M._opts = opts
vim.api.nvim_exec([[
augroup LeanInfoViewOpen
augroup LeanInfoViewUpdate
autocmd!
autocmd BufWinEnter *.lean lua require'lean.infoview'.ensure_open()
autocmd CursorHold *.lean lua require'lean.infoview'.update(vim.api.nvim_get_current_win())
autocmd CursorHoldI *.lean lua require'lean.infoview'.update(vim.api.nvim_get_current_win())
augroup END
]], false)
end

function M.is_open() return M._infoview ~= nil end

function M.ensure_open()
if M.is_open() then return M._infoview.bufnr end

local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, _INFOVIEW_BUF_NAME)
for name, value in pairs(_DEFAULT_BUF_OPTIONS) do
vim.api.nvim_buf_set_option(bufnr, name, value)
end

local current_window = vim.api.nvim_get_current_win()

vim.cmd "botright vsplit"
vim.cmd(string.format("buffer %d", bufnr))
function M.is_open() return M._infoviews_open[vim.api.nvim_get_current_win()] ~= false end

local window = vim.api.nvim_get_current_win()

for name, value in pairs(_DEFAULT_WIN_OPTIONS) do
vim.api.nvim_win_set_option(window, name, value)
end
vim.api.nvim_set_current_win(current_window)

local max_width = M._opts.max_width or 79
if vim.api.nvim_win_get_width(window) > max_width then
vim.api.nvim_win_set_width(window, max_width)
end

vim.api.nvim_exec(string.format([[
augroup LeanInfoViewUpdate
autocmd!
autocmd CursorHold *.lean lua require'lean.infoview'.update(%d)
autocmd CursorHoldI *.lean lua require'lean.infoview'.update(%d)
augroup END
]], bufnr, bufnr), false)

M._infoview = { bufnr = bufnr, window = window }
return M._infoview
function M.open()
M._infoviews_open[vim.api.nvim_get_current_win()] = true
return M._infoviews
end

M.open = M.ensure_open

function M.close()
if not M.is_open() then return end
local src_win = vim.api.nvim_get_current_win()

local infoview = M._infoview
M._infoview = nil

vim.api.nvim_exec([[
augroup LeanInfoViewOpen
autocmd!
augroup END
if M._infoviews[src_win].win then
vim.api.nvim_win_close(M._infoviews[src_win].win, true)
end

augroup LeanInfoViewUpdate
autocmd!
augroup END
]], false)
-- NOTE: it seems this isn't necessary since unlisted buffers are deleted automatically?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this I believe while seeing tests fail for different reasons and causing later tests to fail. But it's possible it indeed isn't necessary. If it doesn't seem to be, feel free to kill it.

--if M._infoviews[src_win].buf then
-- vim.api.nvim_buf_delete(M._infoviews[src_win].buf, { force = true })
--end

vim.api.nvim_win_close(infoview.window, true)
vim.api.nvim_buf_delete(infoview.bufnr, { force = true })
M._infoviews_open[src_win] = false
M._infoviews[src_win] = nil
end

function M.toggle()
Expand Down
11 changes: 7 additions & 4 deletions lua/lean/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ function lean.setup(opts)
local abbreviations = opts.abbreviations or {}
if abbreviations.enable ~= false then lean.abbreviations.enable(abbreviations) end

local infoview = opts.infoview or {}
if infoview.enable ~= false then require('lean.infoview').enable(infoview) end
local treesitter = opts.treesitter or {}
if treesitter.enable ~= false then require('lean.treesitter').enable(treesitter) end

local lsp = opts.lsp or {}
if lsp.enable ~= false then lean.lsp.enable(lsp) end

local treesitter = opts.treesitter or {}
if treesitter.enable ~= false then require('lean.treesitter').enable(treesitter) end
local lsp4 = opts.lsp4 or {}
if lsp4.enable ~= false then lean.lsp.enable4(lsp4) end

local infoview = opts.infoview or {}
if infoview.enable ~= false then require('lean.infoview').enable(infoview) end

if opts.mappings == true then lean.use_suggested_mappings() end
end
Expand Down
Loading