Commit b94f1a65 authored by Hugo Fonseca's avatar Hugo Fonseca Committed by Kushal Pandya

Adding syntax highlighting for terraform / hcl

parent 1972de01
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See https://github.com/microsoft/monaco-languages/blob/master/LICENSE.md
*--------------------------------------------------------------------------------------------*/
/* eslint-disable no-useless-escape */
/* eslint-disable @gitlab/require-i18n-strings */
const conf = {
comments: {
lineComment: '//',
blockComment: ['/*', '*/'],
},
brackets: [['{', '}'], ['[', ']'], ['(', ')']],
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"', notIn: ['string'] },
],
surroundingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"' },
],
};
const language = {
defaultToken: '',
tokenPostfix: '.hcl',
keywords: [
'var',
'local',
'path',
'for_each',
'any',
'string',
'number',
'bool',
'true',
'false',
'null',
'if ',
'else ',
'endif ',
'for ',
'in',
'endfor',
],
operators: [
'=',
'>=',
'<=',
'==',
'!=',
'+',
'-',
'*',
'/',
'%',
'&&',
'||',
'!',
'<',
'>',
'?',
'...',
':',
],
symbols: /[=><!~?:&|+\-*\/\^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
terraformFunctions: /(abs|ceil|floor|log|max|min|pow|signum|chomp|format|formatlist|indent|join|lower|regex|regexall|replace|split|strrev|substr|title|trimspace|upper|chunklist|coalesce|coalescelist|compact|concat|contains|distinct|element|flatten|index|keys|length|list|lookup|map|matchkeys|merge|range|reverse|setintersection|setproduct|setunion|slice|sort|transpose|values|zipmap|base64decode|base64encode|base64gzip|csvdecode|jsondecode|jsonencode|urlencode|yamldecode|yamlencode|abspath|dirname|pathexpand|basename|file|fileexists|fileset|filebase64|templatefile|formatdate|timeadd|timestamp|base64sha256|base64sha512|bcrypt|filebase64sha256|filebase64sha512|filemd5|filemd1|filesha256|filesha512|md5|rsadecrypt|sha1|sha256|sha512|uuid|uuidv5|cidrhost|cidrnetmask|cidrsubnet|tobool|tolist|tomap|tonumber|toset|tostring)/,
terraformMainBlocks: /(module|data|terraform|resource|provider|variable|output|locals)/,
tokenizer: {
root: [
// highlight main blocks
[
/^@terraformMainBlocks([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)(\{)/,
['type', '', 'string', '', 'string', '', '@brackets'],
],
// highlight all the remaining blocks
[
/(\w+[ \t]+)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)(\{)/,
['identifier', '', 'string', '', 'string', '', '@brackets'],
],
// highlight block
[
/(\w+[ \t]+)([ \t]*)([\w-]+|"[\w-]+"|)([ \t]*)([\w-]+|"[\w-]+"|)(=)(\{)/,
['identifier', '', 'string', '', 'operator', '', '@brackets'],
],
// terraform general highlight - shared with expressions
{ include: '@terraform' },
],
terraform: [
// highlight terraform functions
[/@terraformFunctions(\()/, ['type', '@brackets']],
// all other words are variables or keywords
[
/[a-zA-Z_]\w*-*/, // must work with variables such as foo-bar and also with negative numbers
{
cases: {
'@keywords': { token: 'keyword.$0' },
'@default': 'variable',
},
},
],
{ include: '@whitespace' },
{ include: '@heredoc' },
// delimiters and operators
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[
/@symbols/,
{
cases: {
'@operators': 'operator',
'@default': '',
},
},
],
// numbers
[/\d*\d+[eE]([\-+]?\d+)?/, 'number.float'],
[/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'],
[/\d[\d']*/, 'number'],
[/\d/, 'number'],
[/[;,.]/, 'delimiter'], // delimiter: after number because of .\d floats
// strings
[/"/, 'string', '@string'], // this will include expressions
[/'/, 'invalid'],
],
heredoc: [
[
/<<[-]*\s*["]?([\w\-]+)["]?/,
{ token: 'string.heredoc.delimiter', next: '@heredocBody.$1' },
],
],
heredocBody: [
[
/^([\w\-]+)$/,
{
cases: {
'$1==$S2': [
{
token: 'string.heredoc.delimiter',
next: '@popall',
},
],
'@default': 'string.heredoc',
},
},
],
[/./, 'string.heredoc'],
],
whitespace: [
[/[ \t\r\n]+/, ''],
[/\/\*/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
[/#.*$/, 'comment'],
],
comment: [[/[^\/*]+/, 'comment'], [/\*\//, 'comment', '@pop'], [/[\/*]/, 'comment']],
string: [
[/\$\{/, { token: 'delimiter', next: '@stringExpression' }],
[/[^\\"\$]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, 'string', '@popall'],
],
stringInsideExpression: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, 'string', '@pop'],
],
stringExpression: [
[/\}/, { token: 'delimiter', next: '@pop' }],
[/"/, 'string', '@stringInsideExpression'],
{ include: '@terraform' },
],
},
};
export default {
id: 'hcl',
extensions: ['.tf', '.tfvars', '.hcl'],
aliases: ['Terraform', 'tf', 'HCL', 'hcl'],
conf,
language,
};
import vue from './vue'; import vue from './vue';
import hcl from './hcl';
const languages = [vue]; const languages = [vue, hcl];
export default languages; export default languages;
---
title: IDE editor - Adding syntax highlighting for terraform / hcl
merge_request: 44056
author:
type: added
import { editor } from 'monaco-editor';
import { registerLanguages } from '~/ide/utils';
import hcl from '~/ide/lib/languages/hcl';
describe('tokenization for .tf files', () => {
beforeEach(() => {
registerLanguages(hcl);
});
it.each([
['// Foo', [[{ language: 'hcl', offset: 0, type: 'comment.hcl' }]]],
['/* Bar */', [[{ language: 'hcl', offset: 0, type: 'comment.hcl' }]]],
['/*', [[{ language: 'hcl', offset: 0, type: 'comment.hcl' }]]],
[
'foo = "bar"',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'string.hcl' },
],
],
],
[
'variable "foo" {',
[
[
{ language: 'hcl', offset: 0, type: 'type.hcl' },
{ language: 'hcl', offset: 8, type: '' },
{ language: 'hcl', offset: 9, type: 'string.hcl' },
{ language: 'hcl', offset: 14, type: '' },
{ language: 'hcl', offset: 15, type: 'delimiter.curly.hcl' },
],
],
],
[
// eslint-disable-next-line no-template-curly-in-string
' api_key = "${var.foo}"',
[
[
{ language: 'hcl', offset: 0, type: '' },
{ language: 'hcl', offset: 2, type: 'variable.hcl' },
{ language: 'hcl', offset: 9, type: '' },
{ language: 'hcl', offset: 10, type: 'operator.hcl' },
{ language: 'hcl', offset: 11, type: '' },
{ language: 'hcl', offset: 12, type: 'string.hcl' },
{ language: 'hcl', offset: 13, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 15, type: 'keyword.var.hcl' },
{ language: 'hcl', offset: 18, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 19, type: 'variable.hcl' },
{ language: 'hcl', offset: 22, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 23, type: 'string.hcl' },
],
],
],
[
'resource "aws_security_group" "firewall" {',
[
[
{ language: 'hcl', offset: 0, type: 'type.hcl' },
{ language: 'hcl', offset: 8, type: '' },
{ language: 'hcl', offset: 9, type: 'string.hcl' },
{ language: 'hcl', offset: 29, type: '' },
{ language: 'hcl', offset: 30, type: 'string.hcl' },
{ language: 'hcl', offset: 40, type: '' },
{ language: 'hcl', offset: 41, type: 'delimiter.curly.hcl' },
],
],
],
[
' network_interface {',
[
[
{ language: 'hcl', offset: 0, type: '' },
{ language: 'hcl', offset: 2, type: 'identifier.hcl' },
{ language: 'hcl', offset: 20, type: 'delimiter.curly.hcl' },
],
],
],
[
'foo = [1, 2, "foo"]',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'delimiter.square.hcl' },
{ language: 'hcl', offset: 7, type: 'number.hcl' },
{ language: 'hcl', offset: 8, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 9, type: '' },
{ language: 'hcl', offset: 10, type: 'number.hcl' },
{ language: 'hcl', offset: 11, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 12, type: '' },
{ language: 'hcl', offset: 13, type: 'string.hcl' },
{ language: 'hcl', offset: 18, type: 'delimiter.square.hcl' },
],
],
],
[
'resource "foo" "bar" {}',
[
[
{ language: 'hcl', offset: 0, type: 'type.hcl' },
{ language: 'hcl', offset: 8, type: '' },
{ language: 'hcl', offset: 9, type: 'string.hcl' },
{ language: 'hcl', offset: 14, type: '' },
{ language: 'hcl', offset: 15, type: 'string.hcl' },
{ language: 'hcl', offset: 20, type: '' },
{ language: 'hcl', offset: 21, type: 'delimiter.curly.hcl' },
],
],
],
[
'foo = "bar"',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'string.hcl' },
],
],
],
[
'bar = 7',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'number.hcl' },
],
],
],
[
'baz = [1,2,3]',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'delimiter.square.hcl' },
{ language: 'hcl', offset: 7, type: 'number.hcl' },
{ language: 'hcl', offset: 8, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 9, type: 'number.hcl' },
{ language: 'hcl', offset: 10, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 11, type: 'number.hcl' },
{ language: 'hcl', offset: 12, type: 'delimiter.square.hcl' },
],
],
],
[
'foo = -12',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'operator.hcl' },
{ language: 'hcl', offset: 7, type: 'number.hcl' },
],
],
],
[
'bar = 3.14159',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'number.float.hcl' },
],
],
],
[
'foo = true',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'keyword.true.hcl' },
],
],
],
[
'foo = false',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'keyword.false.hcl' },
],
],
],
[
// eslint-disable-next-line no-template-curly-in-string
'bar = "${file("bing/bong.txt")}"',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'operator.hcl' },
{ language: 'hcl', offset: 5, type: '' },
{ language: 'hcl', offset: 6, type: 'string.hcl' },
{ language: 'hcl', offset: 7, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 9, type: 'type.hcl' },
{ language: 'hcl', offset: 13, type: 'delimiter.parenthesis.hcl' },
{ language: 'hcl', offset: 14, type: 'string.hcl' },
{ language: 'hcl', offset: 29, type: 'delimiter.parenthesis.hcl' },
{ language: 'hcl', offset: 30, type: 'delimiter.hcl' },
{ language: 'hcl', offset: 31, type: 'string.hcl' },
],
],
],
[
'a = 1e-10',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 1, type: '' },
{ language: 'hcl', offset: 2, type: 'operator.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'number.float.hcl' },
],
],
],
[
'b = 1e+10',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 1, type: '' },
{ language: 'hcl', offset: 2, type: 'operator.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'number.float.hcl' },
],
],
],
[
'c = 1e10',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 1, type: '' },
{ language: 'hcl', offset: 2, type: 'operator.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'number.float.hcl' },
],
],
],
[
'd = 1.2e-10',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 1, type: '' },
{ language: 'hcl', offset: 2, type: 'operator.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'number.float.hcl' },
],
],
],
[
'e = 1.2e+10',
[
[
{ language: 'hcl', offset: 0, type: 'variable.hcl' },
{ language: 'hcl', offset: 1, type: '' },
{ language: 'hcl', offset: 2, type: 'operator.hcl' },
{ language: 'hcl', offset: 3, type: '' },
{ language: 'hcl', offset: 4, type: 'number.float.hcl' },
],
],
],
])('%s', (string, tokens) => {
expect(editor.tokenize(string, 'hcl')).toEqual(tokens);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment