Sha256: 77db303f73d646a66b8dfba42e52e4d2cde745af2f011938bfff568bce6c062e

Contents?: true

Size: 1.89 KB

Versions: 15

Compression:

Stored size: 1.89 KB

Contents

require_relative '../libbel'
require_relative 'completion_rule'
require_relative 'language'
require_relative 'namespace'

module BEL
  module 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.
    #
    # @param bel_expression   [responds to #to_s] the bel expression to
    # complete on
    # @param search           [IdentifierSearch] the search object used to
    # provide namespace value completions
    # @param position         [responds to #to_i] the position to complete from
    # @return [Array<Completion>]
    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
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
bel-0.3.0.beta5-x64-mingw32 lib/bel/completion.rb
bel-0.3.0.beta5-x86-mingw32 lib/bel/completion.rb
bel-0.3.0.beta5 lib/bel/completion.rb
bel-0.3.0.beta4-x86-mingw32 lib/bel/completion.rb
bel-0.3.0.beta4-x64-mingw32 lib/bel/completion.rb
bel-0.3.0.beta4 lib/bel/completion.rb
bel-0.3.0.beta3-x64-mingw32 lib/bel/completion.rb
bel-0.3.0.beta3-x86-mingw32 lib/bel/completion.rb
bel-0.3.0.beta3 lib/bel/completion.rb
bel-0.3.0.beta2-x64-mingw32 lib/bel/completion.rb
bel-0.3.0.beta2-x86-mingw32 lib/bel/completion.rb
bel-0.3.0.beta2 lib/bel/completion.rb
bel-0.3.0.beta1-x64-mingw32 lib/bel/completion.rb
bel-0.3.0.beta1-x86-mingw32 lib/bel/completion.rb
bel-0.3.0.beta1 lib/bel/completion.rb