Class: Textbringer::RubyMode
- Inherits:
-
ProgrammingMode
show all
- Defined in:
- lib/textbringer/modes/ruby_mode.rb,
sig/lib/textbringer/mode.rbs,
sig/lib/textbringer/modes/ruby_mode.rbs
Defined Under Namespace
Classes: PartialLiteralAnalyzer
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
Class Method Summary
collapse
Instance Method Summary
collapse
define_generic_command, #indent_line, #indent_new_comment_line, #indent_region, inherited, #reindent_then_newline_and_indent
inherited
Methods inherited from Mode
define_generic_command, define_local_command, 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, #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.
92
93
94
95
96
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 92
def initialize(buffer)
super(buffer)
@buffer[:indent_level] = CONFIG[:ruby_indent_level]
@buffer[:indent_tabs_mode] = CONFIG[:ruby_indent_tabs_mode]
end
|
Class Method Details
.define_syntax ⇒ Regexp
24
|
# File 'sig/lib/textbringer/mode.rbs', line 24
def self.define_syntax: (Symbol, Regexp) -> Regexp
|
Instance Method Details
#backward_definition(n = number_prefix_arg || 1) ⇒ nil
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 119
def backward_definition(n = number_prefix_arg || 1)
tokens = Ripper.lex(@buffer.to_s).reverse
@buffer.beginning_of_line
n.times do |i|
tokens = tokens.drop_while { |(l, _), e, t|
l >= @buffer.current_line ||
e != :on_kw || /\A(?:class|module|def)\z/ !~ t
}
(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
|
#beginning_of_indentation ⇒ Integer
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 186
def beginning_of_indentation
loop do
@buffer.re_search_backward(INDENT_BEG_RE)
space = @buffer.match_string(1)
s = @buffer.substring(@buffer.point_min, @buffer.point)
if PartialLiteralAnalyzer.in_literal?(s)
next
end
return space_width(space)
end
rescue SearchError
@buffer.beginning_of_buffer
0
end
|
#calculate_indentation ⇒ Integer
#calculate_indentation ⇒ nil
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 217
def calculate_indentation
if @buffer.current_line == 1
return 0
end
@buffer.save_excursion do
@buffer.beginning_of_line
start_with_period = @buffer.looking_at?(/[ \t]*\./)
bol_pos = @buffer.point
base_indentation = beginning_of_indentation
start_pos = @buffer.point
start_line = @buffer.current_line
tokens = lex(@buffer.substring(start_pos, bol_pos))
_, event, text = tokens.last
if event == :on_nl
_, event, text = tokens[-2]
end
if event == :on_tstring_beg ||
event == :on_heredoc_beg ||
event == :on_regexp_beg ||
(event == :on_regexp_end && text.size > 1) ||
event == :on_tstring_content
return nil
end
i, = find_nearest_beginning_token(tokens)
(line, column), event, = i ? tokens[i] : nil
if event == :on_lparen && tokens.dig(i + 1, 1) != :on_ignored_nl
return column + 1
end
if line
@buffer.goto_line(start_line - 1 + line)
while !@buffer.beginning_of_buffer?
if @buffer.save_excursion {
@buffer.backward_char
@buffer.skip_re_backward(/\s/)
@buffer.char_before == ?,
}
@buffer.backward_line
else
break
end
end
@buffer.looking_at?(/[ \t]*/)
base_indentation = space_width(@buffer.match_string(0))
end
@buffer.goto_char(bol_pos)
if line.nil?
indentation =
base_indentation - * @buffer[:indent_level]
else
indentation = base_indentation + @buffer[:indent_level]
end
if @buffer.looking_at?(/[ \t]*([}\])]|(end|else|elsif|when|in|rescue|ensure)\b)/)
indentation -= @buffer[:indent_level]
end
_, last_event, last_text = tokens.reverse_each.find { |_, e, _|
e != :on_sp && e != :on_nl && e != :on_ignored_nl
}
if start_with_period ||
(last_event == :on_op && last_text != "|") ||
(last_event == :on_kw && /\A(and|or)\z/.match?(last_text)) ||
last_event == :on_period ||
(last_event == :on_comma && event != :on_lbrace &&
event != :on_lparen && event != :on_lbracket) ||
last_event == :on_label
indentation += @buffer[:indent_level]
end
indentation
end
end
|
88
89
90
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 88
def
"#"
end
|
#compile(cmd = read_from_minibuffer("Compile: ",
default: default_compile_command)) ⇒ nil
140
141
142
143
144
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 140
def compile(cmd = read_from_minibuffer("Compile: ",
default: default_compile_command))
shell_execute(cmd, buffer_name: "*Ruby compile result*",
mode: BacktraceMode)
end
|
#default_compile_command ⇒ nil
#default_compile_command ⇒ String
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 150
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
|
#endless_method_def?(tokens, i) ⇒ Boolean
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 344
def endless_method_def?(tokens, i)
ts = tokens.drop(i + 1)
ts.shift while ts[0][1] == :on_sp
_, event = ts.shift
return false if event != :on_ident
ts.shift while ts[0][1] == :on_sp
if ts[0][1] == :on_lparen
ts.shift
count = 1
while count > 0
_, event = ts.shift
return false if event.nil?
case event
when :on_lparen
count +=1
when :on_rparen
count -=1
end
end
ts.shift while ts[0][1] == :on_sp
end
ts[0][1] == :on_op && ts[0][2] == "="
rescue NoMethodError return false
end
|
#find_first_path(arg0) ⇒ String
#find_first_path(arg0) ⇒ nil
390
391
392
393
394
395
396
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 390
def find_first_path(patterns)
patterns.each do |pattern|
paths = Dir.glob(pattern)
return paths.first if !paths.empty?
end
nil
end
|
#find_nearest_beginning_token(arg0) ⇒ Array[untyped]
#find_nearest_beginning_token(arg0) ⇒ Integer
#find_nearest_beginning_token(arg0) ⇒ nil
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 294
def find_nearest_beginning_token(tokens)
stack = []
(tokens.size - 1).downto(0) do |i|
(line, ), event, text = tokens[i]
case event
when :on_kw
_, prev_event, _ = tokens[i - 1]
next if prev_event == :on_symbeg
case text
when "class", "module", "def", "if", "unless", "case",
"do", "for", "while", "until", "begin"
if /\A(if|unless|while|until)\z/.match?(text) &&
modifier?(tokens, i)
next
end
if text == "def" && endless_method_def?(tokens, i)
next
end
if stack.empty?
return i
end
if stack.last != "end"
raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}"
end
stack.pop
when "end"
stack.push(text)
end
when :on_rbrace, :on_rparen, :on_rbracket, :on_embexpr_end
stack.push(text)
when :on_lbrace, :on_lparen, :on_lbracket, :on_tlambeg, :on_embexpr_beg
if stack.empty?
return i
end
if stack.last != BLOCK_END[text]
raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}"
end
stack.pop
end
end
return nil, stack.grep_v(/[)\]]/).size
end
|
#find_test_path(arg0, arg1, arg2) ⇒ String
#find_test_path(arg0, arg1, arg2) ⇒ nil
379
380
381
382
383
384
385
386
387
388
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 379
def find_test_path(base, namespace, name)
patterns = []
if namespace
patterns.push("#{base}/test/**/#{namespace}test_#{name}.rb")
patterns.push("#{base}/spec/**/#{namespace}#{name}_spec.rb")
end
patterns.push("#{base}/test/**/test_#{name}.rb")
patterns.push("#{base}/spec/**/#{name}_spec.rb")
find_first_path(patterns) or raise EditorError, "Test not found"
end
|
#find_test_target_path(arg0, arg1, arg2) ⇒ String
#find_test_target_path(arg0, arg1, arg2) ⇒ String
#find_test_target_path(arg0, arg1, arg2) ⇒ nil
370
371
372
373
374
375
376
377
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 370
def find_test_target_path(base, namespace, name)
patterns = []
if namespace
patterns.push("#{base}/{lib,app}/**/#{namespace}#{name}.rb")
end
patterns.push("#{base}/{lib,app}/**/#{name}.rb")
find_first_path(patterns) or raise EditorError, "Test target not found"
end
|
#forward_definition(n = number_prefix_arg || 1) ⇒ nil
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 98
def forward_definition(n = number_prefix_arg || 1)
tokens = Ripper.lex(@buffer.to_s)
@buffer.forward_line
n.times do |i|
tokens = tokens.drop_while { |(l, _), e, t|
l < @buffer.current_line ||
e != :on_kw || /\A(?:class|module|def)\z/ !~ t
}
(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
|
#lex(source) ⇒ Array[untyped]
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 201
def lex(source)
line_count = source.count("\n")
s = source
lineno = 1
tokens = []
loop do
lexer = Ripper::Lexer.new(s, "-", lineno)
tokens.concat(lexer.lex)
last_line = tokens.dig(-1, 0, 0)
return tokens if last_line.nil? || last_line >= line_count
s = source.sub(/(.*\n?){#{last_line}}/, "")
return tokens if last_line + 1 <= lineno
lineno = last_line + 1
end
end
|
#modifier?(arg0, arg1) ⇒ nil
#modifier?(arg0, arg1) ⇒ Boolean
337
338
339
340
341
342
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 337
def modifier?(tokens, i)
(line,), = tokens[i]
ts = tokens[0...i].reverse_each.take_while { |(l,_),| l == line }
t = ts.find { |_, e| e != :on_sp }
t && !(t[1] == :on_op && t[2] == "=")
end
|
#space_width(s) ⇒ Integer
182
183
184
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 182
def space_width(s)
s.gsub(/\t/, " " * @buffer[:tab_width]).size
end
|
#symbol_pattern ⇒ Regexp
146
147
148
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 146
def symbol_pattern
/[\p{Letter}\p{Number}_$@!?]/
end
|
#toggle_test ⇒ Array[untyped]
#toggle_test ⇒ nil
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/textbringer/modes/ruby_mode.rb', line 162
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
|