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

feat: handle leading whitespace #86

Merged
merged 9 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Example in a markdown (or quarto markdown) document `index.md`:

````
# Some markdown

Hello world

```python
Expand All @@ -39,8 +38,9 @@ We create a hidden buffer for a file `index.md.tmp.py`



import numpy as np
np.zeros(10)
import numpy as np
np.zeros(10)

````

This contains just the python code and blank lines for all other lines (this keeps line numbers the same, which comes straight from the trick that the quarto dev team uses for the vs code extension as well).
Expand Down Expand Up @@ -101,6 +101,9 @@ otter.setup{
write_to_disk = false,
},
strip_wrapping_quote_characters = { "'", '"', "`" },
-- Otter may not work the way you expect when entire code blocks are indented (eg. in Org files)
-- When true, otter handles these cases fully. This is a (minor) performance hit
handle_leading_whitespace = false,
}
```

Expand Down
2 changes: 2 additions & 0 deletions lua/otter/completion/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ source.complete = function(self, params, callback)
local otter_nrs = self.updater()
local win = vim.api.nvim_get_current_win()
local lsp_params = vim.lsp.util.make_position_params(win, self.client.offset_encoding)
lsp_params.position.character = lsp_params.position.character
- keeper.get_leading_offset(lsp_params.position.line, self.main_nr)
lsp_params.textDocument = {
uri = vim.uri_from_bufnr(self.otter_nr),
}
Expand Down
1 change: 1 addition & 0 deletions lua/otter/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local default_config = {
write_to_disk = false,
},
strip_wrapping_quote_characters = { "'", '"', "`" },
handle_leading_whitespace = false,
}

M.cfg = default_config
Expand Down
38 changes: 24 additions & 14 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ M.dev_setup = function()
vim.api.nvim_buf_set_keymap(0, "n", "<leader>lf", ":lua require'otter'.ask_format()<cr>", { silent = true })
end

--- Activate the current buffer by adding and syncronizing
--- Activate the current buffer by adding and synchronizing
--- otter buffers.
---@param languages table|nil
---@param completion boolean|nil
Expand Down Expand Up @@ -78,11 +78,15 @@ M.activate = function(languages, completion, diagnostics, tsquery)

-- create otter buffers
for _, lang in ipairs(languages) do
local ext = extensions[lang]
if ext == nil then
vim.notify("No extension found for language " .. lang, vim.log.levels.WARN)
else
local extension = "." .. ext
if not extensions[lang] then
vim.notify(
("[Otter] %s is an unknown language. Please open an issue/PR to get it added"):format(lang),
vim.log.levels.ERROR
)
goto continue
end
local extension = "." .. extensions[lang]
if extension ~= nil then
local otter_path = path_to_otterpath(main_path, extension)
local otter_uri = "file://" .. otter_path
local otter_nr = vim.uri_to_bufnr(otter_uri)
Expand Down Expand Up @@ -110,23 +114,22 @@ M.activate = function(languages, completion, diagnostics, tsquery)
group = api.nvim_create_augroup("OtterAutowrite" .. otter_nr, {}),
callback = function(_, _)
if api.nvim_buf_is_loaded(otter_nr) then
api.nvim_buf_call(otter_nr,
function()
vim.cmd("write! " .. otter_path)
end
)
api.nvim_buf_call(otter_nr, function()
vim.cmd("write! " .. otter_path)
end)
end
end,
})
else
api.nvim_buf_set_option(otter_nr, "buftype", "nowrite")
end
end
::continue::
end

keeper.sync_raft(main_nr)

-- manually attach language server the corresponds to the fileytype
-- manually attach language server the 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 Down Expand Up @@ -159,9 +162,16 @@ M.activate = function(languages, completion, diagnostics, tsquery)
local sync_diagnostics = function(_, _)
M.sync_raft(main_nr)
for bufnr, ns in pairs(nss) do
local diag = vim.diagnostic.get(bufnr)
local diags = vim.diagnostic.get(bufnr)
vim.diagnostic.reset(ns, main_nr)
vim.diagnostic.set(ns, main_nr, diag, {})
if config.cfg.handle_leading_whitespace then
for _, diag in ipairs(diags) do
local offset = keeper.get_leading_offset(diag.lnum, main_nr)
diag.col = diag.col + offset
diag.end_col = diag.end_col + offset
end
end
vim.diagnostic.set(ns, main_nr, diags, {})
end
end

Expand Down
Loading
Loading