Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Mojo #10743

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/generated/lang-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
| mermaid | ✓ | | | |
| meson | ✓ | | ✓ | |
| mint | | | | `mint` |
| mojo | ✓ | | | `mojo-lsp-server` |
| move | ✓ | | | |
| msbuild | ✓ | | ✓ | |
| nasm | ✓ | ✓ | | |
Expand Down
17 changes: 17 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ markdown-oxide = { command = "markdown-oxide" }
marksman = { command = "marksman", args = ["server"] }
metals = { command = "metals", config = { "isHttpEnabled" = true, metals = { inlayHints = { typeParameters = {enable = true} , hintsInPatternMatch = {enable = true} } } } }
mint = { command = "mint", args = ["ls"] }
mojo-lsp = { command = "mojo-lsp-server" }
nil = { command = "nil" }
nimlangserver = { command = "nimlangserver" }
nimlsp = { command = "nimlsp" }
Expand Down Expand Up @@ -372,6 +373,22 @@ block-comment-tokens = { start = "/*", end = "*/" }
language-servers = [ "mint" ]
indent = { tab-width = 2, unit = " " }

[[language]]
name = "mojo"
scope = "source.mojo"
roots = ["__init__.mojo"]
injection-regex = "mojo"
file-types = ["mojo", "🔥"]
language-servers = [ "mojo-lsp" ]
comment-token = "#"
indent = { tab-width = 4, unit = " " }
auto-format = true
formatter = { command = "mojo", args = ["format", "-q", "-"]}

[[grammar]]
name = "mojo"
source = { git = "https://github.com/lsh/tree-sitter-mojo", rev = "3d7c53b8038f9ebbb57cd2e61296180aa5c1cf64" }

[[language]]
name = "janet"
scope = "source.janet"
Expand Down
152 changes: 152 additions & 0 deletions runtime/queries/mojo/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
(attribute attribute: (identifier) @property)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The highlights need to be updated to use Helix-specific capture names: https://docs.helix-editor.com/master/themes.html#syntax-highlighting

For example @property should become @variable.other.member, (true) and (false) should be captured as @constant.builtin.boolean, escape sequences as @constant.character.escape, etc.

Copy link
Contributor Author

@dmitry-salin dmitry-salin May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the queries to be a superset of the current Python queries.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a superset you can use a comment in the query files ; inherits: python which will do the same thing as copy/pasting in the python query for that file

Copy link
Contributor Author

@dmitry-salin dmitry-salin May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that for now it's OK to use inheritance. If I understand correctly, later if there are any additions, I will need to delete the comment and make a full modified version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the python queries start using a new node that isn't in the mojo grammar then we will need to replace the ; inherits: python. But if you just want to add onto the python queries you can place patterns before or after the inherits comment. The ones before are higher precedence and the ones after lower

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, the differences are mainly related to modifying queries rather than adding new ones. So, I think I'm done with the changes, further improvements depend on the tree-sitter repository.

(type (identifier) @type)

; Function calls

(decorator) @function

(call
function: (attribute attribute: (identifier) @function.method))
(call
function: (identifier) @function)

; Function definitions

(function_definition
name: (identifier) @function)

; Identifier naming conventions

((identifier) @type
(#match? @type "^[A-Z]"))

((identifier) @constant
(#match? @constant "^_*[A-Z][A-Z\\d_]*$"))

; Builtin functions

((call
function: (identifier) @function.builtin)
(#match?
@function.builtin
"^(abs|all|always_inline|any|ascii|bin|bool|breakpoint|bytearray|bytes|callable|chr|classmethod|compile|complex|constrained|delattr|dict|dir|divmod|enumerate|eval|exec|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unroll|vars|zip|__mlir_attr|__mlir_op|__mlir_type|__import__)$"))

; Literals

[
(none)
(true)
(false)
] @constant.builtin

[
(integer)
(float)
] @number

(comment) @comment
(string) @string
(escape_sequence) @escape

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

(interpolation
"{" @punctuation.special
"}" @punctuation.special) @embedded

; Docstrings.
(function_definition
"async"?
"def"
name: (_)
(parameters)?
body: (block (expression_statement (string) @string.doc)))

[
"-"
"-="
"!="
"*"
"**"
"**="
"*="
"/"
"//"
"//="
"/="
"&"
"%"
"%="
"^"
"+"
"->"
"+="
"<"
"<<"
"<="
"<>"
"="
":="
"=="
">"
">="
">>"
"|"
"~"
"and"
"in"
"is"
"not"
"or"
"is not"
"not in"
] @operator

[
"as"
"alias"
"assert"
"async"
"await"
"borrowed"
"break"
"class"
"continue"
"def"
"del"
"elif"
"else"
"except"
"exec"
"finally"
"fn"
"for"
"from"
"global"
"if"
"import"
"inout"
"lambda"
"nonlocal"
"owned"
"pass"
"print"
"raise"
"raises"
"return"
"struct"
"try"
"var"
"while"
"with"
"yield"
"match"
"case"
] @keyword
Loading