Skip to content

Commit

Permalink
Make Settings class more flexible, less brittle
Browse files Browse the repository at this point in the history
I hadn't touched this class for about 4 years, and this morning had to
add another setting to it in order to fix a bug (see commit
2861dc1). With a more flexible design, I could have left it
untouched.

This commit implements that design, teaching the settings class to
fully mediate between the caller who wants to save/restore some
settings, and Vim. It records the settings that are to be changed, makes
the changes, and can restore the settings by "replaying" the recorded
settings.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
  • Loading branch information
wincent committed Mar 4, 2014
1 parent 2861dc1 commit 514194c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
20 changes: 10 additions & 10 deletions ruby/command-t/match_window.rb
Expand Up @@ -49,16 +49,16 @@ def initialize options = {}

# global settings (must manually save and restore)
@settings = Settings.new
::VIM::set_option 'timeout' # ensure mappings timeout
::VIM::set_option 'timeoutlen=0' # respond immediately to mappings
::VIM::set_option 'nohlsearch' # don't highlight search strings
::VIM::set_option 'noinsertmode' # don't make Insert mode the default
::VIM::set_option 'noshowcmd' # don't show command info on last line
::VIM::set_option 'report=9999' # don't show "X lines changed" reports
::VIM::set_option 'sidescroll=0' # don't sidescroll in jumps
::VIM::set_option 'sidescrolloff=0' # don't sidescroll automatically
::VIM::set_option 'noequalalways' # don't auto-balance window sizes
::VIM::set_option "updatetime=#{options[:debounce_interval]}"
@settings.set 'timeout', true # ensure mappings timeout
@settings.set 'hlsearch', false # don't highlight search strings
@settings.set 'insertmode', false # don't make Insert mode the default
@settings.set 'showcmd', false # don't show command info on last line
@settings.set 'equalalways', false # don't auto-balance window sizes
@settings.set 'timeoutlen', 0 # respond immediately to mappings
@settings.set 'report', 9999 # don't show "X lines changed" reports
@settings.set 'sidescroll', 0 # don't sidescroll in jumps
@settings.set 'sidescrolloff', 0 # don't sidescroll automatically
@settings.set 'updatetime', options[:debounce_interval]

# show match window
split_location = options[:match_window_at_top] ? 'topleft' : 'botright'
Expand Down
48 changes: 22 additions & 26 deletions ruby/command-t/settings.rb
Expand Up @@ -25,50 +25,46 @@ module CommandT
# Convenience class for saving and restoring global settings.
class Settings
def initialize
save
@settings = []
end

def save
@timeoutlen = get_number 'timeoutlen'
@report = get_number 'report'
@sidescroll = get_number 'sidescroll'
@sidescrolloff = get_number 'sidescrolloff'
@updatetime = get_number 'updatetime'
@timeout = get_bool 'timeout'
@equalalways = get_bool 'equalalways'
@hlsearch = get_bool 'hlsearch'
@insertmode = get_bool 'insertmode'
@showcmd = get_bool 'showcmd'
def set(setting, value)
case value
when TrueClass, FalseClass
@settings.push([setting, get_bool(setting)])
set_bool setting, value
when Numeric
@settings.push([setting, get_number(setting)])
set_number setting, value
end
end

def restore
set_number 'timeoutlen', @timeoutlen
set_number 'report', @report
set_number 'sidescroll', @sidescroll
set_number 'sidescrolloff', @sidescrolloff
set_number 'updatetime', @updatetime
set_bool 'timeout', @timeout
set_bool 'equalalways', @equalalways
set_bool 'hlsearch', @hlsearch
set_bool 'insertmode', @insertmode
set_bool 'showcmd', @showcmd
@settings.each do |setting, value|
case value
when TrueClass, FalseClass
set_bool setting, value
when Numeric
set_number setting, value
end
end
end

private

def get_number setting
def get_number(setting)
::VIM::evaluate("&#{setting}").to_i
end

def get_bool setting
def get_bool(setting)
::VIM::evaluate("&#{setting}").to_i == 1
end

def set_number setting, value
def set_number(setting, value)
::VIM::set_option "#{setting}=#{value}"
end

def set_bool setting, value
def set_bool(setting, value)
if value
::VIM::set_option setting
else
Expand Down

0 comments on commit 514194c

Please sign in to comment.