Class: Textbringer::RubyMode

Inherits:
ProgrammingMode show all
Defined in:
lib/textbringer/modes/ruby_mode.rb

Constant Summary

Constants inherited from Mode

Mode::DEFAULT_SYNTAX_TABLE

Constants included from Commands

Commands::CLIPBOARD_AVAILABLE, Commands::COMPLETION_POPUP_STATUS, Commands::CTAGS, Commands::EMAIL_REGEXP, Commands::HELP_RING, Commands::ISEARCH_STATUS, Commands::ISPELL_STATUS, Commands::ISPELL_WORD_REGEXP, Commands::KEYBOARD_MACROS, Commands::LSP_DOCUMENT_VERSIONS, Commands::LSP_STATUS, Commands::REGISTERS, Commands::RE_SEARCH_STATUS, Commands::URI_REGEXP

Constants included from Utils

Utils::COMPLETION, Utils::EXPRESSION_COMPLETOR, Utils::EXPRESSION_COMPLETOR_OPTIONS, Utils::HOOKS

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from ProgrammingMode

#indent_line, #indent_new_comment_line, #indent_region, #reindent_then_newline_and_indent

Methods inherited from Mode

define_generic_command, define_local_command, define_syntax, inherited, list, #name, #syntax_table

Methods included from Commands

[], #buffer_uri, #command_help, command_table, #completion_popup_done, completion_popup_mode_active?, #completion_popup_pre_command_hook, #completion_popup_start, #current_prefix_arg, define_command, #ensure_ispell_active, #execute_keyboard_macro, #get_tags, #insert_completion, #isearch_done, #isearch_mode, #isearch_mode?, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, #ispell_done, #ispell_forward, #ispell_mode, #keymap_bindings, list, #lsp_after_set_visited_file_name_hook, #lsp_close_signature_window, #lsp_completion_context, #lsp_find_file_hook, #lsp_open_document, #lsp_position, #lsp_setup_buffer_hooks, #lsp_show_signature_window, #lsp_signature_pre_command_hook, #lsp_text_document_sync_kind, #lsp_utf16_length, #match_beginning, #match_end, #match_string, #message_misspelled, #number_prefix_arg, #prefix_numeric_value, #read_input_method_name, #read_keyboard_macro, #read_register, #read_theme_name, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, delete_completions_window, foreground, foreground!, get_hooks, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, run_hooks_in, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ RubyMode

Returns a new instance of RubyMode.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/textbringer/modes/ruby_mode.rb', line 18

def initialize(buffer)
  super(buffer)
  @buffer[:indent_level] = CONFIG[:ruby_indent_level]
  @buffer[:indent_tabs_mode] = CONFIG[:ruby_indent_tabs_mode]
  @prism_version = nil
  @prism_tokens = nil
  @prism_ast = nil
  @prism_method_call_locs = nil
  @literal_levels = nil
  @literal_levels_version = nil
end

Instance Method Details

#backward_definition(n = number_prefix_arg || 1) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/textbringer/modes/ruby_mode.rb', line 56

def backward_definition(n = number_prefix_arg || 1)
  ensure_prism_tokens
  tokens = @prism_tokens.filter_map { |token, _state|
    type = token.type
    next if type == :EOF
    [token.location.start_line, type]
  }.reverse
  @buffer.beginning_of_line
  n.times do |i|
    tokens = tokens.drop_while { |l, type|
      l >= @buffer.current_line ||
        !DEFINITION_KEYWORDS.include?(type)
    }
    line, = tokens.first
    if line.nil?
      @buffer.beginning_of_buffer
      break
    end
    @buffer.goto_line(line)
    tokens = tokens.drop(1)
  end
  while /\s/ =~ @buffer.char_after
    @buffer.forward_char
  end
end

#comment_startObject



14
15
16
# File 'lib/textbringer/modes/ruby_mode.rb', line 14

def comment_start
  "#"
end

#compile(cmd = read_from_minibuffer("Compile: ", default: default_compile_command)) ⇒ Object



82
83
84
85
86
# File 'lib/textbringer/modes/ruby_mode.rb', line 82

def compile(cmd = read_from_minibuffer("Compile: ",
                                       default: default_compile_command))
  shell_execute(cmd, buffer_name: "*Ruby compile result*",
                mode: BacktraceMode)
end

#default_compile_commandObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/textbringer/modes/ruby_mode.rb', line 92

def default_compile_command
  @buffer[:ruby_compile_command] ||
    if File.exist?("Rakefile")
      prefix = File.exist?("Gemfile") ? "bundle exec " : ""
      prefix + "rake"
    elsif @buffer.file_name
      ruby_install_name + " " + @buffer.file_name
    else
      nil
    end
end

#forward_definition(n = number_prefix_arg || 1) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/textbringer/modes/ruby_mode.rb', line 30

def forward_definition(n = number_prefix_arg || 1)
  ensure_prism_tokens
  tokens = @prism_tokens.filter_map { |token, _state|
    type = token.type
    next if type == :EOF
    [token.location.start_line, type]
  }
  @buffer.forward_line
  n.times do |i|
    tokens = tokens.drop_while { |l, type|
      l < @buffer.current_line ||
        !DEFINITION_KEYWORDS.include?(type)
    }
    line, = tokens.first
    if line.nil?
      @buffer.end_of_buffer
      break
    end
    @buffer.goto_line(line)
    tokens = tokens.drop(1)
  end
  while /\s/ =~ @buffer.char_after
    @buffer.forward_char
  end
end

#highlight(ctx) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/textbringer/modes/ruby_mode.rb', line 120

def highlight(ctx)
  ensure_prism_tokens
  return unless @prism_tokens
  ensure_method_call_locs
  base_pos = ctx.buffer.point_min
  hl_start = ctx.highlight_start
  hl_end = ctx.highlight_end
  in_symbol = false
  after_def = false
  after_class_or_module = false
  @prism_tokens.each do |token_info|
    token = token_info[0]
    type = token.type
    offset = token.location.start_offset
    length = token.location.length
    pos = base_pos + offset
    pos_end = pos + length
    break if pos >= hl_end
    if pos_end > hl_start
      face_name = PRISM_TOKEN_FACES[type]
      if in_symbol
        face_name = :string if face_name.nil? || face_name == :constant ||
          face_name == :operator
      elsif after_def
        face_name = :function_name if type == :IDENTIFIER ||
          type == :CONSTANT || type == :METHOD_NAME ||
          PRISM_TOKEN_FACES[type] == :operator
      elsif face_name == :constant &&
          (after_class_or_module || token.location.slice.match?(/\p{Lower}/))
        face_name = :type
      elsif @prism_method_call_locs.key?(offset)
        face_name = :function_name
      end
      if face_name && (face = Face[face_name])
        ctx.highlight(pos, pos_end, face)
      end
    end
    in_symbol = type == :SYMBOL_BEGIN
    after_def = type == :KEYWORD_DEF ||
      (after_def && (type == :KEYWORD_SELF || type == :DOT ||
                     type == :NEWLINE || type == :IGNORED_NEWLINE ||
                     type == :COMMENT))
    after_class_or_module = (type == :KEYWORD_CLASS || type == :KEYWORD_MODULE) ||
      (after_class_or_module && !(type == :NEWLINE || type == :SEMICOLON))
  end
end

#symbol_patternObject



88
89
90
# File 'lib/textbringer/modes/ruby_mode.rb', line 88

def symbol_pattern
  /[\p{Letter}\p{Number}_$@!?]/
end

#toggle_testObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/textbringer/modes/ruby_mode.rb', line 104

def toggle_test
  case @buffer.file_name
  when %r'(.*)/test/(.*/)?test_(.*?)\.rb\z'
    path = find_test_target_path($1, $2, $3)
    find_file(path)
  when %r'(.*)/spec/(.*/)?(.*?)_spec\.rb\z'
    path = find_test_target_path($1, $2, $3)
    find_file(path)
  when %r'(.*)/(?:lib|app)/(.*/)?(.*?)\.rb\z'
    path = find_test_path($1, $2, $3)
    find_file(path)
  else
    raise EditorError, "Unknown file type"
  end
end