|
2 | 2 | import re |
3 | 3 | import os |
4 | 4 |
|
5 | | -from prompt_toolkit.completion import CompleteEvent |
6 | 5 | from prompt_toolkit.application.current import get_app |
7 | 6 | from prompt_toolkit.application.run_in_terminal import run_in_terminal |
8 | 7 | from prompt_toolkit.eventloop import From, ensure_future |
@@ -413,78 +412,93 @@ def is_callable(text=""): |
413 | 412 | return False |
414 | 413 |
|
415 | 414 | @Condition |
416 | | - def accot(): |
| 415 | + def auto_complete_commit_on_tab(): |
417 | 416 | return settings.auto_complete_commit_on_tab |
418 | 417 |
|
| 418 | + @Condition |
| 419 | + def auto_complete_commit_top_option_on_enter(): |
| 420 | + return settings.auto_complete_commit_top_option_on_enter |
| 421 | + |
| 422 | + @Condition |
| 423 | + def auto_complete_commit_top_option_on_tab(): |
| 424 | + return settings.auto_complete_commit_top_option_on_tab |
| 425 | + |
| 426 | + @Condition |
| 427 | + def auto_complete_commit_only_option_on_tab(): |
| 428 | + return settings.auto_complete_commit_only_option_on_tab |
| 429 | + |
419 | 430 | insert_mode = vi_insert_mode | emacs_insert_mode |
420 | 431 | focused_insert = insert_mode & has_focus(DEFAULT_BUFFER) |
421 | 432 | shown_not_selected = has_completions & ~completion_is_selected |
422 | 433 | alt_enter = [KeyPress(Keys.Escape), KeyPress(Keys.Enter)] |
423 | 434 |
|
424 | | - # apply selected completion |
425 | | - @handle('c-j', filter=focused_insert & completion_is_selected & accot) |
426 | | - @handle("enter", filter=focused_insert & completion_is_selected & accot) |
| 435 | + # apply selected completion option with enter |
| 436 | + @handle('c-j', filter=focused_insert & completion_is_selected) |
| 437 | + @handle("enter", filter=focused_insert & completion_is_selected) |
427 | 438 | def _(event): |
428 | 439 | b = event.current_buffer |
429 | | - text = b.text |
430 | 440 | completion = b.complete_state.current_completion |
431 | | - if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
432 | | - b.insert_text("()") |
433 | | - b.cursor_left() |
434 | | - if text == b.text: |
435 | | - event.cli.key_processor.feed_multiple(alt_enter) |
| 441 | + b.apply_completion(completion) |
| 442 | + if settings.auto_complete_function_parentheses: |
| 443 | + if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
| 444 | + b.insert_text("()") |
| 445 | + b.cursor_left() |
436 | 446 |
|
437 | | - # apply first completion option when completion menu is showing |
438 | | - @handle('c-j', filter=focused_insert & shown_not_selected & accot) |
439 | | - @handle("enter", filter=focused_insert & shown_not_selected & accot) |
| 447 | + # apply selected completion option with tab |
| 448 | + @handle("tab", filter=focused_insert & completion_is_selected & auto_complete_commit_on_tab) |
| 449 | + @handle("c-space", filter=focused_insert & completion_is_selected & auto_complete_commit_on_tab) |
440 | 450 | def _(event): |
441 | 451 | b = event.current_buffer |
442 | | - text = b.text |
443 | | - b.complete_next() |
444 | 452 | completion = b.complete_state.current_completion |
445 | | - if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
446 | | - b.insert_text("()") |
447 | | - b.cursor_left() |
448 | | - if text == b.text: |
449 | | - event.cli.key_processor.feed_multiple(alt_enter) |
| 453 | + b.apply_completion(completion) |
| 454 | + if settings.auto_complete_function_parentheses: |
| 455 | + if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
| 456 | + b.insert_text("()") |
| 457 | + b.cursor_left() |
450 | 458 |
|
451 | | - # apply completion if there is only one option, otherwise start completion |
452 | | - @handle("tab", filter=focused_insert & ~has_completions & accot) |
453 | | - @handle("c-space", filter=focused_insert & ~has_completions & accot) |
| 459 | + # apply first completion option with enter when completion menu is showing |
| 460 | + @handle('c-j', filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter) |
| 461 | + @handle("enter", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter) |
454 | 462 | def _(event): |
455 | 463 | b = event.current_buffer |
456 | | - complete_event = CompleteEvent(completion_requested=True) |
457 | | - completions = list(b.completer.get_completions(b.document, complete_event)) |
458 | | - if len(completions) == 1: |
459 | | - completion = completions[0] |
460 | | - b.apply_completion(completion) |
| 464 | + text = b.text |
| 465 | + completion = b.complete_state.completions[0] |
| 466 | + b.apply_completion(completion) |
| 467 | + if settings.auto_complete_function_parentheses: |
461 | 468 | if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
462 | 469 | b.insert_text("()") |
463 | 470 | b.cursor_left() |
464 | | - else: |
465 | | - b.start_completion(insert_common_part=True) |
| 471 | + if text == b.text: |
| 472 | + event.cli.key_processor.feed_multiple(alt_enter) |
466 | 473 |
|
467 | | - # apply first completion option if completion menu is showing |
468 | | - @handle("tab", filter=focused_insert & shown_not_selected & accot) |
469 | | - @handle("c-space", filter=focused_insert & shown_not_selected & accot) |
| 474 | + # apply first completion option with tab if completion menu is showing |
| 475 | + @handle("tab", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_tab) |
| 476 | + @handle("c-space", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_tab) |
470 | 477 | def _(event): |
471 | 478 | b = event.current_buffer |
472 | | - b.complete_next() |
473 | | - completion = b.complete_state.current_completion |
474 | | - if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
475 | | - b.insert_text("()") |
476 | | - b.cursor_left() |
| 479 | + completion = b.complete_state.completions[0] |
| 480 | + b.apply_completion(completion) |
| 481 | + if settings.auto_complete_function_parentheses: |
| 482 | + if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
| 483 | + b.insert_text("()") |
| 484 | + b.cursor_left() |
477 | 485 |
|
478 | | - # apply selected completion option |
479 | | - @handle("tab", filter=focused_insert & completion_is_selected & accot) |
480 | | - @handle("c-space", filter=focused_insert & completion_is_selected & accot) |
| 486 | + # apply completion if there is only one option, otherwise start completion |
| 487 | + @handle("tab", filter=focused_insert & ~has_completions & auto_complete_commit_only_option_on_tab) |
| 488 | + @handle("c-space", filter=focused_insert & ~has_completions & auto_complete_commit_only_option_on_tab) |
481 | 489 | def _(event): |
482 | 490 | b = event.current_buffer |
483 | | - completion = b.complete_state.current_completion |
484 | | - b.apply_completion(completion) |
485 | | - if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
486 | | - b.insert_text("()") |
487 | | - b.cursor_left() |
| 491 | + b.start_completion() |
| 492 | + completions = b.complete_state.completions |
| 493 | + if len(completions) == 1: |
| 494 | + completion = completions[0] |
| 495 | + b.apply_completion(completion) |
| 496 | + if settings.auto_complete_function_parentheses: |
| 497 | + if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()): |
| 498 | + b.insert_text("()") |
| 499 | + b.cursor_left() |
| 500 | + else: |
| 501 | + b.start_completion(insert_common_part=True) |
488 | 502 |
|
489 | 503 | # cancel completion |
490 | 504 | @handle('c-c', filter=default_focused & has_completions) |
|
0 commit comments