Skip to content

Commit

Permalink
add halfway support for multiline elixir sigils
Browse files Browse the repository at this point in the history
this changeset treats multiline sigils as doc strings, similar to how things work in GitHub as of time of authoring.
  • Loading branch information
bsmithgall committed Jun 5, 2024
1 parent 9266cbd commit 51070b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rouge/lexers/elixir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def self.detect?(text)
# Cribbed and adjusted from Ruby lexer
delimiter_map = { '{' => '}', '[' => ']', '(' => ')', '<' => '>' }
# Match a-z for custom sigils too
sigil_opens = Regexp.union(delimiter_map.keys + %w(| / ' "))
sigil_opens = Regexp.union(delimiter_map.keys + [%r/"{3}/] + %w(| / ' "))
rule %r/~([A-Za-z])?(#{sigil_opens})/ do |m|
open = Regexp.escape(m[2])
close = Regexp.escape(delimiter_map[m[2]] || m[2])
Expand All @@ -123,12 +123,18 @@ def self.detect?(text)
push :list_flags
end

if open == '"""'
toktype = Str::Doc
end

token toktype

push do
rule %r/#{close}/, toktype, :pop!

if interp
if toktype == Str::Doc
rule %r/(?:.|\n)*?"""/, toktype, :pop!
elsif interp
mixin :interpoling
rule %r/#/, toktype
else
Expand Down
15 changes: 15 additions & 0 deletions spec/visual/samples/elixir
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ string |> String.split(~r/[ -]/) |> Enum.map(&abbreviate_word/1) |> Enum.join()
~S|inter #{pol <> "ati#{o}"} n|
~S/inter #{pol <> "ati#{o}"} n/

# multiline sigil
~S"""
Converts double-quotes to single-quotes.

## Examples

iex> convert("\"foo\"")
"'foo'"
"""

~H"""
Current temperature: <%= @temperature %>°F
<button phx-click="inc_temperature">+</button>
"""

# first is Operator, second is &1 variable
&(&1)

Expand Down

0 comments on commit 51070b4

Please sign in to comment.