Skip to content

Commit

Permalink
irb lexer: recognize the SIMPLE prompt (#1943)
Browse files Browse the repository at this point in the history
* irb lexer: recognize the SIMPLE prompt

Added support for IRB's `:SIMPLE` prompt (`>>`), plus some tests
for it and the two other supported prompts (IRB's `:STANDARD` and Pry's
default).

* Update lib/rouge/lexers/irb.rb

Avoid picking output as prompt

+ support for multi-line prompts. Courtesy of @tancnle.

Co-authored-by: Tan Le <numbat@fastmail.com>

---------

Co-authored-by: Tan Le <numbat@fastmail.com>
  • Loading branch information
r3trofitted and tancnle committed Jun 11, 2023
1 parent 5c052c2 commit a5c5f1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rouge/lexers/irb.rb
Expand Up @@ -23,7 +23,13 @@ def lang_lexer
end

def prompt_regex
/^.*?(irb|pry).*?[>"*]/
%r(
^.*?
(
(irb|pry).*?[>"*] |
[>"*]>
)
)x
end

def allow_comments?
Expand Down
30 changes: 30 additions & 0 deletions spec/lexers/irb_spec.rb
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::IRBLexer do
let(:subject) { Rouge::Lexers::IRBLexer.new }
let(:klass) { Rouge::Lexers::IRBLexer }

include Support::Lexing

it "parses IRB's :DEFAULT prompt" do
assert_tokens_equal 'irb(main):001:0> self',
['Generic.Prompt', 'irb(main):001:0>'],
['Text.Whitespace', ' '],
['Name.Builtin', 'self']
end

it "parses IRB's :SIMPLE prompt" do
assert_tokens_equal '>> self',
['Generic.Prompt', '>>'],
['Text.Whitespace', ' '],
['Name.Builtin', 'self']
end

it "parses Pry's default prompt" do
assert_tokens_equal 'pry(main)> self',
['Generic.Prompt', 'pry(main)>'],
['Text.Whitespace', ' '],
['Name.Builtin', 'self']
end
end

0 comments on commit a5c5f1b

Please sign in to comment.