Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbuhr committed Jul 4, 2024
1 parent 722efe1 commit 8565ffb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 79 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
73 changes: 30 additions & 43 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ local keeper = require("otter.keeper")
local otterls = require("otter.lsp")
local path_to_otterpath = require("otter.tools.functions").path_to_otterpath
local config = require("otter.config")
local ms = vim.lsp.protocol.Methods

M.setup = function(opts)
config.cfg = vim.tbl_deep_extend("force", config.cfg, opts or {})
Expand Down Expand Up @@ -60,7 +59,6 @@ M.activate = function(languages, completion, diagnostics, tsquery)
keeper.rafts[main_nr].code_chunks = nil
keeper.rafts[main_nr].last_changetick = nil
keeper.rafts[main_nr].otterls = {}
keeper.rafts[main_nr].ottercontent = {}

local all_code_chunks = keeper.extract_code_chunks(main_nr)
local found_languages = {}
Expand Down Expand Up @@ -123,9 +121,7 @@ M.activate = function(languages, completion, diagnostics, tsquery)
::continue::
end

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 @@ -151,6 +147,11 @@ M.activate = function(languages, completion, diagnostics, tsquery)
end
end

-- this has to happen after the
-- otter buffers got their own
-- lsps
keeper.sync_raft(main_nr)

if diagnostics then
require("otter.diagnostics").setup(main_nr)
end
Expand Down Expand Up @@ -184,47 +185,33 @@ M.activate = function(languages, completion, diagnostics, tsquery)
keeper.rafts[main_nr].otterls.client_id = otterclient_id

-- debugging
-- 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

M.debug = function ()
-- get clients attached to otter buffers
-- keeper.rafts[main_nr].buffers[lang] = otter_nr
local main_nr = api.nvim_get_current_buf()
for lang, nr in pairs(keeper.rafts[main_nr].buffers) do
vim.print(nr)
local otterclients = vim.lsp.get_clients({bufnr = nr})
local otterclient = otterclients[1]
-- vim.print(otterclient.name)
-- vim.print(otterclient.capabilities)
-- vim.print(otterclient.server_capabilities)
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

vim.keymap.set("n", "<leader>oe", "<cmd>lua require('otter').debug()<CR>", { noremap = true, silent = true })

---Deactivate the current buffer by removing otter buffers and clearing diagnostics
---@param completion boolean | nil
Expand Down
10 changes: 3 additions & 7 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ local cfg = require("otter.config").cfg
---keeper.rafts[main_nr].parser = ts.get_parser(main_nr, parsername)
---keeper.rafts[main_nr].code_chunks = nil
---keeper.rafts[main_nr].last_changetick = nil
-- keeper.rafts[main_nr].ottercontent = {}
keeper.rafts = {}

--- table of languages that can be injected
Expand Down Expand Up @@ -351,16 +350,15 @@ end
---@param main_nr integer bufnr of the parent buffer
---@param language string|nil only sync one otter buffer matching a language
---@return boolean success true on success, otherwise false
---@return boolean has_updated true if the raft had to be updated
keeper.sync_raft = function(main_nr, language)
if not keeper.has_raft(main_nr) then
return false, false
return false
end
local all_code_chunks
local changetick = api.nvim_buf_get_changedtick(main_nr)
if keeper.rafts[main_nr].last_changetick == changetick then
all_code_chunks = keeper.rafts[main_nr].code_chunks
return true, false
return true
else
all_code_chunks = keeper.extract_code_chunks(main_nr)
end
Expand Down Expand Up @@ -396,15 +394,13 @@ keeper.sync_raft = function(main_nr, language)

-- replace language lines
api.nvim_buf_set_lines(otter_nr, 0, -1, false, ls)
keeper.rafts[main_nr].ottercontent[lang] = ls
else
-- no code chunks so we wipe the otter buffer
api.nvim_buf_set_lines(otter_nr, 0, -1, false, {})
keeper.rafts[main_nr].ottercontent[lang] = {}
end
end
end
return true, true
return true
end

--- Export the raft of otters as files.
Expand Down
33 changes: 5 additions & 28 deletions lua/otter/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,11 @@ otterls.start = function(main_nr, completion)
local otter_uri = vim.uri_from_bufnr(otter_nr)

-- update the otter buffer of that language
local success, has_updated = keeper.sync_raft(main_nr, lang)
local success = keeper.sync_raft(main_nr, lang)
if not success then
-- no otter buffer for lang
return
end
-- if has_updated then
-- -- send lsp notification with the changes
-- -- to the otter buffer
-- local ls = keeper.rafts[main_nr].ottercontent[lang]
-- local text = fn.unlines(ls)
-- vim.lsp.buf_notify(otter_nr, ms.textDocument_didChange, {
-- textDocument = {
-- uri = otter_uri,
-- version = 1,
-- },
-- contentChanges = {
-- {
-- text = text,
-- },
-- },
-- })
-- end

-- general modifications to params for all methods
params.textDocument = {
Expand All @@ -163,17 +146,11 @@ otterls.start = function(main_nr, completion)
vim.lsp.buf_request(otter_nr, method, params, handler)
end,
notify = function(method, params)

-- if method == ms.textDocument_didOpen then
-- vim.print(params)
-- end

-- vim.print(method)
-- vim.print(params)
-- we don't actually notify otter buffers
-- we send them their own notifications
-- when we change their buffer during a request

-- 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
4 changes: 3 additions & 1 deletion tests/examples/01b.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


```{python}
import math
import sys
Expand Down
1 change: 1 addition & 0 deletions tests/examples/03b.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

```python
import os
import math
```

```python
Expand Down

0 comments on commit 8565ffb

Please sign in to comment.