diff --git a/src/hcl/hcl.contribution.ts b/src/hcl/hcl.contribution.ts new file mode 100644 index 00000000..15b0b513 --- /dev/null +++ b/src/hcl/hcl.contribution.ts @@ -0,0 +1,14 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import { registerLanguage } from '../_.contribution'; + +registerLanguage({ + id: 'hcl', + extensions: ['.tf', '.tfvars', '.hcl'], + aliases: ['Terraform', 'tf', 'HCL', 'hcl'], + loader: () => import('./hcl') +}); diff --git a/src/hcl/hcl.ts b/src/hcl/hcl.ts new file mode 100644 index 00000000..7daf4135 --- /dev/null +++ b/src/hcl/hcl.ts @@ -0,0 +1,117 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import IRichLanguageConfiguration = monaco.languages.LanguageConfiguration; +import ILanguage = monaco.languages.IMonarchLanguage; + +export const conf: IRichLanguageConfiguration = { + comments: { + lineComment: '//', + blockComment: ['/*', '*/'], + }, + brackets: [ + ['{', '}'], + ['[', ']'], + ['(', ')'] + ], + autoClosingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"', notIn: ['string'] }, + { open: '\'', close: '\'', notIn: ['string', 'comment'] }, + ], + surroundingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"' }, + { open: '\'', close: '\'' }, + ] +}; + +export const language = { + defaultToken: '', + tokenPostfix: '.tf', + + keywords: [ + 'var', 'local', 'module', 'data', 'path', 'terraform', + 'resource', 'provider', 'variable', 'output', 'locals', + 'any', 'string', 'number', 'bool', + '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', + 'true', 'false', 'null', + 'if ', 'else ', 'endif ', 'for ', 'in', 'endfor' + ], + + operators: [ + '>=', '<=', '==', '!=', '+', '-', '*', '/', '%', '&&', '||', '!', '<', '>', '?', '...', ':', + ], + + // we include these common regular expressions + symbols: /[=>](?!@symbols)/, '@brackets'], + [/@symbols/, { + cases: { + '@operators': 'delimiter', + '@default': '' + } + }], + + // numbers + [/\d*\d+[eE]([\-+]?\d+)?/, 'number.float'], + [/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'], + [/\d[\d']*/, 'number'], + [/\d/, 'number'], + + // delimiter: after number because of .\d floats + [/[;,.]/, 'delimiter'], + + // strings + [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string + [/"/, 'string', '@string'], + ], + + whitespace: [ + [/[ \t\r\n]+/, ''], + [/\/\*/, 'comment', '@comment'], + [/\/\/.*$/, 'comment'], + [/#.*$/, 'comment'], + ], + + comment: [ + [/[^\/*]+/, 'comment'], + [/\*\//, 'comment', '@pop'], + [/[\/*]/, 'comment'] + ], + + string: [ + [/[^\\"]+/, 'string'], + [/@escapes/, 'string.escape'], + [/\\./, 'string.escape.invalid'], + [/"/, 'string', '@pop'] + ], + }, +}; diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index 1499ed40..24f7cf16 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -21,6 +21,7 @@ import './fsharp/fsharp.contribution'; import './go/go.contribution'; import './graphql/graphql.contribution'; import './handlebars/handlebars.contribution'; +import './hcl/hcl.contribution'; import './html/html.contribution'; import './ini/ini.contribution'; import './java/java.contribution';