An object to split an input line in a manner similar to readline. By having our own implementation, we can expose readline-like completion in a uniform manner to all frontends. This object only needs to be given the line of text to be split and the cursor position on said line, and it
| 904 | |
| 905 | |
| 906 | class CompletionSplitter: |
| 907 | """An object to split an input line in a manner similar to readline. |
| 908 | |
| 909 | By having our own implementation, we can expose readline-like completion in |
| 910 | a uniform manner to all frontends. This object only needs to be given the |
| 911 | line of text to be split and the cursor position on said line, and it |
| 912 | returns the 'word' to be completed on at the cursor after splitting the |
| 913 | entire line. |
| 914 | |
| 915 | What characters are used as splitting delimiters can be controlled by |
| 916 | setting the ``delims`` attribute (this is a property that internally |
| 917 | automatically builds the necessary regular expression)""" |
| 918 | |
| 919 | # Private interface |
| 920 | |
| 921 | # A string of delimiter characters. The default value makes sense for |
| 922 | # IPython's most typical usage patterns. |
| 923 | _delims = DELIMS |
| 924 | |
| 925 | # The expression (a normal string) to be compiled into a regular expression |
| 926 | # for actual splitting. We store it as an attribute mostly for ease of |
| 927 | # debugging, since this type of code can be so tricky to debug. |
| 928 | _delim_expr = None |
| 929 | |
| 930 | # The regular expression that does the actual splitting |
| 931 | _delim_re = None |
| 932 | |
| 933 | def __init__(self, delims=None): |
| 934 | delims = CompletionSplitter._delims if delims is None else delims |
| 935 | self.delims = delims |
| 936 | |
| 937 | @property |
| 938 | def delims(self): |
| 939 | """Return the string of delimiter characters.""" |
| 940 | return self._delims |
| 941 | |
| 942 | @delims.setter |
| 943 | def delims(self, delims): |
| 944 | """Set the delimiters for line splitting.""" |
| 945 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
| 946 | self._delim_re = re.compile(expr) |
| 947 | self._delims = delims |
| 948 | self._delim_expr = expr |
| 949 | |
| 950 | def split_line(self, line, cursor_pos=None): |
| 951 | """Split a line of text with a cursor at the given position. |
| 952 | """ |
| 953 | cut_line = line if cursor_pos is None else line[:cursor_pos] |
| 954 | return self._delim_re.split(cut_line)[-1] |
| 955 | |
| 956 | |
| 957 | class Completer(Configurable): |
no outgoing calls
no test coverage detected
searching dependent graphs…