Takes the full_text that is typed so far and also the text before the cursor to suggest completion type and scope. Returns a tuple with a type of entity ('table', 'column' etc) and a scope. A scope for a column category will be a list of tables.
(full_text, text_before_cursor)
| 117 | |
| 118 | |
| 119 | def suggest_type(full_text, text_before_cursor): |
| 120 | """Takes the full_text that is typed so far and also the text before the |
| 121 | cursor to suggest completion type and scope. |
| 122 | |
| 123 | Returns a tuple with a type of entity ('table', 'column' etc) and a scope. |
| 124 | A scope for a column category will be a list of tables. |
| 125 | """ |
| 126 | |
| 127 | if full_text.startswith("\\i "): |
| 128 | return (Path(),) |
| 129 | |
| 130 | # This is a temporary hack; the exception handling |
| 131 | # here should be removed once sqlparse has been fixed |
| 132 | try: |
| 133 | stmt = SqlStatement(full_text, text_before_cursor) |
| 134 | except (TypeError, AttributeError): |
| 135 | return [] |
| 136 | |
| 137 | # Check for special commands and handle those separately |
| 138 | if stmt.parsed: |
| 139 | # Be careful here because trivial whitespace is parsed as a |
| 140 | # statement, but the statement won't have a first token |
| 141 | tok1 = stmt.parsed.token_first() |
| 142 | if tok1 and tok1.value.startswith("\\"): |
| 143 | text = stmt.text_before_cursor + stmt.word_before_cursor |
| 144 | return suggest_special(text) |
| 145 | |
| 146 | return suggest_based_on_last_token(stmt.last_token, stmt) |
| 147 | |
| 148 | |
| 149 | named_query_regex = re.compile(r"^\s*\\ns\s+[A-z0-9\-_]+\s+") |