Module: RuVim::Lang::Python

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

Constant Summary collapse

KEYWORDS =
%w[
  and as assert async await break class continue def del elif else
  except finally for from global if import in is lambda nonlocal
  not or pass raise return try while with yield
  True False None
].freeze
KEYWORD_RE =
/\b(?:#{KEYWORDS.join("|")})\b/
STRING_TRIPLE_DQ_RE =
/""".*?"""/m
STRING_TRIPLE_SQ_RE =
/'''.*?'''/m
STRING_DOUBLE_RE =
/"(?:\\.|[^"\\])*"/
STRING_SINGLE_RE =
/'(?:\\.|[^'\\])*'/
FSTRING_PREFIX_RE =
/[fFrRbBuU]{1,2}(?=["'])/
NUMBER_RE =
/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?j?)\b/
DECORATOR_RE =
/@[\w.]+/
COMMENT_RE =
/#.*/
CONSTANT_RE =
/\b[A-Z][A-Z0-9_]{1,}\b/
BUILTIN_RE =
/\b(?:print|len|range|type|int|str|float|list|dict|tuple|set|bool|open|input|map|filter|zip|enumerate|sorted|reversed|super|isinstance|issubclass|hasattr|getattr|setattr|delattr|property|staticmethod|classmethod|__\w+__)\b/
INDENT_OPEN_RE =
/:\s*(?:#.*)?$/
INDENT_CLOSE_RE =
/\A\s*(?:return|break|continue|pass|raise)\b/
DEDENT_TRIGGERS =
{
  "s" => /\A(\s*)(?:else|class)\z/,
  ":" => /\A(\s*)(?:else|elif|except|finally)\s*.*:\z/,
  "f" => /\A(\s*)elif\z/
}.freeze

Class Method Summary collapse

Class Method Details

.calculate_indent(lines, target_row, shiftwidth) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruvim/lang/python.rb', line 36

def calculate_indent(lines, target_row, shiftwidth)
  return 0 if target_row == 0

  prev_row = target_row - 1
  prev_row -= 1 while prev_row > 0 && lines[prev_row].to_s.strip.empty?
  prev = lines[prev_row].to_s
  prev_indent = prev[/\A */].size

  if prev.rstrip.match?(INDENT_OPEN_RE)
    return prev_indent + shiftwidth
  end

  target_line = lines[target_row].to_s.strip
  if target_line.match?(/\A(?:else|elif|except|finally)\b/)
    depth = prev_indent - shiftwidth
    return depth < 0 ? 0 : depth
  end

  prev_indent
end

.color_columns(text) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruvim/lang/python.rb', line 65

def color_columns(text)
  cols = {}
  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, FSTRING_PREFIX_RE, Highlighter::STRING_COLOR)
  Highlighter.apply_regex(cols, text, KEYWORD_RE, Highlighter::KEYWORD_COLOR)
  Highlighter.apply_regex(cols, text, BUILTIN_RE, "\e[35m")
  Highlighter.apply_regex(cols, text, DECORATOR_RE, "\e[35m")
  Highlighter.apply_regex(cols, text, NUMBER_RE, Highlighter::NUMBER_COLOR)
  Highlighter.apply_regex(cols, text, CONSTANT_RE, Highlighter::CONSTANT_COLOR)
  Highlighter.apply_regex(cols, text, COMMENT_RE, Highlighter::COMMENT_COLOR, override: true)
  cols
end

.dedent_trigger(char) ⇒ Object



61
62
63
# File 'lib/ruvim/lang/python.rb', line 61

def dedent_trigger(char)
  DEDENT_TRIGGERS[char]
end

.indent_trigger?(line) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruvim/lang/python.rb', line 57

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