From ffaacbefbc28ca16b92205a4562017f734f5e9b1 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 18 Jul 2014 16:52:55 +0200 Subject: [PATCH] new splitlines implementation to get rid of the ugly splitlines we now have as well as (partially) the issue with form feeds --- jedi/api/__init__.py | 4 +--- jedi/common.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 970127b2..725907a6 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -86,9 +86,7 @@ class Script(object): with open(path) as f: source = f.read() - lines = source.splitlines() or [''] - if source and source[-1] == '\n': - lines.append('') + lines = common.splitlines(source) line = max(len(lines), 1) if line is None else line if not (0 < line <= len(lines)): raise ValueError('`line` parameter is not in a valid range.') diff --git a/jedi/common.py b/jedi/common.py index d8d01342..e15d2211 100644 --- a/jedi/common.py +++ b/jedi/common.py @@ -146,3 +146,13 @@ def source_to_unicode(source, encoding=None): # cast to unicode by default return unicode(source, detect_encoding(), 'replace') + + +def splitlines(string): + """ + A splitlines for Python code. In contrast to Python's ``str.splitlines``, + looks at form feeds and other special characters as normal text. Just + splits ``\n`` and ``\r\n``. + Also different: Returns ``['']`` for an empty string input. + """ + return re.split('\n|\r\n', string)