katakata_irb 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +8 -2
- data/README.md +16 -26
- data/bin/console +2 -7
- data/katakata_irb.gemspec +2 -1
- data/lib/katakata_irb/completor.rb +128 -14
- data/lib/katakata_irb/{trex.rb → nesting_parser.rb} +47 -11
- data/lib/katakata_irb/scope.rb +248 -0
- data/lib/katakata_irb/type_simulator.rb +148 -332
- data/lib/katakata_irb/types.rb +96 -22
- data/lib/katakata_irb/version.rb +1 -1
- data/lib/katakata_irb.rb +5 -7
- metadata +21 -15
- data/exe/kirb +0 -11
- data/lib/katakata_irb/reline_patch.rb +0 -43
- data/lib/katakata_irb/reline_patches/escapeseq.patch +0 -45
- data/lib/katakata_irb/reline_patches/indent.patch +0 -25
- data/lib/katakata_irb/reline_patches/raw.patch +0 -95
- data/lib/katakata_irb/reline_patches/scrollbar.patch +0 -34
- data/lib/katakata_irb/reline_patches/wholelines.patch +0 -102
- data/lib/katakata_irb/ruby_lex_patch.rb +0 -221
@@ -1,102 +0,0 @@
|
|
1
|
-
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
|
2
|
-
index 8153aab..1c33a4b 100644
|
3
|
-
--- a/lib/reline/line_editor.rb
|
4
|
-
+++ b/lib/reline/line_editor.rb
|
5
|
-
@@ -449,12 +449,8 @@ class Reline::LineEditor
|
6
|
-
Reline::IOGate.move_cursor_up(@first_line_started_from + @started_from - @scroll_partial_screen)
|
7
|
-
Reline::IOGate.move_cursor_column(0)
|
8
|
-
@scroll_partial_screen = nil
|
9
|
-
- prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines)
|
10
|
-
- if @previous_line_index
|
11
|
-
- new_lines = whole_lines(index: @previous_line_index, line: @line)
|
12
|
-
- else
|
13
|
-
- new_lines = whole_lines
|
14
|
-
- end
|
15
|
-
+ new_lines = whole_lines
|
16
|
-
+ prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines)
|
17
|
-
modify_lines(new_lines).each_with_index do |line, index|
|
18
|
-
@output.write "#{prompt_list ? prompt_list[index] : prompt}#{line}\n"
|
19
|
-
Reline::IOGate.erase_after_cursor
|
20
|
-
@@ -490,11 +486,7 @@ class Reline::LineEditor
|
21
|
-
if @is_multiline
|
22
|
-
if finished?
|
23
|
-
# Always rerender on finish because output_modifier_proc may return a different output.
|
24
|
-
- if @previous_line_index
|
25
|
-
- new_lines = whole_lines(index: @previous_line_index, line: @line)
|
26
|
-
- else
|
27
|
-
- new_lines = whole_lines
|
28
|
-
- end
|
29
|
-
+ new_lines = whole_lines
|
30
|
-
line = modify_lines(new_lines)[@line_index]
|
31
|
-
clear_dialog
|
32
|
-
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines)
|
33
|
-
@@ -1013,11 +1005,7 @@ class Reline::LineEditor
|
34
|
-
end
|
35
|
-
|
36
|
-
private def rerender_changed_current_line
|
37
|
-
- if @previous_line_index
|
38
|
-
- new_lines = whole_lines(index: @previous_line_index, line: @line)
|
39
|
-
- else
|
40
|
-
- new_lines = whole_lines
|
41
|
-
- end
|
42
|
-
+ new_lines = whole_lines
|
43
|
-
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines)
|
44
|
-
all_height = calculate_height_by_lines(new_lines, prompt_list || prompt)
|
45
|
-
diff = all_height - @highest_in_all
|
46
|
-
@@ -1698,7 +1686,7 @@ class Reline::LineEditor
|
47
|
-
return if not @check_new_auto_indent and @previous_line_index # move cursor up or down
|
48
|
-
if @check_new_auto_indent and @previous_line_index and @previous_line_index > 0 and @line_index > @previous_line_index
|
49
|
-
# Fix indent of a line when a newline is inserted to the next
|
50
|
-
- new_lines = whole_lines(index: @previous_line_index, line: @line)
|
51
|
-
+ new_lines = whole_lines
|
52
|
-
new_indent = @auto_indent_proc.(new_lines[0..-3].push(''), @line_index - 1, 0, true)
|
53
|
-
md = @line.match(/\A */)
|
54
|
-
prev_indent = md[0].count(' ')
|
55
|
-
@@ -1713,11 +1701,7 @@ class Reline::LineEditor
|
56
|
-
@line = ' ' * new_indent + @line.lstrip
|
57
|
-
end
|
58
|
-
end
|
59
|
-
- if @previous_line_index
|
60
|
-
- new_lines = whole_lines(index: @previous_line_index, line: @line)
|
61
|
-
- else
|
62
|
-
- new_lines = whole_lines
|
63
|
-
- end
|
64
|
-
+ new_lines = whole_lines
|
65
|
-
new_indent = @auto_indent_proc.(new_lines, @line_index, @byte_pointer, @check_new_auto_indent)
|
66
|
-
new_indent = @cursor_max if new_indent&.> @cursor_max
|
67
|
-
if new_indent&.>= 0
|
68
|
-
@@ -1803,11 +1787,7 @@ class Reline::LineEditor
|
69
|
-
target = before
|
70
|
-
end
|
71
|
-
if @is_multiline
|
72
|
-
- if @previous_line_index
|
73
|
-
- lines = whole_lines(index: @previous_line_index, line: @line)
|
74
|
-
- else
|
75
|
-
- lines = whole_lines
|
76
|
-
- end
|
77
|
-
+ lines = whole_lines
|
78
|
-
if @line_index > 0
|
79
|
-
preposing = lines[0..(@line_index - 1)].join("\n") + "\n" + preposing
|
80
|
-
end
|
81
|
-
@@ -1907,7 +1887,7 @@ class Reline::LineEditor
|
82
|
-
@cursor_max = calculate_width(@line)
|
83
|
-
end
|
84
|
-
|
85
|
-
- def whole_lines(index: @line_index, line: @line)
|
86
|
-
+ def whole_lines(index: @previous_line_index || @line_index, line: @line)
|
87
|
-
temp_lines = @buffer_of_lines.dup
|
88
|
-
temp_lines[index] = line
|
89
|
-
temp_lines
|
90
|
-
@@ -1917,11 +1897,7 @@ class Reline::LineEditor
|
91
|
-
if @buffer_of_lines.size == 1 and @line.nil?
|
92
|
-
nil
|
93
|
-
else
|
94
|
-
- if @previous_line_index
|
95
|
-
- whole_lines(index: @previous_line_index, line: @line).join("\n")
|
96
|
-
- else
|
97
|
-
- whole_lines.join("\n")
|
98
|
-
- end
|
99
|
-
+ whole_lines.join("\n")
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
@@ -1,221 +0,0 @@
|
|
1
|
-
require 'irb'
|
2
|
-
require_relative 'trex'
|
3
|
-
|
4
|
-
module KatakataIrb::RubyLexPatch
|
5
|
-
def self.patch_to_ruby_lex
|
6
|
-
(RubyLex.instance_methods(false) - [:set_prompt, :process_continue, :check_code_block]).each { RubyLex.remove_method _1 }
|
7
|
-
RubyLex.prepend self
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.complete_tokens(code, context: nil)
|
11
|
-
incomplete_tokens = RubyLex.ripper_lex_without_warning(code, context: context)
|
12
|
-
KatakataIrb::TRex.interpolate_ripper_ignored_tokens(code, incomplete_tokens)
|
13
|
-
end
|
14
|
-
|
15
|
-
def calc_nesting_depth(opens)
|
16
|
-
indent_level = 0
|
17
|
-
nesting_level = 0
|
18
|
-
opens.each_with_index do |t, index|
|
19
|
-
case t.event
|
20
|
-
when :on_heredoc_beg
|
21
|
-
if opens[index + 1]&.event != :on_heredoc_beg
|
22
|
-
if t.tok.start_with?('<<~')
|
23
|
-
indent_level += 1
|
24
|
-
else
|
25
|
-
indent_level = 0
|
26
|
-
end
|
27
|
-
end
|
28
|
-
when :on_tstring_beg, :on_regexp_beg, :on_symbeg
|
29
|
-
indent_level += 1 if t.tok[0] == '%'
|
30
|
-
when :on_embdoc_beg
|
31
|
-
indent_level = 0
|
32
|
-
else
|
33
|
-
nesting_level += 1
|
34
|
-
indent_level += 1
|
35
|
-
end
|
36
|
-
end
|
37
|
-
[indent_level, nesting_level]
|
38
|
-
end
|
39
|
-
|
40
|
-
def process_indent_level(tokens)
|
41
|
-
opens = KatakataIrb::TRex.parse(tokens)
|
42
|
-
indent, _nesting = calc_nesting_depth(opens)
|
43
|
-
indent * 2
|
44
|
-
end
|
45
|
-
|
46
|
-
def check_corresponding_token_depth(tokens, line_index)
|
47
|
-
line_results = KatakataIrb::TRex.parse_line(tokens)
|
48
|
-
result = line_results[line_index]
|
49
|
-
return unless result
|
50
|
-
_tokens, prev, opens, min_depth = result
|
51
|
-
depth, = calc_nesting_depth(opens.take(min_depth))
|
52
|
-
prev_depth, = calc_nesting_depth(prev)
|
53
|
-
depth * 2 if depth < prev_depth
|
54
|
-
end
|
55
|
-
|
56
|
-
def ltype_from_open_tokens(opens)
|
57
|
-
return nil if opens.empty?
|
58
|
-
case opens.last.tok
|
59
|
-
when ?`, /^<<[-~]?`/, /^%x.$/
|
60
|
-
?`
|
61
|
-
when ?', /^<<[-~]?'/, /^%q.$/
|
62
|
-
?'
|
63
|
-
when ?", /^<</, /^%.$/, /^%Q.$/
|
64
|
-
?"
|
65
|
-
when ":'", ':"', ':', /^%s.$/
|
66
|
-
':'
|
67
|
-
when /^%[iwIW].$/
|
68
|
-
']'
|
69
|
-
when '/', /^%r.$/
|
70
|
-
'/'
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def check_termination_in_prev_line(code, context: nil)
|
75
|
-
tokens = self.class.ripper_lex_without_warning(code, context: context)
|
76
|
-
last_newline_index = tokens.rindex { |t| t.tok.include?("\n") }
|
77
|
-
index = (0...last_newline_index).reverse_each.find { |i| tokens[i].tok.include?("\n") }
|
78
|
-
return false unless index
|
79
|
-
|
80
|
-
last_line_tokens = tokens[(index + 1)..(tokens.size - 1)]
|
81
|
-
first_token = last_line_tokens.find do |t|
|
82
|
-
![:on_sp, :on_ignored_sp, :on_comment].include?(t.event)
|
83
|
-
end
|
84
|
-
|
85
|
-
if first_token && first_token.state != Ripper::EXPR_DOT
|
86
|
-
tokens_without_last_line = tokens[0..index]
|
87
|
-
if check_termination(tokens_without_last_line.map(&:tok).join, context: context)
|
88
|
-
return last_line_tokens.map(&:tok).join
|
89
|
-
end
|
90
|
-
end
|
91
|
-
false
|
92
|
-
end
|
93
|
-
|
94
|
-
def check_termination(code, context: nil)
|
95
|
-
tokens = KatakataIrb::RubyLexPatch.complete_tokens(code, context: context)
|
96
|
-
opens = KatakataIrb::TRex.parse(tokens)
|
97
|
-
opens.empty? && !process_continue(tokens) && !check_code_block(code, tokens)
|
98
|
-
end
|
99
|
-
|
100
|
-
def set_input(io, p = nil, context: nil, &block)
|
101
|
-
@io = io
|
102
|
-
if @io.respond_to?(:check_termination)
|
103
|
-
@io.check_termination do |code|
|
104
|
-
if Reline::IOGate.in_pasting?
|
105
|
-
rest = check_termination_in_prev_line(code, context: context)
|
106
|
-
if rest
|
107
|
-
Reline.delete_text
|
108
|
-
rest.bytes.reverse_each do |c|
|
109
|
-
Reline.ungetc(c)
|
110
|
-
end
|
111
|
-
true
|
112
|
-
else
|
113
|
-
false
|
114
|
-
end
|
115
|
-
else
|
116
|
-
# Accept any single-line input for symbol aliases or commands that transform args
|
117
|
-
command = code.split(/\s/, 2).first
|
118
|
-
if context.symbol_alias?(command) || context.transform_args?(command)
|
119
|
-
next true
|
120
|
-
end
|
121
|
-
|
122
|
-
code.gsub!(/\s*\z/, '').concat("\n")
|
123
|
-
check_termination(code, context: context)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
if @io.respond_to?(:dynamic_prompt)
|
128
|
-
@io.dynamic_prompt do |lines|
|
129
|
-
lines << '' if lines.empty?
|
130
|
-
code = lines.map{ |l| l + "\n" }.join
|
131
|
-
tokens = KatakataIrb::RubyLexPatch.complete_tokens code, context: context
|
132
|
-
line_results = KatakataIrb::TRex.parse_line(tokens)
|
133
|
-
continue = false
|
134
|
-
tokens_until_line = []
|
135
|
-
line_results.map.with_index do |(line_tokens, _prev_opens, next_opens), line_num_offset|
|
136
|
-
line_tokens.each do |token, _s|
|
137
|
-
tokens_until_line << token if token != tokens_until_line.last
|
138
|
-
end
|
139
|
-
continue = process_continue(tokens_until_line)
|
140
|
-
prompt next_opens, continue, line_num_offset
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
if p.respond_to?(:call)
|
146
|
-
@input = p
|
147
|
-
elsif block_given?
|
148
|
-
@input = block
|
149
|
-
else
|
150
|
-
@input = Proc.new{@io.gets}
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
def set_auto_indent(context)
|
155
|
-
if @io.respond_to?(:auto_indent) and context.auto_indent_mode
|
156
|
-
@io.auto_indent do |lines, line_index, byte_pointer, is_newline|
|
157
|
-
if is_newline
|
158
|
-
tokens = KatakataIrb::RubyLexPatch.complete_tokens(lines[0..line_index].join("\n"), context: context)
|
159
|
-
process_indent_level(tokens)
|
160
|
-
else
|
161
|
-
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
|
162
|
-
last_line = lines[line_index]&.byteslice(0, byte_pointer)
|
163
|
-
code += last_line if last_line
|
164
|
-
tokens = KatakataIrb::RubyLexPatch.complete_tokens(code, context: context)
|
165
|
-
check_corresponding_token_depth(tokens, line_index)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def prompt(opens, continue, line_num_offset)
|
172
|
-
ltype = ltype_from_open_tokens(opens)
|
173
|
-
_indent, nesting_level = calc_nesting_depth(opens)
|
174
|
-
@prompt.call(ltype, nesting_level, opens.any? || continue, @line_no + line_num_offset)
|
175
|
-
end
|
176
|
-
|
177
|
-
# TODO: do not use this. change the api. example: @input.call(prompt)
|
178
|
-
def store_prompt_to_irb(opens, continue, line_num_offset)
|
179
|
-
prompt(opens, continue, line_num_offset)
|
180
|
-
end
|
181
|
-
|
182
|
-
def readmultiline(context)
|
183
|
-
if @io.respond_to? :check_termination
|
184
|
-
store_prompt_to_irb([], false, 0)
|
185
|
-
@input.call
|
186
|
-
else
|
187
|
-
# nomultiline
|
188
|
-
line = ''
|
189
|
-
line_offset = 0
|
190
|
-
store_prompt_to_irb([], false, 0)
|
191
|
-
loop do
|
192
|
-
l = @input.call
|
193
|
-
unless l
|
194
|
-
return if line.empty?
|
195
|
-
next
|
196
|
-
end
|
197
|
-
line << l
|
198
|
-
tokens = KatakataIrb::RubyLexPatch.complete_tokens(line, context: context)
|
199
|
-
_line_tokens, _prev_opens, next_opens = KatakataIrb::TRex.parse_line(tokens).last
|
200
|
-
return line if next_opens.empty?
|
201
|
-
line_offset += 1
|
202
|
-
store_prompt_to_irb(next_opens, true, line_offset)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
def each_top_level_statement(context = nil)
|
208
|
-
loop do
|
209
|
-
begin
|
210
|
-
line = readmultiline(context)
|
211
|
-
break unless line
|
212
|
-
if line != "\n"
|
213
|
-
line.force_encoding(@io.encoding)
|
214
|
-
yield line, @line_no
|
215
|
-
end
|
216
|
-
@line_no += line.count("\n")
|
217
|
-
rescue RubyLex::TerminateLineInput
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|