Module: XCPerfect::Syntax

Defined in:
lib/xcperfect/syntax.rb

Overview

Syntax highlights the function signatures displayed to the user while using the ‘all.rb` formatter

Class Method Summary collapse

Class Method Details

.find_lexer(filename, line) ⇒ Rouge::Lexer

Parameters:

  • filename (String)

    The filename

  • String

    line of code to be highlighted

Returns:

  • (Rouge::Lexer)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xcperfect/syntax.rb', line 35

def self.find_lexer(filename, line)
  case File.extname(filename)
  when '.cpp', '.cc', '.c++', '.cxx', '.hpp', '.h++', '.hxx'
    Rouge::Lexers::Cpp
  when '.m', '.h' then Rouge::Lexers::ObjectiveC
  when '.swift' then Rouge::Lexers::Swift
  when '.ruby', '.rb' then Rouge::Lexers::Ruby
  else
    options = {
      filename: File.basename(filename),
      source: line
    }
    Rouge::Lexer.guesses(options).first
  end
end

.highlight(filename, line) ⇒ Object



18
19
20
21
# File 'lib/xcperfect/syntax.rb', line 18

def self.highlight(filename, line)
  return signature unless Rouge
  highlight_with_formatter(filename, line, Rouge::Formatters::Terminal256.new)
end

.highlight_with_formatter(filename, line, formatter) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/xcperfect/syntax.rb', line 23

def self.highlight_with_formatter(filename, line, formatter)
  lexer = find_lexer(filename, line)
  if lexer
    formatter.format(lexer.lex(line)).rstrip
  else
    line
  end
end