Skip to content

Commit

Permalink
Improve code completion performance (meta control of `MultiColumnComp…
Browse files Browse the repository at this point in the history
…letionsMenu`).

Improve rendering performance of the "meta" control of the
`MultiColumnCompletionsMenu` when there are many completions.
  • Loading branch information
jonathanslenders committed Dec 6, 2022
1 parent 643a66d commit cb925b2
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/prompt_toolkit/layout/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,19 @@ def preferred_width(self, max_available_width: int) -> Optional[int]:
app = get_app()
if app.current_buffer.complete_state:
state = app.current_buffer.complete_state
return 2 + max(get_cwidth(c.display_meta_text) for c in state.completions)

if len(state.completions) >= 30:
# When there are many completions, calling `get_cwidth` for
# every `display_meta_text` is too expensive. In this case,
# just return the max available width. There will be enough
# columns anyway so that the whole screen is filled with
# completions and `create_content` will then take up as much
# space as needed.
return max_available_width

return 2 + max(
get_cwidth(c.display_meta_text) for c in state.completions[:100]
)
else:
return 0

Expand Down

0 comments on commit cb925b2

Please sign in to comment.