Skip to content

Commit

Permalink
feat(utils): preseve folds and extmarks
Browse files Browse the repository at this point in the history
This adapts some code from conform to apply text edits in diffs instead
of wiping the whole buffer. This will preserve folds and extmarks.
  • Loading branch information
mhartington committed Oct 1, 2024
1 parent 01d9761 commit f0998e2
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 164 deletions.
20 changes: 3 additions & 17 deletions .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: JohnnyMorganz/stylua-action@v1.1.2
- uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --check lua

luacheck:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@master

- uses: leafo/gh-actions-lua@v8.0.0
with:
luaVersion: 'luajit-2.1.0-beta3'

- uses: leafo/gh-actions-luarocks@v4.0.0

- name: build
run: |
luarocks install luacheck
- name: test
run: |
luacheck lua
- uses: lunarmodules/luacheck@v1.2.0
2 changes: 1 addition & 1 deletion lua/formatter/defaults/alejandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ return function()
return {
exe = "alejandra",
stdin = true,
args = {"--quiet"},
args = { "--quiet" },
}
end
8 changes: 4 additions & 4 deletions lua/formatter/defaults/biome.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ return function()
return {
exe = "biome",
args = {
"format",
"--stdin-file-path",
util.escape_path(util.get_current_buffer_file_path()),
"format",
"--stdin-file-path",
util.escape_path(util.get_current_buffer_file_path()),
},
stdin = true,
}
end
end
20 changes: 9 additions & 11 deletions lua/formatter/defaults/ktlint.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
local util = require "formatter.util"

return function()
return {
exe = "ktlint",
args = {
"--stdin",
"--format",
"--log-level=none"
},
stdin = true
}
return {
exe = "ktlint",
args = {
"--stdin",
"--format",
"--log-level=none",
},
stdin = true,
}
end
2 changes: 1 addition & 1 deletion lua/formatter/defaults/ocamlformat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return function()
"--enable-outside-detected-project",
"--name",
util.escape_path(util.get_current_buffer_file_name()),
"-"
"-",
},
stdin = true,
}
Expand Down
2 changes: 0 additions & 2 deletions lua/formatter/defaults/stylish_haskell.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local util = require "formatter.util"

return function()
return {
exe = "stylish-haskell",
Expand Down
29 changes: 14 additions & 15 deletions lua/formatter/filetypes/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ local M = {}
local util = require "formatter.util"

function M.clangformat()
return {
exe = "clang-format",
args = {"--assume-filename=.java"},
stdin = true
}
return {
exe = "clang-format",
args = { "--assume-filename=.java" },
stdin = true,
}
end

function M.ideaformat()
Expand All @@ -18,17 +18,16 @@ function M.ideaformat()
}
end


function M.google_java_format()
return {
exe = "google-java-format",
args = {
"--aosp",
util.escape_path(util.get_current_buffer_file_path()),
"--replace"
},
stdin = true
}
return {
exe = "google-java-format",
args = {
"--aosp",
util.escape_path(util.get_current_buffer_file_path()),
"--replace",
},
stdin = true,
}
end

return M
18 changes: 9 additions & 9 deletions lua/formatter/filetypes/kotlin.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local M = {}

function M.ktlint()
return {
exe = "ktlint",
args = {
"--stdin",
"--format",
"--log-level=none"
},
stdin = true
}
return {
exe = "ktlint",
args = {
"--stdin",
"--format",
"--log-level=none",
},
stdin = true,
}
end

return M
4 changes: 2 additions & 2 deletions lua/formatter/filetypes/liquidsoap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local M = {}
function M.liquidsoap_prettier()
return {
exe = "liquidsoap-prettier",
args = {"--write"},
stdin = false
args = { "--write" },
stdin = false,
}
end

Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/filetypes/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ end
function M.luaformat()
return {
exe = "lua-format",
args = {util.escape_path(util.get_current_buffer_file_path())},
stdin = true
args = { util.escape_path(util.get_current_buffer_file_path()) },
stdin = true,
}
end

Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/proto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function M.buf_format()
return {
exe = "buf format",
args = {
'-w',
"-w",
},
stdin = false,
}
Expand Down
18 changes: 14 additions & 4 deletions lua/formatter/filetypes/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ function M.autopep8()
end

function M.isort()
local util = require("formatter.util")
local util = require "formatter.util"
return {
exe = "isort",
args = { "-q", "--filename", util.escape_path(util.get_current_buffer_file_path()), "-" },
args = {
"-q",
"--filename",
util.escape_path(util.get_current_buffer_file_path()),
"-",
},
stdin = true,
}
end
Expand All @@ -33,10 +38,15 @@ function M.docformatter()
end

function M.black()
local util = require("formatter.util")
local util = require "formatter.util"
return {
exe = "black",
args = { "-q", "--stdin-filename", util.escape_path(util.get_current_buffer_file_name()), "-" },
args = {
"-q",
"--stdin-filename",
util.escape_path(util.get_current_buffer_file_name()),
"-",
},
stdin = true,
}
end
Expand Down
9 changes: 7 additions & 2 deletions lua/formatter/filetypes/toml.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
local M = {}

function M.taplo()
local util = require("formatter.util")
local util = require "formatter.util"
return {
exe = "taplo",
args = { "fmt", "--stdin-filepath", util.escape_path(util.get_current_buffer_file_path()),"-" },
args = {
"fmt",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"-",
},
stdin = true,
try_node_modules = true,
}
Expand Down
11 changes: 7 additions & 4 deletions lua/formatter/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ function M.start_task(configs, start_line, end_line, opts)
end

if opts.lock then
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr })
-- vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
end

name = current.name
Expand Down Expand Up @@ -179,7 +180,7 @@ function M.start_task(configs, start_line, end_line, opts)
if binpath then
try_node_modules_path = binpath
.. util.path_separator
.. vim.fn.getenv("PATH")
.. vim.fn.getenv "PATH"
else
try_node_modules_path = false
end
Expand Down Expand Up @@ -232,7 +233,8 @@ function M.start_task(configs, start_line, end_line, opts)
end

if opts.lock then
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
-- vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
end

if not util.is_same(input, output) then
Expand All @@ -247,7 +249,8 @@ function M.start_task(configs, start_line, end_line, opts)
)
return
end
util.set_lines(bufnr, start_line, end_line, output)
util.update_lines(bufnr, input, output)

util.restore_view_per_window(window_to_view)

if opts.write and bufnr == vim.api.nvim_get_current_buf() then
Expand Down
Loading

0 comments on commit f0998e2

Please sign in to comment.