Module: BEL::Completion

Defined in:
lib/bel/completion.rb,
lib/bel/completion_rule.rb

Defined Under Namespace

Modules: Rule Classes: MatchFunctionRule, MatchNamespacePrefixRule, MatchNamespaceValueRule

Constant Summary collapse

SORTED_FUNCTIONS =
BEL::Language::FUNCTIONS.keys.sort.map(&:to_s)
SORTED_NAMESPACES =
BEL::Namespace::NAMESPACE_LATEST.keys.sort.map(&:to_s)
EMPTY_MATCH =
[]

Class Method Summary collapse

Class Method Details

.complete(bel_expression, search = nil, position = nil) ⇒ Array<Completion>

Provides completions on BEL expressions.

If bel_expression is nil then its assumed to be the empty string otherwise the to_s method is called. An empty bel_expression will return all BEL functions as possible completions.

If search is nil then namespace values will not be provided as completion. search is expected to implement IdentifierSearch.

If position is nil then its assumed to be the last index of bel_expression otherwise the to_i method is called.

If position is negative or greater than the length of bel_expression an IndexError is raised.

complete on provide namespace value completions

Parameters:

  • bel_expression (responds to #to_s)

    the bel expression to

  • search (IdentifierSearch) (defaults to: nil)

    the search object used to

  • position (responds to #to_i) (defaults to: nil)

    the position to complete from

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bel/completion.rb', line 30

def self.complete(bel_expression, search = nil, position = nil)
  bel_expression = (bel_expression || '').to_s
  position = (position || bel_expression.length).to_i
  if position < 0 or position > bel_expression.length
    msg = %Q{position #{position}, bel_expression "#{bel_expression}"}
    fail IndexError, msg
  end

  token_list = LibBEL::tokenize_term(bel_expression)
  active_token, active_index = token_list.token_at(position)

  # no active token indicates the position is out of
  # range of all tokens in the list.
  return [] unless active_token

  tokens = token_list.to_a
  options = {
    :search => search
  }
  BEL::Completion::run_rules(tokens, active_index, active_token, options)
end

.rulesObject



21
22
23
24
25
26
27
# File 'lib/bel/completion_rule.rb', line 21

def self.rules
  [
    MatchFunctionRule.new,
    MatchNamespacePrefixRule.new,
    MatchNamespaceValueRule.new
  ]
end

.run_rules(tokens, active_index, active_token, options = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/bel/completion_rule.rb', line 13

def self.run_rules(tokens, active_index, active_token, options = {})
  self.rules.reduce([]) { |completion_results, rule|
    completion_results.concat(
      rule.apply(tokens, active_token, active_index, options)
    )
  }
end