Skip to content

Commit

Permalink
Editor: Move CONF usage for bookmarks logic and use SpyderConfigurato…
Browse files Browse the repository at this point in the history
…rAccessor class in Editor widgets
  • Loading branch information
dalthviz committed Apr 14, 2023
1 parent 5e858dc commit 2f62686
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 136 deletions.
76 changes: 40 additions & 36 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 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,25 @@ 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('help', 'connect/editor'))

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 +2041,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 +2103,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 = save_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 +2457,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 +3260,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 +3277,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 +3384,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
22 changes: 9 additions & 13 deletions spyder/plugins/editor/utils/bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,32 @@
# 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):
def save_bookmarks(filename, bookmarks, old_slots):
"""Save all bookmarks from specific file to config."""
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
41 changes: 21 additions & 20 deletions spyder/plugins/editor/widgets/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
# Local imports
from spyder.api.panel import Panel
from spyder.config.base import _, get_debug_level, running_under_pytest
from spyder.config.manager import CONF
from spyder.plugins.editor.api.decoration import TextDecoration
from spyder.plugins.editor.extensions import (CloseBracketsExtension,
CloseQuotesExtension,
Expand Down Expand Up @@ -122,6 +121,8 @@ def wrapper(self, *args, **kwargs):
class CodeEditor(TextEditBaseWidget):
"""Source Code Editor Widget based exclusively on Qt"""

CONF_SECTION = 'editor'

LANGUAGES = {
'Python': (sh.PythonSH, '#'),
'IPython': (sh.IPythonSH, '#'),
Expand Down Expand Up @@ -288,7 +289,7 @@ def __init__(self, parent=None):
self.current_project_path = None

# Caret (text cursor)
self.setCursorWidth(CONF.get('main', 'cursor/width'))
self.setCursorWidth(self.get_conf('cursor/width', section='main'))

self.text_helper = TextHelper(self)

Expand Down Expand Up @@ -743,7 +744,7 @@ def create_shortcuts(self):
shortcuts = []
for context, name, callback in shortcut_context_name_callbacks:
shortcuts.append(
CONF.config_shortcut(
self.config_shortcut(
callback, context=context, name=name, parent=self))
return shortcuts

Expand Down Expand Up @@ -4517,27 +4518,27 @@ def setup_context_menu(self):
"""Setup context menu"""
self.undo_action = create_action(
self, _("Undo"), icon=ima.icon('undo'),
shortcut=CONF.get_shortcut('editor', 'undo'), triggered=self.undo)
shortcut=self.get_shortcut('undo'), triggered=self.undo)
self.redo_action = create_action(
self, _("Redo"), icon=ima.icon('redo'),
shortcut=CONF.get_shortcut('editor', 'redo'), triggered=self.redo)
shortcut=self.get_shortcut('redo'), triggered=self.redo)
self.cut_action = create_action(
self, _("Cut"), icon=ima.icon('editcut'),
shortcut=CONF.get_shortcut('editor', 'cut'), triggered=self.cut)
shortcut=self.get_shortcut('cut'), triggered=self.cut)
self.copy_action = create_action(
self, _("Copy"), icon=ima.icon('editcopy'),
shortcut=CONF.get_shortcut('editor', 'copy'), triggered=self.copy)
shortcut=self.get_shortcut('copy'), triggered=self.copy)
self.paste_action = create_action(
self, _("Paste"), icon=ima.icon('editpaste'),
shortcut=CONF.get_shortcut('editor', 'paste'),
shortcut=self.get_shortcut('paste'),
triggered=self.paste)
selectall_action = create_action(
self, _("Select All"), icon=ima.icon('selectall'),
shortcut=CONF.get_shortcut('editor', 'select all'),
shortcut=self.get_shortcut('select all'),
triggered=self.selectAll)
toggle_comment_action = create_action(
self, _("Comment")+"/"+_("Uncomment"), icon=ima.icon('comment'),
shortcut=CONF.get_shortcut('editor', 'toggle comment'),
shortcut=self.get_shortcut('toggle comment'),
triggered=self.toggle_comment)
self.clear_all_output_action = create_action(
self, _("Clear all ouput"), icon=ima.icon('ipython_console'),
Expand All @@ -4547,13 +4548,13 @@ def setup_context_menu(self):
triggered=self.convert_notebook)
self.gotodef_action = create_action(
self, _("Go to definition"),
shortcut=CONF.get_shortcut('editor', 'go to definition'),
shortcut=self.get_shortcut('go to definition'),
triggered=self.go_to_definition_from_cursor)

self.inspect_current_object_action = create_action(
self, _("Inspect current object"),
icon=ima.icon('MessageBoxInformation'),
shortcut=CONF.get_shortcut('editor', 'inspect current object'),
shortcut=self.get_shortcut('inspect current object'),
triggered=self.sig_show_object_info)

# Run actions
Expand All @@ -4575,20 +4576,20 @@ def setup_context_menu(self):
writer = self.writer_docstring
self.docstring_action = create_action(
self, _("Generate docstring"),
shortcut=CONF.get_shortcut('editor', 'docstring'),
shortcut=self.get_shortcut('docstring'),
triggered=writer.write_docstring_at_first_line_of_function)

# Document formatting
formatter = CONF.get(
'completions',
formatter = self.get_conf(
('provider_configuration', 'lsp', 'values', 'formatting'),
''
default='',
section='completions',
)
self.format_action = create_action(
self,
_('Format file or selection with {0}').format(
formatter.capitalize()),
shortcut=CONF.get_shortcut('editor', 'autoformatting'),
shortcut=self.get_shortcut('autoformatting'),
triggered=self.format_document_or_range)

self.format_action.setEnabled(False)
Expand Down Expand Up @@ -5435,10 +5436,10 @@ def contextMenuEvent(self, event):
nbformat is not None)
self.gotodef_action.setVisible(self.go_to_definition_enabled)

formatter = CONF.get(
'completions',
formatter = self.get_conf(
('provider_configuration', 'lsp', 'values', 'formatting'),
''
default='',
section='completions'
)
self.format_action.setText(_(
'Format file or selection with {0}').format(
Expand Down
Loading

0 comments on commit 2f62686

Please sign in to comment.