/
editorextension.py
142 lines (109 loc) · 4.4 KB
/
editorextension.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2013-2016 Colin Duquesnoy and others (see pyqode/AUTHORS.rst)
# Copyright (c) 2016- Spyder Project Contributors (see AUTHORS.txt)
#
# Distributed under the terms of the MIT License
# (see NOTICE.txt in the Spyder root directory for details)
# -----------------------------------------------------------------------------
"""
This module contains the editor extension API.
Adapted from pyqode/core/api/mode.py of the
`PyQode project <https://github.com/pyQode/pyQode>`_.
Original file:
<https://github.com/pyQode/pyqode.core/blob/master/pyqode/core/api/mode.py>
"""
import logging
logger = logging.getLogger(__name__)
class EditorExtension(object):
"""
Base class for editor extensions.
An extension is a "thing" that can be installed on an editor to add new
behaviours or to modify its appearance.
A panel (model child class) is added to an editor by using the
PanelsManager:
- :meth:
`spyder.plugins.editor.widgets.codeeditor.CodeEditor.panels.append`
Subclasses may/should override the following methods:
- :meth:`spyder.api.EditorExtension.on_install`
- :meth:`spyder.api.EditorExtension.on_uninstall`
- :meth:`spyder.api.EditorExtension.on_state_changed`
..warning: The editor extension will be identified by its class name, this
means that **there cannot be two editor extensions of the same type on the
same editor instance!**
"""
@property
def editor(self):
"""
Returns a reference to the parent code editor widget.
**READ ONLY**
:rtype: spyder.plugins.editor.widgets.codeeditor.CodeEditor
"""
return self._editor
@property
def enabled(self):
"""
Tells if the editor extension is enabled.
:meth:`spyder.api.EditorExtension.on_state_changed` will be called as
soon as the editor extension state changed.
:type: bool
"""
return self._enabled
@enabled.setter
def enabled(self, enabled):
if enabled != self._enabled:
self._enabled = enabled
self.on_state_changed(enabled)
def __init__(self):
"""
EditorExtension name/identifier.
:class:`spyder.widgets.sourcecode.CodeEditor` uses that as the
attribute key when you install a editor extension.
"""
self.name = self.__class__.__name__
# EditorExtension description
self.description = self.__doc__
self._enabled = False
self._editor = None
self._on_close = False
def __del__(self):
logger.debug('%s.__del__', type(self))
def on_install(self, editor):
"""
Installs the extension on the editor.
:param editor: editor widget instance
:type editor: spyder.plugins.editor.widgets.codeeditor.CodeEditor
.. note:: This method is called by editor when you install a
EditorExtension.
You should never call it yourself, even in a subclasss.
.. warning:: Don't forget to call **super** when subclassing
"""
self._editor = editor
self.enabled = True
def on_uninstall(self):
"""Uninstalls the editor extension from the editor."""
self._on_close = True
self.enabled = False
self._editor = None
def on_state_changed(self, state):
"""
Called when the enable state has changed.
This method does not do anything, you may override it if you need
to connect/disconnect to the editor's signals (connect when state is
true and disconnect when it is false).
:param state: True = enabled, False = disabled
:type state: bool
"""
pass
def clone_settings(self, original):
"""
Clone the settings from another editor extension (same class).
This method is called when splitting an editor widget.
# TODO at the current estate this is not working
:param original: other editor extension (must be the same class).
.. note:: The base method does not do anything, you must implement
this method for every new editor extension/panel (if you plan on
using the split feature). You should also make sure any properties
will be propagated to the clones.
"""
pass