diff options
-rw-r--r-- | lua/conform/formatters/injected.lua | 5 | ||||
-rw-r--r-- | lua/conform/fs.lua | 20 | ||||
-rw-r--r-- | tests/injected/simple.md | 2 | ||||
-rw-r--r-- | tests/injected/simple.md.formatted | 2 | ||||
-rw-r--r-- | tests/injected_spec.lua | 13 |
5 files changed, 11 insertions, 31 deletions
diff --git a/lua/conform/formatters/injected.lua b/lua/conform/formatters/injected.lua index 363889e..fd67439 100644 --- a/lua/conform/formatters/injected.lua +++ b/lua/conform/formatters/injected.lua @@ -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 diff --git a/lua/conform/fs.lua b/lua/conform/fs.lua index c33a2dc..d303dbd 100644 --- a/lua/conform/fs.lua +++ b/lua/conform/fs.lua @@ -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 diff --git a/tests/injected/simple.md b/tests/injected/simple.md index 5fd4b3a..c9685d7 100644 --- a/tests/injected/simple.md +++ b/tests/injected/simple.md @@ -1,5 +1,5 @@ text ```lua -local foo = 'bar' +local foo = "bar" ``` diff --git a/tests/injected/simple.md.formatted b/tests/injected/simple.md.formatted index 20800b4..4f68718 100644 --- a/tests/injected/simple.md.formatted +++ b/tests/injected/simple.md.formatted @@ -1,5 +1,5 @@ text ```lua -|local foo = 'bar'| +|local foo = "bar"| ``` diff --git a/tests/injected_spec.lua b/tests/injected_spec.lua index 1f57e7f..f6c7175 100644 --- a/tests/injected_spec.lua +++ b/tests/injected_spec.lua @@ -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") @@ -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) @@ -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 @@ -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 |