17
0
Files
lite-xl-plugins/gitdiff_highlight-gitdiff.lua
Scott Bradnick 2b02a4397d Accepting request 1033559 from home:sbradnick
- Adjusting gitdiff_highlight-{gitdiff,init}.lua to play nice with
  lite-xl v2.1 plugin scheme (produces red notification bar and
  disables plugin when lite-xl loads otherwise)

- Update to version git20221101.0971a7a:
  * Changed minimap to accomodate new API.
  * language_php: fix strings not getting terminated
  * minimap: check docview has minimap scroll
  * minimap: fixed small mistake
  * Minimap Rework with Scrollbar (#134)
  * language_meson: added meson_options.txt and fixed meson.build patter
  * indentguide: do not apply on CommandView and allow toggling it.
  * Map ruby syntax to Gemfile and Gemfile.lock (#132)
  * Add `language_erb` to README
  * Add language plugin for ERB files (#130)
  * Removed language_cpp, as it's in the core, and not in this repo at all, in this branch.

- Renaming nonicons-userdir.patch to nonicons-userdir.diff

OBS-URL: https://build.opensuse.org/request/show/1033559
OBS-URL: https://build.opensuse.org/package/show/editors/lite-xl-plugins?expand=0&rev=7
2022-11-04 15:15:45 +00:00

86 lines
1.8 KiB
Lua

local gitdiff = {}
-- liquidev is a genius
local function extract_hunks(input)
local hunks = {}
local current_hunk = {}
local function end_hunk(new_line)
if #current_hunk > 0 then
table.insert(hunks, current_hunk)
current_hunk = {new_line}
end
end
for line in input:gmatch("(.-)\n") do
if line:match("^@") then
end_hunk(line)
else
table.insert(current_hunk, line)
end
end
-- add the last hunk to the table
end_hunk("")
return hunks
end
-- this will only work on single-file diffs
function gitdiff.changed_lines(diff)
if diff == nil then return {} end
local changed_lines = {}
local hunks = extract_hunks(diff)
-- iterate over hunks
for _, hunk in pairs(hunks) do
local current_line
local hunk_start = hunk[1]:match("@@%s+-%d+,%d+%s++(%d-),%d+%s+@@")
hunk_start = tonumber(hunk_start)
if hunk_start == nil then -- mod
goto continue
end
current_line = hunk_start - 1
-- remove hunk header
hunk[1] = hunk[1]:gsub("@@%s+-%d+,%d+%s++%d+,%d+%s+@@", "")
for _, line in pairs(hunk) do
if line:match("^%s-%[%-.-%-]$") then
table.insert(changed_lines, {
line_number = current_line,
change_type = "deletion"
})
-- do not add to the current line
goto skip_line
end
if line:match("^%s-{%+.-+}$") then
table.insert(changed_lines, {
line_number = current_line,
change_type = "addition"
})
elseif line:match("{%+.-+}") or line:match("%[%-.-%-]") then
table.insert(changed_lines, {
line_number = current_line,
change_type = "modification"
})
end
current_line = current_line + 1
::skip_line::
end
::continue::
end
local indexed_changed_lines = {}
for _, line in pairs(changed_lines) do
indexed_changed_lines[line.line_number] = line.change_type
end
return indexed_changed_lines
end
return gitdiff