Skip to content

Commit

Permalink
Add TypeSpec support (#11412)
Browse files Browse the repository at this point in the history
* Add TypeSpec support

Adds support for TypeSpec <https://typespec.io> in helix.

* Resolve PR comments

* Pull in LICENSE

Co-authored-by: Michael Davis <[email protected]>

---------

Co-authored-by: Michael Davis <[email protected]>
  • Loading branch information
heaths and the-mikedavis authored Aug 9, 2024
1 parent c9c4452 commit d6431f4
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/lang-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
| tsx |||| `typescript-language-server` |
| twig || | | |
| typescript |||| `typescript-language-server` |
| typespec |||| `tsp-server` |
| typst || | | `tinymist`, `typst-lsp` |
| ungrammar || | | |
| unison || || |
Expand Down
18 changes: 18 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ taplo = { command = "taplo", args = ["lsp", "stdio"] }
templ = { command = "templ", args = ["lsp"] }
terraform-ls = { command = "terraform-ls", args = ["serve"] }
texlab = { command = "texlab" }
typespec = { command = "tsp-server", args = ["--stdio"] }
vala-language-server = { command = "vala-language-server" }
vhdl_ls = { command = "vhdl_ls", args = [] }
vlang-language-server = { command = "v-analyzer" }
Expand Down Expand Up @@ -767,6 +768,23 @@ indent = { tab-width = 2, unit = " " }
name = "typescript"
source = { git = "https://github.com/tree-sitter/tree-sitter-typescript", rev = "b1bf4825d9eaa0f3bdeb1e52f099533328acfbdf", subpath = "typescript" }

[[language]]
name = "typespec"
scope = "source.typespec"
injection-regex = "(tsp|typespec)"
language-id = "typespec"
file-types = ["tsp"]
roots = ["tspconfig.yaml"]
auto-format = true
comment-token = "//"
block-comment-tokens = { start = "/*", end = "*/" }
language-servers = ["typespec"]
indent = { tab-width = 2, unit = " " }

[[grammar]]
name = "typespec"
source = { git = "https://github.com/happenslol/tree-sitter-typespec", rev = "0ee05546d73d8eb64635ed8125de6f35c77759fe" }

[[language]]
name = "tsx"
scope = "source.tsx"
Expand Down
177 changes: 177 additions & 0 deletions runtime/queries/typespec/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
; Keywords

[
"is"
"extends"
"valueof"
] @keyword.operator

[
"namespace"
"scalar"
"interface"
"alias"
] @keyword

[
"model"
"enum"
"union"
] @keyword.storage.type

[
"op"
"fn"
"dec"
] @keyword.function

"extern" @keyword.storage.modifier

[
"import"
"using"
] @keyword.control.import

[
"("
")"
"{"
"}"
"<"
">"
"["
"]"
] @punctuation.bracket

[
","
";"
"."
":"
] @punctuation.delimiter

[
"|"
"&"
"="
"..."
] @operator

"?" @punctuation.special

; Imports

(import_statement
(quoted_string_literal) @string.special.path)

; Namespaces

(using_statement
module: (identifier_or_member_expression) @namespace)

(namespace_statement
name: (identifier_or_member_expression) @namespace)

; Comments

[
(single_line_comment)
] @comment.line

[
(multi_line_comment)
] @comment.block

; Decorators

(decorator
"@" @attribute
name: (identifier_or_member_expression) @attribute)

(augment_decorator_statement
name: (identifier_or_member_expression) @attribute)

(decorator
(decorator_arguments) @variable.parameter)

; Scalars

(scalar_statement
name: (identifier) @type)

; Models

(model_statement
name: (identifier) @type)

(model_property
name: (identifier) @variable.other.member)

; Operations

(operation_statement
name: (identifier) @function.method)

(operation_arguments
(model_property
name: (identifier) @variable.parameter))

(template_parameter
name: (identifier) @type.parameter)

(function_parameter
name: (identifier) @variable.parameter)

; Interfaces

(interface_statement
name: (identifier) @type)

(interface_statement
(interface_body
(interface_member
(identifier) @function.method)))

; Enums

(enum_statement
name: (identifier) @type.enum)

(enum_member
name: (identifier) @constant)

; Unions

(union_statement
name: (identifier) @type)

(union_variant
name: (identifier) @type.enum.variant)

; Aliases

(alias_statement
name: (identifier) @type)

; Built-in types

[
(quoted_string_literal)
(triple_quoted_string_literal)
] @string

(escape_sequence) @constant.character.escape

(boolean_literal) @constant.builtin.boolean

[
(decimal_literal)
(hex_integer_literal)
(binary_integer_literal)
] @constant.numeric.integer

(builtin_type) @type.builtin

; Identifiers

(identifier_or_member_expression) @type
18 changes: 18 additions & 0 deletions runtime/queries/typespec/indents.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
(model_expression)
(tuple_expression)
(namespace_body)
(interface_body)
(union_body)
(enum_body)
(template_arguments)
(template_parameters)
(operation_arguments)
] @indent.begin

[
"}"
")"
">"
"]"
] @indent.end
5 changes: 5 additions & 0 deletions runtime/queries/typespec/injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
([
(single_line_comment)
(multi_line_comment)
] @injection.content
(#set! injection.language "comment"))
51 changes: 51 additions & 0 deletions runtime/queries/typespec/textobjects.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; Classes

(enum_statement
(enum_body) @class.inside) @class.around

(model_statement
(model_expression) @class.inside) @class.around

(union_statement
(union_body) @class.inside) @class.around

; Interfaces

(interface_statement
(interface_body
(interface_member) @function.around) @class.inside) @class.around

; Comments

[
(single_line_comment)
(multi_line_comment)
] @comment.inside

[
(single_line_comment)
(multi_line_comment)
]+ @comment.around

; Functions

[
(decorator)
(decorator_declaration_statement)
(function_declaration_statement)
(operation_statement)
] @function.around

(function_parameter_list
(function_parameter)? @parameter.inside)* @function.inside

(decorator_arguments
(expression_list
(_) @parameter.inside)*) @function.inside

(operation_arguments
(model_property)? @parameter.inside)* @function.inside

(template_parameters
(template_parameter_list
(template_parameter) @parameter.inside)) @function.inside

0 comments on commit d6431f4

Please sign in to comment.