Module: XCPretty::Syntax

Defined in:
lib/xcpretty/syntax.rb

Class Method Summary collapse

Class Method Details

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

Parameters:

  • filename (String)

    The filename

  • contents (String)

    The contents of the file

Returns:

  • (Rouge::Lexer)


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

def self.find_lexer(filename, contents)
  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: contents
    }
    Rouge::Lexer.guesses(options).first
  end
end

.highlight(snippet) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xcpretty/syntax.rb', line 13

def self.highlight(snippet)
  return snippet.contents unless Rouge

  if snippet.file_path.include?(':')
    filename = snippet.file_path.rpartition(':').first
  else
    filename = snippet.file_path
  end

  lexer = find_lexer(filename, snippet.contents)
  if lexer
    formatter = Rouge::Formatters::Terminal256.new
    formatter.format(lexer.lex(snippet.contents))
  else
    snippet.contents
  end
end