Skip to content

The Unified and Autonomous Development Platform

feat: add Terraform and HCL syntax highlighting

Robin Shen71c79dbf77f1
diff --git a/e2e-test/tests/terraform-highlighting.spec.js b/e2e-test/tests/terraform-highlighting.spec.js
new file mode 100644
index 0000000000..77d6f31045
--- /dev/null
+++ b/e2e-test/tests/terraform-highlighting.spec.js
@@ -0,0 +1,96 @@
+import { expect, test, admin } from './fixtures.js';
+import { login } from './helpers.js';
+
+const hclSource = [
+ '# hash comment',
+ '// slash comment',
+ '/* block comment',
+ ' continued comment */',
+ 'resource "example_server" "web" {',
+ ' count = 1.25e+2',
+ ' enabled = true',
+ ' name = "hello ${var.name}!"',
+ ' nested = "${jsonencode({key = "${var.name}"})} suffix"',
+ ' escaped = "$${literal} %%{literal}"',
+ ' script = <<-EOT',
+ ' # literal heredoc text',
+ ' %{ for item in var.items ~}',
+ ' ${item}',
+ ' %{ endfor ~}',
+ ' EOT',
+ ' after = 42',
+ '}',
+ '',
+].join('\n');
+
+async function expectToken(scope, style, text) {
+ const token = scope.locator(`.cm-${style}`).and(scope.getByText(text, { exact: true })).first();
+ await expect(token).toBeVisible();
+ await expect(token).toHaveText(text);
+}
+
+async function expectHclHighlighting(scope) {
+ await expectToken(scope, 'comment', '# hash comment');
+ await expectToken(scope, 'comment', '// slash comment');
+ await expectToken(scope, 'comment', '/* block comment');
+ await expectToken(scope, 'comment', ' continued comment */');
+ await expectToken(scope, 'keyword', 'resource');
+ await expectToken(scope, 'number', '1.25e+2');
+ await expectToken(scope, 'atom', 'true');
+ await expectToken(scope, 'punctuation', '${');
+ await expectToken(scope, 'variable', 'var');
+ await expectToken(scope, 'def', 'jsonencode');
+ await expectToken(scope, 'string', ' suffix"');
+ await expectToken(scope, 'string', '"$${literal} %%{literal}"');
+ await expectToken(scope, 'string', ' # literal heredoc text');
+ await expectToken(scope, 'keyword', 'endfor');
+ await expectToken(scope, 'string', ' EOT');
+ // The heredoc must end, so subsequent attributes are highlighted as code.
+ await expectToken(scope, 'number', '42');
+}
+
+for (const file of ['main.tf', 'values.tfvars', 'terragrunt.hcl']) {
+ test(`highlights ${file} in the blob viewer`, async ({ page, api }) => {
+ const project = await api.createProject();
+ await api.putFile(project, file, hclSource);
+ await login(page, admin.name, admin.password);
+ await page.goto(`${project.name}/~files/main/${file}`);
+
+ await expectHclHighlighting(page.locator('.source-view .CodeMirror'));
+ });
+}
+
+for (const file of ['main.tf.json', 'values.tfvars.json']) {
+ test(`uses JSON highlighting for ${file}`, async ({ page, api }) => {
+ const project = await api.createProject();
+ await api.putFile(project, file, '{"count": 42, "enabled": true, "name": "example"}\n');
+ await login(page, admin.name, admin.password);
+ await page.goto(`${project.name}/~files/main/${file}`);
+
+ const source = page.locator('.source-view .CodeMirror');
+ await expectToken(source, 'property', '"count"');
+ await expectToken(source, 'number', '42');
+ await expectToken(source, 'atom', 'true');
+ await expectToken(source, 'string', '"example"');
+ });
+}
+
+for (const mode of ['UNIFIED', 'SPLIT']) {
+ test(`highlights Terraform in ${mode.toLowerCase()} diffs`, async ({ page, api, baseURL }) => {
+ await page.context().addCookies([{ name: 'onedev.server.diff.viewmode', value: mode, url: baseURL }]);
+ const project = await api.createProject();
+ const added = await api.putFile(project, 'main.tf', hclSource);
+ await login(page, admin.name, admin.password);
+ await page.goto(`${project.name}/~commits/${added.commitHash}`);
+
+ const diff = page.locator('.blob-text-diff');
+ await expectHclHighlighting(diff);
+
+ // Exercise both revisions, including a number embedded in a changed line.
+ const changed = await api.putFile(project, 'main.tf', hclSource.replace('after = 42', 'after = 99'));
+ await page.goto(`${project.name}/~commits/${changed.commitHash}`);
+ await expectToken(diff, 'number', '42');
+ await expectToken(diff, 'number', '99');
+ await expectToken(diff, 'keyword', 'endfor');
+ });
+}
diff --git a/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/hcl/hcl.js b/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/hcl/hcl.js
new file mode 100644
index 0000000000..c993fe9919
--- /dev/null
+++ b/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/hcl/hcl.js
@@ -0,0 +1,122 @@
+(function(mod) {
+ if (typeof exports == "object" && typeof module == "object") // CommonJS
+ mod(require("../../lib/codemirror"));
+ else if (typeof define == "function" && define.amd) // AMD
+ define(["../../lib/codemirror"], mod);
+ else // Plain browser env
+ mod(CodeMirror);
+})(function(CodeMirror) {
+ "use strict";
+
+ CodeMirror.defineMode("hcl", function() {
+ var keywords = /^(?:resource|variable|module|provider|locals|data|output|terraform|backend|provisioner|connection|lifecycle|dynamic|content|for|in|if|else|endif|endfor)$/;
+ var identifier = /^[A-Za-z_\u00a1-\uffff][\w\u00a1-\uffff-]*/;
+
+ function tokenComment(stream, state) {
+ while (!stream.eol()) {
+ if (stream.match("*/")) {
+ state.contexts.pop();
+ break;
+ }
+ stream.next();
+ }
+ return "comment";
+ }
+
+ function tokenTemplate(stream, state, context) {
+ if (context.type == "heredoc" && stream.sol()) {
+ var line = stream.string.replace(/[ \t]+$/, "");
+ if (context.indented) line = line.replace(/^[ \t]+/, "");
+ if (line == context.delimiter) {
+ stream.skipToEnd();
+ state.contexts.pop();
+ return "string";
+ }
+ }
+ while (!stream.eol()) {
+ if (stream.match("$${") || stream.match("%%{")) continue;
+ if (stream.match(/^(?:\$|%)\{~?/, false)) {
+ if (stream.pos > stream.start) return "string";
+ stream.match(/^(?:\$|%)\{~?/);
+ state.contexts.push({type: "interpolation", depth: 0});
+ return "punctuation";
+ }
+ var ch = stream.next();
+ if (context.type == "string") {
+ if (ch == "\\") {
+ stream.next();
+ } else if (ch == '"') {
+ state.contexts.pop();
+ break;
+ }
+ }
+ }
+ return "string";
+ }
+
+ return {
+ startState: function() {
+ return {contexts: [{type: "expression"}]};
+ },
+ copyState: function(state) {
+ // Each editor snapshot needs its own interpolation brace counters.
+ return {contexts: state.contexts.map(function(context) {
+ var copy = {};
+ for (var key in context) copy[key] = context[key];
+ return copy;
+ })};
+ },
+ token: function(stream, state) {
+ var context = state.contexts[state.contexts.length - 1];
+ if (context.type == "comment") return tokenComment(stream, state);
+ if (context.type == "string" || context.type == "heredoc")
+ return tokenTemplate(stream, state, context);
+ if (stream.eatSpace()) return null;
+ if (stream.match("#") || stream.match("//")) {
+ stream.skipToEnd();
+ return "comment";
+ }
+ if (stream.match("/*")) {
+ state.contexts.push({type: "comment"});
+ return tokenComment(stream, state);
+ }
+ if (stream.eat('"')) {
+ state.contexts.push({type: "string"});
+ return "string";
+ }
+ var heredoc = stream.match(/^<<(-?)([A-Za-z_\u00a1-\uffff][\w\u00a1-\uffff-]*)[ \t]*$/);
+ if (heredoc) {
+ state.contexts.push({type: "heredoc", delimiter: heredoc[2], indented: !!heredoc[1]});
+ return "string";
+ }
+ if (context.type == "interpolation") {
+ if (context.depth == 0 && stream.match(/^~?\}/)) {
+ state.contexts.pop();
+ return "punctuation";
+ }
+ if (stream.peek() == "{") context.depth++;
+ else if (stream.peek() == "}") context.depth--;
+ }
+ if (stream.match(/^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/)) return "number";
+ var word = stream.match(identifier);
+ if (word) {
+ if (/^(?:true|false|null)$/.test(word[0])) return "atom";
+ if (keywords.test(word[0])) return "keyword";
+ if (stream.match(/^[ \t]*=(?!=|>)/, false)) return "property";
+ if (stream.match(/^[ \t]*\(/, false)) return "def";
+ return "variable";
+ }
+ if (stream.match(/^(?:[=!<>]=|=>|&&|\|\||[-+*\/%=!?<>:])/)) return "operator";
+ if (stream.match(/^[{}\[\]()]/)) return "bracket";
+ stream.next();
+ return null;
+ },
+ lineComment: "#",
+ blockCommentStart: "/*",
+ blockCommentEnd: "*/",
+ fold: "brace"
+ };
+ });
+
+ CodeMirror.defineMIME("text/x-hcl", "hcl");
+});
diff --git a/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/meta.js b/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/meta.js
index 087b1d3761..dfd48d7a55 100644
--- a/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/meta.js
+++ b/server-core/src/main/java/io/onedev/server/web/asset/codemirror/mode/meta.js
@@ -66,6 +66,7 @@
{name: "Haskell", mime: "text/x-haskell", mode: "haskell", ext: ["hs"]},
{name: "Haskell (Literate)", mime: "text/x-literate-haskell", mode: "haskell-literate", ext: ["lhs"]},
{name: "Haxe", mime: "text/x-haxe", mode: "haxe", ext: ["hx"]},
+ {name: "HCL", mime: "text/x-hcl", mode: "hcl", ext: ["tf", "tfvars", "hcl"], alias: ["terraform"]},
{name: "HXML", mime: "text/x-hxml", mode: "haxe", ext: ["hxml"]},
{name: "ASP.NET", mime: "application/x-aspx", mode: "htmlembedded", ext: ["aspx"], alias: ["asp", "aspx"]},
{name: "HTML", mime: "text/html", mode: "htmlmixed", ext: ["html", "htm", "handlebars", "hbs"], alias: ["xhtml"]},