summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-03-28 02:13:24 +0200
committerKyrylo Silin <silin@kyrylo.org>2019-03-28 02:13:24 +0200
commit79712f279a2b540f36cc2c9a181403b6cd990880 (patch)
treef9eaaf5cdc10479efce2f36c6846b067c6aad359
parent92aeb347d4a64bb80313f3d328dd1afb2edee510 (diff)
downloadpry-79712f279a2b540f36cc2c9a181403b6cd990880.tar.gz
Factor out all CodeRay code to Pry::SyntaxHighlighter
This moves all the code related syntax highlighting and tokenization to a wrapper class of CodeRay. Benefits: * we reduce duplication (no need to call `scan(code, :ruby).term` everywhere) * swapping CodeRay becomes easier because it's isolated in one class
-rw-r--r--lib/pry.rb1
-rw-r--r--lib/pry/code/code_file.rb4
-rw-r--r--lib/pry/code/loc.rb2
-rw-r--r--lib/pry/color_printer.rb8
-rw-r--r--lib/pry/helpers/base_helpers.rb2
-rw-r--r--lib/pry/helpers/documentation_helpers.rb4
-rw-r--r--lib/pry/indent.rb2
-rw-r--r--lib/pry/method.rb2
-rw-r--r--lib/pry/syntax_highlighter.rb24
-rw-r--r--spec/pager_spec.rb2
-rw-r--r--spec/pry_output_spec.rb2
11 files changed, 40 insertions, 13 deletions
diff --git a/lib/pry.rb b/lib/pry.rb
index 50cfb6fd..7a5e7c80 100644
--- a/lib/pry.rb
+++ b/lib/pry.rb
@@ -15,6 +15,7 @@ require 'pry/hooks'
require 'pry/input_completer'
require 'pry/command'
require 'pry/command_set'
+require 'pry/syntax_highlighter'
Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands)
diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb
index 7ac6ef0e..d120af84 100644
--- a/lib/pry/code/code_file.rb
+++ b/lib/pry/code/code_file.rb
@@ -82,8 +82,8 @@ class Pry
# @param [String] filename
# @param [Symbol] default (:unknown) the file type to assume if none could be
# detected.
- # @return [Symbol, nil] The CodeRay type of a file from its extension, or
- # `nil` if `:unknown`.
+ # @return [Symbol, nil] The SyntaxHighlighter type of a file from its
+ # extension, or `nil` if `:unknown`.
def type_from_filename(filename, default = :unknown)
_, @code_type = EXTENSIONS.find do |k, _|
k.any? { |ext| ext == File.extname(filename) }
diff --git a/lib/pry/code/loc.rb b/lib/pry/code/loc.rb
index 5ec4ed87..905c7693 100644
--- a/lib/pry/code/loc.rb
+++ b/lib/pry/code/loc.rb
@@ -54,7 +54,7 @@ class Pry
# @param [Symbol] code_type
# @return [void]
def colorize(code_type)
- tuple[0] = CodeRay.scan(line, code_type).term
+ tuple[0] = SyntaxHighlighter.highlight(line, code_type)
end
# Prepends the line number `lineno` to the `line`.
diff --git a/lib/pry/color_printer.rb b/lib/pry/color_printer.rb
index 4286adc2..3b27b2a3 100644
--- a/lib/pry/color_printer.rb
+++ b/lib/pry/color_printer.rb
@@ -4,8 +4,10 @@ require 'coderay'
# PP subclass for streaming inspect output in color.
class Pry
class ColorPrinter < ::PP
+ Pry::SyntaxHighlighter.overwrite_coderay_comment_token!
+
OBJ_COLOR = begin
- code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
+ code = Pry::SyntaxHighlighter.keyword_token_color
if code.start_with? "\e"
code
else
@@ -13,8 +15,6 @@ class Pry
end
end
- CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
-
def self.pp(obj, out = $DEFAULT_OUTPUT, width = 79, newline = "\n")
q = ColorPrinter.new(out, width, newline)
q.guard_inspect_key { q.pp obj }
@@ -29,7 +29,7 @@ class Pry
elsif str.start_with?('#<') || str == '=' || str == '>'
super highlight_object_literal(str), width
else
- super CodeRay.scan(str, :ruby).term, width
+ super(SyntaxHighlighter.highlight(str), width)
end
end
diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb
index 897362d4..1ad57ab0 100644
--- a/lib/pry/helpers/base_helpers.rb
+++ b/lib/pry/helpers/base_helpers.rb
@@ -40,7 +40,7 @@ class Pry
end
def colorize_code(code)
- CodeRay.scan(code, :ruby).term
+ SyntaxHighlighter.highlight(code)
end
def highlight(string, regexp, highlight_color = :bright_yellow)
diff --git a/lib/pry/helpers/documentation_helpers.rb b/lib/pry/helpers/documentation_helpers.rb
index 741e9b89..b26a143d 100644
--- a/lib/pry/helpers/documentation_helpers.rb
+++ b/lib/pry/helpers/documentation_helpers.rb
@@ -14,7 +14,9 @@ class Pry
def process_rdoc(comment)
comment = comment.dup
- last_match_ruby = proc { CodeRay.scan(Regexp.last_match(1), :ruby).term }
+ last_match_ruby = proc do
+ SyntaxHighlighter.highlight(Regexp.last_match(1))
+ end
comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
.gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
.gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
diff --git a/lib/pry/indent.rb b/lib/pry/indent.rb
index f8a263de..529220af 100644
--- a/lib/pry/indent.rb
+++ b/lib/pry/indent.rb
@@ -280,7 +280,7 @@ class Pry
# @param [String] string The Ruby to lex
# @return [Array] An Array of pairs of [token_value, token_type]
def tokenize(string)
- tokens = CodeRay.scan(string, :ruby)
+ tokens = SyntaxHighlighter.tokenize(string)
tokens = tokens.tokens.each_slice(2) if tokens.respond_to?(:tokens) # Coderay 1.0.0
tokens.to_a
end
diff --git a/lib/pry/method.rb b/lib/pry/method.rb
index 1dee3f07..92c841f8 100644
--- a/lib/pry/method.rb
+++ b/lib/pry/method.rb
@@ -557,7 +557,7 @@ class Pry
def method_name_from_first_line(first_ln)
return nil if first_ln.strip !~ /^def /
- tokens = CodeRay.scan(first_ln, :ruby)
+ tokens = SyntaxHighlighter.tokenize(first_ln)
tokens = tokens.tokens.each_slice(2) if tokens.respond_to?(:tokens)
tokens.each_cons(2) do |t1, t2|
if t2.last == :method || t2.last == :ident && t1 == [".", :operator]
diff --git a/lib/pry/syntax_highlighter.rb b/lib/pry/syntax_highlighter.rb
new file mode 100644
index 00000000..3eb0dd5b
--- /dev/null
+++ b/lib/pry/syntax_highlighter.rb
@@ -0,0 +1,24 @@
+require 'coderay'
+
+class Pry
+ # @api private
+ # @since ?.?.?
+ class SyntaxHighlighter
+ def self.highlight(code, language = :ruby)
+ tokenize(code, language).term
+ end
+
+ def self.tokenize(code, language = :ruby)
+ CodeRay.scan(code, language)
+ end
+
+ def self.keyword_token_color
+ CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
+ end
+
+ # Sets comment token to blue (black by default), so it's more legible.
+ def self.overwrite_coderay_comment_token!
+ CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
+ end
+ end
+end
diff --git a/spec/pager_spec.rb b/spec/pager_spec.rb
index 46552b78..9113d4e3 100644
--- a/spec/pager_spec.rb
+++ b/spec/pager_spec.rb
@@ -23,7 +23,7 @@ describe "Pry::Pager" do
end
def record_string_with_color_codes
- @pt.record(CodeRay.scan("0123456789", :ruby).term + "\n")
+ @pt.record(Pry::SyntaxHighlighter.highlight('0123456789') + "\n")
end
it "records short lines that don't add up to a page" do
diff --git a/spec/pry_output_spec.rb b/spec/pry_output_spec.rb
index 78a9923b..340a9943 100644
--- a/spec/pry_output_spec.rb
+++ b/spec/pry_output_spec.rb
@@ -77,7 +77,7 @@ describe Pry do
it "should colorize strings as though they were ruby" do
pry = Pry.new
accumulator = StringIO.new
- colorized = CodeRay.scan("[1]", :ruby).term
+ colorized = Pry::SyntaxHighlighter.highlight('[1]')
pry.config.output = accumulator
pry.config.print.call(accumulator, [1], pry)
expect(accumulator.string).to eq("=> #{colorized}\n")