MCPcopy Index your code
hub / github.com/ipython/ipython / NavigableAutoSuggestFromHistory

Class NavigableAutoSuggestFromHistory

IPython/terminal/shortcuts/auto_suggest.py:160–438  ·  view source on GitHub ↗

A subclass of AutoSuggestFromHistory that allow navigation to next/previous suggestion from history. To do so it remembers the current position, but it state need to carefully be cleared on the right events.

Source from the content-addressed store, hash-verified

158
159
160class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory):
161 """
162 A subclass of AutoSuggestFromHistory that allow navigation to next/previous
163 suggestion from history. To do so it remembers the current position, but it
164 state need to carefully be cleared on the right events.
165 """
166
167 skip_lines: int
168 _connected_apps: list[PromptSession]
169
170 # handle to the currently running llm task that appends suggestions to the
171 # current buffer; we keep a handle to it in order to cancel it when there is a cursor movement, or
172 # another request.
173 _llm_task: asyncio.Task | None = None
174
175 # This is the constructor of the LLM provider from jupyter-ai
176 # to which we forward the request to generate inline completions.
177 _init_llm_provider: Callable | None
178
179 _llm_provider_instance: Any | None
180 _llm_prefixer: Callable = lambda self, x: "wrong"
181
182 def __init__(self):
183 super().__init__()
184 self.skip_lines = 0
185 self._connected_apps = []
186 self._llm_provider_instance = None
187 self._init_llm_provider = None
188 self._request_number = 0
189
190 def reset_history_position(self, _: Buffer) -> None:
191 self.skip_lines = 0
192
193 def disconnect(self) -> None:
194 self._cancel_running_llm_task()
195 for pt_app in self._connected_apps:
196 text_insert_event = pt_app.default_buffer.on_text_insert
197 text_insert_event.remove_handler(self.reset_history_position)
198
199 def connect(self, pt_app: PromptSession) -> None:
200 self._connected_apps.append(pt_app)
201 # note: `on_text_changed` could be used for a bit different behaviour
202 # on character deletion (i.e. resetting history position on backspace)
203 pt_app.default_buffer.on_text_insert.add_handler(self.reset_history_position)
204 pt_app.default_buffer.on_cursor_position_changed.add_handler(self._dismiss)
205
206 def get_suggestion(
207 self, buffer: Buffer, document: Document
208 ) -> Optional[Suggestion]:
209 text = _get_query(document)
210
211 if text.strip():
212 for suggestion, _ in self._find_next_match(
213 text, self.skip_lines, buffer.history
214 ):
215 return Suggestion(suggestion)
216
217 return None

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…