Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/terminal/shortcuts/auto_suggest.py: 26%

203 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1import re 

2import tokenize 

3from io import StringIO 

4from typing import Callable, List, Optional, Union, Generator, Tuple 

5import warnings 

6 

7from prompt_toolkit.buffer import Buffer 

8from prompt_toolkit.key_binding import KeyPressEvent 

9from prompt_toolkit.key_binding.bindings import named_commands as nc 

10from prompt_toolkit.auto_suggest import AutoSuggestFromHistory, Suggestion 

11from prompt_toolkit.document import Document 

12from prompt_toolkit.history import History 

13from prompt_toolkit.shortcuts import PromptSession 

14from prompt_toolkit.layout.processors import ( 

15 Processor, 

16 Transformation, 

17 TransformationInput, 

18) 

19 

20from IPython.core.getipython import get_ipython 

21from IPython.utils.tokenutil import generate_tokens 

22 

23from .filters import pass_through 

24 

25 

26def _get_query(document: Document): 

27 return document.lines[document.cursor_position_row] 

28 

29 

30class AppendAutoSuggestionInAnyLine(Processor): 

31 """ 

32 Append the auto suggestion to lines other than the last (appending to the 

33 last line is natively supported by the prompt toolkit). 

34 """ 

35 

36 def __init__(self, style: str = "class:auto-suggestion") -> None: 

37 self.style = style 

38 

39 def apply_transformation(self, ti: TransformationInput) -> Transformation: 

40 is_last_line = ti.lineno == ti.document.line_count - 1 

41 is_active_line = ti.lineno == ti.document.cursor_position_row 

42 

43 if not is_last_line and is_active_line: 

44 buffer = ti.buffer_control.buffer 

45 

46 if buffer.suggestion and ti.document.is_cursor_at_the_end_of_line: 

47 suggestion = buffer.suggestion.text 

48 else: 

49 suggestion = "" 

50 

51 return Transformation(fragments=ti.fragments + [(self.style, suggestion)]) 

52 else: 

53 return Transformation(fragments=ti.fragments) 

54 

55 

56class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory): 

57 """ 

58 A subclass of AutoSuggestFromHistory that allow navigation to next/previous 

59 suggestion from history. To do so it remembers the current position, but it 

60 state need to carefully be cleared on the right events. 

61 """ 

62 

63 def __init__( 

64 self, 

65 ): 

66 self.skip_lines = 0 

67 self._connected_apps = [] 

68 

69 def reset_history_position(self, _: Buffer): 

70 self.skip_lines = 0 

71 

72 def disconnect(self): 

73 for pt_app in self._connected_apps: 

74 text_insert_event = pt_app.default_buffer.on_text_insert 

75 text_insert_event.remove_handler(self.reset_history_position) 

76 

77 def connect(self, pt_app: PromptSession): 

78 self._connected_apps.append(pt_app) 

79 # note: `on_text_changed` could be used for a bit different behaviour 

80 # on character deletion (i.e. reseting history position on backspace) 

81 pt_app.default_buffer.on_text_insert.add_handler(self.reset_history_position) 

82 pt_app.default_buffer.on_cursor_position_changed.add_handler(self._dismiss) 

83 

84 def get_suggestion( 

85 self, buffer: Buffer, document: Document 

86 ) -> Optional[Suggestion]: 

87 text = _get_query(document) 

88 

89 if text.strip(): 

90 for suggestion, _ in self._find_next_match( 

91 text, self.skip_lines, buffer.history 

92 ): 

93 return Suggestion(suggestion) 

94 

95 return None 

96 

97 def _dismiss(self, buffer, *args, **kwargs): 

98 buffer.suggestion = None 

99 

100 def _find_match( 

101 self, text: str, skip_lines: float, history: History, previous: bool 

102 ) -> Generator[Tuple[str, float], None, None]: 

103 """ 

104 text : str 

105 Text content to find a match for, the user cursor is most of the 

106 time at the end of this text. 

107 skip_lines : float 

108 number of items to skip in the search, this is used to indicate how 

109 far in the list the user has navigated by pressing up or down. 

110 The float type is used as the base value is +inf 

111 history : History 

112 prompt_toolkit History instance to fetch previous entries from. 

113 previous : bool 

114 Direction of the search, whether we are looking previous match 

115 (True), or next match (False). 

116 

117 Yields 

118 ------ 

119 Tuple with: 

120 str: 

121 current suggestion. 

122 float: 

123 will actually yield only ints, which is passed back via skip_lines, 

124 which may be a +inf (float) 

125 

126 

127 """ 

128 line_number = -1 

129 for string in reversed(list(history.get_strings())): 

130 for line in reversed(string.splitlines()): 

131 line_number += 1 

132 if not previous and line_number < skip_lines: 

133 continue 

134 # do not return empty suggestions as these 

135 # close the auto-suggestion overlay (and are useless) 

136 if line.startswith(text) and len(line) > len(text): 

137 yield line[len(text) :], line_number 

138 if previous and line_number >= skip_lines: 

139 return 

140 

141 def _find_next_match( 

142 self, text: str, skip_lines: float, history: History 

143 ) -> Generator[Tuple[str, float], None, None]: 

144 return self._find_match(text, skip_lines, history, previous=False) 

145 

146 def _find_previous_match(self, text: str, skip_lines: float, history: History): 

147 return reversed( 

148 list(self._find_match(text, skip_lines, history, previous=True)) 

149 ) 

150 

151 def up(self, query: str, other_than: str, history: History) -> None: 

152 for suggestion, line_number in self._find_next_match( 

153 query, self.skip_lines, history 

154 ): 

155 # if user has history ['very.a', 'very', 'very.b'] and typed 'very' 

156 # we want to switch from 'very.b' to 'very.a' because a) if the 

157 # suggestion equals current text, prompt-toolkit aborts suggesting 

158 # b) user likely would not be interested in 'very' anyways (they 

159 # already typed it). 

160 if query + suggestion != other_than: 

161 self.skip_lines = line_number 

162 break 

163 else: 

164 # no matches found, cycle back to beginning 

165 self.skip_lines = 0 

166 

167 def down(self, query: str, other_than: str, history: History) -> None: 

168 for suggestion, line_number in self._find_previous_match( 

169 query, self.skip_lines, history 

170 ): 

171 if query + suggestion != other_than: 

172 self.skip_lines = line_number 

173 break 

174 else: 

175 # no matches found, cycle to end 

176 for suggestion, line_number in self._find_previous_match( 

177 query, float("Inf"), history 

178 ): 

179 if query + suggestion != other_than: 

180 self.skip_lines = line_number 

181 break 

182 

183 

184def accept_or_jump_to_end(event: KeyPressEvent): 

185 """Apply autosuggestion or jump to end of line.""" 

186 buffer = event.current_buffer 

187 d = buffer.document 

188 after_cursor = d.text[d.cursor_position :] 

189 lines = after_cursor.split("\n") 

190 end_of_current_line = lines[0].strip() 

191 suggestion = buffer.suggestion 

192 if (suggestion is not None) and (suggestion.text) and (end_of_current_line == ""): 

193 buffer.insert_text(suggestion.text) 

194 else: 

195 nc.end_of_line(event) 

196 

197 

198def _deprected_accept_in_vi_insert_mode(event: KeyPressEvent): 

199 """Accept autosuggestion or jump to end of line. 

200 

201 .. deprecated:: 8.12 

202 Use `accept_or_jump_to_end` instead. 

203 """ 

204 return accept_or_jump_to_end(event) 

205 

206 

207def accept(event: KeyPressEvent): 

208 """Accept autosuggestion""" 

209 buffer = event.current_buffer 

210 suggestion = buffer.suggestion 

211 if suggestion: 

212 buffer.insert_text(suggestion.text) 

213 else: 

214 nc.forward_char(event) 

215 

216 

217def discard(event: KeyPressEvent): 

218 """Discard autosuggestion""" 

219 buffer = event.current_buffer 

220 buffer.suggestion = None 

221 

222 

223def accept_word(event: KeyPressEvent): 

224 """Fill partial autosuggestion by word""" 

225 buffer = event.current_buffer 

226 suggestion = buffer.suggestion 

227 if suggestion: 

228 t = re.split(r"(\S+\s+)", suggestion.text) 

229 buffer.insert_text(next((x for x in t if x), "")) 

230 else: 

231 nc.forward_word(event) 

232 

233 

234def accept_character(event: KeyPressEvent): 

235 """Fill partial autosuggestion by character""" 

236 b = event.current_buffer 

237 suggestion = b.suggestion 

238 if suggestion and suggestion.text: 

239 b.insert_text(suggestion.text[0]) 

240 

241 

242def accept_and_keep_cursor(event: KeyPressEvent): 

243 """Accept autosuggestion and keep cursor in place""" 

244 buffer = event.current_buffer 

245 old_position = buffer.cursor_position 

246 suggestion = buffer.suggestion 

247 if suggestion: 

248 buffer.insert_text(suggestion.text) 

249 buffer.cursor_position = old_position 

250 

251 

252def accept_and_move_cursor_left(event: KeyPressEvent): 

253 """Accept autosuggestion and move cursor left in place""" 

254 accept_and_keep_cursor(event) 

255 nc.backward_char(event) 

256 

257 

258def _update_hint(buffer: Buffer): 

259 if buffer.auto_suggest: 

260 suggestion = buffer.auto_suggest.get_suggestion(buffer, buffer.document) 

261 buffer.suggestion = suggestion 

262 

263 

264def backspace_and_resume_hint(event: KeyPressEvent): 

265 """Resume autosuggestions after deleting last character""" 

266 nc.backward_delete_char(event) 

267 _update_hint(event.current_buffer) 

268 

269 

270def resume_hinting(event: KeyPressEvent): 

271 """Resume autosuggestions""" 

272 pass_through.reply(event) 

273 # Order matters: if update happened first and event reply second, the 

274 # suggestion would be auto-accepted if both actions are bound to same key. 

275 _update_hint(event.current_buffer) 

276 

277 

278def up_and_update_hint(event: KeyPressEvent): 

279 """Go up and update hint""" 

280 current_buffer = event.current_buffer 

281 

282 current_buffer.auto_up(count=event.arg) 

283 _update_hint(current_buffer) 

284 

285 

286def down_and_update_hint(event: KeyPressEvent): 

287 """Go down and update hint""" 

288 current_buffer = event.current_buffer 

289 

290 current_buffer.auto_down(count=event.arg) 

291 _update_hint(current_buffer) 

292 

293 

294def accept_token(event: KeyPressEvent): 

295 """Fill partial autosuggestion by token""" 

296 b = event.current_buffer 

297 suggestion = b.suggestion 

298 

299 if suggestion: 

300 prefix = _get_query(b.document) 

301 text = prefix + suggestion.text 

302 

303 tokens: List[Optional[str]] = [None, None, None] 

304 substrings = [""] 

305 i = 0 

306 

307 for token in generate_tokens(StringIO(text).readline): 

308 if token.type == tokenize.NEWLINE: 

309 index = len(text) 

310 else: 

311 index = text.index(token[1], len(substrings[-1])) 

312 substrings.append(text[:index]) 

313 tokenized_so_far = substrings[-1] 

314 if tokenized_so_far.startswith(prefix): 

315 if i == 0 and len(tokenized_so_far) > len(prefix): 

316 tokens[0] = tokenized_so_far[len(prefix) :] 

317 substrings.append(tokenized_so_far) 

318 i += 1 

319 tokens[i] = token[1] 

320 if i == 2: 

321 break 

322 i += 1 

323 

324 if tokens[0]: 

325 to_insert: str 

326 insert_text = substrings[-2] 

327 if tokens[1] and len(tokens[1]) == 1: 

328 insert_text = substrings[-1] 

329 to_insert = insert_text[len(prefix) :] 

330 b.insert_text(to_insert) 

331 return 

332 

333 nc.forward_word(event) 

334 

335 

336Provider = Union[AutoSuggestFromHistory, NavigableAutoSuggestFromHistory, None] 

337 

338 

339def _swap_autosuggestion( 

340 buffer: Buffer, 

341 provider: NavigableAutoSuggestFromHistory, 

342 direction_method: Callable, 

343): 

344 """ 

345 We skip most recent history entry (in either direction) if it equals the 

346 current autosuggestion because if user cycles when auto-suggestion is shown 

347 they most likely want something else than what was suggested (otherwise 

348 they would have accepted the suggestion). 

349 """ 

350 suggestion = buffer.suggestion 

351 if not suggestion: 

352 return 

353 

354 query = _get_query(buffer.document) 

355 current = query + suggestion.text 

356 

357 direction_method(query=query, other_than=current, history=buffer.history) 

358 

359 new_suggestion = provider.get_suggestion(buffer, buffer.document) 

360 buffer.suggestion = new_suggestion 

361 

362 

363def swap_autosuggestion_up(event: KeyPressEvent): 

364 """Get next autosuggestion from history.""" 

365 shell = get_ipython() 

366 provider = shell.auto_suggest 

367 

368 if not isinstance(provider, NavigableAutoSuggestFromHistory): 

369 return 

370 

371 return _swap_autosuggestion( 

372 buffer=event.current_buffer, provider=provider, direction_method=provider.up 

373 ) 

374 

375 

376def swap_autosuggestion_down(event: KeyPressEvent): 

377 """Get previous autosuggestion from history.""" 

378 shell = get_ipython() 

379 provider = shell.auto_suggest 

380 

381 if not isinstance(provider, NavigableAutoSuggestFromHistory): 

382 return 

383 

384 return _swap_autosuggestion( 

385 buffer=event.current_buffer, 

386 provider=provider, 

387 direction_method=provider.down, 

388 ) 

389 

390 

391def __getattr__(key): 

392 if key == "accept_in_vi_insert_mode": 

393 warnings.warn( 

394 "`accept_in_vi_insert_mode` is deprecated since IPython 8.12 and " 

395 "renamed to `accept_or_jump_to_end`. Please update your configuration " 

396 "accordingly", 

397 DeprecationWarning, 

398 stacklevel=2, 

399 ) 

400 return _deprected_accept_in_vi_insert_mode 

401 raise AttributeError