Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show match count only for first line in a match, and not for replacements #334

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 18 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 @@ -132,21 +133,31 @@ 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
-- omit ending ':'
state.resultMatchLineCount = state.resultMatchLineCount + 1
lastLocation = { filename = state.resultsLastFilename, count = state.resultMatchLineCount }
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
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
lastLocation.count = state.resultMatchLineCount
end

local markId = addLocationMark(
buf,
context,
lastline + highlight.start_line,
#line,
{ sign = highlight.sign, matchLineCount = state.resultMatchLineCount }
{ sign = highlight.sign, matchLineCount = lastLocation.count }
)
resultLocationByExtmarkId[markId] = lastLocation

lastLocation.sign = highlight.sign
lastLocation.lnum = tonumber(string.sub(line, highlight.start_col + 1, highlight.end_col))
lastLocation.text = line
if context.options.resultsHighlight and lastLocation.text then
addHighlightResult(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
16|
17| file2.ts
18|▒ 3:7: grug.walks(talks) [1]
19|▒ 3:7: walks(talks) [2]
19|▒ 3:7: walks(talks)
20|
21|~
22|~
Expand All @@ -43,7 +43,7 @@
16|00222222222222222222222222222222222222222222222222222222222222222222222222222222
17|00777777772222222222222222222222222222222222222222222222222222222222222222222222
18|88525222222222288888888882222222222222222222222222222222222222222222222222223333
19|11525222222222211111222222222222222222222222222222222222222222222222222222223333
19|11525222222222211111222222222222222222222222222222222222222222222222222222222222
20|00222222222222222222222222222222222222222222222222222222222222222222222222222222
21|00000000000000000000000000000000000000000000000000000000000000000000000000000000
22|00000000000000000000000000000000000000000000000000000000000000000000000000000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
16|
17| file2.ts
18|▒ 2:5: if (grug || talks) { [1]
19|▒ 3:5: grug.walks(talks) [2]
20|▒ 4:5: } [3]
21|▒ 5:5: [4]
22|▒ 6:5: [5]
19|▒ 3:5: grug.walks(talks)
20|▒ 4:5: }
21|▒ 5:5:
22|▒ 6:5:
23|Grug FAR - 1: $A 4,0-1 Top
24|grug-far: added current search to history! 3,1

Expand All @@ -43,9 +43,9 @@
16|00222222222222222222222222222222222222222222222222222222222222222222222222222222
17|00555555552222222222222222222222222222222222222222222222222222222222222222222222
18|66727222222226666666666666666666622222222222222222222222222222222222222222223333
19|66727222266666666666666666666666222222222222222222222222222222222222222222223333
20|66727222266666222222222222222222222222222222222222222222222222222222222222223333
21|66727222266662222222222222222222222222222222222222222222222222222222222222223333
22|66727222222222222222222222222222222222222222222222222222222222222222222222223333
19|66727222266666666666666666666666222222222222222222222222222222222222222222222222
20|66727222266666222222222222222222222222222222222222222222222222222222222222222222
21|66727222266662222222222222222222222222222222222222222222222222222222222222222222
22|66727222222222222222222222222222222222222222222222222222222222222222222222222222
23|88888888888888888888888888888888888888888888888888888888888888888888888888888888
24|99999999999999999999999999999999999999999999999999999999999999999999999999999999
Loading
Loading