Skip to content

Commit

Permalink
Merge from 3.x: PR #7132
Browse files Browse the repository at this point in the history
Fixes #7080
  • Loading branch information
ccordoba12 committed Jun 15, 2018
2 parents 0b4e2b0 + 40b430d commit ef7f1ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion spyder/plugins/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Standard library imports
import os.path as osp
import sys
import re

# Third party imports
from qtpy.QtCore import Signal, Slot
Expand All @@ -17,13 +18,14 @@


# Local imports
from spyder.utils import encoding
from spyder.config.base import _
from spyder.api.plugins import SpyderPluginWidget
from spyder.api.preferences import PluginConfigPage
from spyder.py3compat import is_text_string, to_text_string
from spyder.utils import encoding
from spyder.utils import icon_manager as ima
from spyder.utils.qthelpers import add_actions, create_action
from spyder.utils.sourcecode import normalize_eols
from spyder.widgets.tabs import Tabs
from spyder.widgets.sourcecode import codeeditor
from spyder.widgets.findreplace import FindReplace
Expand Down Expand Up @@ -233,6 +235,12 @@ def add_history(self, filename):
editor.toggle_wrap_mode( self.get_option('wrap') )

text, _ = encoding.read(filename)
text = normalize_eols(text)
linebreaks = [m.start() for m in re.finditer('\n', text)]
maxNline = self.get_option('max_entries')
if len(linebreaks) > maxNline:
text = text[linebreaks[-maxNline - 1] + 1:]
encoding.write(text, filename)
editor.set_text(text)
editor.set_cursor_position('eof')

Expand Down
21 changes: 21 additions & 0 deletions spyder/plugins/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from qtpy.QtGui import QTextOption

from spyder.plugins import history
from spyder.py3compat import to_text_string

#==============================================================================
# Utillity Functions
Expand Down Expand Up @@ -74,6 +75,26 @@ def historylog_with_tab(historylog, mocker, monkeypatch):
#==============================================================================
# Tests
#==============================================================================
def test_max_entries(historylog, tmpdir):
"""Test that history is truncated at max_entries."""
max_entries = historylog.get_option('max_entries')

# Write more than max entries in a test file
history = ''
for i in range(max_entries + 1):
history = history + '{}\n'.format(i)

history_file = tmpdir.join('history.py')
history_file.write(history)

# Load test file in plugin
historylog.add_history(to_text_string(history_file))

# Assert that we have max_entries after loading history and
# that there's no 0 in the first line
assert len(history_file.readlines()) == max_entries
assert '0' not in history_file.readlines()[0]


def test_init(historylog):
"""Test HistoryLog.__init__.
Expand Down
8 changes: 8 additions & 0 deletions spyder/utils/sourcecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def has_mixed_eol_chars(text):
return repr(correct_text) != repr(text)


def normalize_eols(text, eol='\n'):
"""Use the same eol's in text"""
for eol_char, _ in EOL_CHARS:
if eol_char != eol:
text = text.replace(eol_char, eol)
return text


def fix_indentation(text, indent_chars):
"""Replace tabs by spaces"""
return text.replace('\t', indent_chars)
Expand Down
5 changes: 5 additions & 0 deletions spyder/utils/tests/test_sourcecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from spyder.utils import sourcecode


def test_normalize_eols():
text = "a\nb\r\nc\rd"
assert sourcecode.normalize_eols(text) == "a\nb\nc\nd"


def test_get_primary_at():
code = 'import functools\nfunctools.partial'
assert sourcecode.get_primary_at(code, len(code)) == 'functools.partial'
Expand Down

0 comments on commit ef7f1ff

Please sign in to comment.