Skip to content

Commit

Permalink
feat: show range in the debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
rintoj committed Jun 14, 2024
1 parent 6ddd013 commit cda3d3d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/diff/calculate-edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ describe('calculateEdit', () => {
function trim(text: string) {
return text
.split('\n')
.map(i => i.replace(/^ /g, ''))
.map(i => i.replace(/^ /g, ''))
.join('\n')
}
6 changes: 3 additions & 3 deletions src/diff/calculate-edit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import minimumEditDistance from 'minimum-edit-distance'
import minEditDistance from 'minimum-edit-distance'
import { EditAction, EditActionType, createDelete, createInsert, createReplace } from './actions'
import { printDebugInfo } from './print-debug'
import { Token, splitTokens } from './token'

const { diff, reconstruct } = minimumEditDistance
const { diff } = minEditDistance

function parseTrace(traces: string[], originalToken: Token[]) {
let index = originalToken.length
Expand Down Expand Up @@ -55,5 +55,5 @@ export function applyEdits(content: string, actions: EditAction[]) {
break
}
}
return tokens.map(t => t.text).join('\n')
return tokens.map(t => t.text.split('\n')[0]).join('\n')
}
40 changes: 32 additions & 8 deletions src/diff/print-debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { green, red, yellow } from 'chalk'
import { EditAction, EditActionType } from './actions'
import { Token } from './token'
import { Position, Token } from './token'

function colorize(text: string, actionType?: EditActionType) {
switch (actionType) {
Expand Down Expand Up @@ -28,15 +28,34 @@ function colorizeHeader(text: string, actionType?: EditActionType) {

function toSeparator(showNewLine?: boolean) {
if (!showNewLine) return ''
return '\n'.padEnd(toLine(1, '').length + 1)
return '__SEPARATOR__'.padEnd(toLine(1, '').length + 1)
}

function toIndex(index: number) {
return `${index}`.padStart(3)
}

function toLine(index: number, text: string, actionType?: EditActionType) {
return `${toIndex(index)}: ${text}`
function toText(text: string) {
return text
.split('\n')
.map(i => i)
.join('⏎')
.split('__SEPARATOR__')
.join('\n'.padEnd(13))
}

function toPosition(position?: Position) {
if (!position) return ''.padStart(6)
return `${position.line}:${position.character}`
}

function toRange(token?: Token) {
if (!token) return ''.padStart(12)
return `${toPosition(token.range.start)}-${toPosition(token.range.end)}`.padStart(12)
}

function toLine(index: number, text: string, token?: Token) {
return `${toRange(token)}${toIndex(index)}: ${toText(text)}`
}

function toLineText(
Expand Down Expand Up @@ -80,7 +99,7 @@ function toActions(actions: EditAction[]) {
.reverse()
.map(
action =>
`${colorizeHeader(action.type.toString().padStart(7), action.type)} => ${toIndex(action.token.index)}: ${action.token.text}`,
`${colorizeHeader(action.type.toString().padStart(7), action.type)} ${toRange(action.token)} => ${toIndex(action.token.index)}: ${toText(action.token.text)}`,
)
}

Expand All @@ -94,10 +113,15 @@ function toDivider(length = 80) {
export function printDebugInfo(actions: EditAction[], originalTokens: Token[]) {
const actionText = toActions(actions)
const divider = toDivider()
const changes = originalTokens.flatMap((l, i) =>
const changes = originalTokens.flatMap((token, index) =>
toLine(
i,
toLineText(l.text, insertActionsByIndex(actions, i), otherActionsByIndex(actions, i)),
index,
toLineText(
token.text,
insertActionsByIndex(actions, index),
otherActionsByIndex(actions, index),
),
token,
),
)
console.log([divider, ...actionText, divider, ...changes, divider].join('\n'))
Expand Down
4 changes: 2 additions & 2 deletions src/diff/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ export function splitTokens(content: string) {
let line = 0
let position = 0
// const tokens = [...(content.match(/\s+|\S+/g) ?? [content])]
const tokens = content.split('\n')
const tokens = content.split('\n').map(l => `${l}\n`)
return tokens.map((token, index) => {
const composed = new Token(
index,
new Range(new Position(line, position), new Position(line, position + token.length)),
token,
)
if (token === '\n') {
if (token.indexOf('\n') >= 0) {
line++
position = 0
} else {
Expand Down

0 comments on commit cda3d3d

Please sign in to comment.