Class: BELParser::Completion::WildcardMatchParameterCompleter

Inherits:
BaseCompleter
  • Object
show all
Includes:
QuotedValue
Defined in:
lib/bel_parser/completion.rb

Constant Summary collapse

L =
BELParser::Levenshtein

Instance Method Summary collapse

Methods included from QuotedValue

#map_value

Methods inherited from BaseCompleter

#initialize

Methods included from Parsers::AST::Sexp

#annotation_definition, #argument, #blank_line, build, #comment, #comment_line, #document_property, #domain, #function, #identifier, #keyword, #list, #list_item, #multi_identifier, #name, #namespace_definition, #nested_statement, #object, #observed_term, #parameter, #pattern, #prefix, #relationship, #set, #simple_statement, #statement, #string, #subject, #term, #unset, #uri, #url, #value

Constructor Details

This class inherits a constructor from BELParser::Completion::BaseCompleter

Instance Method Details

#complete(string_literal, caret_position, options = {}) ⇒ Object



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/bel_parser/completion.rb', line 806

def complete(string_literal, caret_position, options = {})
  return [] if string_literal.length < 3

  query =
    case
    when caret_position == string_literal.length
      "#{string_literal}*"
    when caret_position == 0
      "*#{string_literal}"
    else
      ante = string_literal.slice(0...caret_position)
      post = string_literal.slice(caret_position..-1)
      "#{ante}*#{post}"
    end

  # find namespace URI if prefix was provided
  prefix = options[:prefix]
  if prefix
    specified_prefix  = prefix.to_s.upcase
    matched_namespace = @namespaces[specified_prefix]
    uri               = matched_namespace ? matched_namespace.uri : nil
  else
    uri = nil
  end

  @search
    .search(query, :namespace_concept, uri, nil, size: 100)
    .sort { |match1, match2|
      L.distance(string_literal.downcase, match1.pref_label.downcase) <=>
      L.distance(string_literal.downcase, match2.pref_label.downcase)
    }
    .map { |match|
      match_namespace = @namespaces.values.find { |ns| ns.uri == match.scheme_uri }
      if match_namespace
        [match_namespace.keyword, match.pref_label]
      else
        nil
      end
    }
    .compact
    .take(20)
    .sort_by { |(_, v)| v }
    .uniq
    .map     { |(ns, v)|
      ns_value, value_ast = map_value(ns, v)

      [
        ns_value,
        argument(
          parameter(
            prefix(
              identifier(
                ns)),
            value_ast))
      ]
    }
end