Module: RuVim::Lang::Perl

Defined in:
lib/ruvim/lang/perl.rb

Constant Summary collapse

KEYWORDS =
%w[
  my our local sub return if elsif else unless while until for
  foreach do given when default last next redo goto
  use require package no BEGIN END
  die warn print say chomp chop push pop shift unshift
  open close read write seek tell
  map grep sort reverse join split
  defined undef delete exists ref bless
  eval try catch finally
  and or not xor
  eq ne lt gt le ge cmp
].freeze
KEYWORD_RE =
/\b(?:#{KEYWORDS.join("|")})\b/
STRING_DOUBLE_RE =
/"(?:\\.|[^"\\])*"/
STRING_SINGLE_RE =
/'(?:\\.|[^'\\])*'/
REGEX_RE =
%r{(?<=[=~(,;\s])(?:m|s|tr|y)?/(?:\\.|[^/\\])*/[gimxsecpodualn]*}
QW_RE =
/\bqw\s*[({<\/|].*?[)}>\/|]/
SCALAR_RE =
/\$[\w]+/
ARRAY_RE =
/@[\w]+/
HASH_RE =
/%[\w]+/
SPECIAL_VAR_RE =
/\$[_!@&`'+\\\/\-\[\]]/
NUMBER_RE =
/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?)\b/
COMMENT_RE =
/#.*/
POD_RE =
/\A=[a-zA-Z]\w*/
INDENT_OPEN_RE =
/\{\s*(?:#.*)?$/
INDENT_CLOSE_RE =
/\A\s*\}/
DEDENT_TRIGGERS =
{
  "}" => /\A(\s*)\}/
}.freeze

Class Method Summary collapse

Class Method Details

.calculate_indent(lines, target_row, shiftwidth) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruvim/lang/perl.rb', line 41

def calculate_indent(lines, target_row, shiftwidth)
  depth = 0
  (0...target_row).each do |row|
    line = lines[row].to_s
    line.each_char do |ch|
      case ch
      when "{" then depth += 1
      when "}" then depth -= 1
      end
    end
  end

  target_line = lines[target_row].to_s.lstrip
  depth -= 1 if target_line.match?(INDENT_CLOSE_RE)
  depth = 0 if depth < 0
  depth * shiftwidth
end

.color_columns(text) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruvim/lang/perl.rb', line 67

def color_columns(text)
  cols = {}
  # POD documentation
  if text.match?(POD_RE)
    text.length.times { |i| cols[i] = Highlighter::COMMENT_COLOR }
    return cols
  end
  Highlighter.apply_regex(cols, text, STRING_DOUBLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, STRING_SINGLE_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, KEYWORD_RE, Highlighter::KEYWORD_COLOR)
  Highlighter.apply_regex(cols, text, SCALAR_RE, Highlighter::VARIABLE_COLOR)
  Highlighter.apply_regex(cols, text, ARRAY_RE, "\e[35m")
  Highlighter.apply_regex(cols, text, HASH_RE, Highlighter::CONSTANT_COLOR)
  Highlighter.apply_regex(cols, text, NUMBER_RE, Highlighter::NUMBER_COLOR)
  Highlighter.apply_regex(cols, text, COMMENT_RE, Highlighter::COMMENT_COLOR, override: true)
  cols
end

.dedent_trigger(char) ⇒ Object



63
64
65
# File 'lib/ruvim/lang/perl.rb', line 63

def dedent_trigger(char)
  DEDENT_TRIGGERS[char]
end

.indent_trigger?(line) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ruvim/lang/perl.rb', line 59

def indent_trigger?(line)
  line.to_s.rstrip.match?(INDENT_OPEN_RE)
end