Skip to content

Commit

Permalink
Use more idiomatic code to solve problem
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrmont committed Jul 18, 2019
1 parent 46f0dfa commit 8f66481
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/rouge/lexers/rust.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Rust < RegexLexer
'rust,should_panic', 'rs,should_panic'
filenames '*.rs'
mimetypes 'text/x-rust'
@beginning=true

def self.detect?(text)
return true if text.shebang? 'rustc'
Expand Down Expand Up @@ -50,6 +49,7 @@ def macro_closed?

start {
@macro_delims = { ']' => 0, ')' => 0, '}' => 0 }
push :bol
}

delim_map = { '[' => ']', '(' => ')', '{' => '}' }
Expand All @@ -61,10 +61,15 @@ def macro_closed?
)x
size = /8|16|32|64/

state :try_doc_comment do
# Although not officially part of Rust, the rustdoc tool allows code in
# comments to begin with `#`. Code like this will be evaluated but not
# included in the HTML output produced by rustdoc. So that code intended
# for these comments can be higlighted with Rouge, the Rust lexer needs
# to check if the beginning of the line begins with a `# `.
state :bol do
mixin :whitespace
rule %r/#\s[^\n]*/, Comment::Preproc
rule %r//, Text, :pop!
rule %r/#\s[^\n]*/, Comment::Special
rule(//) { pop! }
end

state :attribute do
Expand All @@ -82,11 +87,7 @@ def macro_closed?
end

state :root do
if @beginning
rule %r//, Text, :try_doc_comment
@beginning = false
end
rule %r/\n/, Text, :try_doc_comment
rule %r/\n/, Text, :bol
mixin :whitespace
rule %r/#!?\[/, Name::Decorator, :attribute
rule %r/\b(?:#{Rust.keywords.join('|')})\b/, Keyword
Expand Down

0 comments on commit 8f66481

Please sign in to comment.