Skip to content

Commit

Permalink
adding line_group
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicDuck committed Dec 14, 2024
1 parent 3394fa6 commit 8ffef30
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
1 change: 0 additions & 1 deletion lua/grug-far/actions/applyChange.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local sync = require('grug-far.actions.sync')
local openLocation = require('grug-far.actions.openLocation')
local resultsList = require('grug-far.render.resultsList')
-- TODO (sbadragan): update README.md

--- gets adjacent location
---@param buf integer
Expand Down
11 changes: 11 additions & 0 deletions lua/grug-far/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ M.ResultHighlightType = {
DiffSeparator = 7,
}

---@enum ResultLineGroup
M.ResultLineGroup = {
MatchLines = 1,
ReplacementLines = 2,
ContextLines = 3,
DiffSeparator = 4,
FilePath = 5,
}

M.DiffSeparatorChars = ' '

---@class ResultHighlight
Expand All @@ -30,6 +39,8 @@ M.DiffSeparatorChars = ' '
---@field end_line integer
---@field end_col integer
---@field sign? ResultHighlightSign
---@field line_group ResultLineGroup
---@field line_group_id integer

---@class ParsedResultsData
---@field lines string[]
Expand Down
49 changes: 46 additions & 3 deletions lua/grug-far/engine/astgrep/parseResults.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local utils = require('grug-far.utils')
local engine = require('grug-far.engine')
local ResultHighlightType = engine.ResultHighlightType
local ResultLineGroup = engine.ResultLineGroup

local M = {}

Expand All @@ -24,6 +25,12 @@ local HighlightByType = {
[ResultHighlightType.DiffSeparator] = 'Normal',
}

local last_line_group_id = 0
local function get_next_line_group_id()
last_line_group_id = last_line_group_id + 1
return last_line_group_id
end

---@class AstgrepMatchPos
---@field line integer
---@field column integer
Expand Down Expand Up @@ -54,16 +61,19 @@ local HighlightByType = {
---@param range AstgrepMatchRange
---@param lines string[] lines table to add to
---@param highlights ResultHighlight[] highlights table to add to
---@param line_group ResultLineGroup
---@param lineNumberSign? ResultHighlightSign
---@param matchHighlightType? ResultHighlightType
local function addResultLines(
resultLines,
range,
lines,
highlights,
line_group,
lineNumberSign,
matchHighlightType
)
local line_group_id = get_next_line_group_id()
local numlines = #lines
for j, resultLine in ipairs(resultLines) do
local current_line = numlines + j - 1
Expand All @@ -73,6 +83,8 @@ local function addResultLines(
local prefix = string.format('%-7s', line_no .. (col_no and ':' .. col_no .. ':' or '-'))

table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = ResultHighlightType.LineNumber,
hl = HighlightByType[ResultHighlightType.LineNumber],
start_line = current_line,
Expand All @@ -83,6 +95,8 @@ local function addResultLines(
})
if col_no then
table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = ResultHighlightType.ColumnNumber,
hl = HighlightByType[ResultHighlightType.ColumnNumber],
start_line = current_line,
Expand All @@ -95,6 +109,8 @@ local function addResultLines(
resultLine = prefix .. resultLine
if matchHighlightType then
table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = matchHighlightType,
hl = HighlightByType[matchHighlightType],
start_line = current_line,
Expand Down Expand Up @@ -147,6 +163,8 @@ function M.parseResults(matches)
if isFileBoundary then
stats.files = stats.files + 1
table.insert(highlights, {
line_group = ResultLineGroup.FilePath,
line_group_id = get_next_line_group_id(),
hl_type = ResultHighlightType.FilePath,
hl = HighlightByType[ResultHighlightType.FilePath],
start_line = #lines,
Expand All @@ -170,15 +188,30 @@ function M.parseResults(matches)
local leadingRange = vim.deepcopy(match.range)
leadingRange.start.column = nil
leadingRange.start.line = match.range.start.line - #leadingLines
addResultLines(leadingLines, leadingRange, lines, highlights, change_sign)
addResultLines(
leadingLines,
leadingRange,
lines,
highlights,
ResultLineGroup.ContextLines,
change_sign
)
end

-- add match lines
local lineNumberSign = match.replacement and removed_sign or change_sign
local matchHighlightType = match.replacement and ResultHighlightType.MatchRemoved
or ResultHighlightType.Match
local matchLines = vim.split(matchLinesStr, '\n')
addResultLines(matchLines, match.range, lines, highlights, lineNumberSign, matchHighlightType)
addResultLines(
matchLines,
match.range,
lines,
highlights,
ResultLineGroup.MatchLines,
lineNumberSign,
matchHighlightType
)

-- add replacements lines
if match.replacement then
Expand All @@ -196,6 +229,7 @@ function M.parseResults(matches)
replaceRange,
lines,
highlights,
ResultLineGroup.ReplacementLines,
added_sign,
ResultHighlightType.MatchAdded
)
Expand All @@ -207,7 +241,14 @@ function M.parseResults(matches)
local trailingRange = vim.deepcopy(match.range)
trailingRange.start.column = nil
trailingRange.start.line = match.range['end'].line + 1
addResultLines(trailingLines, trailingRange, lines, highlights, change_sign)
addResultLines(
trailingLines,
trailingRange,
lines,
highlights,
ResultLineGroup.ContextLines,
change_sign
)
end

-- add separator
Expand All @@ -217,6 +258,8 @@ function M.parseResults(matches)
and match.file == matches[i + 1].file
then
table.insert(highlights, {
line_group = ResultLineGroup.DiffSeparator,
line_group_id = get_next_line_group_id(),
hl_type = ResultHighlightType.DiffSeparator,
hl = HighlightByType[ResultHighlightType.DiffSeparator],
start_line = #lines,
Expand Down
33 changes: 31 additions & 2 deletions lua/grug-far/engine/ripgrep/parseResults.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local utils = require('grug-far.utils')
local engine = require('grug-far.engine')
local ResultHighlightType = engine.ResultHighlightType
local ResultLineGroup = engine.ResultLineGroup

local M = {}

Expand All @@ -24,6 +25,12 @@ local HighlightByType = {
[ResultHighlightType.DiffSeparator] = 'Normal',
}

local last_line_group_id = 0
local function get_next_line_group_id()
last_line_group_id = last_line_group_id + 1
return last_line_group_id
end

---@class RipgrepJsonSubmatch
---@field match {text: string}
---@field replacement {text: string}
Expand Down Expand Up @@ -64,16 +71,19 @@ local HighlightByType = {
---@param ranges { start: { column: integer?, line: integer}, end: {column: integer?, line: integer}}[]
---@param lines string[] lines table to add to
---@param highlights ResultHighlight[] highlights table to add to
---@param line_group ResultLineGroup
---@param lineNumberSign? ResultHighlightSign
---@param matchHighlightType? ResultHighlightType
local function addResultLines(
resultLines,
ranges,
lines,
highlights,
line_group,
lineNumberSign,
matchHighlightType
)
local line_group_id = get_next_line_group_id()
local numlines = #lines
local first_range = ranges[1]
for j, resultLine in ipairs(resultLines) do
Expand All @@ -84,6 +94,8 @@ local function addResultLines(
local prefix = line_no .. (col_no and ':' .. col_no .. ':' or '-')

table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = ResultHighlightType.LineNumber,
hl = HighlightByType[ResultHighlightType.LineNumber],
start_line = current_line,
Expand All @@ -94,6 +106,8 @@ local function addResultLines(
})
if col_no then
table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = ResultHighlightType.ColumnNumber,
hl = HighlightByType[ResultHighlightType.ColumnNumber],
start_line = current_line,
Expand All @@ -108,6 +122,8 @@ local function addResultLines(
for _, range in ipairs(ranges) do
if range.start.line <= current_line_number and range['end'].line >= current_line_number then
table.insert(highlights, {
line_group = line_group,
line_group_id = line_group_id,
hl_type = matchHighlightType,
hl = HighlightByType[matchHighlightType],
start_line = current_line,
Expand Down Expand Up @@ -150,6 +166,8 @@ function M.parseResults(matches, isSearchWithReplace, showDiff)
and last_line_number < data.line_number - 1
then
table.insert(highlights, {
line_group = ResultLineGroup.DiffSeparator,
line_group_id = get_next_line_group_id(),
hl_type = ResultHighlightType.DiffSeparator,
hl = HighlightByType[ResultHighlightType.DiffSeparator],
start_line = #lines,
Expand All @@ -165,6 +183,8 @@ function M.parseResults(matches, isSearchWithReplace, showDiff)
if match.type == 'begin' then
stats.files = stats.files + 1
table.insert(highlights, {
line_group = ResultLineGroup.FilePath,
line_group_id = get_next_line_group_id(),
hl_type = ResultHighlightType.FilePath,
hl = HighlightByType[ResultHighlightType.FilePath],
start_line = #lines,
Expand Down Expand Up @@ -208,7 +228,15 @@ function M.parseResults(matches, isSearchWithReplace, showDiff)
end)
:totable()

addResultLines(match_lines, ranges, lines, highlights, lineNumberSign, matchHighlightType)
addResultLines(
match_lines,
ranges,
lines,
highlights,
ResultLineGroup.MatchLines,
lineNumberSign,
matchHighlightType
)
end

-- add replacement lines
Expand Down Expand Up @@ -255,6 +283,7 @@ function M.parseResults(matches, isSearchWithReplace, showDiff)
ranges,
lines,
highlights,
ResultLineGroup.ReplacementLines,
lineNumberSign,
matchHighlightType
)
Expand All @@ -275,7 +304,7 @@ function M.parseResults(matches, isSearchWithReplace, showDiff)
column = nil,
},
},
}, lines, highlights, change_sign)
}, lines, highlights, ResultLineGroup.ContextLines, change_sign)
end
end

Expand Down
12 changes: 5 additions & 7 deletions lua/grug-far/render/resultsList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local opts = require('grug-far.opts')
local utils = require('grug-far.utils')
local treesitter = require('grug-far.render.treesitter')
local ResultHighlightType = require('grug-far.engine').ResultHighlightType
local ResultLineGroup = require('grug-far.engine').ResultLineGroup

local M = {}

Expand Down Expand Up @@ -114,8 +115,6 @@ function M.appendResultsChunk(buf, context, data)
local resultLocationByExtmarkId = state.resultLocationByExtmarkId
---@type ResultLocation?
local lastLocation = nil
---@type ResultLocation?
local prevLastLocation = nil

for i = 1, #data.highlights do
local highlight = data.highlights[i]
Expand All @@ -134,17 +133,16 @@ function M.appendResultsChunk(buf, context, data)
local markId = addLocationMark(buf, context, lastline + highlight.start_line, #line, options)
resultLocationByExtmarkId[markId] = { filename = state.resultsLastFilename }
elseif hl_type == ResultHighlightType.LineNumber then
prevLastLocation = lastLocation
lastLocation = { filename = state.resultsLastFilename }
lastLocation.sign = highlight.sign
lastLocation.lnum = tonumber(string.sub(line, highlight.start_col + 1, highlight.end_col))
lastLocation.text = line

if
not (
prevLastLocation
and lastLocation.filename == prevLastLocation.filename
and lastLocation.lnum == prevLastLocation.lnum
highlight.line_group == ResultLineGroup.MatchLines
and not (
data.highlights[i - 1]
and data.highlights[i - 1].line_group_id == highlight.line_group_id
)
then
state.resultMatchLineCount = state.resultMatchLineCount + 1
Expand Down

0 comments on commit 8ffef30

Please sign in to comment.