mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Refactoring Reline accessor.
This commit is contained in:
parent
d5b237325b
commit
9c0cd6a09e
2 changed files with 307 additions and 0 deletions
133
lib/reline2.rb
Normal file
133
lib/reline2.rb
Normal file
|
@ -0,0 +1,133 @@
|
|||
require 'io/console'
|
||||
require 'timeout'
|
||||
require "forwardable"
|
||||
require 'reline/version'
|
||||
require 'reline/config'
|
||||
require 'reline/key_actor'
|
||||
require 'reline/key_stroke'
|
||||
require 'reline/line_editor'
|
||||
|
||||
module Reline2
|
||||
FILENAME_COMPLETION_PROC = nil
|
||||
USERNAME_COMPLETION_PROC = nil
|
||||
|
||||
class Core
|
||||
Key = Struct.new('Key', :char, :combined_char, :with_meta)
|
||||
if RUBY_PLATFORM =~ /mswin|mingw/
|
||||
IS_WINDOWS = true
|
||||
else
|
||||
IS_WINDOWS = false
|
||||
end
|
||||
|
||||
CursorPos = Struct.new(:x, :y)
|
||||
|
||||
ATTR_ACCESSOR_NAMES = %i(
|
||||
completion_append_character
|
||||
basic_word_break_characters
|
||||
completer_word_break_characters
|
||||
basic_quote_characters
|
||||
completer_quote_characters
|
||||
filename_quote_characters
|
||||
special_prefixes
|
||||
completion_case_fold
|
||||
completion_proc
|
||||
output_modifier_proc
|
||||
prompt_proc
|
||||
auto_indent_proc
|
||||
pre_input_hook
|
||||
dig_perfect_match_proc
|
||||
).freeze
|
||||
ATTR_ACCESSOR_NAMES.each &method(:attr_accessor)
|
||||
|
||||
def initialize
|
||||
@config = Reline::Config.new
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@ambiguous_width = nil
|
||||
|
||||
self.basic_word_break_characters = " \t\n`><=;|&{("
|
||||
self.completer_word_break_characters = " \t\n`><=;|&{("
|
||||
self.basic_quote_characters = '"\''
|
||||
self.completer_quote_characters = '"\''
|
||||
self.filename_quote_characters = ""
|
||||
self.special_prefixes = ""
|
||||
end
|
||||
|
||||
def completion_append_character=(val)
|
||||
if val.nil?
|
||||
@completion_append_character = nil
|
||||
elsif val.size == 1
|
||||
@completion_append_character = val.encode(Encoding::default_external)
|
||||
elsif val.size > 1
|
||||
@completion_append_character = val[0].encode(Encoding::default_external)
|
||||
else
|
||||
@completion_append_character = nil
|
||||
end
|
||||
end
|
||||
|
||||
def basic_word_break_characters=(v)
|
||||
@basic_word_break_characters = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def completer_word_break_characters=(v)
|
||||
@completer_word_break_characters = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def basic_quote_characters=(v)
|
||||
@basic_quote_characters = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def completer_quote_characters=(v)
|
||||
@completer_quote_characters = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def filename_quote_characters=(v)
|
||||
@filename_quote_characters = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def special_prefixes=(v)
|
||||
@special_prefixes = v.encode(Encoding::default_external)
|
||||
end
|
||||
|
||||
def completion_proc=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@completion_proc = p
|
||||
end
|
||||
|
||||
def output_modifier_proc=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@output_modifier_proc = p
|
||||
end
|
||||
|
||||
def prompt_proc=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@prompt_proc = p
|
||||
end
|
||||
|
||||
def auto_indent_proc=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@auto_indent_proc = p
|
||||
end
|
||||
|
||||
def pre_input_hook=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@pre_input_hook = p
|
||||
end
|
||||
|
||||
def dig_perfect_match_proc=(p)
|
||||
raise ArgumentError unless p.is_a?(Proc)
|
||||
@dig_perfect_match_proc = p
|
||||
end
|
||||
end
|
||||
|
||||
extend SingleForwardable
|
||||
|
||||
Core::ATTR_ACCESSOR_NAMES.each { |name|
|
||||
def_delegators :core, "#{name}", "#{name}="
|
||||
}
|
||||
|
||||
private
|
||||
|
||||
def self.core
|
||||
@core ||= Core.new
|
||||
end
|
||||
end
|
174
test/test_reline2.rb
Normal file
174
test/test_reline2.rb
Normal file
|
@ -0,0 +1,174 @@
|
|||
require_relative 'helper'
|
||||
require "reline2"
|
||||
|
||||
class Reline2::Test < Reline::TestCase
|
||||
def test_completion_append_character
|
||||
assert_equal(Reline2.completion_append_character, nil)
|
||||
|
||||
Reline2.completion_append_character = ""
|
||||
assert_equal(Reline2.completion_append_character, nil)
|
||||
|
||||
Reline2.completion_append_character = "a"
|
||||
assert_equal(Reline2.completion_append_character, "a")
|
||||
assert_equal(Reline2.completion_append_character.encoding, Encoding::default_external)
|
||||
|
||||
Reline2.completion_append_character = "ba"
|
||||
assert_equal(Reline2.completion_append_character, "b")
|
||||
assert_equal(Reline2.completion_append_character.encoding, Encoding::default_external)
|
||||
|
||||
Reline2.completion_append_character = "cba"
|
||||
assert_equal(Reline2.completion_append_character, "c")
|
||||
assert_equal(Reline2.completion_append_character.encoding, Encoding::default_external)
|
||||
|
||||
Reline2.completion_append_character = nil
|
||||
assert_equal(Reline2.completion_append_character, nil)
|
||||
end
|
||||
|
||||
def test_basic_word_break_characters
|
||||
assert_equal(Reline2.basic_word_break_characters, " \t\n`><=;|&{(")
|
||||
|
||||
Reline2.basic_word_break_characters = "ああ"
|
||||
assert_equal(Reline2.basic_word_break_characters, "ああ")
|
||||
assert_equal(Reline2.basic_word_break_characters.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_completer_word_break_characters
|
||||
assert_equal(Reline2.completer_word_break_characters, " \t\n`><=;|&{(")
|
||||
|
||||
Reline2.completer_word_break_characters = "ああ"
|
||||
assert_equal(Reline2.completer_word_break_characters, "ああ")
|
||||
assert_equal(Reline2.completer_word_break_characters.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_basic_quote_characters
|
||||
assert_equal(Reline2.basic_quote_characters, '"\'')
|
||||
|
||||
Reline2.basic_quote_characters = "“"
|
||||
assert_equal(Reline2.basic_quote_characters, "“")
|
||||
assert_equal(Reline2.basic_quote_characters.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_completer_quote_characters
|
||||
assert_equal(Reline2.completer_quote_characters, '"\'')
|
||||
|
||||
Reline2.completer_quote_characters = "“"
|
||||
assert_equal(Reline2.completer_quote_characters, "“")
|
||||
assert_equal(Reline2.completer_quote_characters.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_filename_quote_characters
|
||||
assert_equal(Reline2.filename_quote_characters, '')
|
||||
|
||||
Reline2.filename_quote_characters = "\'"
|
||||
assert_equal(Reline2.filename_quote_characters, "\'")
|
||||
assert_equal(Reline2.filename_quote_characters.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_special_prefixes
|
||||
assert_equal(Reline2.special_prefixes, '')
|
||||
|
||||
Reline2.special_prefixes = "\'"
|
||||
assert_equal(Reline2.special_prefixes, "\'")
|
||||
assert_equal(Reline2.special_prefixes.encoding, Encoding::default_external)
|
||||
end
|
||||
|
||||
def test_completion_case_fold
|
||||
assert_equal(Reline2.completion_case_fold, nil)
|
||||
|
||||
Reline2.completion_case_fold = true
|
||||
assert_equal(Reline2.completion_case_fold, true)
|
||||
|
||||
Reline2.completion_case_fold = "hoge"
|
||||
assert_equal(Reline2.completion_case_fold, "hoge")
|
||||
end
|
||||
|
||||
def test_completion_proc
|
||||
assert_equal(Reline2.completion_proc, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.completion_proc = p
|
||||
assert_equal(Reline2.completion_proc, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.completion_proc = l
|
||||
assert_equal(Reline2.completion_proc, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.completion_proc = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.completion_proc = "hoge" }
|
||||
end
|
||||
|
||||
def test_output_modifier_proc
|
||||
assert_equal(Reline2.output_modifier_proc, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.output_modifier_proc = p
|
||||
assert_equal(Reline2.output_modifier_proc, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.output_modifier_proc = l
|
||||
assert_equal(Reline2.output_modifier_proc, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.output_modifier_proc = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.output_modifier_proc = "hoge" }
|
||||
end
|
||||
|
||||
def test_prompt_proc
|
||||
assert_equal(Reline2.prompt_proc, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.prompt_proc = p
|
||||
assert_equal(Reline2.prompt_proc, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.prompt_proc = l
|
||||
assert_equal(Reline2.prompt_proc, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.prompt_proc = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.prompt_proc = "hoge" }
|
||||
end
|
||||
|
||||
def test_auto_indent_proc
|
||||
assert_equal(Reline2.auto_indent_proc, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.auto_indent_proc = p
|
||||
assert_equal(Reline2.auto_indent_proc, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.auto_indent_proc = l
|
||||
assert_equal(Reline2.auto_indent_proc, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.auto_indent_proc = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.auto_indent_proc = "hoge" }
|
||||
end
|
||||
|
||||
def test_pre_input_hook
|
||||
assert_equal(Reline2.pre_input_hook, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.pre_input_hook = p
|
||||
assert_equal(Reline2.pre_input_hook, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.pre_input_hook = l
|
||||
assert_equal(Reline2.pre_input_hook, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.pre_input_hook = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.pre_input_hook = "hoge" }
|
||||
end
|
||||
|
||||
def test_dig_perfect_match_proc
|
||||
assert_equal(Reline2.dig_perfect_match_proc, nil)
|
||||
|
||||
p = proc {}
|
||||
Reline2.dig_perfect_match_proc = p
|
||||
assert_equal(Reline2.dig_perfect_match_proc, p)
|
||||
|
||||
l = lambda {}
|
||||
Reline2.dig_perfect_match_proc = l
|
||||
assert_equal(Reline2.dig_perfect_match_proc, l)
|
||||
|
||||
assert_raise(ArgumentError) { Reline2.dig_perfect_match_proc = 42 }
|
||||
assert_raise(ArgumentError) { Reline2.dig_perfect_match_proc = "hoge" }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue