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, with_clean_env, 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_name_locs = nil
  @literal_levels = nil
  @literal_levels_version = nil
end

Instance Method Details

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/textbringer/modes/ruby_mode.rb', line 51

def backward_definition(n = number_prefix_arg || 1)
  tokens = filter_prism_tokens_line_and_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



72
73
74
75
76
# File 'lib/textbringer/modes/ruby_mode.rb', line 72

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



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/textbringer/modes/ruby_mode.rb', line 82

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
# File 'lib/textbringer/modes/ruby_mode.rb', line 30

def forward_definition(n = number_prefix_arg || 1)
  tokens = filter_prism_tokens_line_and_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



110
111
112
113
114
115
116
117
118
119
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
# File 'lib/textbringer/modes/ruby_mode.rb', line 110

def highlight(ctx)
  ensure_prism_tokens
  return unless @prism_tokens
  ensure_method_name_locs
  base_pos = ctx.buffer.point_min
  hl_start = ctx.highlight_start
  hl_end = ctx.highlight_end
  in_symbol = 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 == :keyword || face_name == :operator
      elsif @prism_method_name_locs.key?(offset)
        face_name = :function_name
      elsif face_name == :constant &&
          (after_class_or_module || token.location.slice.match?(/\p{Lower}/))
        face_name = :type
      end
      if face_name && (face = Face[face_name])
        ctx.highlight(pos, pos_end, face)
      end
    end
    in_symbol = type == :SYMBOL_BEGIN
    after_class_or_module = (type == :KEYWORD_CLASS || type == :KEYWORD_MODULE) ||
      (after_class_or_module && !(type == :NEWLINE || type == :SEMICOLON))
  end
end

#symbol_patternObject



78
79
80
# File 'lib/textbringer/modes/ruby_mode.rb', line 78

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

#toggle_testObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/textbringer/modes/ruby_mode.rb', line 94

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