feat: Add HCL support

Signed-off-by: Xuanwo <github@xuanwo.io>
pull/264/head
Xuanwo 2022-04-23 01:10:08 +07:00
parent 82c06d348a
commit 4b3213acdc
No known key found for this signature in database
GPG Key ID: C423B4FA6B48E945
8 changed files with 219 additions and 2 deletions

@ -39,7 +39,10 @@ impl TreeSitterParser {
.flag_if_supported("-Wno-ignored-qualifiers")
// Workaround for: https://github.com/ganezdragon/tree-sitter-perl/issues/16
// should be removed after fixed.
.flag_if_supported("-Wno-return-type");
.flag_if_supported("-Wno-return-type")
// Workaround for: https://github.com/MichaHoffmann/tree-sitter-hcl/issues/18
// should be removed after fixed.
.flag_if_supported("-Wno-missing-field-initializers");
if cfg!(windows) {
cpp_build.flag("/std:c++14");
@ -127,6 +130,11 @@ fn main() {
src_dir: "vendor/tree-sitter-haskell-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-hcl",
src_dir: "vendor/tree-sitter-hcl-src",
extra_files: vec!["scanner.cc"],
},
TreeSitterParser {
name: "tree-sitter-janet-simple",
src_dir: "vendor/tree-sitter-janet-simple-src",

@ -40,6 +40,9 @@ b8c51005df7e1eaaeaf738a4353ac88f -
sample_files/haskell_before.hs sample_files/haskell_after.hs
9c668c79e56f1e1cecf1c02759c195a9 -
sample_files/hcl_before.hcl sample_files/hcl_after.hcl
bbd894db56c616c18bbe3afb23f1a72b -
sample_files/helpful_before.el sample_files/helpful_after.el
bce74573e003cc6b729a63a4bc34c4af -

@ -0,0 +1,89 @@
resource "example" "literals" {
attr1 = "val1"
tupl1 = [ 1, 2, 3.4, "foo" ]
tupl2 = []
obj1 = { foo = "bar", baz = quz }
null1 = null
bool1 = true
bool2 = false
esc1 = "\" \t \UFF11FF22 \uFFFF \n"
esc2 = "$${} %%{}"
num1 = 2
num2 = 2.112
num3 = "Hello, World"
num4 = 2.112e+12
num5 = 2.112E+12
num6 = 2.112E-12
num7 = 0x21FF
strx = "Hello, Hcl"
}
resource "example" "comments" {
// comment, World!
/*
comment
*/
}
resource "example" "splat_expressions" {
splat1 = foo.*.bar.baz[0]
splat2 = foo[*].bar.baz[0]
}
resource "example" "for_expressions" {
for1 = { for i, v in ["a", "a", "b"] : v => i... }
for2 = [ for k, v in x : "${k}-${v}" ]
for3 = { for k, v in x: k => v }
for5 = { for v in y : v => v + 1 }
for6 = [ for v in x : v if v < 3 ]
}
resource "example" "function_expressions" {
func1 = is_number("123")
func3 = withobject({
"foo" : 2,
"bar" : baz,
key : val,
fizz : buzz,
})
}
resource "example" "binary_expressions" {
cond1 = (1 == 2) ? 1 : "Hello, HCL"
bin1 = ((1+2)%3)*4
}
resource "example" "template_expressions" {
tpl1 = "prefix-${var.bar}"
tpl2 = "prefix-${func("bar")}"
tpl3 = "prefix-${func("nested-${var.bar}")}"
tpl4 = <<EOF
%{ for a in f(b) ~}
${func("foo${ a }")}
%{ endfor ~}
EOF
tpl5 = <<-EOF
%{~if cond~}
"foo"
%{~else~}
4
%{~endif~}
EOF
tpl6 = <<-EOF
%{ for a in f(b) ~}
%{~if a~} "true" %{~else~} "false" %{~endif~}
%{ endfor ~}
EOF
}
resource "example" "nested_blocks" {
nested_block "first" {
attr1 = "foo"
nested_block "second" {
attr1 = "bar"
}
}
}

@ -0,0 +1,95 @@
resource "example" "literals" {
attr1 = "val1"
tupl1 = [ 1, 2, 3.4, "foo" ]
tupl2 = []
obj1 = { foo = "bar", baz = quz }
null1 = null
bool1 = true
bool2 = false
esc1 = "\" \t \UFF11FF22 \uFFFF \n"
esc2 = "$${} %%{}"
num1 = 2
num2 = 2.112
num3 = 2.112e-12
num4 = 2.112e+12
num5 = 2.112E+12
num6 = 2.112E-12
num7 = 0x21FF
}
resource "example" "comments" {
// comment
# comment
/*
comment
*/
}
resource "example" "splat_expressions" {
splat1 = foo.*.bar.baz[0]
splat2 = foo[*].bar.baz[0]
}
resource "example" "for_expressions" {
for1 = { for i, v in ["a", "a", "b"] : v => i... }
for2 = [ for k, v in x : "${k}-${v}" ]
for3 = { for k, v in x: k => v }
for4 = [ for v in x : v ]
for5 = { for v in x : v => v }
for6 = [ for v in x : v if v < 3 ]
}
resource "example" "function_expressions" {
func1 = is_number("123")
func2 = multiline(
arg1,
arg2,
arg3...
)
func3 = withobject({
"foo" : 2,
"bar" : baz,
key : val,
fizz : buzz,
})
}
resource "example" "binary_expressions" {
cond1 = (1 == 2) ? 1 : "foobar"
bin1 = ((1+2)%3)*4
}
resource "example" "template_expressions" {
tpl1 = "prefix-${var.bar}"
tpl2 = "prefix-${func("bar")}"
tpl3 = "prefix-${func("nested-${var.bar}")}"
tpl4 = <<EOF
%{ for a in f(b) ~}
${func("foo${ a }")}
%{ endfor ~}
EOF
tpl5 = <<-EOF
%{~if cond~}
"foo"
%{~else~}
4
%{~endif~}
EOF
tpl6 = <<-EOF
%{ for a in f(b) ~}
%{~if a~} "true" %{~else~} "false" %{~endif~}
%{ endfor ~}
EOF
}
resource "example" "nested_blocks" {
nested_block "first" {
attr1 = "foo"
nested_block "second" {
attr1 = "bar"
}
}
}

@ -32,6 +32,7 @@ pub enum Language {
Gleam,
Go,
Haskell,
Hcl,
Janet,
Java,
JavaScript,
@ -109,6 +110,7 @@ fn from_emacs_mode_header(src: &str) -> Option<Language> {
"gleam" => Some(Gleam),
"go" => Some(Go),
"haskell" => Some(Haskell),
"hcl" => Some(Hcl),
"janet" => Some(Janet),
"java" => Some(Java),
"js" | "js2" => Some(JavaScript),
@ -212,6 +214,7 @@ pub fn from_extension(extension: &OsStr) -> Option<Language> {
"gleam" => Some(Gleam),
"go" => Some(Go),
"hs" => Some(Haskell),
"hcl" => Some(Hcl),
"janet" | "jdn" => Some(Janet),
"java" => Some(Java),
"cjs" | "js" | "mjs" => Some(JavaScript),

@ -56,7 +56,7 @@ fn prefer_outer_delimiter(language: guess_language::Language) -> bool {
EmacsLisp | Clojure | CommonLisp | Janet => true,
// JSON and TOML are like Lisp: the outer delimiter in an array object
// is the most relevant.
Json | Toml => true,
Json | Toml | Hcl => true,
// For everything else, prefer the inner delimiter. These
// languages have syntax like `foo(bar)` or `foo[bar]` where
// the inner delimiter is more relevant.

@ -57,6 +57,7 @@ extern "C" {
fn tree_sitter_elm() -> ts::Language;
fn tree_sitter_go() -> ts::Language;
fn tree_sitter_haskell() -> ts::Language;
fn tree_sitter_hcl() -> ts::Language;
fn tree_sitter_janet_simple() -> ts::Language;
fn tree_sitter_java() -> ts::Language;
fn tree_sitter_javascript() -> ts::Language;
@ -307,6 +308,23 @@ pub fn from_language(language: guess::Language) -> TreeSitterConfig {
.unwrap(),
}
}
Hcl => {
let language = unsafe { tree_sitter_hcl() };
TreeSitterConfig {
name: "Hcl",
language,
atom_nodes: vec!["string"].into_iter().collect(),
delimiter_tokens: vec![
("[", "]"),
("(", ")"),
("{", "}"),
("%{", "}"),
("%{~", "~}"),
("${", "}"),
],
highlight_query: ts::Query::new(language, "").unwrap(),
}
}
Janet => {
let language = unsafe { tree_sitter_janet_simple() };
TreeSitterConfig {

@ -0,0 +1 @@
tree-sitter-hcl/src