Skip to content

Commit

Permalink
Refactor option support
Browse files Browse the repository at this point in the history
Define the extension options separate from the general options.

Test for the presence of an option before trying to evaluate it. This
protects against options being unset after they were first initialized.
  • Loading branch information
lilyball committed Jul 9, 2014
1 parent 5a682db commit fd83a66
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions autoload/preview.vim
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ require 'shellwords'
class Preview
include Singleton

EXT_OPTIONS = {
:markdown => "g:PreviewMarkdownExt",
:textile => "g:PreviewTextileExt",
:rdoc => "g:PreviewRdocExt",
:ronn => "g:PreviewRonnExt",
:html => "g:PreviewHtmlExt",
:rst => "g:PreviewRstExt"
}

OPTIONS = {
:browsers => "g:PreviewBrowsers",
:css_path => "g:PreviewCSSPath",
:markdown_ext => "g:PreviewMarkdownExt",
:textile_ext => "g:PreviewTextileExt",
:rdoc_ext => "g:PreviewRdocExt",
:ronn_ext => "g:PreviewRonnExt",
:html_ext => "g:PreviewHtmlExt",
:rst_ext => "g:PreviewRstExt"
}
:css_path => "g:PreviewCSSPath"
}.merge!(EXT_OPTIONS)

DEPENDECIES = {
# :format => {:gem => 'name of gem' , :require => 'file to require'}
Expand All @@ -92,10 +95,9 @@ class Preview

def show
update_fnames
ext_opts = OPTIONS.keys.find_all{|k| k.to_s =~ /_ext$/}
ext_opts.each do |opt|
if exts_match?(option(opt).split(','))
send "show_" + opt.to_s[/^(.*)_ext$/, 1]
EXT_OPTIONS.keys.each do |opt|
if has_option(opt) and exts_match?(option(opt).split(','))
send "show_#{opt}"
return
end
end
Expand Down Expand Up @@ -254,6 +256,11 @@ class Preview
VIM.evaluate(OPTIONS[name])
end

def has_option(name)
raise "Unknown option #{name.inspect}" unless OPTIONS.keys.include?(name)
VIM.evaluate("exists('#{OPTIONS[name]}')") != 0
end

def load_dependencies(format)
require DEPENDECIES[format][:require]
true
Expand Down

0 comments on commit fd83a66

Please sign in to comment.