Skip to content

Commit

Permalink
Merge pull request #15 from tigormal/main
Browse files Browse the repository at this point in the history
Runnables, injections and minor improvements
  • Loading branch information
foxoman authored Nov 6, 2024
2 parents 69d10fb + 2269626 commit 5cbd177
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 28 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ To use nph as a formatter, add this to your settings
```

## To-Do List for Project:
- Fix Project Config options
- Add runnable functionality
- Implement Debug Task
- Develop Macros
- Create code snippets
- [ ] Fix Project Config options
- [x] Add runnable functionality
- [ ] Implement Debug Task
- [ ] Develop Macros
- [ ] Create code snippets
4 changes: 4 additions & 0 deletions extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ language = "Nim"
[grammars.nim]
repository = "https://github.com/alaviss/tree-sitter-nim"
commit = "897e5d346f0b59ed62b517cfb0f1a845ad8f0ab7"

[grammars.nim_format_string]
repository = "https://github.com/aMOPel/tree-sitter-nim-format-string"
commit = "d45f75022d147cda056e98bfba68222c9c8eca3a"
1 change: 1 addition & 0 deletions languages/nim/brackets.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
("(" @open ")" @close)
("[" @open "]" @close)
("{" @open "}" @close)
("{." @open ".}" @close)
("#[" @open "]#" @close)
3 changes: 2 additions & 1 deletion languages/nim/config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name = "Nim"
code_fence_block_name = "nim"
grammar = "nim"
path_suffixes = ["nim"]
path_suffixes = ["nim", "nims"]
autoclose_before = ";:.,=}])>"

brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "{.", end = ".", close = true, newline = true, not_in = ["string", "comment"] },
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["comment", "string"] },
Expand Down
48 changes: 27 additions & 21 deletions languages/nim/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@
(generalized_string
function: (identifier) @_string_prefix
.
(string_content) @injection.content
(#set! injection.language "regex")
(string_content) @content
(#set! "language" "regex")
(#any-of? @_string_prefix "re" "rex"))

; format string in generalized_strings
(generalized_string
function: (identifier) @_string_prefix
.
(string_content) @injection.content
(#set! injection.language "nim_format_string")
(string_content) @content
(#set! "language" "nim_format_string")
(#eq? @_string_prefix "fmt"))

; format string in normal strings with & prefix
(prefix_expression
operator: (operator) @_string_prefix
.
(_
(string_content) @injection.content)
(#set! injection.language "nim_format_string")
(string_content) @content)
(#set! "language" "nim_format_string")
(#eq? @_string_prefix "&"))

; sql in generalized_strings
; and anything you like as long as the function name is the same as the injected language's parser

(generalized_string
function: (identifier) @language
(string_content) @content
(#not-any-of? @language "re" "rex" "fmt" "md"))

(generalized_string
function: (identifier) @injection.language
(string_content) @injection.content
(#not-any-of? @injection.language "re" "rex" "fmt"))
function: (identifier) @_string_prefix
.
(string_content) @content
(#set! "language" "markdown")
(#eq? @_string_prefix "md"))

; =============================================================================
; emit pragma
Expand All @@ -45,35 +53,33 @@
; {.emit: "<javascript code>".}
; normal strings
((comment
(comment_content) @injection.language)
(comment_content) @language)
.
(pragma_statement
(pragma_list
(colon_expression
left: (identifier) @_emit_keyword
(#eq? @_emit_keyword "emit")
right: (_
(string_content) @injection.content)))))
(string_content) @content)))))


; =============================================================================
; asm statement
; works same as emit pragma, needs preceding comment with language name
((comment
(comment_content) @injection.language)
.
(assembly_statement
((assembly_statement
(_
(string_content) @injection.content)))
(string_content) @content))
(#set! "language" "asm"))

; =============================================================================
; comments
; NOTE: ts "comment" parser heavily impacts performance
; markdown parser in documentation_comment
(documentation_comment
(comment_content) @injection.content
(#set! injection.language "markdown_inline"))
(comment_content) @content
(#set! "language" "markdown_inline"))

; markdown parser in block_documentation_comment
(block_documentation_comment
(comment_content) @injection.content
(#set! injection.language "markdown"))
(comment_content) @content
(#set! "language" "markdown"))
58 changes: 58 additions & 0 deletions languages/nim/runnables.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
; runnable main module
((when
condition: (identifier) @run (#eq? @run "isMainModule"))
(#set! tag nim-main))

((when
condition: (parenthesized ((identifier) @run (#eq? @run "isMainModule")) ))
(#set! tag nim-main))

; runnable tests
; suite
((call
function: (identifier) @run (#eq? @run "suite")
(argument_list
((interpreted_string_literal
. (string_content) @nim_suite)
)
":"
)) @_nim-unittest
(#set! tag nim-unittest))

; single test
(source_file ((call
function: (identifier) @run (#eq? @run "test")
(argument_list
((interpreted_string_literal
. (string_content) @nim_test)
)
":"
)) @_nim-unittest
(#set! tag nim-unittest-single)))

; test in the suite
(
(call
function: (identifier) @_suite_name (#eq? @_suite_name "suite")
(argument_list
((interpreted_string_literal
. (string_content) @nim_suite)
)
":"
; .
(statement_list
(_)?
(call
function: (identifier) @run (#eq? @run "test")
(argument_list
((interpreted_string_literal
. (string_content) @nim_test)
)
":"
)
)
)
)
) @_nim-unittest
(#set! tag nim-unittest-suite)
)
27 changes: 26 additions & 1 deletion languages/nim/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"command": "nim",
"args": ["c", "-r", "$ZED_FILE"],
"use_new_terminal": false,
"reveal": "always"
"reveal": "always",
"tags": ["nim-main"]
},
{
"label": "Nimble Run",
Expand All @@ -26,5 +27,29 @@
"args": ["test"],
"use_new_terminal": false,
"reveal": "always"
},
{
"label": "Unittest suite",
"command": "nim",
"args": ["c", "-r", "$ZED_FILE", "\"$ZED_CUSTOM_nim_suite::\""],
"use_new_terminal": false,
"reveal": "always",
"tags": ["nim-unittest"]
},
{
"label": "Unittest single test",
"command": "nim",
"args": ["c", "-r", "$ZED_FILE", "\"$ZED_CUSTOM_nim_test\""],
"use_new_terminal": false,
"reveal": "always",
"tags": ["nim-unittest-single"]
},
{
"label": "Unittest test in suite",
"command": "nim",
"args": ["c", "-r", "$ZED_FILE", "\"$ZED_CUSTOM_nim_suite::$ZED_CUSTOM_nim_test\""],
"use_new_terminal": false,
"reveal": "always",
"tags": ["nim-unittest-suite"]
}
]
3 changes: 3 additions & 0 deletions languages/nim_format_string/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "Nim Format String"
grammar = "nim_format_string"
path_suffixes = ["nim_format_string"]
20 changes: 20 additions & 0 deletions languages/nim_format_string/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(string_literal) @string
(matching_curlies
opening_curly: (opening_curly) @punctuation.special
equals: (equals)? @punctuation.special
closing_curly: (closing_curly) @punctuation.special)

(format_specifiers
colon: (colon) @punctuation.delimiter
fill_align: (fill_align)? @conditional.ternary
sign: (sign)? @operator
hash: (hash)? @punctuation.special
zero: (zero)? @field
min_width: (min_width)? @constant
precision: (precision)? @constant
type: (type)? @type)

(matching_curlies
nim_expression: (nim_expression
escaped_curly: (escaped_curly)+ @string.escape)
)
1 change: 1 addition & 0 deletions languages/nim_format_string/injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((matching_curlies (nim_expression) @content) (#set! "language" "nim"))

0 comments on commit 5cbd177

Please sign in to comment.