summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorChristian Segundo2023-10-08 22:30:48 +0200
committerChristian Segundo2023-10-08 22:30:48 +0200
commit9ff53495505b8b70971d50ef05b2863a9ce27112 (patch)
tree2c121e4de865febbe809379c6ec0fc725c92125d /plugin
parenta4644e03b1aa7daf0f2d4c0474d04c3b3aabebbd (diff)
parentafa6b4b6195483d73cb6a91bc757652301275ed6 (diff)
downloadpromqlfmt-9ff53495505b8b70971d50ef05b2863a9ce27112.tar.gz
Merge branch 'ft/ci'
Diffstat (limited to 'plugin')
-rw-r--r--plugin/promql.lua50
1 files changed, 46 insertions, 4 deletions
diff --git a/plugin/promql.lua b/plugin/promql.lua
index e44d1c3..e3f1bb0 100644
--- a/plugin/promql.lua
+++ b/plugin/promql.lua
@@ -6,11 +6,14 @@
--)
-- How it's going
-if vim.g.promqlfmt == 1 then
+if vim.g.loaded_promqlfmt == 1 then
return
end
-vim.g.promqlfmt = 1
+vim.g.loaded_promqlfmt = 1
+
+-- remove once 0.10 is released
+local vim_is_old = vim.version().minor < 10 and vim.version().major == 0
--- Format a PromQL query using promtool
--- @param query string @query to format
@@ -22,6 +25,18 @@ local promql_format = function(query, padding, padchar)
padchar = padchar or " "
local success, result = pcall(function()
+ if vim_is_old then
+ return {
+ code = 0,
+ stdout = vim.fn.system({
+ "promtool",
+ "promql",
+ "format",
+ "--experimental",
+ query,
+ }),
+ }
+ end
return vim
.system(
{ "promtool", "promql", "format", "--experimental", query },
@@ -65,7 +80,25 @@ local buf_get_selected_text = function(bufnr)
end_line = nil,
end_row = nil,
}
- local region = vim.region(bufnr or 0, "'<", "'>", vim.fn.visualmode(), true)
+
+ local pos1, pos2 = "'<", "'>"
+ if vim_is_old then
+ local pos = vim.fn.getpos("'<")
+ pos1 = { pos[2] - 1, pos[3] - 1 + pos[4] }
+ pos = vim.fn.getpos("'>")
+ pos2 = { pos[2] - 1, pos[3] - 1 + pos[4] }
+
+ if pos1[1] > pos2[1] or (pos1[1] == pos2[1] and pos1[2] > pos2[2]) then
+ pos1, pos2 = pos2, pos1
+ end
+
+ -- getpos() may return {0,0,0,0}
+ if pos1[1] < 0 or pos1[2] < 0 then
+ return {}
+ end
+ end
+
+ local region = vim.region(bufnr or 0, pos1, pos2, vim.fn.visualmode(), true)
local maxcol = vim.v.maxcol
for line, cols in vim.spairs(region) do
if r.start_line == nil then
@@ -82,7 +115,16 @@ local buf_get_selected_text = function(bufnr)
r.end_row = endcol
end
- r.end_row = r.end_row > last_col_line(0, r.end_line) and -1 or r.end_row
+ -- 0.9 does not support negative end_row
+ if vim_is_old then
+ local last_row = last_col_line(0, r.end_line)
+ if r.end_row == -1 or r.end_row > last_row then
+ r.end_row = last_row
+ end
+ else
+ r.end_row = r.end_row > last_col_line(0, r.end_line) and -1 or r.end_row
+ end
+
return r
end