Skip to content

tools/editDiff

The text work of the edit tool: line endings, fuzzy matching, applying several replacements against one original, and the two diff renderings of the result (a numbered display diff and a unified patch).

Since v0.0.0


Signature

export interface AppliedEdits {
readonly baseContent: string
readonly newContent: string
}

Source

Since v0.0.0

A run of lines that both sides share, or that one side removed or added.

Signature

export interface DiffPart {
readonly value: string
readonly count: number
readonly added: boolean
readonly removed: boolean
}

Source

Since v0.0.0

One replacement.

Signature

export interface Edit {
readonly oldText: string
readonly newText: string
}

Source

Since v0.0.0

Signature

export interface FuzzyMatchResult {
readonly found: boolean
readonly index: number
readonly matchLength: number
/** Whether fuzzy matching was used (false: an exact match). */
readonly usedFuzzyMatch: boolean
/** The content the offsets refer to: the original for an exact match, the normalized content for a fuzzy one. */
readonly contentForReplacement: string
}

Source

Since v0.0.0

Apply one or more replacements to LF-normalized content. Every edit is matched against the same original; when one needs fuzzy matching, all of them match in fuzzy-normalized space and only the touched lines take normalized text.

Signature

declare const applyEditsToNormalizedContent: (
normalizedContent: string,
edits: ReadonlyArray<Edit>,
path: string
) => Effect.Effect<AppliedEdits, CodingToolError>

Source

Since v0.0.0

Apply replacements matched against baseContent (a normalized view of originalContent) to the original: each replacement widens to the lines it touches, those lines are rewritten from the base, and every other line keeps its original bytes.

Signature

declare const applyReplacementsPreservingUnchangedLines: (
originalContent: string,
baseContent: string,
replacements: ReadonlyArray<TextReplacement>
) => string

Source

Since v0.0.0

Signature

declare const detectLineEnding: (content: string) => "\r\n" | "\n"

Source

Since v0.0.0

Diff two texts by lines.

Signature

declare const diffLines: (oldText: string, newText: string) => Array<DiffPart>

Source

Since v0.0.0

Find oldText exactly, else in fuzzy-normalized space.

Signature

declare const fuzzyFindText: (content: string, oldText: string) => FuzzyMatchResult

Source

Since v0.0.0

A display diff with line numbers and contextLines of context around each change, and the first changed line of the new file.

Signature

declare const generateDiffString: (
oldContent: string,
newContent: string,
contextLines?: number
) => { readonly diff: string; readonly firstChangedLine: number | undefined }

Source

Since v0.0.0

A standard unified patch with file headers only (--- path, +++ path) and contextLines of context.

Signature

declare const generateUnifiedPatch: (
path: string,
oldContent: string,
newContent: string,
contextLines?: number
) => string

Source

Since v0.0.0

Normalize for fuzzy matching: NFKC, trailing whitespace stripped per line, smart quotes, Unicode dashes and special spaces replaced by their ASCII forms.

Signature

declare const normalizeForFuzzyMatch: (text: string) => string

Source

Since v0.0.0

Signature

declare const normalizeToLF: (text: string) => string

Source

Since v0.0.0

Signature

declare const restoreLineEndings: (text: string, ending: "\r\n" | "\n") => string

Source

Since v0.0.0

Strip a UTF-8 BOM, returning it and the rest.

Signature

declare const stripBom: (content: string) => { readonly bom: string; readonly text: string }

Source

Since v0.0.0