Skip to content

Commit

Permalink
fix: lsps not updating with new contents (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbuhr authored Jul 4, 2024
1 parent e878798 commit 3638522
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 24 deletions.
1 change: 1 addition & 0 deletions lua/otter/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local default_config = {
},
strip_wrapping_quote_characters = { "'", '"', "`" },
handle_leading_whitespace = true,
debug = false,
}

M.cfg = default_config
Expand Down
45 changes: 40 additions & 5 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ M.activate = function(languages, completion, diagnostics, tsquery)
::continue::
end

-- this has to happen again after the
-- otter buffers got their own lsps
-- to really make sure the clients are
-- attached to their otter buffers
keeper.sync_raft(main_nr)

-- manually attach language server the corresponds to the filetype
-- manually attach language server that corresponds to the filetype
-- without setting the filetype
-- to prevent other plugins we don't need in the otter buffers
-- from automatically attaching when ft is set
Expand All @@ -133,7 +137,6 @@ M.activate = function(languages, completion, diagnostics, tsquery)
if config.cfg.buffers.write_to_disk then
-- and also write out once before lsps can complain
local otter_path = keeper.rafts[main_nr].paths[lang]
vim.print(otter_path)
api.nvim_buf_call(otter_nr, function()
vim.cmd("write! " .. otter_path)
end)
Expand All @@ -150,6 +153,10 @@ M.activate = function(languages, completion, diagnostics, tsquery)
end
end

-- see above.
-- needs to happen here again
keeper.sync_raft(main_nr)

if diagnostics then
require("otter.diagnostics").setup(main_nr)
end
Expand All @@ -175,14 +182,42 @@ M.activate = function(languages, completion, diagnostics, tsquery)

-- remove the need to use keybindings for otter ask_ functions
-- by being our own lsp server-client combo
local client_id = otterls.start(main_nr, completion)
if client_id == nil then
local otterclient_id = otterls.start(main_nr, completion)
if otterclient_id == nil then
vim.notify_once("[otter] activation of otter-ls failed", vim.log.levels.WARN, {})
end

keeper.rafts[main_nr].otterls.client_id = client_id
keeper.rafts[main_nr].otterls.client_id = otterclient_id

-- debugging
if require("otter.config").cfg.debug == true then
-- listen to lsp requests and notifications
vim.api.nvim_create_autocmd("LspNotify", {
callback = function(args)
local bufnr = args.buf
local client_id = args.data.client_id
local method = args.data.method
local params = args.data.params
vim.print(bufnr .. "[" .. client_id .. "]" .. ": " .. method)
if method == "textDocument/didChange" then
-- vim.print(params)
end
end,
})

vim.api.nvim_create_autocmd("LspRequest", {
callback = function(args)
local bufnr = args.buf
local client_id = args.data.client_id
local request_id = args.data.request_id
local request = args.data.request
vim.print(bufnr .. ": " .. request.method)
end,
})
end
end


---Deactivate the current buffer by removing otter buffers and clearing diagnostics
---@param completion boolean | nil
---@param diagnostics boolean | nil
Expand Down
3 changes: 2 additions & 1 deletion lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ keeper.sync_raft = function(main_nr, language)

-- replace language lines
api.nvim_buf_set_lines(otter_nr, 0, -1, false, ls)
else -- no code chunks so we wipe the otter buffer
else
-- no code chunks so we wipe the otter buffer
api.nvim_buf_set_lines(otter_nr, 0, -1, false, {})
end
end
Expand Down
26 changes: 13 additions & 13 deletions lua/otter/lsp/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,18 @@ M[ms.textDocument_declaration] = function(err, response, ctx, conf)
vim.lsp.handlers[ms.textDocument_declaration](err, response, ctx, conf)
end

M[ms.textDocument_completion] = function(err, response, ctx, conf)
-- this handler doesn't actually get called
-- the magic happened before where we modified the request
-- I assume nvim-cmp and nvims omnifunc handle the response directly
vim.lsp.handlers[ms.textDocument_completion](err, response, ctx, conf)
end

M[ms.completionItem_resolve] = function(err, response, ctx, conf)
-- this handler doesn't actually get called
-- the magic happened before where we modified the request
-- I assume nvim-cmp and nvims omnifunc handle the response directly
vim.lsp.handlers[ms.completionItem_resolve](err, response, ctx, conf)
end
-- M[ms.textDocument_completion] = function(err, response, ctx, conf)
-- -- this handler doesn't actually get called
-- -- the magic happened before where we modified the request
-- -- I assume nvim-cmp and nvims omnifunc handle the response directly
-- vim.lsp.handlers[ms.textDocument_completion](err, response, ctx, conf)
-- end
--
-- M[ms.completionItem_resolve] = function(err, response, ctx, conf)
-- -- this handler doesn't actually get called
-- -- the magic happened before where we modified the request
-- -- I assume nvim-cmp and nvims omnifunc handle the response directly
-- vim.lsp.handlers[ms.completionItem_resolve](err, response, ctx, conf)
-- end

return M
28 changes: 24 additions & 4 deletions lua/otter/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ local keeper = require("otter.keeper")
local ms = vim.lsp.protocol.Methods
local fn = require("otter.tools.functions")

local capabilities = vim.lsp.protocol.make_client_capabilities()



local otterls = {}

--- @param main_nr integer main buffer
Expand All @@ -13,7 +17,7 @@ otterls.start = function(main_nr, completion)
local main_uri = vim.uri_from_bufnr(main_nr)
local client_id = vim.lsp.start({
name = "otter-ls" .. "[" .. main_nr .. "]",
capabilities = vim.lsp.protocol.make_client_capabilities(),
capabilities = capabilities,
handlers = handlers,
cmd = function(dispatchers)
local members = {
Expand Down Expand Up @@ -58,6 +62,11 @@ otterls.start = function(main_nr, completion)
referencesProvider = true,
documentSymbolProvider = true,
completionProvider = completion_options,
textDocumentSync = {
-- we don't do anything with this, yet
openClose = true,
change = 2, -- 0 none; -- 1 = full; 2 = incremental
},
},
serverInfo = {
name = "otter-ls",
Expand Down Expand Up @@ -103,11 +112,16 @@ otterls.start = function(main_nr, completion)
return
end

-- update the otter buffer of that language
keeper.sync_raft(main_nr, lang)
local otter_nr = keeper.rafts[main_nr].buffers[lang]
local otter_uri = vim.uri_from_bufnr(otter_nr)

-- update the otter buffer of that language
local success = keeper.sync_raft(main_nr, lang)
if not success then
-- no otter buffer for lang
return
end

-- general modifications to params for all methods
params.textDocument = {
uri = otter_uri,
Expand All @@ -131,7 +145,13 @@ otterls.start = function(main_nr, completion)
-- our handler
vim.lsp.buf_request(otter_nr, method, params, handler)
end,
notify = function(method, params) end,
notify = function(method, params)
-- we don't actually notify otter buffers
-- they get their notifications
-- via nvim's clients attached to
-- the buffers
-- when we change their text
end,
is_closing = function() end,
terminate = function() end,
}
Expand Down
8 changes: 8 additions & 0 deletions lua/otter/tools/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ M.concat = function(ls)
return s .. "\n"
end

M.unlines = function(ls)
local s = ""
for _, l in ipairs(ls) do
s = s .. "\n" .. l
end
return s .. "\n"
end

M.spaces = function(n)
local s = {}
for i = 1, n do
Expand Down
8 changes: 8 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local buf = 1

local ls = {
'import math'
}

vim.api.nvim_buf_set_lines(buf, 2, -1, false, ls)

3 changes: 3 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sys

import math
8 changes: 7 additions & 1 deletion tests/examples/01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ console.log('hello world')
</html>

<script>
consol
console
</script>


Expand All @@ -53,3 +53,9 @@ def hello():
```{python}
hello()
```


```{python}
hello()
import math
```
19 changes: 19 additions & 0 deletions tests/examples/01b.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Hello


```{python}
import sys
```

```{python}
x = 1
y = "hello"
print("hello world")
y.lower()
```

26 changes: 26 additions & 0 deletions tests/examples/03b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Hello


```python
import os
import math
```

```python
print('hello world')
```


```python
def hello(s: str):
""""hello world"""
print(f"hello {s}")
```

```python
hello()

import math

```

8 changes: 8 additions & 0 deletions tests/examples/03c.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hello



```r
print('hello world')
```

0 comments on commit 3638522

Please sign in to comment.