@parcel/codeframe 2.8.4-nightly.0 → 2.9.0
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/lib/codeframe.js +39 -20
- package/lib/codeframe.js.map +1 -1
- package/package.json +2 -2
package/lib/codeframe.js
CHANGED
@@ -35897,7 +35897,9 @@ const $796233c1fee0ccb8$var$DEFAULT_TERMINAL_WIDTH = 80;
|
|
35897
35897
|
const $796233c1fee0ccb8$var$highlightSyntax = (txt, lang)=>{
|
35898
35898
|
if (lang) try {
|
35899
35899
|
return (0, (/*@__PURE__*/$parcel$interopDefault($9786466eb6148e3d$exports))).highlight(lang, txt).value;
|
35900
|
-
} catch (e) {
|
35900
|
+
} catch (e) {
|
35901
|
+
// fallback for unknown languages...
|
35902
|
+
}
|
35901
35903
|
return (0, (/*@__PURE__*/$parcel$interopDefault($9786466eb6148e3d$exports))).highlightAuto(txt).value;
|
35902
35904
|
};
|
35903
35905
|
function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts = {}) {
|
@@ -35913,18 +35915,21 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
35913
35915
|
before: 1,
|
35914
35916
|
after: 2
|
35915
35917
|
}
|
35916
|
-
};
|
35918
|
+
};
|
35919
|
+
// Highlights messages and prefixes when colors are enabled
|
35917
35920
|
const highlighter = (s, bold)=>{
|
35918
35921
|
if (opts.useColor) {
|
35919
35922
|
let redString = (0, ($parcel$interopDefault($dGKPK$chalk))).red(s);
|
35920
35923
|
return bold ? (0, ($parcel$interopDefault($dGKPK$chalk))).bold(redString) : redString;
|
35921
35924
|
}
|
35922
35925
|
return s;
|
35923
|
-
};
|
35926
|
+
};
|
35927
|
+
// Prefix lines with the line number
|
35924
35928
|
const lineNumberPrefixer = (params)=>{
|
35925
35929
|
let { lineNumber: lineNumber , lineNumberLength: lineNumberLength , isHighlighted: isHighlighted } = params;
|
35926
35930
|
return `${isHighlighted ? highlighter(">") : " "} ${lineNumber ? lineNumber.padStart(lineNumberLength, " ") : " ".repeat(lineNumberLength)} | `;
|
35927
|
-
};
|
35931
|
+
};
|
35932
|
+
// Make columns/lines start at 1
|
35928
35933
|
let originalHighlights = highlights;
|
35929
35934
|
highlights = highlights.map((h)=>{
|
35930
35935
|
return {
|
@@ -35938,9 +35943,11 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
35938
35943
|
},
|
35939
35944
|
message: h.message
|
35940
35945
|
};
|
35941
|
-
});
|
35946
|
+
});
|
35947
|
+
// Find first and last highlight
|
35942
35948
|
let firstHighlight = highlights.length > 1 ? highlights.sort((a, b)=>a.start.line - b.start.line)[0] : highlights[0];
|
35943
|
-
let lastHighlight = highlights.length > 1 ? highlights.sort((a, b)=>b.end.line - a.end.line)[0] : highlights[0];
|
35949
|
+
let lastHighlight = highlights.length > 1 ? highlights.sort((a, b)=>b.end.line - a.end.line)[0] : highlights[0];
|
35950
|
+
// Calculate first and last line index of codeframe
|
35944
35951
|
let startLine = firstHighlight.start.line - opts.padding.before;
|
35945
35952
|
startLine = startLine < 0 ? 0 : startLine;
|
35946
35953
|
let endLineIndex = lastHighlight.end.line + opts.padding.after;
|
@@ -35952,16 +35959,21 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
35952
35959
|
endLineIndex = Math.min(maxLine, lastHighlight.end.line + opts.padding.after);
|
35953
35960
|
tail = originalHighlights.filter((h)=>h.start.line > endLineIndex);
|
35954
35961
|
}
|
35955
|
-
let lineNumberLength = (endLineIndex + 1).toString(10).length;
|
35962
|
+
let lineNumberLength = (endLineIndex + 1).toString(10).length;
|
35963
|
+
// Split input into lines and highlight syntax
|
35956
35964
|
let lines = code.split($796233c1fee0ccb8$var$NEWLINE);
|
35957
|
-
let syntaxHighlightedLines = (opts.syntaxHighlighting ? $796233c1fee0ccb8$var$highlightSyntax(code, opts.language) : code).replace($796233c1fee0ccb8$var$TAB_REPLACE_REGEX, $796233c1fee0ccb8$var$TAB_REPLACEMENT).split($796233c1fee0ccb8$var$NEWLINE);
|
35965
|
+
let syntaxHighlightedLines = (opts.syntaxHighlighting ? $796233c1fee0ccb8$var$highlightSyntax(code, opts.language) : code).replace($796233c1fee0ccb8$var$TAB_REPLACE_REGEX, $796233c1fee0ccb8$var$TAB_REPLACEMENT).split($796233c1fee0ccb8$var$NEWLINE);
|
35966
|
+
// Loop over all lines and create codeframe
|
35958
35967
|
let resultLines = [];
|
35959
35968
|
for(let currentLineIndex = startLine; currentLineIndex < syntaxHighlightedLines.length; currentLineIndex++){
|
35960
35969
|
if (currentLineIndex > endLineIndex) break;
|
35961
|
-
if (currentLineIndex > syntaxHighlightedLines.length - 1) break;
|
35962
|
-
|
35970
|
+
if (currentLineIndex > syntaxHighlightedLines.length - 1) break;
|
35971
|
+
// Find highlights that need to get rendered on the current line
|
35972
|
+
let lineHighlights = highlights.filter((highlight)=>highlight.start.line <= currentLineIndex && highlight.end.line >= currentLineIndex).sort((a, b)=>(a.start.line < currentLineIndex ? 0 : a.start.column) - (b.start.line < currentLineIndex ? 0 : b.start.column));
|
35973
|
+
// Check if this line has a full line highlight
|
35963
35974
|
let isWholeLine = lineHighlights.length && !!lineHighlights.find((h)=>h.start.line < currentLineIndex && h.end.line > currentLineIndex);
|
35964
|
-
let lineLengthLimit = opts.terminalWidth > lineNumberLength + 7 ? opts.terminalWidth - (lineNumberLength + 5) : 10;
|
35975
|
+
let lineLengthLimit = opts.terminalWidth > lineNumberLength + 7 ? opts.terminalWidth - (lineNumberLength + 5) : 10;
|
35976
|
+
// Split the line into line parts that will fit the provided terminal width
|
35965
35977
|
let colOffset = 0;
|
35966
35978
|
let lineEndCol = lineLengthLimit;
|
35967
35979
|
let syntaxHighlightedLine = syntaxHighlightedLines[currentLineIndex];
|
@@ -35973,7 +35985,8 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
35973
35985
|
colOffset = colOffset > 0 ? colOffset : 0;
|
35974
35986
|
lineEndCol = colOffset + lineLengthLimit;
|
35975
35987
|
syntaxHighlightedLine = (0, (/*@__PURE__*/$parcel$interopDefault($0ae586f9cf7cb410$exports)))(syntaxHighlightedLine, colOffset, lineEndCol);
|
35976
|
-
}
|
35988
|
+
}
|
35989
|
+
// Write the syntax highlighted line part
|
35977
35990
|
resultLines.push(lineNumberPrefixer({
|
35978
35991
|
lineNumber: (currentLineIndex + 1).toString(10),
|
35979
35992
|
lineNumberLength: lineNumberLength,
|
@@ -35989,18 +36002,21 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
35989
36002
|
for(let highlightIndex = 0; highlightIndex < lineHighlights.length; highlightIndex++){
|
35990
36003
|
// Set highlight to current highlight
|
35991
36004
|
highlight = lineHighlights[highlightIndex];
|
35992
|
-
highlightHasEnded = false;
|
36005
|
+
highlightHasEnded = false;
|
36006
|
+
// Calculate the startColumn and get the real width by doing a substring of the original
|
35993
36007
|
// line and replacing tabs with our tab replacement to support tab handling
|
35994
36008
|
let startCol = 0;
|
35995
36009
|
if (highlight.start.line === currentLineIndex && highlight.start.column > colOffset) startCol = lines[currentLineIndex].substring(colOffset, highlight.start.column).replace($796233c1fee0ccb8$var$TAB_REPLACE_REGEX, $796233c1fee0ccb8$var$TAB_REPLACEMENT).length;
|
35996
|
-
|
36010
|
+
// Calculate the endColumn and get the real width by doing a substring of the original
|
35997
36011
|
// line and replacing tabs with our tab replacement to support tab handling
|
35998
36012
|
let endCol = lineWidth - 1;
|
35999
36013
|
if (highlight.end.line === currentLineIndex) {
|
36000
|
-
endCol = lines[currentLineIndex].substring(colOffset, highlight.end.column).replace($796233c1fee0ccb8$var$TAB_REPLACE_REGEX, $796233c1fee0ccb8$var$TAB_REPLACEMENT).length;
|
36014
|
+
endCol = lines[currentLineIndex].substring(colOffset, highlight.end.column).replace($796233c1fee0ccb8$var$TAB_REPLACE_REGEX, $796233c1fee0ccb8$var$TAB_REPLACEMENT).length;
|
36015
|
+
// If the endCol is too big for this line part, trim it so we can handle it in the next one
|
36001
36016
|
if (endCol > lineWidth) endCol = lineWidth - 1;
|
36002
36017
|
highlightHasEnded = true;
|
36003
|
-
}
|
36018
|
+
}
|
36019
|
+
// If endcol is smaller than lastCol it overlaps with another highlight and is no longer visible, we can skip those
|
36004
36020
|
if (endCol >= lastCol) {
|
36005
36021
|
let characters = endCol - startCol + 1;
|
36006
36022
|
if (startCol > lastCol) // startCol is before lastCol, so add spaces as padding before the highlight indicators
|
@@ -36008,12 +36024,15 @@ function $796233c1fee0ccb8$export$2e2bcd8739ae039(code, highlights, inputOpts =
|
|
36008
36024
|
else if (lastCol > startCol) // If last column is larger than the start, there's overlap in highlights
|
36009
36025
|
// This line adjusts the characters count to ensure we don't add too many characters
|
36010
36026
|
characters += startCol - lastCol;
|
36011
|
-
|
36012
|
-
highlightLine += highlighter("^".repeat(characters));
|
36027
|
+
// Append the highlight indicators
|
36028
|
+
highlightLine += highlighter("^".repeat(characters));
|
36029
|
+
// Set the lastCol equal to character count between start of line part and highlight end-column
|
36013
36030
|
lastCol = endCol + 1;
|
36014
|
-
}
|
36031
|
+
}
|
36032
|
+
// There's no point in processing more highlights if we reached the end of the line
|
36015
36033
|
if (endCol >= lineEndCol - 1) break;
|
36016
|
-
}
|
36034
|
+
}
|
36035
|
+
// Append the highlight message if the current highlights ends on this line part
|
36017
36036
|
if (highlight && highlight.message && highlightHasEnded) highlightLine += " " + highlighter(highlight.message, true);
|
36018
36037
|
}
|
36019
36038
|
if (highlightLine) resultLines.push(lineNumberPrefixer({
|