1
0
forked from VimPlug/jedi

minor api refactorings

This commit is contained in:
Dave Halter
2014-01-04 13:33:09 +01:00
parent 036f119e68
commit 962a678417
2 changed files with 12 additions and 13 deletions

View File

@@ -64,6 +64,8 @@ class Script(object):
if source_path is not None: if source_path is not None:
warnings.warn("Use path instead of source_path.", DeprecationWarning) warnings.warn("Use path instead of source_path.", DeprecationWarning)
path = source_path path = source_path
self._source_path = path
self.path = None if path is None else os.path.abspath(path)
if source is None: if source is None:
with open(path) as f: with open(path) as f:
@@ -72,24 +74,21 @@ class Script(object):
lines = source.splitlines() or [''] lines = source.splitlines() or ['']
if source and source[-1] == '\n': if source and source[-1] == '\n':
lines.append('') lines.append('')
line = max(len(lines), 1) if line is None else line
self._line = max(len(lines), 1) if line is None else line if not (0 < line <= len(lines)):
if not (0 < self._line <= len(lines)):
raise ValueError('`line` parameter is not in a valid range.') raise ValueError('`line` parameter is not in a valid range.')
line_len = len(lines[self._line - 1]) line_len = len(lines[line - 1])
self._column = line_len if column is None else column column = line_len if column is None else column
if not (0 <= self._column <= line_len): if not (0 <= column <= line_len):
raise ValueError('`column` parameter is not in a valid range.') raise ValueError('`column` parameter is not in a valid range.')
self._pos = line, column
api_classes.clear_caches() api_classes.clear_caches()
debug.reset_time() debug.reset_time()
self.source = modules.source_to_unicode(source, encoding) self.source = modules.source_to_unicode(source, encoding)
self._pos = self._line, self._column
self._module = modules.ModuleWithCursor( self._module = modules.ModuleWithCursor(
path, source=self.source, position=self._pos) path, source=self.source, position=self._pos)
self._source_path = path
self.path = None if path is None else os.path.abspath(path)
self._evaluator = Evaluator() self._evaluator = Evaluator()
debug.speed('init') debug.speed('init')
@@ -246,7 +245,7 @@ class Script(object):
return scopes return scopes
def _get_under_cursor_stmt(self, cursor_txt): def _get_under_cursor_stmt(self, cursor_txt):
offset = self._line - 1, self._column offset = self._pos[0] - 1, self._pos[1]
r = Parser(cursor_txt, no_docstr=True, offset=offset) r = Parser(cursor_txt, no_docstr=True, offset=offset)
try: try:
stmt = r.module.statements[0] stmt = r.module.statements[0]
@@ -358,11 +357,11 @@ class Script(object):
call = call.next call = call.next
# reset cursor position: # reset cursor position:
(row, col) = call.name.end_pos (row, col) = call.name.end_pos
_pos = (row, max(col - 1, 0)) pos = (row, max(col - 1, 0))
self._module = modules.ModuleWithCursor( self._module = modules.ModuleWithCursor(
self._source_path, self._source_path,
source=self.source, source=self.source,
position=_pos) position=pos)
# then try to find the path again # then try to find the path again
goto_path = self._module.get_path_under_cursor() goto_path = self._module.get_path_under_cursor()

View File

@@ -185,7 +185,7 @@ class Evaluator(object):
yield g yield g
else: else:
yield scope, _get_defined_names_for_position(scope, yield scope, _get_defined_names_for_position(scope,
position, in_func_scope) position, in_func_scope)
except StopIteration: except StopIteration:
reraise(common.MultiLevelStopIteration, sys.exc_info()[2]) reraise(common.MultiLevelStopIteration, sys.exc_info()[2])
if scope.isinstance(pr.ForFlow) and scope.is_list_comp: if scope.isinstance(pr.ForFlow) and scope.is_list_comp: