Skip to content

Commit

Permalink
Merge pull request spyder-ide#23100 from dalthviz/follow_up_22965
Browse files Browse the repository at this point in the history
PR: Use style functions from `spyder-kernels` (IPython Console)
  • Loading branch information
ccordoba12 authored Nov 28, 2024
2 parents a642045 + 0d5319c commit 58ab1c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 108 deletions.
5 changes: 3 additions & 2 deletions spyder/plugins/ipythonconsole/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
from pygments.token import Name
import pytest
from qtpy.QtWidgets import QMainWindow
from spyder_kernels.utils.style import create_style_class

# Local imports
from spyder.api.plugins import Plugins
from spyder.app.cli_options import get_options
from spyder.config.gui import get_color_scheme
from spyder.config.manager import CONF
from spyder.plugins.debugger.plugin import Debugger
from spyder.plugins.help.utils.sphinxify import CSS_PATH
from spyder.plugins.ipythonconsole.plugin import IPythonConsole
from spyder.plugins.ipythonconsole.utils.style import create_style_class
from spyder.utils.conda import get_list_conda_envs


Expand Down Expand Up @@ -57,7 +58,7 @@ def pytest_runtest_makereport(item, call):
# ---- Utillity Functions
# =============================================================================
def get_console_font_color(syntax_style):
styles = create_style_class(syntax_style).styles
styles = create_style_class(get_color_scheme(syntax_style)).styles
font_color = styles[Name]
return font_color

Expand Down
105 changes: 3 additions & 102 deletions spyder/plugins/ipythonconsole/utils/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
Style for IPython Console
"""

# Local imports
from spyder.config.gui import get_color_scheme

# Third party imports
from pygments.style import Style
from pygments.token import (Name, Keyword, Comment, String, Number,
Punctuation, Operator)

from qtconsole.styles import dark_color

# Local imports
from spyder.config.gui import get_color_scheme


def create_qss_style(color_scheme):
"""Returns a QSS stylesheet with Spyder color scheme settings.
Expand Down Expand Up @@ -78,98 +74,3 @@ def give_font_style(is_italic):
inverted_background_color)

return (sheet_formatted, dark_color(font_color))


def create_pygments_dict(color_scheme_name):
"""
Create a dictionary that saves the given color scheme as a
Pygments style.
"""

def give_font_weight(is_bold):
if is_bold:
return 'bold'
else:
return ''

def give_font_style(is_italic):
if is_italic:
return 'italic'
else:
return ''

color_scheme = get_color_scheme(color_scheme_name)

fon_c, fon_fw, fon_fs = color_scheme['normal']
font_color = fon_c
font_font_weight = give_font_weight(fon_fw)
font_font_style = give_font_style(fon_fs)
key_c, key_fw, key_fs = color_scheme['keyword']
keyword_color = key_c
keyword_font_weight = give_font_weight(key_fw)
keyword_font_style = give_font_style(key_fs)
bui_c, bui_fw, bui_fs = color_scheme['builtin']
builtin_color = bui_c
builtin_font_weight = give_font_weight(bui_fw)
builtin_font_style = give_font_style(bui_fs)
str_c, str_fw, str_fs = color_scheme['string']
string_color = str_c
string_font_weight = give_font_weight(str_fw)
string_font_style = give_font_style(str_fs)
num_c, num_fw, num_fs = color_scheme['number']
number_color = num_c
number_font_weight = give_font_weight(num_fw)
number_font_style = give_font_style(num_fs)
com_c, com_fw, com_fs = color_scheme['comment']
comment_color = com_c
comment_font_weight = give_font_weight(com_fw)
comment_font_style = give_font_style(com_fs)
def_c, def_fw, def_fs = color_scheme['definition']
definition_color = def_c
definition_font_weight = give_font_weight(def_fw)
definition_font_style = give_font_style(def_fs)
ins_c, ins_fw, ins_fs = color_scheme['instance']
instance_color = ins_c
instance_font_weight = give_font_weight(ins_fw)
instance_font_style = give_font_style(ins_fs)

font_token = font_font_style + ' ' + font_font_weight + ' ' + font_color
definition_token = (definition_font_style + ' ' + definition_font_weight +
' ' + definition_color)
builtin_token = (builtin_font_style + ' ' + builtin_font_weight + ' ' +
builtin_color)
instance_token = (instance_font_style + ' ' + instance_font_weight + ' ' +
instance_color)
keyword_token = (keyword_font_style + ' ' + keyword_font_weight + ' ' +
keyword_color)
comment_token = (comment_font_style + ' ' + comment_font_weight + ' ' +
comment_color)
string_token = (string_font_style + ' ' + string_font_weight + ' ' +
string_color)
number_token = (number_font_style + ' ' + number_font_weight + ' ' +
number_color)

syntax_style_dic = {Name: font_token.strip(),
Name.Class: definition_token.strip(),
Name.Function: definition_token.strip(),
Name.Builtin: builtin_token.strip(),
Name.Builtin.Pseudo: instance_token.strip(),
Keyword: keyword_token.strip(),
Keyword.Type: builtin_token.strip(),
Comment: comment_token.strip(),
String: string_token.strip(),
Number: number_token.strip(),
Punctuation: font_token.strip(),
Operator.Word: keyword_token.strip()}

return syntax_style_dic


def create_style_class(color_scheme_name):
"""Create a Pygments Style class with the given color scheme."""

class StyleClass(Style):
default_style = ""
styles = create_pygments_dict(color_scheme_name)

return StyleClass
9 changes: 5 additions & 4 deletions spyder/plugins/ipythonconsole/widgets/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from qtpy.QtGui import QClipboard, QTextCursor, QTextFormat
from qtpy.QtWidgets import QApplication, QMessageBox
from spyder_kernels.comms.frontendcomm import CommError
from spyder_kernels.utils.style import create_style_class
from traitlets import observe

# Local imports
Expand All @@ -33,8 +34,7 @@
ClientContextMenuActions,
ClientContextMenuSections
)
from spyder.plugins.ipythonconsole.utils.style import (
create_qss_style, create_style_class)
from spyder.plugins.ipythonconsole.utils.style import create_qss_style
from spyder.plugins.ipythonconsole.utils.kernel_handler import (
KernelConnectionState)
from spyder.plugins.ipythonconsole.widgets import (
Expand Down Expand Up @@ -1440,13 +1440,14 @@ def _syntax_style_changed(self, changed=None):
# ignore premature calls
return
if self.syntax_style:
self._highlighter._style = create_style_class(self.syntax_style)
color_scheme = get_color_scheme(self.syntax_style)
self._highlighter._style = create_style_class(color_scheme)
self._highlighter._clear_caches()
if changed is None:
return
self.set_kernel_configuration(
"traceback_highlight_style",
get_color_scheme(self.syntax_style),
color_scheme,
)
else:
self._highlighter.set_style_sheet(self.style_sheet)
Expand Down

0 comments on commit 58ab1c8

Please sign in to comment.