Created
November 2, 2024 17:17
-
-
Save cpcloud/7c2dc1ad9bd8a623a5d98cdf0314b25c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import tree_sitter_python as tspython | |
from IPython import get_ipython | |
from IPython.core.completer import SimpleCompletion, SimpleMatcherResult | |
from tree_sitter import Language, Parser | |
UNDERSCORE_PATTERN = re.compile(r".*\b_.$") | |
PY_LANGUAGE = Language(tspython.language()) | |
parser = Parser(PY_LANGUAGE) | |
query = PY_LANGUAGE.query("(identifier) @name") | |
def ibis_underscore_completer(self, context) -> SimpleMatcherResult: | |
if UNDERSCORE_PATTERN.match(context.token) is None: | |
return self.python_matcher(context) | |
import ibis.expr.types as ir | |
ns = self.namespace | |
tree = parser.parse(context.full_text.encode()) | |
root = tree.root_node | |
captures = query.captures(root) | |
name_nodes = captures["name"] | |
attrs = next( | |
( | |
[f".{attr}" for attr in dir(obj) if not attr.startswith("_")] | |
for node in reversed(name_nodes) | |
if isinstance(obj := ns.get(node.text.decode()), ir.Table) | |
), | |
[], | |
) | |
if not attrs: | |
return self.python_matcher(context) | |
_, fragment = self._attr_matches(context.line_with_cursor) | |
return { | |
"completions": [ | |
SimpleCompletion(text=attr, type="attribute") for attr in attrs | |
], | |
"suppress": bool(attrs), | |
"matched_fragment": fragment, | |
} | |
ibis_underscore_completer.matcher_api_version = 2 | |
ip = get_ipython() | |
ip.set_custom_completer(ibis_underscore_completer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First you need to install tree sitter and tree sitter python bindings:
Stick this in
~/.ipython/profile_default/startup
(or in thestartup
directory of the IPython profile you're using).