Skip to content

Commit d8a735b

Browse files
committed
feat: add ignoreMatchingLines prop support
resolved #120
1 parent 538b3aa commit d8a735b

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

src/CodeDiff.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ interface Props {
2121
hideHeader?: boolean
2222
hideStat?: boolean
2323
theme?: 'light' | 'dark'
24+
// Give a pattern to ignore matching lines eg: '(time|token)' (**Only support side-by-side**)
25+
ignoreMatchingLines?: string
2426
}
2527
2628
const props = withDefaults(defineProps<Props>(), {
@@ -36,6 +38,7 @@ const props = withDefaults(defineProps<Props>(), {
3638
hideHeader: false,
3739
hideStat: false,
3840
theme: 'light',
41+
ignoreMatchingLines: undefined,
3942
})
4043
4144
const emits = defineEmits<{
@@ -68,7 +71,7 @@ const newString = computed(() => {
6871
const raw = computed(() =>
6972
isUnifiedViewer.value
7073
? createUnifiedDiff(oldString.value, newString.value, props.language, props.diffStyle, props.context)
71-
: createSplitDiff(oldString.value, newString.value, props.language, props.diffStyle, props.context),
74+
: createSplitDiff(oldString.value, newString.value, props.language, props.diffStyle, props.context, props.ignoreMatchingLines),
7275
)
7376
const diffChange = ref(raw.value)
7477
const isNotChanged = computed(() => diffChange.value.stat.additionsNum === 0 && diffChange.value.stat.deletionsNum === 0)
@@ -110,6 +113,10 @@ watch(() => props, () => {
110113
<slot name="stat" :stat="diffChange.stat">
111114
<span class="diff-stat-added">+{{ diffChange.stat.additionsNum }} additions</span>
112115
<span class="diff-stat-deleted">-{{ diffChange.stat.deletionsNum }} deletions</span>
116+
<span
117+
v-if="diffChange.stat.ignoreNum.additions + diffChange.stat.ignoreNum.deletions > 0"
118+
class="diff-stat-ignored"
119+
>±{{ diffChange.stat.ignoreNum.additions + diffChange.stat.ignoreNum.deletions }} lines</span>
113120
</slot>
114121
</span>
115122
</span>

src/style.scss

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@
3838
width: 50%;
3939
}
4040
.diff-stat {
41+
display: inline-flex;
42+
align-items: center;
43+
gap: 8px;
4144
.diff-stat-added {
4245
color: var(--color-diffstat-addition-bg);
4346
}
4447
.diff-stat-deleted {
45-
margin-left: 8px;
4648
color: var(--color-danger-emphasis);
4749
}
50+
.diff-stat-ignored {
51+
color: var(--color-fg-subtle);
52+
}
4853
}
4954
}
5055
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface UnifiedLineChange {
4242
export interface DiffStat {
4343
additionsNum: number
4444
deletionsNum: number
45+
ignoreNum: object
4546
}
4647

4748
export interface SplitViewerChange {

src/utils.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,28 @@ function getHighlightCode(language: string, code: string) {
135135
.replace(new RegExp(closeEntity, 'g'), '</span>')
136136
}
137137

138-
function calcDiffStat(changes: Change[]): DiffStat {
138+
function calcDiffStat(changes: Change[], ignoreRegex?: RegExp): DiffStat {
139139
const count = (s: string, c: string) => (s.match(new RegExp(c, 'g')) || []).length
140+
const ignoreCount = (lines: string[]) => lines.filter(line => ignoreRegex?.test(line)).length
140141

141142
let additionsNum = 0
142143
let deletionsNum = 0
144+
const ignoreNum = { additions: 0, deletions: 0 }
143145
for (const change of changes) {
144-
if (change.added)
145-
additionsNum += count(change.value.trim(), '\n') + 1
146-
147-
if (change.removed)
148-
deletionsNum += count(change.value.trim(), '\n') + 1
146+
if (change.added) {
147+
const ignoreLines = ignoreCount(change.value.trim().split('\n'))
148+
additionsNum += count(change.value.trim(), '\n') + 1 - ignoreLines
149+
ignoreNum.additions += ignoreLines
150+
continue
151+
}
152+
if (change.removed) {
153+
const ignoreLines = ignoreCount(change.value.trim().split('\n'))
154+
deletionsNum += count(change.value.trim(), '\n') + 1 - ignoreLines
155+
ignoreNum.deletions += ignoreLines
156+
continue
157+
}
149158
}
150-
return { additionsNum, deletionsNum }
159+
return { additionsNum, deletionsNum, ignoreNum }
151160
}
152161

153162
export function createSplitDiff(
@@ -156,10 +165,12 @@ export function createSplitDiff(
156165
language = 'plaintext',
157166
diffStyle = 'word',
158167
context = 10,
168+
ignoreMatchingLines?: string,
159169
): SplitViewerChange {
160170
const newEmptySplitDiff = (): DiffLine => ({ type: DiffType.EMPTY })
161171
const newSplitDiff = (type: DiffType, num: number, code: string): DiffLine => ({ type, num, code })
162172
const changes = diffLines(oldString, newString)
173+
const ignoreRegex = ignoreMatchingLines ? new RegExp(ignoreMatchingLines) : undefined
163174

164175
let delNum = 0
165176
let addNum = 0
@@ -169,7 +180,7 @@ export function createSplitDiff(
169180
const result: SplitViewerChange = {
170181
changes: rawChanges,
171182
collector: [],
172-
stat: calcDiffStat(changes),
183+
stat: calcDiffStat(changes, ignoreRegex),
173184
}
174185

175186
for (let i = 0; i < changes.length; i++) {
@@ -256,14 +267,17 @@ export function createSplitDiff(
256267

257268
const leftLine = curLines.length === nextLines.length ? renderWords(nextLine, curLine, diffStyle) : curLine
258269
const rightLine = curLines.length === nextLines.length ? renderWords(curLine, nextLine, diffStyle) : nextLine
270+
// 忽略匹配的行等价于相等
271+
const leftDiffType = ignoreRegex?.test(leftLine) ? DiffType.EQUAL : DiffType.DELETE
272+
const rightDiffType = ignoreRegex?.test(rightLine) ? DiffType.EQUAL : DiffType.ADD
259273

260274
const left
261275
= j < cur.count!
262-
? newSplitDiff(DiffType.DELETE, delNum, getHighlightCode(language, leftLine))
276+
? newSplitDiff(leftDiffType, delNum, getHighlightCode(language, leftLine))
263277
: newEmptySplitDiff()
264278
const right
265279
= j < next.count!
266-
? newSplitDiff(DiffType.ADD, addNum, getHighlightCode(language, rightLine))
280+
? newSplitDiff(rightDiffType, addNum, getHighlightCode(language, rightLine))
267281
: newEmptySplitDiff()
268282

269283
rawChanges.push({ left, right })

0 commit comments

Comments
 (0)