def get_suggestion(self, cli, buffer, document): cur_text = document.text_before_cursor words = None for regex, method_name in RULES: match = regex.search(cur_text) if match: gen_suggestions = getattr(self.sugg_gen, method_name) suggestions = gen_suggestions(match) words = suggestions if words: for sugg in words: return Suggestion(sugg) else: history = buffer.history # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Only create a suggestion when this is not an empty line. if text.strip(): # Find first matching line in history. for string in reversed(list(history)): for line in reversed(string.splitlines()): if line.startswith(text): return Suggestion(line[len(text):])
def get_suggestion(self, buffer, document): text = document.text.rsplit('\n', 1)[-1] text_parsed = shlex.split(text) if text != '': for test_command in self.commands: test_command_replaced = test_command for token in self.substitution_tokens: test_command_replaced = test_command.replace(token, '') if len(shlex.split(text)) < 1: text_replaced = text else: text_replaced = shlex.split(text)[0] # self.write_file(str(text_replaced)) if test_command_replaced.startswith(text_replaced): test_command_parsed = shlex.split(test_command) if len(text_parsed) <= len(test_command_parsed): to_test = test_command_parsed[len(text_parsed) - 1] # self.write_file('{} -> {}'.format(to_test, text_parsed[len(text_parsed)-1])) if to_test in self.substitution_tokens and self.substitution_tokens[ to_test] is not None: part_input = text_parsed[len(text_parsed) - 1] part_suggested = self.substitution_tokens[to_test]( part_input) if part_suggested is None: return Suggestion('') return Suggestion(part_suggested[len(part_input):]) return Suggestion(to_test[len(text):]) return Suggestion(test_command[len(text):]) else: return Suggestion('help')
def get_suggestion(self, buffer, document): try: if "(" not in document.text or ")" in document.text: return method, args = document.text.rsplit("(", maxsplit=1) base, current = _parse_document(self.locals, method) if isinstance(base, dict): fn = base[current] else: fn = getattr(base, current) if isinstance(fn, type): fn = fn.__init__ if hasattr(fn, "_autosuggest"): inputs = fn._autosuggest() else: inputs = [f" {i}" for i in fn.__code__.co_varnames[: fn.__code__.co_argcount]] if fn.__defaults__: for i in range(-1, -1 - len(fn.__defaults__), -1): inputs[i] = f"{inputs[i]}={fn.__defaults__[i]}" if inputs and inputs[0] in (" self", " cls"): inputs = inputs[1:] if not args and not inputs: return Suggestion(")") args = args.split(",") inputs[0] = inputs[0][1:] remaining_inputs = inputs[len(args) - 1 :] remaining_inputs[0] = remaining_inputs[0][len(args[-1]) :] return Suggestion(",".join(remaining_inputs) + ")") except Exception: return
def get_suggestion(self, cli, buffer, document): in_quote = False for word in document.text.split(' '): if not in_quote and word.startswith('"'): in_quote = True elif in_quote and word.endswith('"'): in_quote = False # If the last word is in a quote, don't suggest anything. if in_quote: return None args = shlex.split(document.text) if len(args) == 0: return None args = list(args) if document.text.endswith(' '): args.append('') command_name = args.pop(0) if len(args) == 0: for name in sorted(self.commands.commands): if name.startswith(command_name): return Suggestion(name[len(command_name):]) try: command = self.commands.command(command_name) except UnknownCommand: return None suggestion = command.suggest_tail(*args) if suggestion is None: return self.history_suggester.get_suggestion(cli, buffer, document) return Suggestion(suggestion)
def get_suggestion(self, buffer, document): completer = buffer.completer.get_completer() # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Consider only last word: for tw in text.split(): for kw in completer.words: if tw.startswith(kw): text = text.replace(kw, f'{kw} ') # Make the last word a '' if text ends with a space: text_words = text.split() if len(text) == 0 or text[-1].isspace(): text_words.append('') text = text_words[-1] completer.bottom = text # Only create a suggestion when this is not an empty line. if text.strip(): # Find first matching line in history. for string in completer.words: for line in reversed(string.splitlines()): if line.startswith(text): return Suggestion(line[len(text):])
def get_suggestion(self, buffer, document): w = document.get_word_before_cursor() if w in completions: return Suggestion(completions[w]) return None
def get_suggestion(self, buffer, document): completer = buffer.completer.get_completer() # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Only create a suggestion when this is not an empty line. if text == '' or text[-1].isspace() or text[-1] == ':': return # Consider only two last words: words = text.split() if ':' in words[-1]: key, word = words[-1].split(':')[-2:] key = key + ':' elif len(words) > 1: key, word = words[-2:] else: key, word = '', words[-1] if key in completer.words: options = np.unique([ str(getattr(bib,key[:-1])) for bib in completer.bibs if getattr(bib,key[:-1]) is not None]) else: options = completer.words # Find first matching line in history. for string in options: for line in reversed(string.splitlines()): if line.startswith(word): return Suggestion(line[len(word):])
def get_suggestion(self, buffer, document): # Get filename with self.ctx: cmds = [ i for i in split_pipeline( split_args(document.current_line, echo_errors=False)) ] if len(cmds) == 0: return Suggestion(text='') if len(cmds[-1]) == 0: return Suggestion(text='') filename = cmds[-1][-1] # Get completion possible_filenames = glob.glob(filename + '*') if len(possible_filenames) == 0: return Suggestion(text='') else: return Suggestion(text=possible_filenames[0][len(filename):])
def get_suggestion(self, buffer, document): try: text = "\n".join(self.console.buffer + [document.text]) base, _, comma_data = _parse_document(self.locals, text) # find the active function call del base[-1] while base[-1] == self.locals: del base[-1] del comma_data[-1] obj = base[-1] # calculate distance from last comma count, offset = comma_data[-1] lines = text.count("\n") + 1 if offset[0] < lines: distance = len(document.text) else: distance = len(document.text) - offset[1] if hasattr(obj, "_autosuggest"): inputs = obj._autosuggest(obj) else: inputs = [ f" {i}" for i in obj.__code__.co_varnames[:obj.__code__.co_argcount] ] if obj.__defaults__: for i in range(-1, -1 - len(obj.__defaults__), -1): inputs[i] = f"{inputs[i]}={obj.__defaults__[i]}" if inputs and inputs[0] in (" self", " cls"): inputs = inputs[1:] if not count and not inputs: return Suggestion(")") inputs[0] = inputs[0][1:] remaining_inputs = inputs[count:] remaining_inputs[0] = remaining_inputs[0][distance:] return Suggestion(f"{','.join(remaining_inputs)})") except Exception: return
def get_suggestion(self, buffer, document): try: if "(" not in document.text or ")" in document.text: return method, args = document.text.rsplit("(", maxsplit=1) base, current = _parse_document(self.locals, method) if isinstance(base, dict): obj = base[current] else: obj = getattr(base, current) if inspect.isclass(obj): obj = obj.__init__ elif (callable(obj) and not hasattr(obj, "_autosuggest") and not inspect.ismethod(obj) and not inspect.isfunction(obj)): # object is a callable class instance obj = obj.__call__ if hasattr(obj, "_autosuggest"): inputs = obj._autosuggest() else: inputs = [ f" {i}" for i in obj.__code__.co_varnames[:obj.__code__.co_argcount] ] if obj.__defaults__: for i in range(-1, -1 - len(obj.__defaults__), -1): inputs[i] = f"{inputs[i]}={obj.__defaults__[i]}" if inputs and inputs[0] in (" self", " cls"): inputs = inputs[1:] if not args and not inputs: return Suggestion(")") args = args.split(",") inputs[0] = inputs[0][1:] remaining_inputs = inputs[len(args) - 1:] remaining_inputs[0] = remaining_inputs[0][len(args[-1]):] return Suggestion(",".join(remaining_inputs) + ")") except Exception: return
def get_suggestion(self, buffer, document): if hasattr(buffer.completer, 'get_completer'): completer = buffer.completer.get_completer() else: completer = buffer.completer # Consider only the last line for the suggestion: text = document.text[0:document.cursor_position_col] text_words = text.split() key = '' for word in reversed(text_words): for kw in completer._keys: if word.startswith(kw): key = kw break if key != '': break if len(text_words) == 0 or text.endswith(' '): last_word = '' else: last_word = text_words[-1] last_word = last_word.replace(key, '', 1) text_since_key = text[text.rindex(key):] # Colon-keys can only be in last or previous: if key == '': words = completer.keys[:] elif key.endswith(':'): text_from_key = text_since_key.replace(key, '', 1).lstrip() words_from_key = len(text_from_key.split()) if int(text.endswith(' ')) + words_from_key > 1: words = completer.keys[:] else: words = completer.key_words[key] # Quote-keys: else: authors = re.search(r':"([^"]*)(["]?)', text_since_key) # open-ended quotes: if authors.group(2) == '': last_word = authors.group(1) words = completer.key_words[key+'"'] # closed quotes: else: words = completer.keys[:] completer.bottom = last_word # Only create a suggestion when this is not an empty line. if last_word.strip(): for word in words: if word.startswith(last_word): return Suggestion(word[len(last_word):])
def get_suggestion(self, cli, buffer, document): # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Only create a suggestion when this is not an empty line. if text.strip(): # Find first matching line in history. for string in self.items: for line in reversed(string.splitlines()): if line.startswith(text): return Suggestion(line[len(text):])
def get_suggestion(self, buffer, document): completer = buffer.completer # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Consider only last word: text = re.split('(\W+)', text)[-1] # Only create a suggestion when this is not an empty line. if text.strip(): # Find first matching line in history. for string in completer.get_completer().words: for line in reversed(string.splitlines()): if line.startswith(text): return Suggestion(line[len(text):])
def get_suggestion(self, buffer, document): # Get name of command with self.ctx: cmds = [ i for i in split_pipeline( split_args(document.current_line, echo_errors=False)) ] if len(cmds) == 0: return Suggestion(text='') if len(cmds[0]) == 0: return Suggestion(text='') cmdname = cmds[-1][0] # Get help if len(cmds[-1]) < 2: cmdhelp = None elif cmdname in self.helps: cmdhelp = self.helps[cmdname] else: try: proc = subprocess.run([cmdname, '--help'], capture_output=True) except FileNotFoundError: return Suggestion(text='') cmdhelp = parse_help(proc.stdout.decode('utf-8')) self.helps[cmdname] = cmdhelp if cmdhelp is None or len(cmdhelp.options) == 0: text = '' else: arg = cmds[-1][-1] # The last arg on the current line is_possible_opt = (lambda opt: opt.startswith(arg)) possible_opts = filter(is_possible_opt, cmdhelp.options) text = '' for i in possible_opts: text = i[len(arg):] break return Suggestion(text=text)
def get_suggestion(self, buffer: "Buffer", document: Document) -> Optional[Suggestion]: # Consider only the last line for the suggestion. text = document.text.rsplit("\n", 1)[-1] # Only create a suggestion when this is not an empty line. if text.strip(): # Find first matching line in history. for string in self.completions: if string.startswith(text): return Suggestion(string[len(text):]) return None
def get_suggestion(self, buffer: "Buffer", document: Document) -> Optional[Suggestion]: """ When looking for most recent suggestion look for one starting with the "[identifier]: " prefix """ history = buffer.history # Consider only the last line for the suggestion. text = document.text.rsplit("\n", 1)[-1] # Only create a suggestion when this is not an empty line. if text.strip(): obj = self.my_app.selected_object prefix = object_to_identifier(obj) + ": " # Find first matching line in history. for string in reversed(list(history.get_strings())): for line in reversed(string.splitlines()): loc = line.find(prefix) # Add one character for a space after SELECT identifier if loc >= 0 and line[loc + len(prefix):].startswith(text): return Suggestion(line[loc + len(prefix) + len(text):]) return None
def get_suggestion(self, buffer, document): completer = buffer.completer.get_completer() # Consider only the last line for the suggestion. text = document.text.rsplit('\n', 1)[-1] # Insert a space after colon if there is a key in the input: for tw in text.split(): for kw in completer.keys: if tw.startswith(kw) and kw.endswith(':'): text = text.replace(kw, f'{kw} ') # Search for keys from end to begining: text_words = text.split() for word in reversed(text_words): if word in completer.keys: key = word break else: key = '' words = completer.keys[:] if key in completer.key_words: words += completer.key_words[key] if key in words: words.remove(key) if text == '' or text.endswith(' '): last_word = '' else: last_word = text_words[-1] completer.bottom = last_word # Only create a suggestion when this is not an empty line. if last_word.strip(): for word in words: if word.startswith(last_word): return Suggestion(word[len(last_word):])
def get_suggestion(self, buffer, document): for c in self.commands: if c.startswith(document.text): return Suggestion(c.replace(document.text, ''))
def get_suggestion(self, buffer, document): text = document.text.rsplit('\n', 1)[-1] if text.strip(): for option in self.options: if option.startswith(text): return Suggestion(option[len(text):])