Файли
openidea/src/editor/core/cm/shellInterpolation.test.ts
T
Daniel AdámekтаDanielADK a05a40e0a3 fix(editor): end a <<- heredoc at its tab-indented delimiter in shell highlighting (#3259)
The stock CodeMirror shell mode ends a heredoc body only on a line equal to the delimiter and drops the `-` of `<<-`, so a tab-indented closing delimiter highlighted the rest of the file as heredoc text. shellInterpolation now tracks heredocs itself: it reads the delimiter as bash does (quotes removed, ends at `|`, `;`, `>`…), strips leading tabs for `<<-`, queues several heredocs on one line, starts bodies on the next line, and leaves `<<` in `$(( ))` and `<<<` here-strings as operators.

Co-authored-by: DanielADK <[email protected]>
2026-11-14 09:08:41 +08:00

68 рядки
3.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { StreamLanguage } from "@codemirror/language";
import { shell } from "@codemirror/legacy-modes/mode/shell";
import { highlightTree, tags } from "@lezer/highlight";
import { shellInterpolation } from "./shellInterpolation";
import { openideaHighlightStyle } from "./highlight";
const language = StreamLanguage.define(shellInterpolation(shell));
function tokens(doc: string) {
const result: { text: string; cls: string }[] = [];
highlightTree(language.parser.parse(doc), openideaHighlightStyle, (from, to, cls) => {
result.push({ text: doc.slice(from, to), cls });
});
return result;
}
const token = (text: string, tag: typeof tags.string) => ({ text, cls: openideaHighlightStyle.style([tag]) });
describe("shell interpolation", () => {
it("separates variables, command substitutions and arithmetic inside strings", () => {
const result = tokens('echo "$name ${count:-1} $(echo hi) $((count + 1))"');
for (const text of ["$", "${", "}", "$(", ")", "$((", "))"]) {
expect(result).toContainEqual(token(text, tags.special(tags.brace)));
}
for (const text of ["name", "count"]) expect(result).toContainEqual(token(text, tags.variableName));
expect(result).toContainEqual(token("1", tags.number));
expect(result).toContainEqual(token("+", tags.operator));
expect(result).toContainEqual(token("echo", tags.standard(tags.variableName)));
});
it("highlights parameter operators without treating length expansion as a comment", () => {
const result = tokens('echo "${#name} ${name:-1}"');
expect(result).toContainEqual(token("#", tags.operator));
expect(result).toContainEqual(token("name", tags.variableName));
expect(result).toContainEqual(token(":-", tags.operator));
expect(result.some((item) => item.cls === openideaHighlightStyle.style([tags.comment]))).toBe(false);
});
it("keeps nested command substitutions and quoted strings independent", () => {
const result = tokens('echo "$(echo "$(echo hi)") after"');
expect(result.filter((item) => item.text === "$(")).toHaveLength(2);
expect(result.filter((item) => item.text === ")")).toHaveLength(2);
expect(result).toContainEqual(token(' after"', tags.string));
});
it("ends a <<- heredoc at a tab-indented delimiter", () => {
const result = tokens("\tcat <<- 'EOF'\n\t\tDROP DATABASE x;\n\tEOF\n\tcat $dir/database.sql");
expect(result).toContainEqual(token("\t\tDROP DATABASE x;", tags.special(tags.string)));
expect(result).toContainEqual(token("\tEOF", tags.special(tags.string)));
expect(result.filter((item) => item.text === "cat")).toHaveLength(2);
expect(result).toContainEqual(token("dir", tags.variableName));
});
it("keeps the heredoc line and arithmetic shifts as code", () => {
const result = tokens("cat <<EOF | grep x\nEOF\necho $((1 << 2))\necho done");
expect(result).toContainEqual(token("grep", tags.standard(tags.variableName)));
expect(result).toContainEqual(token("<<", tags.operator));
expect(result.filter((item) => item.text === "echo")).toHaveLength(2);
});
it("leaves single quotes, escaped dollars, comments and heredocs intact", () => {
for (const doc of ["echo '$name ${count}'", 'echo "\\$name"', '# "${count}"', "cat <<'EOF'\n$name ${count}\nEOF"]) {
expect(tokens(doc).some((item) => item.cls === openideaHighlightStyle.style([tags.special(tags.brace)]))).toBe(
false,
);
}
});
});