Module: Ripl::MultiLine::Ripper

Defined in:
lib/ripl/multi_line/ripper.rb

Overview

# # # This multi-line implementation uses Ripper

works on:         2.0  1.9  1.8(gem)
analyze features: [:literal, :string]
                  [:literal, :regexp]
                  [:literal, :array]   (mri only)
                  [:literal, :hash]    (mri only)
                  [:statement]
                  [:forced]
notes:            statement could also be [

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#multiline?(string) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ripl/multi_line/ripper.rb', line 21

def multiline?(string)
  return [:forced] if string =~ /;\s*\Z/

  expr = ::Ripper::SexpBuilder.new(string).parse

  return [:statement] if !expr # not always statements...

  # literals are problematic..
  last_expr = expr[-1][-1]

  return [:literal, :regexp] if last_expr == [:regexp_literal, [:regexp_new], nil]

  delimiters = %q_(?:[\[<({][\]>)}]|(.)\1)_

  return [:literal, :string] if last_expr == [:string_literal, [:string_content]] &&
    string !~ /(?:""|''|%q?#{delimiters})\s*\Z/i # empty literal at $

  return [:literal, :string] if last_expr == [:xstring_literal, [:xstring_new]] &&
    string !~ /(?:``|%x#{delimiters})\s*\Z/i

  return [:literal, :string] if last_expr == [:words_new] &&
    string !~ /%W#{delimiters}\s*\Z/
 
  return [:literal, :string] if last_expr == [:qwords_new] &&
    string !~ /%w#{delimiters}\s*\Z/
end