Module: SublimeSyntaxConvertor::Formatter

Included in:
BeginEndPattern, Convertor, MatchPattern, SyntaxYaml
Defined in:
lib/sublime_syntax_convertor/formatter.rb

Instance Method Summary collapse

Instance Method Details

#format_captures(cap) ⇒ Object



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

def format_captures(cap)
  captures = {}
  cap.each do |key, value|
    unless value.key?('name')
      puts "patterns and includes are not supported within captures: #{cap}"
      next
    end

    begin
      captures[key.to_i] = value['name']
    rescue
      puts 'named capture used, this is unsupported'
      captures[key] = value['name']
    end
  end
  captures
end

#format_comment(str) ⇒ Object



3
4
5
6
7
# File 'lib/sublime_syntax_convertor/formatter.rb', line 3

def format_comment(str)
  str = str.strip.gsub("\t", "    ")
  str = str.rstrip + "\n" if str.include?("\n")
  str
end

#format_external_syntax(key) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/sublime_syntax_convertor/formatter.rb', line 53

def format_external_syntax(key)
  fail 'invalid external syntax name' if '#$'.include?(key[0])
  if key.include?('#')
    syntax, rule = key.split('#')
    return "scope:#{syntax}##{rule}"
  else
    return "scope:#{key}"
  end
end

#format_regex(str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sublime_syntax_convertor/formatter.rb', line 9

def format_regex(str)
  if str.include? "\n"
    lines = str.split("\n")
    # trim common indentation off of each line
    if lines.size > 1
      common_indent = leading_whitespace(lines[1])
      lines[2..-1].each do |line|
        cur_indent = leading_whitespace(line)
        if cur_indent.start_with?(common_indent)
          next
        elsif common_indent.start_with?(cur_indent)
          common_indent = cur_indent
        else
          common_indent = ''
        end
      end
      # Generally the first line doesn't have any indentation, add some
      lines[0] = common_indent + lines[0].lstrip unless lines[0].start_with?(common_indent)
    else
      common_indent = leading_whitespace(lines[0])
    end
    str = lines.map { |line| line[common_indent.size..-1] }.join("\n").rstrip
  end
  str
end

#leading_whitespace(str) ⇒ Object



89
90
91
# File 'lib/sublime_syntax_convertor/formatter.rb', line 89

def leading_whitespace(str)
  str[0...(str.size - str.lstrip.size)]
end

#needs_quoting?(str) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sublime_syntax_convertor/formatter.rb', line 63

def needs_quoting?(str)
  (
    str == "" ||
    str.start_with?('<<') ||
    "\"'%-:?@`&*!,#|>0123456789=".include?(str[0]) ||
    %w(true false null).include?(str) ||
    str.include?("# ") ||
    str.include?(': ') ||
    str.include?('[') ||
    str.include?(']') ||
    str.include?('{') ||
    str.include?('}') ||
    str.include?("\n") ||
    ":#".include?(str[-1]) ||
    str.strip != str
  )
end

#quote(str) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/sublime_syntax_convertor/formatter.rb', line 81

def quote(str)
  if str.include?("\\") || str.include?('"')
    return "'" + str.gsub("'", "''") + "'"
  else
    return '"' + str.gsub("\\", "\\\\").gsub('"', '\\"') + '"'
  end
end