src/eric7/QScintilla/EditorOutlineModel.py

Tue, 08 Nov 2022 18:18:01 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 08 Nov 2022 18:18:01 +0100
branch
eric7
changeset 9491
91bcf8b893ee
parent 9482
a2bc06a54d9d
child 9612
93b496cc3c88
permissions
-rw-r--r--

Changed the editor outline model to use the new way of getting the class browser module.

# -*- coding: utf-8 -*-

# Copyright (c) 2020 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the editor outline model.
"""

import contextlib
import os

from PyQt6.QtCore import QCoreApplication, QModelIndex

from eric7 import Preferences
from eric7.UI.BrowserModel import (
    BrowserClassAttributesItem,
    BrowserClassItem,
    BrowserCodingItem,
    BrowserGlobalsItem,
    BrowserImportItem,
    BrowserImportsItem,
    BrowserItem,
    BrowserMethodItem,
    BrowserModel,
)
from eric7.Utilities import ClassBrowsers


class EditorOutlineModel(BrowserModel):
    """
    Class implementing the editor outline model.
    """

    SupportedLanguages = {
        "IDL": "idl",
        "JavaScript": "javascript",
        "Protocol": "protobuf",
        "Python3": "python",
        "MicroPython": "python",
        "Cython": "python",
        "Ruby": "ruby",
    }

    def __init__(self, editor, populate=True):
        """
        Constructor

        @param editor reference to the editor containing the source text
        @type Editor
        @param populate flag indicating to populate the outline
        @type bool
        """
        super().__init__(nopopulate=True)

        self.__editor = editor

        self.__populated = False

        rootData = QCoreApplication.translate("EditorOutlineModel", "Name")
        self.rootItem = BrowserItem(None, rootData)

        if populate:
            self.__populateModel()

    def __populateModel(self, repopulate=False):
        """
        Private slot to populate the model.

        @param repopulate flag indicating a repopulation
        @type bool
        """
        self.__filename = self.__editor.getFileName()
        self.__module = os.path.basename(self.__filename)

        language = self.__editor.getLanguage()
        if language in EditorOutlineModel.SupportedLanguages:
            mod = ClassBrowsers.getClassBrowserModule(
                EditorOutlineModel.SupportedLanguages[language]
            )
            if mod:
                dictionary = mod.scan(
                    self.__editor.text(), self.__filename, self.__module
                )
                mod.clearModulesCache()

                keys = list(dictionary.keys())
                if len(keys) > 0:
                    parentItem = self.rootItem

                    if repopulate:
                        last = len(keys) - 1
                        if "@@Coding@@" in keys and not Preferences.getEditor(
                            "SourceOutlineShowCoding"
                        ):
                            last -= 1
                        self.beginInsertRows(QModelIndex(), 0, last)

                    for key in keys:
                        if key.startswith("@@"):
                            # special treatment done later
                            continue
                        cl = dictionary[key]
                        with contextlib.suppress(AttributeError):
                            if cl.module == self.__module:
                                node = BrowserClassItem(parentItem, cl, self.__filename)
                                self._addItem(node, parentItem)
                    if "@@Coding@@" in keys and Preferences.getEditor(
                        "SourceOutlineShowCoding"
                    ):
                        node = BrowserCodingItem(
                            parentItem,
                            QCoreApplication.translate(
                                "EditorOutlineModel", "Coding: {0}"
                            ).format(dictionary["@@Coding@@"].coding),
                            dictionary["@@Coding@@"].linenumber,
                        )
                        self._addItem(node, parentItem)
                    if "@@Globals@@" in keys:
                        node = BrowserGlobalsItem(
                            parentItem,
                            dictionary["@@Globals@@"].globals,
                            QCoreApplication.translate("EditorOutlineModel", "Globals"),
                        )
                        self._addItem(node, parentItem)
                    if "@@Import@@" in keys or "@@ImportFrom@@" in keys:
                        node = BrowserImportsItem(
                            parentItem,
                            QCoreApplication.translate("EditorOutlineModel", "Imports"),
                        )
                        self._addItem(node, parentItem)
                        if "@@Import@@" in keys:
                            for importedModule in (
                                dictionary["@@Import@@"].getImports().values()
                            ):
                                m_node = BrowserImportItem(
                                    node,
                                    importedModule.importedModuleName,
                                    importedModule.file,
                                    importedModule.linenos,
                                )
                                self._addItem(m_node, node)
                                for (
                                    importedName,
                                    linenos,
                                ) in importedModule.importedNames.items():
                                    mn_node = BrowserImportItem(
                                        m_node,
                                        importedName,
                                        importedModule.file,
                                        linenos,
                                        isModule=False,
                                    )
                                    self._addItem(mn_node, m_node)
                    if repopulate:
                        self.endInsertRows()

                self.__populated = True
                return

        self.clear()
        self.__populated = False

    def isPopulated(self):
        """
        Public method to check, if the model is populated.

        @return flag indicating a populated model
        @rtype bool
        """
        return self.__populated

    def repopulate(self):
        """
        Public slot to repopulate the model.
        """
        self.clear()
        self.__populateModel(repopulate=True)

    def editor(self):
        """
        Public method to retrieve a reference to the editor.

        @return reference to the editor
        @rtype Editor
        """
        return self.__editor

    def fileName(self):
        """
        Public method to retrieve the file name of the editor.

        @return file name of the editor
        @rtype str
        """
        return self.__filename

    def itemIndexByLine(self, lineno):
        """
        Public method to find an item's index given a line number.

        @param lineno one based line number of the item
        @type int
        @return index of the item found
        @rtype QModelIndex
        """

        def findItem(lineno, parent):
            """
            Function to iteratively search for an item containing the given
            line.

            @param lineno one based line number of the item
            @type int
            @param parent reference to the parent item
            @type BrowserItem
            @return found item or None
            @rtype BrowserItem
            """
            if not parent.isPopulated():
                if parent.isLazyPopulated():
                    self.populateItem(parent)
                else:
                    return None
            for child in parent.children():
                if isinstance(child, BrowserClassAttributesItem):
                    itm = findItem(lineno, child)
                    if itm is not None:
                        return itm
                elif isinstance(child, (BrowserClassItem, BrowserMethodItem)):
                    start, end = child.boundaries()
                    if end == -1:
                        end = 1000000  # assume end of file
                    if start <= lineno <= end:
                        itm = findItem(lineno, child)
                        if itm is not None:
                            return itm
                        else:
                            return child
                elif hasattr(child, "linenos"):
                    if lineno in child.linenos():
                        return child
                elif hasattr(child, "lineno") and lineno == child.lineno():
                    return child
            else:
                return None

        if self.__populated:
            for rootChild in self.rootItem.children():
                itm = None
                if isinstance(rootChild, BrowserClassItem):
                    start, end = rootChild.boundaries()
                    if end == -1:
                        end = 1000000  # assume end of file
                    if start <= lineno <= end:
                        itm = findItem(lineno, rootChild)
                        if itm is None:
                            itm = rootChild
                elif isinstance(rootChild, (BrowserImportsItem, BrowserGlobalsItem)):
                    itm = findItem(lineno, rootChild)
                elif (
                    isinstance(rootChild, BrowserCodingItem)
                    and lineno == rootChild.lineno()
                ):
                    itm = rootChild
                if itm is not None:
                    return self.createIndex(itm.row(), 0, itm)
            else:
                return QModelIndex()

        return QModelIndex()

eric ide

mercurial