Skip to content

Commit

Permalink
fix(injected): code block at end of markdown file
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 26, 2023
1 parent f245cca commit 9245b61
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 31 deletions.
5 changes: 4 additions & 1 deletion lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ return {
local errors = require("conform.errors")
local log = require("conform.log")
local util = require("conform.util")
local text = table.concat(lines, "\n")
-- Need to add a trailing newline; some parsers need this.
-- For example, if a markdown code block ends at the end of the file, a trailing newline is
-- required otherwise the ``` will be grabbed as part of the injected block
local text = table.concat(lines, "\n") .. "\n"
local buf_lang = vim.treesitter.language.get_lang(vim.bo[ctx.buf].filetype)
local ok, parser = pcall(vim.treesitter.get_string_parser, text, buf_lang)
if not ok then
Expand Down
20 changes: 0 additions & 20 deletions lua/conform/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,4 @@ M.join = function(...)
return table.concat({ ... }, M.sep)
end

---@param filepath string
---@return boolean
M.exists = function(filepath)
local stat = uv.fs_stat(filepath)
return stat ~= nil and stat.type ~= nil
end

---@param filepath string
---@return string?
M.read_file = function(filepath)
if not M.exists(filepath) then
return nil
end
local fd = assert(uv.fs_open(filepath, "r", 420)) -- 0644
local stat = assert(uv.fs_fstat(fd))
local content = uv.fs_read(fd, stat.size)
uv.fs_close(fd)
return content
end

return M
2 changes: 1 addition & 1 deletion tests/injected/simple.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
text

```lua
local foo = 'bar'
local foo = "bar"
```
2 changes: 1 addition & 1 deletion tests/injected/simple.md.formatted
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
text

```lua
|local foo = 'bar'|
|local foo = "bar"|
```
13 changes: 5 additions & 8 deletions tests/injected_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require("plenary.async").tests.add_to_env()
local conform = require("conform")
local fs = require("conform.fs")
local injected = require("conform.formatters.injected")
local runner = require("conform.runner")
local test_util = require("tests.test_util")
Expand Down Expand Up @@ -49,7 +48,7 @@ describe("injected formatter", function()
elseif i == #lines and line == "" then
-- Simulate formatters removing trailing newline
else
table.insert(ret, "|" .. line:gsub("%s+", " ") .. "|")
table.insert(ret, "|" .. line .. "|")
end
end
callback(nil, ret)
Expand All @@ -65,11 +64,9 @@ describe("injected formatter", function()
local filepath = "./tests/injected/" .. filename
local formatted_file = filepath .. ".formatted"
it(filename, function()
local content = fs.read_file(filepath)
assert(content)
local lines = vim.split(content, "\n", { plain = true })
local bufnr = vim.fn.bufadd(filepath)
vim.fn.bufload(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
local config = assert(conform.get_formatter_config("injected", bufnr))
local ctx = runner.build_context(bufnr, config)
local err, new_lines, done
Expand All @@ -82,9 +79,9 @@ describe("injected formatter", function()
return done
end)
assert(err == nil, err)
local expected = fs.read_file(formatted_file)
assert(expected)
local expected_lines = vim.split(expected, "\n", { plain = true })
local expected_bufnr = vim.fn.bufadd(formatted_file)
vim.fn.bufload(expected_bufnr)
local expected_lines = vim.api.nvim_buf_get_lines(expected_bufnr, 0, -1, true)
assert.are.same(expected_lines, new_lines)
end)
end
Expand Down

0 comments on commit 9245b61

Please sign in to comment.