Class: Canopus::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/snippet.rb,
sig/snippet.rbs

Overview

LSP/TextMate snippets. All exported offsets are UTF-8 byte offsets.

Defined Under Namespace

Classes: Occurrence, Transform

Constant Summary collapse

MAX_SOURCE =

Returns:

  • (Integer)
1 << 20
MAX_OUTPUT =

Returns:

  • (Integer)
4 << 20
MAX_DEPTH =

Returns:

  • (Integer)
32
MAX_NODES =

Returns:

  • (Integer)
10_000
VARIABLES =

Returns:

  • (Array[String])
%w[TM_SELECTED_TEXT TM_CURRENT_LINE TM_CURRENT_WORD TM_LINE_INDEX TM_LINE_NUMBER
TM_FILENAME TM_FILENAME_BASE TM_DIRECTORY TM_FILEPATH RELATIVE_FILEPATH CLIPBOARD
WORKSPACE_NAME WORKSPACE_FOLDER CURSOR_INDEX CURSOR_NUMBER CURRENT_YEAR CURRENT_YEAR_SHORT
CURRENT_MONTH CURRENT_MONTH_NAME CURRENT_MONTH_NAME_SHORT CURRENT_DATE CURRENT_DAY_NAME
CURRENT_DAY_NAME_SHORT CURRENT_HOUR CURRENT_MINUTE CURRENT_SECOND CURRENT_MILLISECOND
CURRENT_SECONDS_UNIX CURRENT_MILLISECONDS_UNIX CURRENT_TIMEZONE_OFFSET CURRENT_TIMEZONE_NAME
RANDOM RANDOM_HEX UUID BLOCK_COMMENT_START BLOCK_COMMENT_END LINE_COMMENT].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, variables: {}) ⇒ Snippet

Returns a new instance of Snippet.

Parameters:

  • source (String)
  • variables: (Hash[String | Symbol, String?]) (defaults to: {})

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/canopus/snippet.rb', line 24

def initialize(source, variables: {})
  raise Error, "snippet must be valid UTF-8" unless source.is_a?(String) && source.valid_encoding? && (source.encoding == Encoding::UTF_8 || (source.encoding.ascii_compatible? && source.ascii_only?))
  raise Error, "snippet exceeds #{MAX_SOURCE} bytes" if source.bytesize > MAX_SOURCE
  raise Error, "snippet variables must be a Hash" unless variables.is_a?(Hash)
  @variables = variables.to_h do |key, value|
    raise Error, "snippet variable values must be UTF-8 strings or nil" unless value.nil? || (value.is_a?(String) && value.valid_encoding? && (value.encoding == Encoding::UTF_8 || (value.encoding.ascii_compatible? && value.ascii_only?)))
    raise Error, "snippet variable exceeds output limit" if value && value.bytesize > MAX_OUTPUT
    [key.to_s, value&.encode(Encoding::UTF_8)]
  end
  @scanner, @node_count, @maximum_index = StringScanner.new(source), 0, 0
  nodes = parse
  nodes = resolve_variables(nodes)
  @defaults, @choices = {}, {}
  collect_defaults(nodes)
  @text, @tabstops, @transforms = +"", Hash.new { |h, k| h[k] = [] }, []
  @occurrences = []
  render(nodes)
  @text.freeze
  @tabstops = @tabstops.sort_by { |index, _| index.zero? ? Float::INFINITY : index }.to_h.transform_values { |ranges| ranges.freeze }.freeze
  @choices.freeze
  @transforms.freeze
  @occurrences.freeze
  # The parse tree and variable values are not retained by a live editor.
  @scanner = @variables = @defaults = nil
end

Instance Attribute Details

#choices ⇒ Hash[Integer, Array[String]] (readonly)

Returns the value of attribute choices.

Returns:

  • (Hash[Integer, Array[String]])


22
23
24
# File 'lib/canopus/snippet.rb', line 22

def choices
  @choices
end

#occurrences ⇒ Array[Occurrence] (readonly)

Returns the value of attribute occurrences.

Returns:



22
23
24
# File 'lib/canopus/snippet.rb', line 22

def occurrences
  @occurrences
end

#tabstops ⇒ Hash[Integer, Array[Range[Integer]]] (readonly)

Returns the value of attribute tabstops.

Returns:

  • (Hash[Integer, Array[Range[Integer]]])


22
23
24
# File 'lib/canopus/snippet.rb', line 22

def tabstops
  @tabstops
end

#text ⇒ String (readonly)

Returns the value of attribute text.

Returns:

  • (String)


22
23
24
# File 'lib/canopus/snippet.rb', line 22

def text
  @text
end

#transforms ⇒ Array[Occurrence] (readonly)

Returns the value of attribute transforms.

Returns:



22
23
24
# File 'lib/canopus/snippet.rb', line 22

def transforms
  @transforms
end