Add option to mark whitespace characters in diffs

Co-authored-by: René Pfeuffer<rene.pfeuffer@cloudogu.com>
This commit is contained in:
Viktor Egorov
2024-04-26 15:15:52 +02:00
parent 3773d77889
commit cdbf86b8c9
17 changed files with 6215 additions and 395 deletions

View File

@@ -53,6 +53,7 @@ export type TokenizeRequest = MessageData<
{
language: string;
hunks: unknown;
whitespace?: boolean;
}
>;

View File

@@ -154,15 +154,81 @@ const isTokenizeMessage = (message: Request): message is TokenizeRequest => {
const TOKENIZE_NODE_LIMIT = 600;
const replaceSpaces = (node: RefractorNode): RefractorNode => {
if (node?.type === "text") {
if (node.value.indexOf(" ") < 0 && node.value.indexOf("\t") < 0) {
return node;
}
return {
type: "element",
tagName: "span",
properties: {},
children: node.value.split("").flatMap((c) => {
if (c === " ") {
return [
{
type: "element",
tagName: "span",
children: [{ type: "text", value: " " }],
properties: {
className: ["space_char"],
},
},
{
type: "element",
tagName: "span",
children: [{ type: "text", value: "" }],
properties: {},
},
];
} else if (c === "\t") {
return [
{
type: "element",
tagName: "span",
children: [{ type: "text", value: "\t" }],
properties: {
className: ["tabulator_char"],
},
},
{
type: "element",
tagName: "span",
children: [{ type: "text", value: "" }],
properties: {},
},
];
} else {
return {
type: "element",
tagName: "span",
properties: {},
children: [{ type: "text", value: c }],
};
}
}),
};
} else if (node?.type === "element") {
return {
...node,
children: node.children.map(replaceSpaces),
};
}
return node;
};
const runTokenize = ({ id, payload }: TokenizeRequest) => {
const { hunks, language } = payload;
const { hunks, language, whitespace } = payload;
const options = {
highlight: language !== "text",
highlight: true, // we always want to run refractor to enable the display of whitespace characters
language: language,
refractor: {
...refractor,
highlight: (value: string, lang: string) => {
if (whitespace) {
return refractor.highlight(value, lang).children.map(replaceSpaces);
}
return refractor.highlight(value, lang).children;
},
},