Sha256: 7400618dd271c823d9e9fe00e5570bc35525a7b6fc92d604f92276aa6d8c3a8b

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

class AuthorEngine
  class CodeEditor < View
    class Highlighting
      include AuthorEngine::Part::Colors

      def initialize
        @highlight_colors = {
          instance_variable: xml_color(green),
          keyword: xml_color(red),
          method: xml_color(yellow),
          ident: xml_color(yellow),
          comment: xml_color(dark_gray),
          constant: xml_color(orange),

          delimiter: xml_color(blue),
          content: xml_color(blue),
          integer: xml_color(blue),
          float: xml_color(blue),
          symbol: xml_color(blue),
        }

        @last_text = ""
      end

      def highlight(string:, text:)
        return unless @last_text != string
        @last_text = string

        buffer = ""

        tokens = CodeRay.scan(string, :ruby).tokens
        tokens.each_with_index do |token, index|
          buffer = "#{buffer}#{style(text: token, token: tokens[index+1])}" if token.is_a?(String)
        end

        text.message = buffer
      end

      def style(text:, token:)
        color = @highlight_colors.dig(token)
        if color
          return "<c=#{color}>#{text}</c>"
        else
          return text
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
author_engine-0.9.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.8.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.7.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.6.1 lib/author_engine/code_editor/highlighting.rb
author_engine-0.6.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.5.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.4.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.3.1 lib/author_engine/code_editor/highlighting.rb
author_engine-0.3.0 lib/author_engine/code_editor/highlighting.rb
author_engine-0.1.0 lib/author_engine/code_editor/highlighting.rb