Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Panels and extensions cleanup (Editor) #14810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions spyder/plugins/editor/extensions/closebrackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def _autoinsert_brackets(self, char):
"""Control automatic insertation of brackets in various situations."""
pair = self.BRACKETS_PAIR[char]

line_text = self.editor.get_text('sol', 'eol')
line_to_cursor = self.editor.get_text('sol', 'cursor')
cursor = self.editor.textCursor()
trailing_text = self.editor.get_text('cursor', 'eol').strip()

Expand Down
6 changes: 3 additions & 3 deletions spyder/plugins/editor/extensions/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def is_start_of_function(text):
"""Return True if text is the beginning of the function definition."""
if isinstance(text, str) or isinstance(text, unicode):
if isinstance(text, str):
function_prefix = ['def', 'async def']
text = text.lstrip()

Expand Down Expand Up @@ -122,7 +122,7 @@ def get_function_definition_from_below_last_line(self):
line_number = cursor.blockNumber() + 1
number_of_lines_of_function = 0

for idx in range(min(line_number, 20)):
for __ in range(min(line_number, 20)):
if cursor.block().blockNumber() == 0:
return None

Expand Down Expand Up @@ -154,7 +154,7 @@ def get_function_body(self, func_indent):
number_of_lines = self.code_editor.blockCount()
body_list = []

for idx in range(number_of_lines - line_number + 1):
for __ in range(number_of_lines - line_number + 1):
text = to_text_string(cursor.block().text())
text_indent = get_indent(text)

Expand Down
18 changes: 9 additions & 9 deletions spyder/plugins/editor/extensions/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ def on_state_changed(self, state):
else:
try:
self.editor.sig_key_pressed.disconnect(self._on_key_pressed)
self.editor.sig_insert_completion.disconnect(self.insert_snippet)
self.editor.sig_insert_completion.disconnect(
self.insert_snippet)
self.editor.sig_cursor_position_changed.disconnect(
self.cursor_changed)
self.editor.sig_text_was_inserted.disconnect(self._redraw_snippets)
self.editor.sig_text_was_inserted.disconnect(
self._redraw_snippets)
self.editor.sig_will_insert_text.disconnect(self._process_text)
self.editor.sig_will_paste_text.disconnect(self._process_text)
self.editor.sig_will_remove_selection.disconnect(
Expand Down Expand Up @@ -154,7 +156,8 @@ def _undo(self):
redo_info = (ast_copy, self.starting_position,
self.active_snippet)
self.redo_stack.insert(0, redo_info)
self.ast, self.starting_position, self.active_snippet = info
(self.ast, self.starting_position,
self.active_snippet) = info
self._update_ast()
self.editor.clear_extra_selections('code_snippets')
self.draw_snippets()
Expand All @@ -178,7 +181,8 @@ def _redo(self):
undo_info = (ast_copy, self.starting_position,
self.active_snippet)
self.undo_stack.insert(0, undo_info)
self.ast, self.starting_position, self.active_snippet = info
(self.ast, self.starting_position,
self.active_snippet) = info
self._update_ast()
self.editor.clear_extra_selections('code_snippets')
self.draw_snippets()
Expand All @@ -200,7 +204,7 @@ def _on_key_pressed(self, event):

if self.is_snippet_active:
line, column = self.editor.get_cursor_line_column()
node, snippet, text_node = self._find_node_by_position(
node, snippet, __ = self._find_node_by_position(
line, column)
if key == Qt.Key_Tab:
event.accept()
Expand Down Expand Up @@ -249,16 +253,13 @@ def delete_text(self, line, column):
self._remove_selection(start, end)
return
node, snippet, text_node = self._find_node_by_position(line, column)
leaf_kind = node.name
node_position = node.position
if len(node_position) == 1:
# Single, terminal node
x, y = node_position[0]
node_position = ((x, y), (x, y))

first_text_position = text_node.position[0][0]
first_text_start, first_text_end = first_text_position
node_start, node_end = node_position
if first_text_position == (line, column):
# Snippet is dissolved and replaced by its own text
snippet_number = snippet.number
Expand Down Expand Up @@ -579,7 +580,6 @@ def _remove_selection(self, selection_start, selection_end):
self.reset()
return
poly = self._region_to_polygon(selection_start, selection_end)
bboxes = [sum(segment, tuple()) for segment in poly]
for segment in poly:
(_, start_column), (_, end_column) = segment
bbox = sum(segment, tuple())
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/editor/panels/classfunctiondropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class or function definition in the file.
else:
combobox.addItem(fqn, item)

line, column = self._editor.get_cursor_line_column()
line, __ = self._editor.get_cursor_line_column()
self.update_selected(line)

def update_data(self, data):
Expand Down
94 changes: 6 additions & 88 deletions spyder/plugins/editor/panels/codefolding.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def indicators_icons(self, value):
# this should never happen since we're working with clones
pass


@property
def highlight_caret_scope(self):
"""
Expand Down Expand Up @@ -180,18 +179,6 @@ def __init__(self, highlight_caret_scope=False):
self.folding_levels = {}
self.folding_nesting = {}

def __compute_line_offsets(self, text, reverse=False):
lines = text.splitlines(True)
line_start_offset = {}
offset = 0
for i, line in enumerate(lines):
if not reverse:
line_start_offset[i + 1] = offset
else:
line_start_offset[offset] = i + 1
offset += len(line)
return line_start_offset

def update_folding(self, folding_info):
"""Update folding panel folding ranges."""
if folding_info is None:
Expand Down Expand Up @@ -252,7 +239,6 @@ def paintEvent(self, event):
# on the folding panel.
super(FoldingPanel, self).paintEvent(event)
painter = QPainter(self)
document = self.editor.document()

if not self._display_folding and not self._key_pressed:
if any(self.folding_status.values()):
Expand Down Expand Up @@ -312,8 +298,7 @@ def _draw_rect(self, rect, painter):
:param painter: The widget's painter.
"""
c = self.editor.sideareas_color
grad = QLinearGradient(rect.topLeft(),
rect.topRight())
grad = QLinearGradient(rect.topLeft(), rect.topRight())
if sys.platform == 'darwin':
grad.setColorAt(0, c.lighter(100))
grad.setColorAt(1, c.lighter(110))
Expand Down Expand Up @@ -351,7 +336,7 @@ def _draw_fold_indicator(self, top, mouse_over, collapsed, painter):
:param painter: QPainter
"""
rect = QRect(0, top, self.sizeHint().width(),
self.sizeHint().height())
self.sizeHint().height())
if self._native_icons:
opt = QStyleOptionViewItem()

Expand Down Expand Up @@ -380,7 +365,6 @@ def _draw_fold_indicator(self, top, mouse_over, collapsed, painter):

def find_parent_scope(self, block):
"""Find parent scope, if the block is not a fold trigger."""
original = block
block_line = block.blockNumber()
if block_line not in self.folding_regions:
for start_line in self.folding_regions:
Expand Down Expand Up @@ -641,7 +625,8 @@ def _on_key_pressed(self, event):
self._key_pressed = True
if cursor.hasSelection():
# change selection to encompass the whole scope.
positions_to_check = cursor.selectionStart(), cursor.selectionEnd()
positions_to_check = (cursor.selectionStart(),
cursor.selectionEnd())
else:
positions_to_check = (cursor.position(), )
for pos in positions_to_check:
Expand All @@ -651,7 +636,8 @@ def _on_key_pressed(self, event):
self.folding_status[start_line]):
end_line = self.folding_regions[start_line]
if delete_request and cursor.hasSelection():
tc = TextHelper(self.editor).select_lines(start_line, end_line)
tc = TextHelper(self.editor).select_lines(
start_line, end_line)
if tc.selectionStart() > cursor.selectionStart():
start = cursor.selectionStart()
else:
Expand All @@ -665,38 +651,6 @@ def _on_key_pressed(self, event):
self.editor.setTextCursor(tc)
self._key_pressed = False

@staticmethod
def _show_previous_blank_lines(block):
"""
Show the block previous blank lines
"""
# set previous blank lines visibles
pblock = block.previous()
while (pblock.text().strip() == '' and
pblock.blockNumber() >= 0):
pblock.setVisible(True)
pblock = pblock.previous()

def refresh_decorations(self, force=False):
"""
Refresh decorations colors. This function is called by the syntax
highlighter when the style changed so that we may update our
decorations colors according to the new style.
"""
cursor = self.editor.textCursor()
if (self._prev_cursor is None or force or
self._prev_cursor.blockNumber() != cursor.blockNumber()):
for deco_line in self._block_decos:
deco = self._block_decos[deco_line]
self.editor.decorations.remove(deco)
for deco_line in self._block_decos:
deco = self._block_decos[deco_line]
deco.set_outline(drift_color(
self._get_scope_highlight_color(), 110))
deco.set_background(self._get_scope_highlight_color())
self.editor.decorations.add(deco)
self._prev_cursor = cursor

def _refresh_editor_and_scrollbars(self):
"""
Refrehes editor content and scollbars.
Expand Down Expand Up @@ -753,23 +707,6 @@ def expand_all(self):
self._refresh_editor_and_scrollbars()
self.expand_all_triggered.emit()

def _on_action_toggle(self):
"""Toggle the current fold trigger."""
block = self.editor.textCursor().block()
block = self.find_parent_scope(block)
self.toggle_fold_trigger(block)

def _on_action_collapse_all_triggered(self):
"""Closes all top levels fold triggers recursively."""
self.collapse_all()

def _on_action_expand_all_triggered(self):
"""
Expands all fold triggers.
:return:
"""
self.expand_all()

def _highlight_caret_scope(self):
"""
Highlight the scope of the current caret position.
Expand All @@ -793,22 +730,3 @@ def _highlight_caret_scope(self):
else:
self._clear_scope_decos()
self._block_nbr = block_nbr

def clone_settings(self, original):
self.native_icons = original.native_icons
self.indicators_icons = original.indicators_icons
self.highlight_caret_scope = original.highlight_caret_scope
self.custom_fold_region_background = \
original.custom_fold_region_background

def is_collapsed(self, block):
line_number = block.blockNumber()
return (line_number in self.folding_regions and
line_number in self.folding_status)

def get_range(self, block):
line_number = block.blockNumber()
end_number = line_number
if line_number in self.folding_regions:
end_number = self.folding_regions[line_number]
return line_number, end_number
3 changes: 2 additions & 1 deletion spyder/plugins/editor/panels/linenumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def draw_pixmap(xleft, ytop, pixmap):
if errors:
draw_pixmap(1, top, self.error_icon.pixmap(icon_size))
elif warnings:
draw_pixmap(1, top, self.warning_icon.pixmap(icon_size))
draw_pixmap(
1, top, self.warning_icon.pixmap(icon_size))
elif infos:
draw_pixmap(1, top, self.info_icon.pixmap(icon_size))
elif hints:
Expand Down
26 changes: 9 additions & 17 deletions spyder/plugins/editor/panels/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,43 +102,35 @@ def clear(self):
panel = self.remove(key)
panel.setParent(None)

def get(self, name_or_klass):
def get(self, name_or_class):
"""
Gets a specific panel instance.

:param name_or_klass: Name or class of the panel to retrieve.
:return: The specified panel instance.
"""
if not is_text_string(name_or_klass):
name_or_klass = name_or_klass.__name__
if not is_text_string(name_or_class):
name_or_class = name_or_class.__name__
for zone in range(4):
try:
panel = self._panels[zone][name_or_klass]
panel = self._panels[zone][name_or_class]
except KeyError:
pass
else:
return panel
raise KeyError(name_or_klass)

def keys(self):
"""Returns the list of installed panel names."""
return self._modes.keys()

def values(self):
"""Returns the list of installed panels."""
return self._modes.values()
raise KeyError(name_or_class)

def __iter__(self):
lst = []
for zone, zone_dict in self._panels.items():
for name, panel in zone_dict.items():
for __, zone_dict in self._panels.items():
for __, panel in zone_dict.items():
lst.append(panel)
return iter(lst)

def __len__(self):
lst = []
for zone, zone_dict in self._panels.items():
for name, panel in zone_dict.items():
for __, zone_dict in self._panels.items():
for __, panel in zone_dict.items():
lst.append(panel)
return len(lst)

Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/editor/panels/scrollflag.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def make_slider_range(self, cursor_pos, scale_factor, offset, groove_rect):
vsb.pageStep(), scale_factor, offset) - offset
slider_height = max(slider_height, self.get_slider_min_height())

# Calcul the minimum and maximum y-value to constraint the slider
# Calculate the minimum and maximum y-value to constraint the slider
# range indicator position to the height span of the scrollbar area
# where the slider may move.
min_ypos = offset
Expand Down
4 changes: 2 additions & 2 deletions spyder/plugins/editor/panels/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def __setitem__(self, key, value):
def merge_interval(parent, node):
"""Build code folding tree representation from interval tree."""
match = False
start, end = node.fold_range
start, __ = node.fold_range
while parent.parent is not None and not match:
parent_start, parent_end = parent.fold_range
__, parent_end = parent.fold_range
if parent_end <= start:
parent = parent.parent
else:
Expand Down
15 changes: 0 additions & 15 deletions spyder/plugins/editor/utils/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,21 +725,6 @@ def set_fold_trigger(block, val):
state |= int(val) << 26
block.setUserState(state)

@staticmethod
def is_collapsed(block):
"""
Checks if the block is expanded or collased.

:param block: QTextBlock
:return: False for an open trigger, True for for closed trigger
"""
if block is None:
return False
state = block.userState()
if state == -1:
state = 0
return bool(state & 0x08000000)

@staticmethod
def set_collapsed(block, val):
"""
Expand Down