@pierre/diffs 1.1.0-beta.2 → 1.1.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts
CHANGED
|
@@ -90,5 +90,6 @@ import { renderDiffWithHighlighter } from "./utils/renderDiffWithHighlighter.js"
|
|
|
90
90
|
import { renderFileWithHighlighter } from "./utils/renderFileWithHighlighter.js";
|
|
91
91
|
import { setLanguageOverride } from "./utils/setLanguageOverride.js";
|
|
92
92
|
import { setPreNodeProperties } from "./utils/setWrapperNodeProps.js";
|
|
93
|
+
import { trimPatchContext } from "./utils/trimPatchContext.js";
|
|
93
94
|
import { codeToHtml, createCssVariablesTheme as createCSSVariablesTheme } from "shiki";
|
|
94
|
-
export { ALTERNATE_FILE_NAMES_GIT, AnnotationLineMap, AnnotationSide, AnnotationSpan, AttachedLanguages, AttachedThemes, BaseCodeOptions, BaseDiffOptions, BundledLanguage, COMMIT_METADATA_SPLIT, CORE_CSS_ATTRIBUTE, ChangeContent, ChangeTypes, CodeToHastOptions, CodeToTokenTransformStream, CodeToTokenTransformStreamOptions, ContextContent, CreateFileHeaderElementProps, DEFAULT_EXPANDED_REGION, DEFAULT_THEMES, DIFFS_TAG_NAME, DIFF_HEADER_HEIGHT, DecorationItem, DiffHunksRenderer, DiffLineAnnotation, DiffLineEventBaseProps, DiffsHighlighter, DiffsThemeNames, EXTENSION_TO_FILE_FORMAT, ExpansionDirections, ExtensionFormatMap, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT, FILE_CONTEXT_BLOB, FILE_GAP, File, FileContents, FileDiff, FileDiffHydrationProps, FileDiffMetadata, FileDiffOptions, FileDiffRenderProps, FileHyrdateProps, FileOptions, FileRenderProps, FileRenderResult, FileRenderer, FileRendererOptions, FileStream, FileStreamOptions, ForcePlainTextOptions, GIT_DIFF_FILE_BREAK_REGEX, GapSpan, GetHoveredLineResult, HEADER_METADATA_SLOT_ID, HUNK_HEADER, HUNK_SEPARATOR_HEIGHT, Hunk, HunkData, HunkExpansionRegion, HunkLineType, HunkSeparators, HunksRenderResult, INDEX_LINE_METADATA, LINE_HEIGHT, LINE_HUNK_COUNT, LanguageRegistration, LineAnnotation, LineDiffTypes, LineEventBaseProps, LineInfo, LineSelectionManager, LineSelectionOptions, LineSpans, LineTypes, LogTypes, MouseEventManager, MouseEventManagerBaseOptions, MouseEventManagerMode, MouseEventManagerOptions, ObservedAnnotationNodes, ObservedGridNodes, OnDiffLineClickProps, OnDiffLineEnterLeaveProps, OnLineClickProps, OnLineEnterLeaveProps, ParsedLine, ParsedPatch, PrePropertiesConfig, RecallToken, RegisteredCustomThemes, RenderDiffFilesResult, RenderDiffOptions, RenderDiffResult, RenderFileMetadata, RenderFileOptions, RenderFileResult, RenderHeaderMetadataCallback, RenderHeaderMetadataProps, RenderRange, RenderWindow, RenderedDiffASTCache, RenderedFileASTCache, ResizeManager, ResolvedLanguages, ResolvedThemes, ResolvingLanguages, ResolvingThemes, SPLIT_WITH_NEWLINES, SVGSpriteNames, SVGSpriteSheet, ScrollSyncManager, SelectedLineRange, SelectionSide, SharedRenderState, ShikiStreamTokenizer, ShikiStreamTokenizerEnqueueResult, ShikiStreamTokenizerOptions, ShikiTransformer, SupportedLanguages, ThemeRegistrationResolved, ThemeTypes, ThemedDiffResult, ThemedFileResult, ThemedToken, ThemesType, UNIFIED_DIFF_FILE_BREAK_REGEX, UNSAFE_CSS_ATTRIBUTE, VirtualWindowSpecs, areDiffLineAnnotationsEqual, areFilesEqual, areHunkDataEqual, areLanguagesAttached, areLineAnnotationsEqual, areObjectsEqual, areOptionsEqual, arePrePropertiesEqual, areRenderRangesEqual, areSelectionsEqual, areThemesAttached, areThemesEqual, areVirtualWindowSpecsEqual, areWorkerStatsEqual, attachResolvedLanguages, attachResolvedThemes, cleanLastNewline, cleanUpResolvedLanguages, cleanUpResolvedThemes, codeToHtml, createAnnotationElement, createAnnotationWrapperNode, createBufferElement, createCSSVariablesTheme, createDiffSpanDecoration, createEmptyRowBuffer, createFileHeaderElement, createHastElement, createHoverContentNode, createIconElement, createNoNewlineElement, createPreElement, createPreWrapperProperties, createRowNodes, createSeparator, createSpanFromToken, createStyleElement, createTextNodeElement, createTransformerWithState, createUnsafeCSSStyleNode, createWindowFromScrollPosition, diffAcceptRejectHunk, disposeHighlighter, extendFileFormatMap, findCodeElement, formatCSSVariablePrefix, getFiletypeFromFileName, getHighlighterIfLoaded, getHighlighterOptions, getHighlighterThemeStyles, getHunkSeparatorSlotName, getIconForType, getLineAnnotationName, getLineEndingType, getLineNodes, getOrCreateCodeNode, getResolvedLanguages, getResolvedOrResolveLanguage, getResolvedOrResolveTheme, getResolvedThemes, getSharedHighlighter, getSingularPatch, getThemes, getTotalLineCountFromHunks, hasResolvedLanguages, hasResolvedThemes, isDefaultRenderRange, isHighlighterLoaded, isHighlighterLoading, isHighlighterNull, isWorkerContext, parseDiffFromFile, parseLineType, parsePatchFiles, pluckLineSelectionOptions, pluckMouseEventOptions, preloadHighlighter, prerenderHTMLIfNecessary, processFile, processLine, processPatch, pushOrJoinSpan, queueRender, registerCustomCSSVariableTheme, registerCustomTheme, renderDiffWithHighlighter, renderFileWithHighlighter, resolveLanguage, resolveLanguages, resolveTheme, resolveThemes, setLanguageOverride, setPreNodeProperties, wrapCoreCSS, wrapUnsafeCSS };
|
|
95
|
+
export { ALTERNATE_FILE_NAMES_GIT, AnnotationLineMap, AnnotationSide, AnnotationSpan, AttachedLanguages, AttachedThemes, BaseCodeOptions, BaseDiffOptions, BundledLanguage, COMMIT_METADATA_SPLIT, CORE_CSS_ATTRIBUTE, ChangeContent, ChangeTypes, CodeToHastOptions, CodeToTokenTransformStream, CodeToTokenTransformStreamOptions, ContextContent, CreateFileHeaderElementProps, DEFAULT_EXPANDED_REGION, DEFAULT_THEMES, DIFFS_TAG_NAME, DIFF_HEADER_HEIGHT, DecorationItem, DiffHunksRenderer, DiffLineAnnotation, DiffLineEventBaseProps, DiffsHighlighter, DiffsThemeNames, EXTENSION_TO_FILE_FORMAT, ExpansionDirections, ExtensionFormatMap, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT, FILE_CONTEXT_BLOB, FILE_GAP, File, FileContents, FileDiff, FileDiffHydrationProps, FileDiffMetadata, FileDiffOptions, FileDiffRenderProps, FileHyrdateProps, FileOptions, FileRenderProps, FileRenderResult, FileRenderer, FileRendererOptions, FileStream, FileStreamOptions, ForcePlainTextOptions, GIT_DIFF_FILE_BREAK_REGEX, GapSpan, GetHoveredLineResult, HEADER_METADATA_SLOT_ID, HUNK_HEADER, HUNK_SEPARATOR_HEIGHT, Hunk, HunkData, HunkExpansionRegion, HunkLineType, HunkSeparators, HunksRenderResult, INDEX_LINE_METADATA, LINE_HEIGHT, LINE_HUNK_COUNT, LanguageRegistration, LineAnnotation, LineDiffTypes, LineEventBaseProps, LineInfo, LineSelectionManager, LineSelectionOptions, LineSpans, LineTypes, LogTypes, MouseEventManager, MouseEventManagerBaseOptions, MouseEventManagerMode, MouseEventManagerOptions, ObservedAnnotationNodes, ObservedGridNodes, OnDiffLineClickProps, OnDiffLineEnterLeaveProps, OnLineClickProps, OnLineEnterLeaveProps, ParsedLine, ParsedPatch, PrePropertiesConfig, RecallToken, RegisteredCustomThemes, RenderDiffFilesResult, RenderDiffOptions, RenderDiffResult, RenderFileMetadata, RenderFileOptions, RenderFileResult, RenderHeaderMetadataCallback, RenderHeaderMetadataProps, RenderRange, RenderWindow, RenderedDiffASTCache, RenderedFileASTCache, ResizeManager, ResolvedLanguages, ResolvedThemes, ResolvingLanguages, ResolvingThemes, SPLIT_WITH_NEWLINES, SVGSpriteNames, SVGSpriteSheet, ScrollSyncManager, SelectedLineRange, SelectionSide, SharedRenderState, ShikiStreamTokenizer, ShikiStreamTokenizerEnqueueResult, ShikiStreamTokenizerOptions, ShikiTransformer, SupportedLanguages, ThemeRegistrationResolved, ThemeTypes, ThemedDiffResult, ThemedFileResult, ThemedToken, ThemesType, UNIFIED_DIFF_FILE_BREAK_REGEX, UNSAFE_CSS_ATTRIBUTE, VirtualWindowSpecs, areDiffLineAnnotationsEqual, areFilesEqual, areHunkDataEqual, areLanguagesAttached, areLineAnnotationsEqual, areObjectsEqual, areOptionsEqual, arePrePropertiesEqual, areRenderRangesEqual, areSelectionsEqual, areThemesAttached, areThemesEqual, areVirtualWindowSpecsEqual, areWorkerStatsEqual, attachResolvedLanguages, attachResolvedThemes, cleanLastNewline, cleanUpResolvedLanguages, cleanUpResolvedThemes, codeToHtml, createAnnotationElement, createAnnotationWrapperNode, createBufferElement, createCSSVariablesTheme, createDiffSpanDecoration, createEmptyRowBuffer, createFileHeaderElement, createHastElement, createHoverContentNode, createIconElement, createNoNewlineElement, createPreElement, createPreWrapperProperties, createRowNodes, createSeparator, createSpanFromToken, createStyleElement, createTextNodeElement, createTransformerWithState, createUnsafeCSSStyleNode, createWindowFromScrollPosition, diffAcceptRejectHunk, disposeHighlighter, extendFileFormatMap, findCodeElement, formatCSSVariablePrefix, getFiletypeFromFileName, getHighlighterIfLoaded, getHighlighterOptions, getHighlighterThemeStyles, getHunkSeparatorSlotName, getIconForType, getLineAnnotationName, getLineEndingType, getLineNodes, getOrCreateCodeNode, getResolvedLanguages, getResolvedOrResolveLanguage, getResolvedOrResolveTheme, getResolvedThemes, getSharedHighlighter, getSingularPatch, getThemes, getTotalLineCountFromHunks, hasResolvedLanguages, hasResolvedThemes, isDefaultRenderRange, isHighlighterLoaded, isHighlighterLoading, isHighlighterNull, isWorkerContext, parseDiffFromFile, parseLineType, parsePatchFiles, pluckLineSelectionOptions, pluckMouseEventOptions, preloadHighlighter, prerenderHTMLIfNecessary, processFile, processLine, processPatch, pushOrJoinSpan, queueRender, registerCustomCSSVariableTheme, registerCustomTheme, renderDiffWithHighlighter, renderFileWithHighlighter, resolveLanguage, resolveLanguages, resolveTheme, resolveThemes, setLanguageOverride, setPreNodeProperties, trimPatchContext, wrapCoreCSS, wrapUnsafeCSS };
|
package/dist/index.js
CHANGED
|
@@ -88,6 +88,7 @@ import { diffAcceptRejectHunk } from "./utils/diffAcceptRejectHunk.js";
|
|
|
88
88
|
import { getLineEndingType } from "./utils/getLineEndingType.js";
|
|
89
89
|
import { getSingularPatch } from "./utils/getSingularPatch.js";
|
|
90
90
|
import { setLanguageOverride } from "./utils/setLanguageOverride.js";
|
|
91
|
+
import { trimPatchContext } from "./utils/trimPatchContext.js";
|
|
91
92
|
import { codeToHtml, createCssVariablesTheme as createCSSVariablesTheme } from "shiki";
|
|
92
93
|
|
|
93
|
-
export { ALTERNATE_FILE_NAMES_GIT, AttachedLanguages, AttachedThemes, COMMIT_METADATA_SPLIT, CORE_CSS_ATTRIBUTE, CodeToTokenTransformStream, DEFAULT_EXPANDED_REGION, DEFAULT_THEMES, DIFFS_TAG_NAME, DIFF_HEADER_HEIGHT, DiffHunksRenderer, EXTENSION_TO_FILE_FORMAT, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT, FILE_CONTEXT_BLOB, FILE_GAP, File, FileDiff, FileRenderer, FileStream, GIT_DIFF_FILE_BREAK_REGEX, HEADER_METADATA_SLOT_ID, HUNK_HEADER, HUNK_SEPARATOR_HEIGHT, INDEX_LINE_METADATA, LINE_HEIGHT, LINE_HUNK_COUNT, LineSelectionManager, MouseEventManager, RegisteredCustomThemes, ResizeManager, ResolvedLanguages, ResolvedThemes, ResolvingLanguages, ResolvingThemes, SPLIT_WITH_NEWLINES, SVGSpriteSheet, ScrollSyncManager, ShikiStreamTokenizer, UNIFIED_DIFF_FILE_BREAK_REGEX, UNSAFE_CSS_ATTRIBUTE, areDiffLineAnnotationsEqual, areFilesEqual, areHunkDataEqual, areLanguagesAttached, areLineAnnotationsEqual, areObjectsEqual, areOptionsEqual, arePrePropertiesEqual, areRenderRangesEqual, areSelectionsEqual, areThemesAttached, areThemesEqual, areVirtualWindowSpecsEqual, areWorkerStatsEqual, attachResolvedLanguages, attachResolvedThemes, cleanLastNewline, cleanUpResolvedLanguages, cleanUpResolvedThemes, codeToHtml, createAnnotationElement, createAnnotationWrapperNode, createBufferElement, createCSSVariablesTheme, createDiffSpanDecoration, createEmptyRowBuffer, createFileHeaderElement, createHastElement, createHoverContentNode, createIconElement, createNoNewlineElement, createPreElement, createPreWrapperProperties, createRowNodes, createSeparator, createSpanFromToken, createStyleElement, createTextNodeElement, createTransformerWithState, createUnsafeCSSStyleNode, createWindowFromScrollPosition, diffAcceptRejectHunk, disposeHighlighter, extendFileFormatMap, findCodeElement, formatCSSVariablePrefix, getFiletypeFromFileName, getHighlighterIfLoaded, getHighlighterOptions, getHighlighterThemeStyles, getHunkSeparatorSlotName, getIconForType, getLineAnnotationName, getLineEndingType, getLineNodes, getOrCreateCodeNode, getResolvedLanguages, getResolvedOrResolveLanguage, getResolvedOrResolveTheme, getResolvedThemes, getSharedHighlighter, getSingularPatch, getThemes, getTotalLineCountFromHunks, hasResolvedLanguages, hasResolvedThemes, isDefaultRenderRange, isHighlighterLoaded, isHighlighterLoading, isHighlighterNull, isWorkerContext, parseDiffFromFile, parseLineType, parsePatchFiles, pluckLineSelectionOptions, pluckMouseEventOptions, preloadHighlighter, prerenderHTMLIfNecessary, processFile, processLine, processPatch, pushOrJoinSpan, queueRender, registerCustomCSSVariableTheme, registerCustomTheme, renderDiffWithHighlighter, renderFileWithHighlighter, resolveLanguage, resolveLanguages, resolveTheme, resolveThemes, setLanguageOverride, setPreNodeProperties, wrapCoreCSS, wrapUnsafeCSS };
|
|
94
|
+
export { ALTERNATE_FILE_NAMES_GIT, AttachedLanguages, AttachedThemes, COMMIT_METADATA_SPLIT, CORE_CSS_ATTRIBUTE, CodeToTokenTransformStream, DEFAULT_EXPANDED_REGION, DEFAULT_THEMES, DIFFS_TAG_NAME, DIFF_HEADER_HEIGHT, DiffHunksRenderer, EXTENSION_TO_FILE_FORMAT, FILENAME_HEADER_REGEX, FILENAME_HEADER_REGEX_GIT, FILE_CONTEXT_BLOB, FILE_GAP, File, FileDiff, FileRenderer, FileStream, GIT_DIFF_FILE_BREAK_REGEX, HEADER_METADATA_SLOT_ID, HUNK_HEADER, HUNK_SEPARATOR_HEIGHT, INDEX_LINE_METADATA, LINE_HEIGHT, LINE_HUNK_COUNT, LineSelectionManager, MouseEventManager, RegisteredCustomThemes, ResizeManager, ResolvedLanguages, ResolvedThemes, ResolvingLanguages, ResolvingThemes, SPLIT_WITH_NEWLINES, SVGSpriteSheet, ScrollSyncManager, ShikiStreamTokenizer, UNIFIED_DIFF_FILE_BREAK_REGEX, UNSAFE_CSS_ATTRIBUTE, areDiffLineAnnotationsEqual, areFilesEqual, areHunkDataEqual, areLanguagesAttached, areLineAnnotationsEqual, areObjectsEqual, areOptionsEqual, arePrePropertiesEqual, areRenderRangesEqual, areSelectionsEqual, areThemesAttached, areThemesEqual, areVirtualWindowSpecsEqual, areWorkerStatsEqual, attachResolvedLanguages, attachResolvedThemes, cleanLastNewline, cleanUpResolvedLanguages, cleanUpResolvedThemes, codeToHtml, createAnnotationElement, createAnnotationWrapperNode, createBufferElement, createCSSVariablesTheme, createDiffSpanDecoration, createEmptyRowBuffer, createFileHeaderElement, createHastElement, createHoverContentNode, createIconElement, createNoNewlineElement, createPreElement, createPreWrapperProperties, createRowNodes, createSeparator, createSpanFromToken, createStyleElement, createTextNodeElement, createTransformerWithState, createUnsafeCSSStyleNode, createWindowFromScrollPosition, diffAcceptRejectHunk, disposeHighlighter, extendFileFormatMap, findCodeElement, formatCSSVariablePrefix, getFiletypeFromFileName, getHighlighterIfLoaded, getHighlighterOptions, getHighlighterThemeStyles, getHunkSeparatorSlotName, getIconForType, getLineAnnotationName, getLineEndingType, getLineNodes, getOrCreateCodeNode, getResolvedLanguages, getResolvedOrResolveLanguage, getResolvedOrResolveTheme, getResolvedThemes, getSharedHighlighter, getSingularPatch, getThemes, getTotalLineCountFromHunks, hasResolvedLanguages, hasResolvedThemes, isDefaultRenderRange, isHighlighterLoaded, isHighlighterLoading, isHighlighterNull, isWorkerContext, parseDiffFromFile, parseLineType, parsePatchFiles, pluckLineSelectionOptions, pluckMouseEventOptions, preloadHighlighter, prerenderHTMLIfNecessary, processFile, processLine, processPatch, pushOrJoinSpan, queueRender, registerCustomCSSVariableTheme, registerCustomTheme, renderDiffWithHighlighter, renderFileWithHighlighter, resolveLanguage, resolveLanguages, resolveTheme, resolveThemes, setLanguageOverride, setPreNodeProperties, trimPatchContext, wrapCoreCSS, wrapUnsafeCSS };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/utils/trimPatchContext.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A utility function to trim out excess context lines from a patch file. It
|
|
4
|
+
* will maintain line numbers, and properly update the hunk context markers, as
|
|
5
|
+
* well as be able to create new hunks where necessary if there's excessive
|
|
6
|
+
* context between changes
|
|
7
|
+
*/
|
|
8
|
+
declare function trimPatchContext(patch: string, contextSize?: number): string;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { trimPatchContext };
|
|
11
|
+
//# sourceMappingURL=trimPatchContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimPatchContext.d.ts","names":[],"sources":["../../src/utils/trimPatchContext.ts"],"sourcesContent":[],"mappings":";;AAkBA;;;;;iBAAgB,gBAAA"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { HUNK_HEADER } from "../constants.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/trimPatchContext.ts
|
|
4
|
+
/**
|
|
5
|
+
* A utility function to trim out excess context lines from a patch file. It
|
|
6
|
+
* will maintain line numbers, and properly update the hunk context markers, as
|
|
7
|
+
* well as be able to create new hunks where necessary if there's excessive
|
|
8
|
+
* context between changes
|
|
9
|
+
*/
|
|
10
|
+
function trimPatchContext(patch, contextSize = 10) {
|
|
11
|
+
const lines = [];
|
|
12
|
+
let currentHunk;
|
|
13
|
+
for (const line of patch.split("\n")) {
|
|
14
|
+
const parsedHunkHeader = line.match(HUNK_HEADER);
|
|
15
|
+
if (parsedHunkHeader != null) {
|
|
16
|
+
if (currentHunk != null) {
|
|
17
|
+
if (currentHunk.hunkLines.length > 0) {
|
|
18
|
+
flushContextLines(currentHunk, contextSize);
|
|
19
|
+
flushHunk(currentHunk, lines);
|
|
20
|
+
}
|
|
21
|
+
currentHunk = void 0;
|
|
22
|
+
}
|
|
23
|
+
const additionStart = parseInt(parsedHunkHeader[3]);
|
|
24
|
+
const deletionStart = parseInt(parsedHunkHeader[1]);
|
|
25
|
+
const additionCount = parseInt(parsedHunkHeader[4] ?? "1");
|
|
26
|
+
const deletionCount = parseInt(parsedHunkHeader[2] ?? "1");
|
|
27
|
+
if (isNaN(additionStart) || isNaN(deletionStart) || isNaN(additionCount) || isNaN(deletionCount)) lines.push(line);
|
|
28
|
+
else currentHunk = {
|
|
29
|
+
hunkContextString: parsedHunkHeader[5] ?? "",
|
|
30
|
+
additionStart,
|
|
31
|
+
deletionStart,
|
|
32
|
+
additionCount: 0,
|
|
33
|
+
deletionCount: 0,
|
|
34
|
+
hunkLines: [],
|
|
35
|
+
contextLines: []
|
|
36
|
+
};
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (currentHunk == null) {
|
|
40
|
+
lines.push(line);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (line.startsWith(" ")) {
|
|
44
|
+
currentHunk.contextLines.push(line);
|
|
45
|
+
if (currentHunk.hunkLines.length > 0 && currentHunk.contextLines.length === contextSize * 2 + 1) {
|
|
46
|
+
const removedItems = currentHunk.contextLines.slice(contextSize);
|
|
47
|
+
flushContextLines(currentHunk, contextSize);
|
|
48
|
+
const { additionCount: emittedAdditionCount, deletionCount: emittedDeletionCount } = currentHunk;
|
|
49
|
+
flushHunk(currentHunk, lines);
|
|
50
|
+
removedItems.shift();
|
|
51
|
+
currentHunk = {
|
|
52
|
+
hunkContextString: "",
|
|
53
|
+
additionStart: currentHunk.additionStart + emittedAdditionCount + 1,
|
|
54
|
+
deletionStart: currentHunk.deletionStart + emittedDeletionCount + 1,
|
|
55
|
+
deletionCount: 0,
|
|
56
|
+
additionCount: 0,
|
|
57
|
+
contextLines: removedItems,
|
|
58
|
+
hunkLines: []
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
} else if (line !== "") {
|
|
62
|
+
flushContextLines(currentHunk, contextSize);
|
|
63
|
+
currentHunk.hunkLines.push(line);
|
|
64
|
+
if (line.startsWith("+")) currentHunk.additionCount += 1;
|
|
65
|
+
else if (line.startsWith("-")) currentHunk.deletionCount += 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (currentHunk != null && currentHunk.hunkLines.length > 0) {
|
|
69
|
+
flushContextLines(currentHunk, contextSize);
|
|
70
|
+
flushHunk(currentHunk, lines);
|
|
71
|
+
}
|
|
72
|
+
return lines.join("\n");
|
|
73
|
+
}
|
|
74
|
+
function flushContextLines(hunk, contextSize) {
|
|
75
|
+
if (hunk.contextLines.length > contextSize) if (hunk.hunkLines.length === 0) {
|
|
76
|
+
const difference = hunk.contextLines.length - contextSize;
|
|
77
|
+
hunk.contextLines.splice(0, difference);
|
|
78
|
+
hunk.additionStart += difference;
|
|
79
|
+
hunk.deletionStart += difference;
|
|
80
|
+
} else hunk.contextLines.length = contextSize;
|
|
81
|
+
if (hunk.contextLines.length > 0) {
|
|
82
|
+
hunk.hunkLines.push(...hunk.contextLines);
|
|
83
|
+
hunk.additionCount += hunk.contextLines.length;
|
|
84
|
+
hunk.deletionCount += hunk.contextLines.length;
|
|
85
|
+
hunk.contextLines.length = 0;
|
|
86
|
+
}
|
|
87
|
+
return hunk;
|
|
88
|
+
}
|
|
89
|
+
function flushHunk(hunk, lines) {
|
|
90
|
+
lines.push(`@@ -${formatHunkRange(hunk.deletionStart, hunk.deletionCount)} +${formatHunkRange(hunk.additionStart, hunk.additionCount)} @@${hunk.hunkContextString !== "" ? ` ${hunk.hunkContextString}` : ""}`);
|
|
91
|
+
lines.push(...hunk.hunkLines);
|
|
92
|
+
}
|
|
93
|
+
function formatHunkRange(start, count) {
|
|
94
|
+
return count === 1 ? `${start}` : `${start},${count}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
export { trimPatchContext };
|
|
99
|
+
//# sourceMappingURL=trimPatchContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimPatchContext.js","names":["lines: string[]","currentHunk: CurrentHunk | undefined"],"sources":["../../src/utils/trimPatchContext.ts"],"sourcesContent":["import { HUNK_HEADER } from '../constants';\n\ninterface CurrentHunk {\n hunkContextString: string;\n additionStart: number;\n deletionStart: number;\n additionCount: number;\n deletionCount: number;\n hunkLines: string[];\n contextLines: string[];\n}\n\n/**\n * A utility function to trim out excess context lines from a patch file. It\n * will maintain line numbers, and properly update the hunk context markers, as\n * well as be able to create new hunks where necessary if there's excessive\n * context between changes\n */\nexport function trimPatchContext(patch: string, contextSize = 10): string {\n const lines: string[] = [];\n\n let currentHunk: CurrentHunk | undefined;\n for (const line of patch.split('\\n')) {\n const parsedHunkHeader = line.match(HUNK_HEADER);\n // If we've come across a new hunk boundary, then we should close out the\n // current hunk and setup a new one\n if (parsedHunkHeader != null) {\n if (currentHunk != null) {\n if (currentHunk.hunkLines.length > 0) {\n flushContextLines(currentHunk, contextSize);\n flushHunk(currentHunk, lines);\n }\n currentHunk = undefined;\n }\n\n const additionStart = parseInt(parsedHunkHeader[3]);\n const deletionStart = parseInt(parsedHunkHeader[1]);\n const additionCount = parseInt(parsedHunkHeader[4] ?? '1');\n const deletionCount = parseInt(parsedHunkHeader[2] ?? '1');\n\n // If we can't parse valid numbers out of the hunk header\n // lets just skip the hunk altogether\n if (\n isNaN(additionStart) ||\n isNaN(deletionStart) ||\n isNaN(additionCount) ||\n isNaN(deletionCount)\n ) {\n lines.push(line);\n } else {\n currentHunk = {\n hunkContextString: parsedHunkHeader[5] ?? '',\n additionStart,\n deletionStart,\n additionCount: 0,\n deletionCount: 0,\n hunkLines: [],\n contextLines: [],\n };\n }\n continue;\n }\n\n // If we don't have a current hunk, then we should just assume this is\n // general metadata\n if (currentHunk == null) {\n lines.push(line);\n continue;\n }\n\n // If we are dealing with a context line...\n if (line.startsWith(' ')) {\n currentHunk.contextLines.push(line);\n // If we've exceeded double our context window size + 1, that means we\n // should create a new hunk...\n if (\n currentHunk.hunkLines.length > 0 &&\n currentHunk.contextLines.length === contextSize * 2 + 1\n ) {\n const removedItems = currentHunk.contextLines.slice(contextSize);\n flushContextLines(currentHunk, contextSize);\n const {\n additionCount: emittedAdditionCount,\n deletionCount: emittedDeletionCount,\n } = currentHunk;\n flushHunk(currentHunk, lines);\n\n removedItems.shift();\n currentHunk = {\n // NOTE(amadeus): Not sure there's an easy way to manage this context\n // string stuff, so lets just remove it from auto generated hunks\n hunkContextString: '',\n additionStart: currentHunk.additionStart + emittedAdditionCount + 1,\n deletionStart: currentHunk.deletionStart + emittedDeletionCount + 1,\n deletionCount: 0,\n additionCount: 0,\n contextLines: removedItems,\n hunkLines: [],\n };\n }\n } else if (line !== '') {\n flushContextLines(currentHunk, contextSize);\n currentHunk.hunkLines.push(line);\n if (line.startsWith('+')) {\n currentHunk.additionCount += 1;\n } else if (line.startsWith('-')) {\n currentHunk.deletionCount += 1;\n }\n }\n }\n\n if (currentHunk != null && currentHunk.hunkLines.length > 0) {\n flushContextLines(currentHunk, contextSize);\n flushHunk(currentHunk, lines);\n }\n\n return lines.join('\\n');\n}\n\nfunction flushContextLines(hunk: CurrentHunk, contextSize: number) {\n if (hunk.contextLines.length > contextSize) {\n // If this context group is at the beginning of the hunk, truncate from the\n // beginning\n if (hunk.hunkLines.length === 0) {\n const difference = hunk.contextLines.length - contextSize;\n hunk.contextLines.splice(0, difference);\n hunk.additionStart += difference;\n hunk.deletionStart += difference;\n }\n // Otherwise truncate from the end\n else {\n hunk.contextLines.length = contextSize;\n }\n }\n if (hunk.contextLines.length > 0) {\n hunk.hunkLines.push(...hunk.contextLines);\n hunk.additionCount += hunk.contextLines.length;\n hunk.deletionCount += hunk.contextLines.length;\n hunk.contextLines.length = 0;\n }\n return hunk;\n}\n\nfunction flushHunk(hunk: CurrentHunk, lines: string[]) {\n lines.push(\n `@@ -${formatHunkRange(hunk.deletionStart, hunk.deletionCount)} +${formatHunkRange(hunk.additionStart, hunk.additionCount)} @@${hunk.hunkContextString !== '' ? ` ${hunk.hunkContextString}` : ''}`\n );\n lines.push(...hunk.hunkLines);\n}\n\nfunction formatHunkRange(start: number, count: number): string {\n return count === 1 ? `${start}` : `${start},${count}`;\n}\n"],"mappings":";;;;;;;;;AAkBA,SAAgB,iBAAiB,OAAe,cAAc,IAAY;CACxE,MAAMA,QAAkB,EAAE;CAE1B,IAAIC;AACJ,MAAK,MAAM,QAAQ,MAAM,MAAM,KAAK,EAAE;EACpC,MAAM,mBAAmB,KAAK,MAAM,YAAY;AAGhD,MAAI,oBAAoB,MAAM;AAC5B,OAAI,eAAe,MAAM;AACvB,QAAI,YAAY,UAAU,SAAS,GAAG;AACpC,uBAAkB,aAAa,YAAY;AAC3C,eAAU,aAAa,MAAM;;AAE/B,kBAAc;;GAGhB,MAAM,gBAAgB,SAAS,iBAAiB,GAAG;GACnD,MAAM,gBAAgB,SAAS,iBAAiB,GAAG;GACnD,MAAM,gBAAgB,SAAS,iBAAiB,MAAM,IAAI;GAC1D,MAAM,gBAAgB,SAAS,iBAAiB,MAAM,IAAI;AAI1D,OACE,MAAM,cAAc,IACpB,MAAM,cAAc,IACpB,MAAM,cAAc,IACpB,MAAM,cAAc,CAEpB,OAAM,KAAK,KAAK;OAEhB,eAAc;IACZ,mBAAmB,iBAAiB,MAAM;IAC1C;IACA;IACA,eAAe;IACf,eAAe;IACf,WAAW,EAAE;IACb,cAAc,EAAE;IACjB;AAEH;;AAKF,MAAI,eAAe,MAAM;AACvB,SAAM,KAAK,KAAK;AAChB;;AAIF,MAAI,KAAK,WAAW,IAAI,EAAE;AACxB,eAAY,aAAa,KAAK,KAAK;AAGnC,OACE,YAAY,UAAU,SAAS,KAC/B,YAAY,aAAa,WAAW,cAAc,IAAI,GACtD;IACA,MAAM,eAAe,YAAY,aAAa,MAAM,YAAY;AAChE,sBAAkB,aAAa,YAAY;IAC3C,MAAM,EACJ,eAAe,sBACf,eAAe,yBACb;AACJ,cAAU,aAAa,MAAM;AAE7B,iBAAa,OAAO;AACpB,kBAAc;KAGZ,mBAAmB;KACnB,eAAe,YAAY,gBAAgB,uBAAuB;KAClE,eAAe,YAAY,gBAAgB,uBAAuB;KAClE,eAAe;KACf,eAAe;KACf,cAAc;KACd,WAAW,EAAE;KACd;;aAEM,SAAS,IAAI;AACtB,qBAAkB,aAAa,YAAY;AAC3C,eAAY,UAAU,KAAK,KAAK;AAChC,OAAI,KAAK,WAAW,IAAI,CACtB,aAAY,iBAAiB;YACpB,KAAK,WAAW,IAAI,CAC7B,aAAY,iBAAiB;;;AAKnC,KAAI,eAAe,QAAQ,YAAY,UAAU,SAAS,GAAG;AAC3D,oBAAkB,aAAa,YAAY;AAC3C,YAAU,aAAa,MAAM;;AAG/B,QAAO,MAAM,KAAK,KAAK;;AAGzB,SAAS,kBAAkB,MAAmB,aAAqB;AACjE,KAAI,KAAK,aAAa,SAAS,YAG7B,KAAI,KAAK,UAAU,WAAW,GAAG;EAC/B,MAAM,aAAa,KAAK,aAAa,SAAS;AAC9C,OAAK,aAAa,OAAO,GAAG,WAAW;AACvC,OAAK,iBAAiB;AACtB,OAAK,iBAAiB;OAItB,MAAK,aAAa,SAAS;AAG/B,KAAI,KAAK,aAAa,SAAS,GAAG;AAChC,OAAK,UAAU,KAAK,GAAG,KAAK,aAAa;AACzC,OAAK,iBAAiB,KAAK,aAAa;AACxC,OAAK,iBAAiB,KAAK,aAAa;AACxC,OAAK,aAAa,SAAS;;AAE7B,QAAO;;AAGT,SAAS,UAAU,MAAmB,OAAiB;AACrD,OAAM,KACJ,OAAO,gBAAgB,KAAK,eAAe,KAAK,cAAc,CAAC,IAAI,gBAAgB,KAAK,eAAe,KAAK,cAAc,CAAC,KAAK,KAAK,sBAAsB,KAAK,IAAI,KAAK,sBAAsB,KAChM;AACD,OAAM,KAAK,GAAG,KAAK,UAAU;;AAG/B,SAAS,gBAAgB,OAAe,OAAuB;AAC7D,QAAO,UAAU,IAAI,GAAG,UAAU,GAAG,MAAM,GAAG"}
|