# -*- coding: utf-8 -*- # # Copyright © Spyder Project Contributors # Licensed under the terms of the MIT License # (see spyder/__init__.py for details) """Completion widget class.""" # Standard library imports import html import sys # Third psrty imports from qtpy.QtCore import QPoint, Qt, Signal, Slot from qtpy.QtGui import QFontMetrics from qtpy.QtWidgets import (QAbstractItemView, QApplication, QListWidget, QListWidgetItem, QToolTip) # Local imports from spyder.utils.icon_manager import ima from spyder.plugins.completion.api import CompletionItemKind from spyder.py3compat import to_text_string from spyder.widgets.helperwidgets import HTMLDelegate DEFAULT_COMPLETION_ITEM_HEIGHT = 15 DEFAULT_COMPLETION_ITEM_WIDTH = 250 class CompletionWidget(QListWidget): """Completion list widget.""" ITEM_TYPE_MAP = { CompletionItemKind.TEXT: 'text', CompletionItemKind.METHOD: 'method', CompletionItemKind.FUNCTION: 'function', CompletionItemKind.CONSTRUCTOR: 'constructor', CompletionItemKind.FIELD: 'field', CompletionItemKind.VARIABLE: 'variable', CompletionItemKind.CLASS: 'class', CompletionItemKind.INTERFACE: 'interface', CompletionItemKind.MODULE: 'module', CompletionItemKind.PROPERTY: 'property', CompletionItemKind.UNIT: 'unit', CompletionItemKind.VALUE: 'value', CompletionItemKind.ENUM: 'enum', CompletionItemKind.KEYWORD: 'keyword', CompletionItemKind.SNIPPET: 'snippet', CompletionItemKind.COLOR: 'color', CompletionItemKind.FILE: 'file', CompletionItemKind.REFERENCE: 'reference', } ICON_MAP = {} sig_show_completions = Signal(object) # Signal with the info about the current completion item documentation # str: completion name # str: completion signature/documentation, # QPoint: QPoint where the hint should be shown sig_completion_hint = Signal(str, str, QPoint) def __init__(self, parent, ancestor): super(CompletionWidget, self).__init__(ancestor) self.textedit = parent self._language = None self.setWindowFlags(Qt.SubWindow | Qt.FramelessWindowHint) self.hide() self.itemActivated.connect(self.item_selected) self.currentRowChanged.connect(self.row_changed) self.is_internal_console = False self.completion_list = None self.completion_position = None self.automatic = False self.current_selected_item_label = None self.current_selected_item_point = None self.display_index = [] # Setup item rendering self.setItemDelegate(HTMLDelegate(self, margin=3)) self.setMinimumWidth(DEFAULT_COMPLETION_ITEM_WIDTH) # Initial item height and width fm = QFontMetrics(self.textedit.font()) self.item_height = fm.height() self.item_width = self.width() def setup_appearance(self, size, font): """Setup size and font of the completion widget.""" self.resize(*size) self.setFont(font) def is_empty(self): """Check if widget is empty.""" if self.count() == 0: return True return False def show_list(self, completion_list, position, automatic): """Show list corresponding to position.""" self.current_selected_item_label = None self.current_selected_item_point = None if not completion_list: self.hide() return self.automatic = automatic if position is None: # Somehow the position was not saved. # Hope that the current position is still valid self.completion_position = self.textedit.textCursor().position() elif self.textedit.textCursor().position() < position: # hide the text as we moved away from the position self.hide() return else: self.completion_position = position # Completions are handled differently for the Internal # console. if not isinstance(completion_list[0], dict): self.is_internal_console = True self.completion_list = completion_list # Check everything is in order self.update_current(new=True) # If update_current called close, stop loading if not self.completion_list: return # If only one, must be chosen if not automatic single_match = self.count() == 1 if single_match and not self.automatic: self.item_selected(self.item(0)) # signal used for testing self.sig_show_completions.emit(completion_list) return self.show() self.setFocus() self.raise_() self.textedit.position_widget_at_cursor(self) if not self.is_internal_console: tooltip_point = self.rect().topRight() tooltip_point = self.mapToGlobal(tooltip_point) if self.completion_list is not None: for completion in self.completion_list: completion['point'] = tooltip_point # Show hint for first completion element self.setCurrentRow(0) self.row_changed(0) # signal used for testing self.sig_show_completions.emit(completion_list) def set_language(self, language): """Set the completion language.""" self._language = language.lower() def update_list(self, current_word, new=True): """ Update the displayed list by filtering self.completion_list based on the current_word under the cursor (see check_can_complete). If we're not updating the list with new completions, we filter out textEdit completions, since it's difficult to apply them correctly after the user makes edits. If no items are left on the list the autocompletion should stop """ self.clear() self.display_index = [] height = self.item_height width = self.item_width for i, completion in enumerate(self.completion_list): if not self.is_internal_console: if not new and 'textEdit' in completion: continue completion_label = completion['filterText'] else: completion_label = completion[0] if not self.check_can_complete(completion_label, current_word): continue item = QListWidgetItem() if not self.is_internal_console: self.set_item_display( item, completion, height=height, width=width) item.setData(Qt.UserRole, completion) else: completion_text = self.get_html_item_representation( completion_label, '', height=height, width=width) item.setData(Qt.UserRole, completion_label) item.setText(completion_text) self.addItem(item) self.display_index.append(i) if self.count() == 0: self.hide() def _get_cached_icon(self, name): if name not in self.ICON_MAP: self.ICON_MAP[name] = ima.icon(name) return self.ICON_MAP[name] def set_item_display(self, item_widget, item_info, height, width): """Set item text & icons using the info available.""" item_provider = item_info['provider'] item_type = self.ITEM_TYPE_MAP.get(item_info['kind'], 'no_match') item_label = item_info['label'] icon_provider = ("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0l" "EQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=") img_height = height - 2 img_width = img_height * 0.8 icon_provider, icon_scale = item_info.get('icon', (None, 1)) if icon_provider is not None: icon_height = img_height icon_width = icon_scale * icon_height icon_provider = ima.icon(icon_provider) icon_provider = ima.base64_from_icon_obj( icon_provider, icon_width, icon_height) item_text = self.get_html_item_representation( item_label, item_type, icon_provider=icon_provider, img_height=img_height, img_width=img_width, height=height, width=width) item_widget.setText(item_text) item_widget.setIcon(self._get_cached_icon(item_type)) def get_html_item_representation(self, item_completion, item_type, icon_provider=None, img_height=0, img_width=0, height=DEFAULT_COMPLETION_ITEM_HEIGHT, width=DEFAULT_COMPLETION_ITEM_WIDTH): """Get HTML representation of and item.""" height = to_text_string(height) width = to_text_string(width) # Unfortunately, both old- and new-style Python string formatting # have poor performance due to being implemented as functions that # parse the format string. # f-strings in new versions of Python are fast due to Python # compiling them into efficient string operations, but to be # compatible with old versions of Python, we manually join strings. parts = [ '
', html.escape(item_completion).replace(' ', ' '), ' | ', '', item_type, ' | ', ] if icon_provider is not None: img_height = to_text_string(img_height) img_width = to_text_string(img_width) parts.extend([ '', '', ' | ', ]) parts.extend([ '