class SnipmateToYas::Snipmate::SingleParser

Parses a single vim snipmate snippet

Constants

EMPTY_PLACEHOLDER_REGEXP
HEADING_LINE
INTERPOLATION_REGEXP
SNIPPET
TEMPLATE

Public Class Methods

new(snipmate_snippet, interpolation_converter = nil) click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 19
def initialize(snipmate_snippet, interpolation_converter = nil)
  fail(ArgumentError, 'snippet must not be nil') if snipmate_snippet.nil?

  @snipmate_snippet = snipmate_snippet
  @interpolation_converter =
    interpolation_converter || DefaultInterpolationConverter.new
end

Public Instance Methods

convert() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 27
def convert
  validate_snippet
  Yas::Snippet.new(text: TEMPLATE.result(binding), expand_key: key)
end

Protected Instance Methods

body() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 58
def body
  remove_braces_from_empty_placeholders(
    convert_interpolations(raw_body)
  ).chomp
end
convert_interpolations(snippet) click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 70
def convert_interpolations(snippet)
  snippet.gsub(INTERPOLATION_REGEXP) do
    interpolation =
      @interpolation_converter.convert(Regexp.last_match[:interpolation])

    interpolation.nil? ? '' : "`#{interpolation}`"
  end
end
detect_indentation() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 85
def detect_indentation
  match = @snipmate_snippet.lines[1].match(/^(?<indentation>\s+).*$/)
  return match[:indentation] if match
  ''
end
heading_tokens() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 54
def heading_tokens
  @snipmate_snippet.lines.first.match(HEADING_LINE)
end
key() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 50
def key
  heading_tokens[:key]
end
name() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 45
def name
  return heading_tokens[:name] unless heading_tokens[:name].empty?
  heading_tokens[:key]
end
raw_body() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 79
def raw_body
  @snipmate_snippet.lines.drop(1).map do |line|
    line.gsub(/^#{detect_indentation}/, '')
  end.join('')
end
remove_braces_from_empty_placeholders(snippet) click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 64
def remove_braces_from_empty_placeholders(snippet)
  snippet.gsub(EMPTY_PLACEHOLDER_REGEXP) do
    "$#{Regexp.last_match[:index]}"
  end
end
validate_snippet() click to toggle source
# File lib/snipmate_to_yas/snipmate/single_parser.rb, line 34
def validate_snippet
  fail(
    ArgumentError,
    "Snippet '#{@snipmate_snippet}' is not in recognizable format"
  ) unless @snipmate_snippet.match(HEADING_LINE)

  fail(
    ArgumentError, "Snippet '#{@snipmate_snippet}' must contain a body"
  ) if body.empty?
end