Skip to content

Commit

Permalink
Merge branch 'tests-for-ruby' into feature/modernize-tree-sitter
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioszabo committed Feb 25, 2023
2 parents 4a88e8c + 00185e8 commit cf9abdb
Show file tree
Hide file tree
Showing 12 changed files with 613 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/package-tests-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ jobs:
- name: Checkout the latest code
uses: actions/checkout@v2

- name: Setup node
uses: actions/setup-node@v2-beta
with:
node-version: 16

- name: Install Dependencies
run: yarn install || yarn install

Expand Down
98 changes: 98 additions & 0 deletions packages/language-ruby/grammars/tree-sitter-2-ruby.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: 'Ruby'
scopeName: 'source.ruby'
type: 'tree-sitter-2'
parser: 'tree-sitter-ruby'

injectionRegex: 'rb|ruby'
treeSitter:
grammar: 'ts/grammar.wasm'
syntaxQuery: 'ts/highlights.scm'
localsQuery: 'ts/locals.scm'

firstLineRegex: [
# shebang line
'^#!.*\\b(\w*ruby|rake)\\r?\\n'

# vim modeline
'vim\\b.*\\bset\\b.*\\b(filetype|ft|syntax)=ruby'
]

fileTypes: [
'rb',
'rake',
'Podfile',
'Brewfile',
'Rakefile',
'Gemfile'
]

comments:
start: '# '

folds: [
{
type: ['block', 'do_block']
start: {type: 'block_parameters'}
end: {index: -1}
}
{
type: 'begin'
start: {index: 0}
end: {type: 'rescue'}
}
{
type: 'heredoc_body',
end: {type: 'heredoc_end'}
}
{
type: [
'hash'
'array'
'begin'
'block'
'do_block'
]
start: {index: 0}
end: {index: -1}
}
{
type: 'argument_list'
start: {index: 0, type: '('}
end: {index: -1}
}
{
type: 'class'
start: {type: 'superclass'}
end: {index: -1}
}
{
type: 'class'
start: {index: 1}
end: {index: -1}
}
{
type: ['method', 'singleton_method']
start: {type: 'method_parameters'}
end: {index: -1}
}
{
type: ['method', 'singleton_method']
start: {index: 1}
end: {index: -1}
}
{
type: 'then',
start: {index: 0, type: '"then"'}
}
{
type: 'then'
}
{
type: 'case',
end: {index: -1}
}
{
type: 'else'
start: {index: 0}
}
]
Binary file added packages/language-ruby/grammars/ts/grammar.wasm
Binary file not shown.
146 changes: 146 additions & 0 deletions packages/language-ruby/grammars/ts/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
; Keywords

[
"alias"
"and"
"begin"
"break"
"case"
"class"
"def"
"do"
"else"
"elsif"
"end"
"ensure"
"for"
"if"
"in"
"module"
"next"
"or"
"rescue"
"retry"
"return"
"then"
"unless"
"until"
"when"
"while"
"yield"
] @keyword

((identifier) @keyword
(#match? @keyword "^(private|protected|public)$"))

; Function calls

((identifier) @function.method.builtin
(#eq? @function.method.builtin "require"))

"defined?" @function.method.builtin

(call
method: [(identifier) (constant)] @function.method)

; Function definitions

(alias (identifier) @function.method)
(setter (identifier) @function.method)
(method name: [(identifier) (constant)] @function.method)
(singleton_method name: [(identifier) (constant)] @function.method)

; Identifiers

[
(class_variable)
(instance_variable)
] @property

((identifier) @constant.builtin
(#match? @constant.builtin "^__(FILE|LINE|ENCODING)__$"))

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

(constant) @constructor

(self) @variable.builtin
(super) @variable.builtin

(block_parameter (identifier) @variable.parameter)
(block_parameters (identifier) @variable.parameter)
(destructured_parameter (identifier) @variable.parameter)
(hash_splat_parameter (identifier) @variable.parameter)
(lambda_parameters (identifier) @variable.parameter)
(method_parameters (identifier) @variable.parameter)
(splat_parameter (identifier) @variable.parameter)

(keyword_parameter name: (identifier) @variable.parameter)
(optional_parameter name: (identifier) @variable.parameter)

((identifier) @function.method
(#is-not? local))
(identifier) @variable

; Literals

[
(string)
(bare_string)
(subshell)
(heredoc_body)
(heredoc_beginning)
] @string

[
(simple_symbol)
(delimited_symbol)
(hash_key_symbol)
(bare_symbol)
] @string.special.symbol

(regex) @string.special.regex
(escape_sequence) @escape

[
(integer)
(float)
] @number

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

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

(comment) @comment

; Operators

[
"="
"=>"
"->"
] @operator

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

[
"("
")"
"["
"]"
"{"
"}"
"%w("
"%i("
] @punctuation.bracket
27 changes: 27 additions & 0 deletions packages/language-ruby/grammars/ts/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
((method) @local.scope
(#set! local.scope-inherits false))

[
(lambda)
(block)
(do_block)
] @local.scope

(block_parameter (identifier) @local.definition)
(block_parameters (identifier) @local.definition)
(destructured_parameter (identifier) @local.definition)
(hash_splat_parameter (identifier) @local.definition)
(lambda_parameters (identifier) @local.definition)
(method_parameters (identifier) @local.definition)
(splat_parameter (identifier) @local.definition)

(keyword_parameter name: (identifier) @local.definition)
(optional_parameter name: (identifier) @local.definition)

(identifier) @local.reference

(assignment left: (identifier) @local.definition)
(operator_assignment left: (identifier) @local.definition)
(left_assignment_list (identifier) @local.definition)
(rest_assignment (identifier) @local.definition)
(destructured_left_assignment (identifier) @local.definition)
64 changes: 64 additions & 0 deletions packages/language-ruby/grammars/ts/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
; Method definitions

(
(comment)* @doc
.
[
(method
name: (_) @name) @definition.method
(singleton_method
name: (_) @name) @definition.method
]
(#strip! @doc "^#\\s*")
(#select-adjacent! @doc @definition.method)
)

(alias
name: (_) @name) @definition.method

(setter
(identifier) @ignore)

; Class definitions

(
(comment)* @doc
.
[
(class
name: [
(constant) @name
(scope_resolution
name: (_) @name)
]) @definition.class
(singleton_class
value: [
(constant) @name
(scope_resolution
name: (_) @name)
]) @definition.class
]
(#strip! @doc "^#\\s*")
(#select-adjacent! @doc @definition.class)
)

; Module definitions

(
(module
name: [
(constant) @name
(scope_resolution
name: (_) @name)
]) @definition.module
)

; Calls

(call method: (identifier) @name) @reference.call

(
[(identifier) (constant)] @name @reference.call
(#is-not? local)
(#not-match? @name "^(lambda|load|require|require_relative|__FILE__|__LINE__)$")
)
33 changes: 33 additions & 0 deletions packages/language-ruby/spec/fixtures/classes-wasm-ts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "a"
# ^ function.method.builtin

class Car < Vehicle
# <- keyword
# ^ constructor

def init(id)
# <- keyword
# ^ function.method

@id = id
# <- property
# ^ variable.parameter

yield
# <- keyword
return
# <- keyword
next
# <- keyword
end

private
# ^ keyword

public
# ^ keyword

protected
# ^ keyword
end
# <- keyword
Loading

0 comments on commit cf9abdb

Please sign in to comment.