Module: Canopus::Editor::SnippetExpandable

Included in:
Canopus::Editor
Defined in:
lib/canopus/editor/snippet_expandable.rb

Instance Method Summary collapse

Instance Method Details

#choose_snippet(value) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/canopus/editor/snippet_expandable.rb', line 90

def choose_snippet(value)
  choices = snippet_choices
  return false unless choices
  value = choices[value] if value.is_a?(Integer) && value >= 0
  return false unless choices.include?(value)
  snippet_select(@snippet_order[@snippet_position])
  replace_selections(value, kind: :snippet_choice)
  true
end

#clear_snippet ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/canopus/editor/snippet_expandable.rb', line 103

def clear_snippet
  active = @snippet_instances
  @snippet_subscription&.detach
  @snippet_subscription = nil
  snippet_fields.each { |field| snippet_release(field) } if @snippet_instances
  @snippet_instances = @snippet_order = @snippet_position = nil
  set_selections(@selections) if active
  nil
end

#insert_snippet(source, variables: {}, workspace_root: nil, clipboard: nil) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/canopus/editor/snippet_expandable.rb', line 47

def insert_snippet(source, variables: {}, workspace_root: nil, clipboard: nil)
  raise Error, "snippet variables must be a Hash" unless variables.is_a?(Hash)
  # Parse every cursor expansion before changing text or disposing a live session.
  selections = merged_selections(@selections, touching: false)
  snippets = selections.each_with_index.map do |selection, index|
    builtins = snippet_variables(workspace_root: workspace_root, clipboard: clipboard, selection: selection, cursor_index: index)
    Snippet.new(source, variables: builtins.merge(variables.transform_keys(&:to_s)))
  end
  starts, delta = [], 0
  selections.zip(snippets).each do |selection, snippet|
    starts << selection.start + delta
    delta += snippet.text.bytesize - selection.range.size
  end
  replace_selections(snippets.map(&:text), kind: :snippet)
  clear_snippet
  @snippet_instances = snippets.zip(starts).map do |snippet, start|
    fields, mirrors = {}, []
    snippet.occurrences.each do |occurrence|
      field = snippet_field(start + occurrence.range.begin...start + occurrence.range.end, occurrence.parents, occurrence.index)
      if occurrence.transform
        mirrors << {index: occurrence.index, field: field, transform: occurrence.transform}
      else
        (fields[occurrence.index] ||= []) << field
      end
    end
    mirrors.each { |mirror| fields[mirror[:index]] ||= [mirror[:field]] }
    fields[0] ||= [snippet_field(start + snippet.text.bytesize...start + snippet.text.bytesize)]
    {snippet: snippet, fields: fields, mirrors: mirrors}
  end
  @snippet_order = @snippet_instances.flat_map { |instance| instance[:fields].keys }.uniq.sort_by { |index| index.zero? ? Float::INFINITY : index }
  @snippet_position = -1
  @snippet_subscription = @buffer.on_edit { |patch| snippet_edited(patch) }
  next_snippet
  snippets.last
end

#next_snippet ⇒ Object



100
# File 'lib/canopus/editor/snippet_expandable.rb', line 100

def next_snippet = snippet_move(1)

#previous_snippet ⇒ Object



101
# File 'lib/canopus/editor/snippet_expandable.rb', line 101

def previous_snippet = snippet_move(-1)

#snippet_active? ⇒ Boolean

Returns:

  • (Boolean)


83
# File 'lib/canopus/editor/snippet_expandable.rb', line 83

def snippet_active? = !@snippet_instances.nil?

#snippet_choices ⇒ Object



85
86
87
88
# File 'lib/canopus/editor/snippet_expandable.rb', line 85

def snippet_choices
  return nil unless snippet_active?
  @snippet_instances.lazy.map { |instance| instance[:snippet].choices[@snippet_order[@snippet_position]] }.find(&:itself)
end

#snippet_variables(workspace_root: nil, clipboard: nil, now: Time.now, selection: primary, cursor_index: @selections.index(selection) || 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/canopus/editor/snippet_expandable.rb', line 10

def snippet_variables(workspace_root: nil, clipboard: nil, now: Time.now, selection: primary, cursor_index: @selections.index(selection) || 0)
  point = @buffer.rope.point_at(selection.start)
  line = @buffer.line(point.row).delete_suffix("\n").delete_suffix("\r")
  column = @buffer.rope.byteslice(@buffer.rope.line_start(point.row), selection.start - @buffer.rope.line_start(point.row)).to_s.length
  word = line[0...column][/\w*\z/] + line[column..][/\A\w*/]
  path = @buffer.path
  root = workspace_root && File.expand_path(workspace_root)
  comments = case language_document.definition.name
  when "ruby" then ["=begin", "=end"]
  when "html", "markdown" then ["<!--", "-->"]
  when "javascript", "typescript", "rust", "go", "c", "cpp", "json", "css" then ["/*", "*/"]
  else ["", ""]
  end
  variables = {
    "TM_SELECTED_TEXT" => @buffer.rope.byteslice(selection.range).to_s,
    "TM_CURRENT_LINE" => line, "TM_CURRENT_WORD" => word,
    "TM_LINE_INDEX" => point.row.to_s, "TM_LINE_NUMBER" => (point.row + 1).to_s,
    "TM_FILENAME" => path && File.basename(path), "TM_FILENAME_BASE" => path && File.basename(path, File.extname(path)),
    "TM_DIRECTORY" => path && File.dirname(path), "TM_FILEPATH" => path,
    "RELATIVE_FILEPATH" => path && (root ? snippet_relative_path(path, root) : File.basename(path)),
    "CLIPBOARD" => clipboard, "WORKSPACE_NAME" => root && File.basename(root), "WORKSPACE_FOLDER" => root,
    "CURSOR_INDEX" => cursor_index.to_s, "CURSOR_NUMBER" => (cursor_index + 1).to_s,
    "CURRENT_MILLISECOND" => (now.nsec / 1_000_000).to_s.rjust(3, "0"),
    "CURRENT_SECONDS_UNIX" => now.to_i.to_s, "CURRENT_MILLISECONDS_UNIX" => (now.to_r * 1000).floor.to_s,
    "CURRENT_TIMEZONE_OFFSET" => now.strftime("%:z"),
    "CURRENT_TIMEZONE_NAME" => ENV["TZ"]&.match?(/\A(?:UTC|[A-Za-z_]+\/[A-Za-z_\/+-]+)\z/) ? ENV["TZ"] : nil,
    "RANDOM" => SecureRandom.random_number(1_000_000).to_s.rjust(6, "0"), "RANDOM_HEX" => SecureRandom.hex(3), "UUID" => SecureRandom.uuid,
    "BLOCK_COMMENT_START" => comments[0], "BLOCK_COMMENT_END" => comments[1],
    "LINE_COMMENT" => language_document.definition.comment == comments[0] ? "" : language_document.definition.comment
  }
  {"YEAR" => "%Y", "YEAR_SHORT" => "%y", "MONTH" => "%m", "MONTH_NAME" => "%B", "MONTH_NAME_SHORT" => "%b",
    "DATE" => "%d", "DAY_NAME" => "%A", "DAY_NAME_SHORT" => "%a", "HOUR" => "%H", "MINUTE" => "%M", "SECOND" => "%S"}.each do |name, format|
    variables["CURRENT_#{name}"] = now.strftime(format)
  end
  variables
end