Module: BEL::Completion

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

Overview

Completion defines an API for completing BEL Expressions.

Defined Under Namespace

Modules: Rule Classes: MatchFunctionRule, MatchNamespacePrefixRule, MatchNamespaceValueRule

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.complete(bel_expression, specification, search, namespaces, 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.

A Resource::Search plugin must be provided that provides completions for namespaces and namespace values.

A Resource::Namespaces API must be provided to resolve namespace properties given a URI.

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

  • specification (BELParser::Language::Specification)

    language specification

  • search (#search_namespace, #search)

    the search object used to

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

    the position to complete from

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bel/completion.rb', line 35

def self.complete(bel_expression, specification, search, namespaces, position = nil)
  #raise ArgumentError.new(
  #  "search should be a BEL::Resource::Search plugin implementation"
  #) unless search
  #raise ArgumentError.new(
  #  "namespaces should be a BEL::Resource::Namespaces object"
  #) unless namespaces

  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,
    :specification => specification,
    :search        => search,
    :namespaces    => namespaces
  )
end

.rulesObject

Retrieve all completion rules.



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

def self.rules
  constants.map do |const|
    obj = const_get(const)
    obj.new if obj.include?(Rule)
  end.compact
end

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

Run all defined rules.



10
11
12
13
14
15
16
17
# File 'lib/bel/completion_rule.rb', line 10

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)
    )
    completion_results
  }
end