Module: BELParser::Completion
- Extended by:
- Parsers, Parsers::AST::Sexp
- Defined in:
- lib/bel_parser/completion.rb
Defined Under Namespace
Modules: QuotedValue Classes: AllFunctionArgumentCompleter, AllFunctionCompleter, AllFunctionTermCompleter, AllNamespacePrefixArgumentCompleter, AllNamespacePrefixCompleter, AllRelationshipCompleter, BaseCompleter, ExactMatchParameterCompleter, FunctionArgumentCompleter, FunctionCompleter, FunctionTermCompleter, MergeCompletion, NamespacePrefixArgumentCompleter, NamespacePrefixCompleter, RelationshipCompleter, WildcardMatchParameterCompleter
Class Method Summary collapse
- .complete(input, spec, search, namespaces, caret_position = input.length, include_invalid_semantics = false) ⇒ Object
- .complete_argument(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
- .complete_function(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
- .complete_parameter(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
- .complete_relationship(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
- .find_node(ast, caret_position) ⇒ Object
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
Methods included from Parsers
Class Method Details
.complete(input, spec, search, namespaces, caret_position = input.length, include_invalid_semantics = false) ⇒ Object
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 46 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bel_parser/completion.rb', line 14 def self.complete(input, spec, search, namespaces, caret_position = input.length, include_invalid_semantics = false) # Algorithm # 1. Parse AST using statement_autocomplete ragel FSM. # 2. Given cursor find node to complete. # 3. Determine completers that should run given node type and surrounding nodes in the AST. # 4. Compute completion AST for each suggestion. # 5. For each suggestion, transform original AST into full completion. # 6. Run semantic validation on each completion AST. # 7. Return combined completion AST and semantic details. ast, caret_position = BELParser::Parsers::Expression::StatementAutocomplete.parse(input, caret_position) completing_node = find_node(ast, caret_position) return [] unless completing_node completions = case completing_node.type when :parameter complete_parameter(completing_node, caret_position, ast, spec, search, namespaces) when :function complete_function(completing_node, caret_position, ast, spec, search, namespaces) when :argument complete_argument(completing_node, caret_position, ast, spec, search, namespaces) when :relationship complete_relationship(completing_node, caret_position, ast, spec, search, namespaces) else [] end will_match_partial = true urir = BELParser::Resource.default_uri_reader urlr = BELParser::Resource.default_url_reader validator = BELParser::Language::ExpressionValidator.new( spec, namespaces, urir, urlr, will_match_partial ) validated_completions = completions .map { |(completion_ast, completion_result)| if completion_result[:type] == :namespace_prefix # namespace_prefix completions are always valid completion_result[:validation] = { expression: completion_result[:value], valid_syntax: true, valid_semantics: true, message: 'Valid semantics', warnings: [], term_signatures: [] } completion_result else = '' terms = completion_ast.traverse.select { |node| node.type == :term }.to_a semantics_functions = BELParser::Language::Semantics.semantics_functions.reject { |fun| fun == BELParser::Language::Semantics::SignatureMapping } semantic_warnings = completion_ast .traverse .flat_map { |node| semantics_functions.flat_map { |func| func.map(node, spec, namespaces, will_match_partial) } } .compact if semantic_warnings.empty? valid = true else valid = false = semantic_warnings.reduce('') { |msg, warning| msg << "#{warning}\n" } << "\n" end term_semantics = terms.map { |term| term_result = validator.validate(term) valid &= term_result.valid_semantics? bel_term = serialize(term) unless valid << "Term: #{bel_term}\n" term_result.invalid_signature_mappings.map { |m| << " #{m}\n" } << "\n" end { term: bel_term, valid_signatures: term_result.valid_signature_mappings.map(&:to_s), invalid_signatures: term_result.invalid_signature_mappings.map(&:to_s) } } completion_result[:validation] = { expression: completion_result[:value], valid_syntax: true, valid_semantics: valid, message: valid ? 'Valid semantics' : , warnings: semantic_warnings.map(&:to_s), term_signatures: term_semantics } completion_result end } .group_by { |completion_result| completion_result[:validation][:valid_semantics] } if include_invalid_semantics (validated_completions[true] || []) + (validated_completions[false] || []) else validated_completions[true] || [] end end |
.complete_argument(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/bel_parser/completion.rb', line 374 def self.complete_argument( completing_node, caret_position, ast, spec, search, namespaces ) if completing_node.child.nil? all_prefix_completions = AllNamespacePrefixArgumentCompleter .new(spec, search, namespaces) .complete(nil, nil) .map { |(bel_prefix, completion_ast)| completion_ast.character_range = completing_node.character_range completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completing_node.range_start + bel_prefix.length + 1 } ] } all_function_completions = AllFunctionArgumentCompleter .new(spec, search, namespaces) .complete(nil, nil) .map { |(function, completion_ast)| short = function.short.to_s long = function.long.to_s completion_ast.character_range = [ completing_node.range_start, completing_node.range_start + short.length ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :function, id: long, label: long, value: completion, caret_position: short.length + 1 } ] } all_prefix_completions + all_function_completions elsif completing_node.parameter? parameter = completing_node.child prefix, value = parameter.children if prefix && Range.new(*prefix.character_range, false).include?(caret_position) prefix_str = prefix.identifier.string_literal prefix_completions = NamespacePrefixArgumentCompleter .new(spec, search, namespaces) .complete(prefix_str, nil) .map { |(bel_prefix, completion_ast)| completion_ast.character_range = completing_node.character_range completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completing_node.range_start + bel_prefix.length + 1 } ] } prefix_completions else # completing value of parameter value_str = case value.first_child.type when :identifier value.first_child.string_literal when :string value.first_child.string_value end prefix_string = nil prefix_completions = if prefix && prefix.identifier # ... prefix exists, store it for later value lookup prefix_string = prefix.identifier.string_literal [] else # ... prefix is nil, try to complete it, lookup values later without prefix prefix_string = nil NamespacePrefixArgumentCompleter .new(spec, search, namespaces) .complete(value_str, nil) .map { |(bel_prefix, completion_ast)| completion_ast.character_range = completing_node.character_range completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completion_ast.range_start + bel_prefix.length + 1 } ] } end function_completions = [] if prefix_string.nil? completer = if ast.subject.term.function.nil? || (!ast.object.nil? && ast.object.term? && ast.object.child.function.nil?) FunctionTermCompleter else FunctionArgumentCompleter end function_completions = completer .new(spec, search, namespaces) .complete(value_str, caret_position) .map { |(function, completion_ast)| short = function.short.to_s long = function.long.to_s completion_ast.character_range = [ completing_node.range_start, completing_node.range_start + short.length ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :function, id: long, label: long, value: completion, caret_position: completing_node.range_start + short.length + 1 } ] } end exact_match_completions = ExactMatchParameterCompleter .new(spec, search, namespaces) .complete(value_str, caret_position - value.range_start, prefix: prefix_string) .map { |(ns_value, completion_ast)| completion_ast.character_range = completing_node.character_range completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_value, id: ns_value, label: ns_value, value: completion, caret_position: value.range_start + ns_value.length } ] } wildcard_completions = WildcardMatchParameterCompleter .new(spec, search, namespaces) .complete(value_str, caret_position - value.range_start, prefix: prefix_string) .map { |(ns_value, completion_ast)| completion_ast.character_range = completing_node.character_range completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_value, id: ns_value, label: ns_value, value: completion, caret_position: value.range_start + ns_value.length } ] } prefix_completions + function_completions + (exact_match_completions + wildcard_completions).uniq end else # TODO Completing term argument, will we ever get here? puts "#{completing_node.type}: child is a term, how do we proceed?" [] end end |
.complete_function(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bel_parser/completion.rb', line 139 def self.complete_function( completing_node, caret_position, ast, spec, search, namespaces ) string_literal = if completing_node.identifier.nil? '' else completing_node.identifier.string_literal end FunctionCompleter .new(spec, search, namespaces) .complete(string_literal, caret_position) .map { |(function, completion_ast)| short = function.short.to_s long = function.long.to_s completion_ast.character_range = [ completing_node.range_start, completing_node.range_start + short.length ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :function, id: long, label: long, value: completion, caret_position: short.length + 1 } ] } end |
.complete_parameter(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/bel_parser/completion.rb', line 216 def self.complete_parameter( completing_node, caret_position, ast, spec, search, namespaces ) prefix, value = completing_node.children # Completing prefix if Range.new(*prefix.character_range, false).include?(caret_position) if prefix.identifier.nil? # Provide all namespace prefix completions. all_prefix_completions = AllNamespacePrefixCompleter .new(spec, search, namespaces) .complete(nil, nil) .map { |(bel_prefix, completion_ast)| completion_ast.character_range = [ prefix.range_start, prefix.range_start + bel_prefix.length + 1 ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completing_node.range_start + bel_prefix.length + 1 } ] } all_prefix_completions else # Match provided namespace prefix. string_literal = prefix.identifier.string_literal prefix_completions = NamespacePrefixCompleter .new(spec, search, namespaces) .complete(string_literal, caret_position) .map { |(bel_prefix, completion_ast)| completion_ast.character_range = [ prefix.range_start, prefix.range_start + bel_prefix.length + 1 ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completing_node.range_start + bel_prefix.length + 1 } ] } prefix_completions end else string_literal = case value.first_child.type when :identifier value.first_child.string_literal when :string value.first_child.string_value end prefix_str = if prefix && prefix.identifier prefix.identifier.string_literal else nil end function_completions = FunctionTermCompleter .new(spec, search, namespaces) .complete(string_literal, caret_position) .map { |(function, completion_ast)| short = function.short.to_s long = function.long.to_s completion = serialize(completion_ast) [ completion_ast, { type: :function, id: long, label: long, value: completion, caret_position: short.length + 1 } ] } prefix_completions = NamespacePrefixArgumentCompleter .new(spec, search, namespaces) .complete(string_literal, nil) .map { |(bel_prefix, completion_ast)| completion = serialize(completion_ast) [ completion_ast, { type: :namespace_prefix, id: bel_prefix, label: bel_prefix, value: completion, caret_position: completing_node.range_start + bel_prefix.length + 1 } ] } exact_match_completions = ExactMatchParameterCompleter .new(spec, search, namespaces) .complete(string_literal, caret_position - value.range_start, prefix: prefix_str) .map { |(ns_value, completion_ast)| completion = "(#{serialize(completion_ast)})" [ completion_ast, { type: :namespace_value, id: ns_value, label: ns_value, value: completion, caret_position: 0 } ] } wildcard_completions = WildcardMatchParameterCompleter .new(spec, search, namespaces) .complete(string_literal, caret_position - value.range_start, prefix: prefix_str) .map { |(ns_value, completion_ast)| completion = "(#{serialize(completion_ast)})" [ completion_ast, { type: :namespace_value, id: ns_value, label: ns_value, value: completion, caret_position: 0 } ] } function_completions + prefix_completions + (exact_match_completions + wildcard_completions).uniq end end |
.complete_relationship(completing_node, caret_position, ast, spec, search, namespaces) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/bel_parser/completion.rb', line 177 def self.complete_relationship( completing_node, caret_position, ast, spec, search, namespaces ) string_literal = completing_node.string_literal completer = if string_literal.nil? AllRelationshipCompleter.new(spec, search, namespaces) else RelationshipCompleter.new(spec, search, namespaces) end completer .complete(string_literal, caret_position) .map { |(relationship, completion_ast)| short = relationship.short.to_s long = relationship.long.to_s completion_ast.character_range = [ completing_node.range_start, completing_node.range_start + short.length ] completion_ast = MergeCompletion.new(completion_ast).process(ast) completion = serialize(completion_ast) [ completion_ast, { type: :relationship, id: long, label: long, value: completion, caret_position: short.length + 1 } ] } end |
.find_node(ast, caret_position) ⇒ Object
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/bel_parser/completion.rb', line 585 def self.find_node(ast, caret_position) ast.traverse do |node| next if node.type == :term || caret_position < node.range_start || caret_position > node.range_end case node.type when :argument return node if node.child.nil? || node.parameter? when :parameter, :function, :relationship return node end end nil end |