MCPcopy
hub / github.com/dbcli/pgcli / get_column_matches

Method get_column_matches

pgcli/pgcompleter.py:487–545  ·  view source on GitHub ↗
(self, suggestion, word_before_cursor)

Source from the content-addressed store, hash-verified

485 return [m.completion for m in matches]
486
487 def get_column_matches(self, suggestion, word_before_cursor):
488 tables = suggestion.table_refs
489 do_qualify = (
490 suggestion.qualifiable
491 and {
492 "always": True,
493 "never": False,
494 "if_more_than_one_table": len(tables) > 1,
495 }[self.qualify_columns]
496 )
497
498 def qualify(col, tbl):
499 return (tbl + "." + self.case(col)) if do_qualify else self.case(col)
500
501 _logger.debug("Completion column scope: %r", tables)
502 scoped_cols = self.populate_scoped_cols(tables, suggestion.local_tables)
503
504 def make_cand(name, ref):
505 synonyms = (name, generate_alias(self.case(name), alias_map=self.alias_map))
506 return Candidate(qualify(name, ref), 0, "column", synonyms)
507
508 def flat_cols():
509 return [make_cand(c.name, t.ref) for t, cols in scoped_cols.items() for c in cols]
510
511 if suggestion.require_last_table:
512 # require_last_table is used for 'tb11 JOIN tbl2 USING (...' which should
513 # suggest only columns that appear in the last table and one more
514 ltbl = tables[-1].ref
515 other_tbl_cols = {c.name for t, cs in scoped_cols.items() if t.ref != ltbl for c in cs}
516 scoped_cols = {t: [col for col in cols if col.name in other_tbl_cols] for t, cols in scoped_cols.items() if t.ref == ltbl}
517 lastword = last_word(word_before_cursor, include="most_punctuations")
518 if lastword == "*":
519 if suggestion.context == "insert":
520
521 def _filter(col):
522 if not col.has_default:
523 return True
524 return not any(p.match(col.default) for p in self.insert_col_skip_patterns)
525
526 scoped_cols = {t: [col for col in cols if _filter(col)] for t, cols in scoped_cols.items()}
527 if self.asterisk_column_order == "alphabetic":
528 for cols in scoped_cols.values():
529 cols.sort(key=operator.attrgetter("name"))
530 if lastword != word_before_cursor and len(tables) == 1 and word_before_cursor[-len(lastword) - 1] == ".":
531 # User typed x.*; replicate "x." for all columns except the
532 # first, which gets the original (as we only replace the "*"")
533 sep = ", " + word_before_cursor[:-1]
534 collist = sep.join(self.case(c.completion) for c in flat_cols())
535 else:
536 collist = ", ".join(qualify(c.name, t.ref) for t, cs in scoped_cols.items() for c in cs)
537
538 return [
539 Match(
540 completion=Completion(collist, -1, display_meta="columns", display="*"),
541 priority=(1, 1, 1),
542 )
543 ]
544

Callers

nothing calls this directly

Calls 4

populate_scoped_colsMethod · 0.95
caseMethod · 0.95
find_matchesMethod · 0.95
last_wordFunction · 0.85

Tested by

no test coverage detected