Skip to content

Commit

Permalink
Merge pull request #20825 from dalthviz/editor_conf
Browse files Browse the repository at this point in the history
PR: Move CONF usage for bookmarks logic and use `SpyderConfiguratorAccessor` class in Editor widgets (Editor)
  • Loading branch information
ccordoba12 authored Apr 20, 2023
2 parents f389b63 + 47080c6 commit 11dee3c
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 138 deletions.
80 changes: 43 additions & 37 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from spyder.api.plugins import Plugins, SpyderPluginWidget
from spyder.api.widgets.menus import SpyderMenu
from spyder.config.base import _, get_conf_path, running_under_pytest
from spyder.config.manager import CONF
from spyder.config.utils import (get_edit_filetypes, get_edit_filters,
get_filter)
from spyder.py3compat import qbytearray_to_str, to_text_string
Expand All @@ -60,7 +59,7 @@
from spyder.plugins.editor.widgets.printer import (
SpyderPrinter, SpyderPrintPreviewDialog)
from spyder.plugins.editor.utils.bookmarks import (load_bookmarks,
save_bookmarks)
update_bookmarks)
from spyder.plugins.editor.widgets.status import (CursorPositionStatus,
EncodingStatus, EOLStatus,
ReadWriteStatus, VCSStatus)
Expand Down Expand Up @@ -476,10 +475,10 @@ def update_run_focus_file(self):

def register_file_run_metadata(self, filename, filename_ext):
"""Register opened files with the Run plugin."""
all_uuids = CONF.get('editor', 'file_uuids', default={})
all_uuids = self.get_conf('file_uuids', default={})
file_id = all_uuids.get(filename, str(uuid.uuid4()))
all_uuids[filename] = file_id
CONF.set('editor', 'file_uuids', all_uuids)
self.set_conf('file_uuids', all_uuids)

metadata: RunConfigurationMetadata = {
'name': filename,
Expand Down Expand Up @@ -948,15 +947,16 @@ def get_plugin_actions(self):
_("Remove trailing spaces"),
triggered=self.remove_trailing_spaces)

formatter = CONF.get(
'completions',
formatter = self.get_conf(
('provider_configuration', 'lsp', 'values', 'formatting'),
'')
default='',
section='completions'
)
self.formatting_action = create_action(
self,
_('Format file or selection with {0}').format(
formatter.capitalize()),
shortcut=CONF.get_shortcut('editor', 'autoformatting'),
shortcut=self.get_shortcut('autoformatting'),
context=Qt.WidgetShortcut,
triggered=self.format_document_or_selection)
self.formatting_action.setEnabled(False)
Expand Down Expand Up @@ -1033,13 +1033,13 @@ def get_plugin_actions(self):
self.go_to_next_file_action = create_action(
self,
_("Go to next file"),
shortcut=CONF.get_shortcut('editor', 'go to previous file'),
shortcut=self.get_shortcut('go to previous file'),
triggered=self.go_to_next_file,
)
self.go_to_previous_file_action = create_action(
self,
_("Go to previous file"),
shortcut=CONF.get_shortcut('editor', 'go to next file'),
shortcut=self.get_shortcut('go to next file'),
triggered=self.go_to_previous_file,
)
self.register_shortcut(
Expand Down Expand Up @@ -1479,7 +1479,7 @@ def update_font(self):
color_scheme = self.get_color_scheme()
for editorstack in self.editorstacks:
editorstack.set_default_font(font, color_scheme)
completion_size = CONF.get('main', 'completion/size')
completion_size = self.get_conf('completion/size', section='main')
for finfo in editorstack.data:
comp_widget = finfo.editor.completion_widget
comp_widget.setup_appearance(completion_size, font)
Expand Down Expand Up @@ -1526,10 +1526,10 @@ def toogle(checked):
if conf_name not in ['pycodestyle', 'pydocstyle']:
action.setChecked(self.get_option(conf_name))
else:
opt = CONF.get(
'completions',
opt = self.get_conf(
('provider_configuration', 'lsp', 'values', conf_name),
False
default=False,
section='completions'
)
action.setChecked(opt)

Expand Down Expand Up @@ -1562,10 +1562,11 @@ def _toggle_checkable_action(self, checked, method_name, conf_name):
self.set_option(conf_name, checked)
else:
if conf_name in ('pycodestyle', 'pydocstyle'):
CONF.set(
'completions',
self.set_conf(
('provider_configuration', 'lsp', 'values', conf_name),
checked)
checked,
section='completions'
)
if self.main.get_plugin(Plugins.Completions, error=False):
completions = self.main.completions
completions.after_configuration_update([])
Expand Down Expand Up @@ -1679,26 +1680,27 @@ def register_editorstack(self, editorstack):
for method, setting in settings:
getattr(editorstack, method)(self.get_option(setting))

editorstack.set_help_enabled(CONF.get('help', 'connect/editor'))
editorstack.set_help_enabled(
self.get_conf('connect/editor', section='help')
)

hover_hints = CONF.get(
'completions',
('provider_configuration', 'lsp', 'values',
'enable_hover_hints'),
True
hover_hints = self.get_conf(
('provider_configuration', 'lsp', 'values', 'enable_hover_hints'),
default=True,
section='completions'
)

format_on_save = CONF.get(
'completions',
format_on_save = self.get_conf(
('provider_configuration', 'lsp', 'values', 'format_on_save'),
False
default=False,
section='completions'
)

edge_line_columns = CONF.get(
'completions',
edge_line_columns = self.get_conf(
('provider_configuration', 'lsp', 'values',
'pycodestyle/max_line_length'),
79
default=79,
section='completions'
)

editorstack.set_hover_hints_enabled(hover_hints)
Expand Down Expand Up @@ -2041,10 +2043,11 @@ def refresh_formatting(self, status):
self.formatting_action.setEnabled(status)

def refresh_formatter_name(self):
formatter = CONF.get(
'completions',
formatter = self.get_conf(
('provider_configuration', 'lsp', 'values', 'formatting'),
'')
default='',
section='completions'
)
self.formatting_action.setText(
_('Format file or selection with {0}').format(
formatter.capitalize()))
Expand Down Expand Up @@ -2102,7 +2105,9 @@ def save_bookmarks(self, filename, bookmarks):
bookmarks = to_text_string(bookmarks)
filename = osp.normpath(osp.abspath(filename))
bookmarks = eval(bookmarks)
save_bookmarks(filename, bookmarks)
old_slots = self.get_conf('bookmarks', default={})
new_slots = update_bookmarks(filename, bookmarks, old_slots)
self.set_conf('bookmarks', new_slots)

#------ File I/O
def __load_temp_file(self):
Expand Down Expand Up @@ -2454,7 +2459,8 @@ def _convert(fname):
self._clone_file_everywhere(finfo)
current_editor = current_es.set_current_filename(filename,
focus=focus)
current_editor.set_bookmarks(load_bookmarks(filename))
slots = self.get_conf('bookmarks', default={})
current_editor.set_bookmarks(load_bookmarks(filename, slots))
self.register_widget_shortcuts(current_editor)
current_es.analyze_script()
self.__add_recent_file(filename)
Expand Down Expand Up @@ -3256,7 +3262,7 @@ def focus_run_configuration(self, uuid: str):
@Slot(int)
def save_bookmark(self, slot_num):
"""Save current line and position as bookmark."""
bookmarks = CONF.get('editor', 'bookmarks')
bookmarks = self.get_conf('bookmarks')
editorstack = self.get_current_editorstack()
if slot_num in bookmarks:
filename, line_num, column = bookmarks[slot_num]
Expand All @@ -3273,7 +3279,7 @@ def save_bookmark(self, slot_num):
@Slot(int)
def load_bookmark(self, slot_num):
"""Set cursor to bookmarked file and position."""
bookmarks = CONF.get('editor', 'bookmarks')
bookmarks = self.get_conf('bookmarks')
if slot_num in bookmarks:
filename, line_num, column = bookmarks[slot_num]
else:
Expand Down Expand Up @@ -3380,7 +3386,7 @@ def apply_plugin_settings(self, options):
tab_stop_width_spaces_n = 'tab_stop_width_spaces'
tab_stop_width_spaces_o = self.get_option(tab_stop_width_spaces_n)
help_n = 'connect_to_oi'
help_o = CONF.get('help', 'connect/editor')
help_o = self.get_conf('connect/editor', section='help')
todo_n = 'todo_list'
todo_o = self.get_option(todo_n)

Expand Down
41 changes: 27 additions & 14 deletions spyder/plugins/editor/utils/bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,49 @@
# Standard imports
import os.path as osp

# Local imports
from spyder.config.manager import CONF


def _load_all_bookmarks():
def _load_all_bookmarks(slots):
"""Load all bookmarks from config."""
slots = CONF.get('editor', 'bookmarks', {})
for slot_num in list(slots.keys()):
if not osp.isfile(slots[slot_num][0]):
slots.pop(slot_num)
return slots


def load_bookmarks(filename):
def load_bookmarks(filename, slots):
"""Load all bookmarks for a specific file from config."""
bookmarks = _load_all_bookmarks()
bookmarks = _load_all_bookmarks(slots)
return {k: v for k, v in bookmarks.items() if v[0] == filename}


def load_bookmarks_without_file(filename):
def load_bookmarks_without_file(filename, slots):
"""Load all bookmarks but those from a specific file."""
bookmarks = _load_all_bookmarks()
bookmarks = _load_all_bookmarks(slots)
return {k: v for k, v in bookmarks.items() if v[0] != filename}


def save_bookmarks(filename, bookmarks):
"""Save all bookmarks from specific file to config."""
def update_bookmarks(filename, bookmarks, old_slots):
"""
Compute an updated version of all the bookmarks from a specific file.
Parameters
----------
filename : str
File path that the bookmarks are related too.
bookmarks : dict
New or changed bookmarks for the file.
old_slots : dict
Base general bookmarks entries available before any changes where done.
Returns
-------
updated_slots : dict
Updated general bookmarks.
"""
if not osp.isfile(filename):
return
slots = load_bookmarks_without_file(filename)
updated_slots = load_bookmarks_without_file(filename, old_slots)
for slot_num, content in bookmarks.items():
slots[slot_num] = [filename, content[0], content[1]]
CONF.set('editor', 'bookmarks', slots)
updated_slots[slot_num] = [filename, content[0], content[1]]
return updated_slots
8 changes: 5 additions & 3 deletions spyder/plugins/editor/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from qtpy.QtWidgets import QApplication, QMainWindow, QPlainTextEdit, QToolTip

# Local imports
from spyder.api.config.mixins import SpyderConfigurationAccessor
from spyder.config.gui import get_font
from spyder.config.manager import CONF
from spyder.plugins.editor.api.decoration import TextDecoration, DRAW_ORDERS
from spyder.plugins.editor.utils.decoration import TextDecorationsManager
from spyder.plugins.editor.widgets.completion import CompletionWidget
Expand All @@ -36,7 +36,9 @@
from spyder.widgets.mixins import BaseEditMixin


class TextEditBaseWidget(QPlainTextEdit, BaseEditMixin):
class TextEditBaseWidget(
QPlainTextEdit, BaseEditMixin, SpyderConfigurationAccessor
):
"""Text edit base widget"""
BRACE_MATCHING_SCOPE = ('sof', 'eof')
focus_in = Signal()
Expand Down Expand Up @@ -121,7 +123,7 @@ def reset_current_cell():
self._current_line_block = None

def setup_completion(self):
size = CONF.get('main', 'completion/size')
size = self.get_conf('completion/size', section='main')
font = get_font()
self.completion_widget.setup_appearance(size, font)

Expand Down
Loading

0 comments on commit 11dee3c

Please sign in to comment.