1
0
forked from VimPlug/jedi

Fix some code_lines issues

This commit is contained in:
Dave Halter
2018-03-17 19:41:26 +01:00
parent 094affaf84
commit 60da6034c0
7 changed files with 17 additions and 15 deletions

View File

@@ -119,13 +119,19 @@ class Script(object):
cache_path=settings.cache_directory
)
debug.speed('parsed')
self._code_lines = parso.split_lines(source)
self._code_lines = parso.split_lines(source, keepends=True)
self._code = source
line = max(len(self._code_lines), 1) if line is None else line
if not (0 < line <= len(self._code_lines)):
raise ValueError('`line` parameter is not in a valid range.')
line_len = len(self._code_lines[line - 1])
line_string = self._code_lines[line - 1]
line_len = len(line_string)
if line_string.endswith('\r\n'):
line_len -= 1
if line_string.endswith('\n'):
line_len -= 1
column = line_len if column is None else column
if not (0 <= column <= line_len):
raise ValueError('`column` parameter is not in a valid range.')

View File

@@ -5,7 +5,6 @@ the interesting information about completion and goto operations.
"""
import re
from parso.cache import parser_cache
from parso.python.tree import search_ancestor
from jedi import settings
@@ -13,7 +12,6 @@ from jedi.evaluate.utils import ignored, unite
from jedi.cache import memoize_method
from jedi.evaluate import imports
from jedi.evaluate import compiled
from jedi.evaluate.filters import ParamName
from jedi.evaluate.imports import ImportName
from jedi.evaluate.context import instance
from jedi.evaluate.context import ClassContext, FunctionContext, FunctionExecutionContext
@@ -532,7 +530,6 @@ class Definition(BaseDefinition):
)
return typ + ' ' + code
definition = tree_name.get_definition() or tree_name
# Remove the prefix, because that's not what we want for get_code
# here.

View File

@@ -7,7 +7,6 @@ from textwrap import dedent
from parso.python.parser import Parser
from parso.python import tree
from parso import split_lines
from jedi._compatibility import u
from jedi.evaluate.syntax_tree import eval_atom
@@ -44,7 +43,7 @@ def _get_code(code_lines, start_pos, end_pos):
lines[-1] = lines[-1][:end_pos[1]]
# Remove first line indentation.
lines[0] = lines[0][start_pos[1]:]
return '\n'.join(lines)
return ''.join(lines)
class OnErrorLeaf(Exception):
@@ -283,11 +282,11 @@ def get_call_signature_details(module, position):
@time_cache("call_signatures_validity")
def cache_call_signatures(evaluator, context, bracket_leaf, code_lines, user_pos):
"""This function calculates the cache key."""
index = user_pos[0] - 1
line_index = user_pos[0] - 1
before_cursor = code_lines[index][:user_pos[1]]
other_lines = code_lines[bracket_leaf.start_pos[0]:index]
whole = '\n'.join(other_lines + [before_cursor])
before_cursor = code_lines[line_index][:user_pos[1]]
other_lines = code_lines[bracket_leaf.start_pos[0]:line_index]
whole = ''.join(other_lines + [before_cursor])
before_bracket = re.match(r'.*\(', whole, re.DOTALL)
module_path = context.get_root_context().py__file__()

View File

@@ -231,7 +231,7 @@ def _get_typing_replacement_module(grammar):
with open(typing_path) as f:
code = unicode(f.read())
_typing_module = grammar.parse(code)
_typing_module_code_lines = split_lines(code)
_typing_module_code_lines = split_lines(code, keepends=True)
return _typing_module, _typing_module_code_lines

View File

@@ -311,7 +311,7 @@ def collections_namedtuple(evaluator, obj, arguments):
generated_class = next(module.iter_classdefs())
parent_context = ModuleContext(
evaluator, module, None,
code_lines=parso.split_lines(code),
code_lines=parso.split_lines(code, keepends=True),
)
return ContextSet(ClassContext(evaluator, parent_context, generated_class))

View File

@@ -165,7 +165,7 @@ def test_get_line_code(Script):
# On custom code
first_line = 'def foo():\n'
line = ' foo'
code = '%s%s' % (first_line, line)
code = first_line + line
assert get_line_code(code) == first_line
# With before/after

View File

@@ -84,5 +84,5 @@ def test_namedtuple_goto_definitions(Script):
d1, = Script(source).goto_definitions()
assert d1.get_line_code() == "class Foo(tuple):"
assert d1.get_line_code() == "class Foo(tuple):\n"
assert d1.module_path is None