Файли
openidea/src/editor/core/cm/gomod.test.ts
T
Kamal ElFadlтаelfadl 14a31fe8a1 feat(editor): add syntax highlighting for go.mod and go.work files (#1695)
go.mod and go.work opened as plain text: their language definitions had no ui-mode, so no CodeMirror language loaded. Add a GoMod mode (a small stream parser): directives as keywords, module paths as strings, versions as numbers, // comments; shared by go.mod and go.work.

Co-authored-by: elfadl <[email protected]>
2026-11-07 11:12:41 +08:00

74 рядки
2.4 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { goModParser, initialGoModState, type GoModState } from "./gomod";
import { StringStream } from "@codemirror/language";
function tokenize(line: string, state: GoModState) {
const stream = new StringStream(line, 4, 1);
const tokens: { text: string; token: string | null }[] = [];
while (!stream.eol()) {
const start = stream.pos;
const kind = goModParser.token(stream, state);
if (stream.pos > start && kind !== null) {
tokens.push({ text: stream.string.slice(start, stream.pos), token: kind });
}
}
return tokens;
}
describe("goModParser", () => {
it("highlights module declaration and go version", () => {
const state = initialGoModState();
const line1 = tokenize("module onpay-aplikasi-api", state);
expect(line1).toEqual([
{ text: "module", token: "keyword" },
{ text: "onpay-aplikasi-api", token: "string" },
]);
const line2 = tokenize("go 1.27.0", state);
expect(line2).toEqual([
{ text: "go", token: "keyword" },
{ text: "1.27.0", token: "number" },
]);
});
it("highlights require blocks with module paths and versions", () => {
const state = initialGoModState();
const line1 = tokenize("require (", state);
expect(line1).toEqual([
{ text: "require", token: "keyword" },
{ text: "(", token: "paren" },
]);
const line2 = tokenize("\tcloud.google.com/go/auth v0.20.0", state);
expect(line2).toEqual([
{ text: "cloud.google.com/go/auth", token: "string" },
{ text: "v0.20.0", token: "number" },
]);
const line3 = tokenize("\tgithub.com/shopspring/decimal v1.4.0 // indirect", state);
expect(line3).toEqual([
{ text: "github.com/shopspring/decimal", token: "string" },
{ text: "v1.4.0", token: "number" },
{ text: "// indirect", token: "comment" },
]);
const line4 = tokenize(")", state);
expect(line4).toEqual([{ text: ")", token: "paren" }]);
});
it("highlights replace directive with operator", () => {
const state = initialGoModState();
const line = tokenize("replace example.com/foo => ../bar", state);
expect(line).toEqual([
{ text: "replace", token: "keyword" },
{ text: "example.com/foo", token: "string" },
{ text: "=>", token: "operator" },
{ text: "../bar", token: "string" },
]);
});
});