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

[Ruby] Support code on same line as heredoc begin token #466

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
174 changes: 106 additions & 68 deletions Ruby/Ruby.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1345,97 +1345,135 @@ contexts:
# heredoc with embedded HTML and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)HTML)\b"?)'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.embedded.html.ruby
- meta_content_scope: text.html.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: scope:text.html.basic
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-html, trailing-heredoc-start]
# heredoc with embedded SQL and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)SQL)\b"?)'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.embedded.sql.ruby
- meta_content_scope: text.sql.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: scope:source.sql
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-sql, trailing-heredoc-start]
# heredoc with embedded css and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)CSS)\b"?)'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.embedded.css.ruby
- meta_content_scope: text.css.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: 'scope:source.css'
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-css, trailing-heredoc-start]
# heredoc with embedded javascript and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)(?:JS|JAVASCRIPT))\b"?)'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.embedded.js.ruby
- meta_content_scope: text.js.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: 'scope:source.js'
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-js, trailing-heredoc-start]
# heredoc with embedded ruby and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)RUBY)\b"?)'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.embedded.ruby.ruby
- meta_content_scope: text.ruby.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char
- include: expressions
push: [heredoc-ruby, trailing-heredoc-start]
# Escaped to prevent recursion?
# heredoc with embedded shell and indented terminator
# - match: '(<<[-~]("?)((?:[_\w]+_|)(?:SH|SHELL))\b\1)'
# scope: punctuation.definition.string.begin.ruby
# push:
# - meta_scope: string.unquoted.embedded.shell.ruby
# - meta_content_scope: text.shell.embedded.ruby
# - match: ^\s*\2$
# scope: punctuation.definition.string.end.ruby
# pop: true
# - include: Shell-Unix-Generic.sublime-syntax
# - include: interpolated-ruby
# - include: escaped-char
# push: [heredoc-shell, trailing-heredoc-start]
- match: (\=)\s*(<<(\w+))
captures:
1: keyword.operator.assignment.ruby
2: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.heredoc.ruby
- match: ^\s*\3$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-assign, trailing-heredoc-no-embedding-start]
# heredoc with indented terminator
- match: '(<<[-~](\w+))'
scope: punctuation.definition.string.begin.ruby
push:
- meta_scope: string.unquoted.heredoc.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char
push: [heredoc-plain, trailing-heredoc-no-embedding-start]

heredoc-html:
- meta_scope: string.unquoted.embedded.html.ruby
- meta_content_scope: text.html.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: scope:text.html.basic
- include: interpolated-ruby
- include: escaped-char

heredoc-sql:
- meta_scope: string.unquoted.embedded.sql.ruby
- meta_content_scope: text.sql.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: scope:source.sql
- include: interpolated-ruby
- include: escaped-char

heredoc-css:
- meta_scope: string.unquoted.embedded.css.ruby
- meta_content_scope: text.css.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: 'scope:source.css'
- include: interpolated-ruby
- include: escaped-char

heredoc-js:
- meta_scope: string.unquoted.embedded.js.ruby
- meta_content_scope: text.js.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: 'scope:source.js'
- include: interpolated-ruby
- include: escaped-char

heredoc-ruby:
- meta_scope: string.unquoted.embedded.ruby.ruby
- meta_content_scope: text.ruby.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char
- include: expressions

#heredoc-shell:
# - meta_scope: string.unquoted.embedded.shell.ruby
# - meta_content_scope: text.shell.embedded.ruby
# - match: ^\s*\2$
# scope: punctuation.definition.string.end.ruby
# pop: true
# - include: Shell-Unix-Generic.sublime-syntax
# - include: interpolated-ruby
# - include: escaped-char

# This prevents clear_scopes from applying to the push token
Copy link
Contributor

Choose a reason for hiding this comment

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

@wbond what does this mean? Why is this necessary? This context seems useless to me.

I'm trying to implement clear_scopes in https://github.com/trishume/syntect and the fact that this context looks useless to me suggests I might be misunderstanding the semantics of either clear_scopes or push/pop operations.

If you could explain to me why this is necessary I would really appreciate it. Also, what is a "push token" and why would clear_scopes apply to it?

Copy link
Member Author

Choose a reason for hiding this comment

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

clear_scopes is implemented in the same way as meta_scope in that it applies to the match the pushed, and to the match that pops.

The match that pushed is what I mean by "push token".

Without this, clear_scopes would clear the scope from the opening heredoc token, but we don't want that. I decided not to implement clear_scopes_content since this method can be used to emulate it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok that makes sense, and in retrospect is explained in the docs, thanks for clearing up my confusion. Thanks!

trailing-heredoc-start:
- match: ''
set: trailing-heredoc

trailing-heredoc:
- clear_scopes: 2
- match: '$'
pop: true
- include: expressions

heredoc-assign:
- meta_scope: string.unquoted.heredoc.ruby
- match: ^\s*\3$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char

heredoc-plain:
- meta_scope: string.unquoted.heredoc.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: interpolated-ruby
- include: escaped-char

# This prevents clear_scopes from applying to the push token
trailing-heredoc-no-embedding-start:
- match: ''
set: trailing-heredoc-no-embedding

trailing-heredoc-no-embedding:
- clear_scopes: 1
- match: '$'
pop: true
- include: expressions

data-section:
- match: ^__END__\n
Expand Down
18 changes: 18 additions & 0 deletions Ruby/syntax_test_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ def #{sym}(*args, &block)
end
RUBY

DB.fetch(<<-SQL, conn).name
#^^^^^^^^^^^^^^^^^^^^^^^^^^ source.ruby
# ^^^^^^ string.unquoted
# ^^^^^^^^^^^^ - string.unquoted
# ^ punctuation.separator
# ^ punctuation.definition.group.end
SELECT * FROM users;
#^^^^^^^^^^^^^^^^^^^ text.sql.embedded
SQL

foo, bar = <<BAR, 2
#^^^^^^^^^^^^^^^^^^ source.ruby
# ^^^^^ string.unquoted
# ^^^ - string
# ^ punctuation.separator
# ^ constant.numeric
BAR

str = "number %d" % 11
# ^^ string.quoted.double.ruby constant.other.placeholder.ruby
str = 'number %d' % 11
Expand Down