1
0
forked from VimPlug/jedi

move parsing.PushBackIterator and parsing.indent_block to common

This commit is contained in:
David Halter
2012-12-13 16:38:15 +01:00
parent 2d8d180354
commit cab74d5f51
7 changed files with 60 additions and 51 deletions

View File

@@ -37,6 +37,7 @@ import debug
import settings
import keywords
import helpers
import common
import builtin
import api_classes
@@ -401,7 +402,7 @@ class Script(object):
return None
debug.speed('func_call user_stmt')
with helpers.scale_speed_settings(settings.scale_get_in_function_call):
with common.scale_speed_settings(settings.scale_get_in_function_call):
origins = evaluate.follow_call(call)
debug.speed('func_call followed')

View File

@@ -9,6 +9,7 @@ if is_py3k:
import types
import inspect
import common
import debug
import parsing
import imports
@@ -222,7 +223,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0):
if doc:
doc = ('r"""\n%s\n"""\n' % doc)
if indent:
doc = parsing.indent_block(doc)
doc = common.indent_block(doc)
return doc
return ''
@@ -303,7 +304,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0):
except KeyError:
mixin = {}
cl_code = _generate_code(cl, mixin, depth + 1)
code += parsing.indent_block(cl_code)
code += common.indent_block(cl_code)
code += '\n'
# functions
@@ -318,7 +319,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0):
# normal code generation
code += 'def %s(%s):\n' % (name, params)
code += doc_str
code += parsing.indent_block('%s\n\n' % ret)
code += common.indent_block('%s\n\n' % ret)
else:
# generation of code with mixins
# the parser only supports basic functions with a newline after
@@ -336,7 +337,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0):
continue
ret = 'pass'
code += '@property\ndef %s(self):\n' % (name)
code += parsing.indent_block(get_doc(func) + '%s\n\n' % ret)
code += common.indent_block(get_doc(func) + '%s\n\n' % ret)
# variables
for name, value in stmts.items():

47
jedi/common.py Normal file
View File

@@ -0,0 +1,47 @@
""" A universal module with functions / classes without dependencies. """
import contextlib
import settings
class PushBackIterator(object):
def __init__(self, iterator):
self.pushes = []
self.iterator = iterator
def push_back(self, value):
self.pushes.append(value)
def __iter__(self):
return self
def next(self):
""" Python 2 Compatibility """
return self.__next__()
def __next__(self):
if self.pushes:
return self.pushes.pop()
else:
return next(self.iterator)
@contextlib.contextmanager
def scale_speed_settings(factor):
a = settings.max_executions
b = settings.max_until_execution_unique
settings.max_executions *= factor
settings.max_until_execution_unique *= factor
yield
settings.max_executions = a
settings.max_until_execution_unique = b
def indent_block(text, indention=' '):
""" This function indents a text block with a default of four spaces """
temp = ''
while text and text[-1] == '\n':
temp += text[-1]
text = text[:-1]
lines = text.split('\n')
return '\n'.join(map(lambda s: indention + s, lines)) + temp

View File

@@ -18,6 +18,7 @@ import sys
import itertools
import copy
import common
import cache
import parsing
import debug
@@ -643,7 +644,7 @@ class Execution(Executable):
else:
yield None, var_arg
return iter(parsing.PushBackIterator(iterate()))
return iter(common.PushBackIterator(iterate()))
def get_set_vars(self):
return self.get_defined_names()

View File

@@ -254,12 +254,3 @@ def scan_array_for_pos(arr, pos):
return call, check_arr_index(), stop
@contextlib.contextmanager
def scale_speed_settings(factor):
a = settings.max_executions
b = settings.max_until_execution_unique
settings.max_executions *= factor
settings.max_until_execution_unique *= factor
yield
settings.max_executions = a
settings.max_until_execution_unique = b

View File

@@ -12,7 +12,6 @@ import cache
import parsing
import builtin
import debug
import evaluate
import settings
import imports

View File

@@ -37,44 +37,13 @@ import keyword
import os
import debug
import common
class ParserError(Exception):
pass
def indent_block(text, indention=' '):
""" This function indents a text block with a default of four spaces """
temp = ''
while text and text[-1] == '\n':
temp += text[-1]
text = text[:-1]
lines = text.split('\n')
return '\n'.join(map(lambda s: indention + s, lines)) + temp
class PushBackIterator(object):
def __init__(self, iterator):
self.pushes = []
self.iterator = iterator
def push_back(self, value):
self.pushes.append(value)
def __iter__(self):
return self
def next(self):
""" Python 2 Compatibility """
return self.__next__()
def __next__(self):
if self.pushes:
return self.pushes.pop()
else:
return next(self.iterator)
class Base(object):
"""
This is just here to have an isinstance check, which is also used on
@@ -191,7 +160,7 @@ class Scope(Simple):
string += stmt.get_code()
if first_indent:
string = indent_block(string, indention=indention)
string = common.indent_block(string, indention=indention)
return string
@Python3Method
@@ -1632,7 +1601,7 @@ class PyFuzzyParser(object):
debug.warning('indentation error on line %s, ignoring it' %
(self.start_pos[0]))
self._line_of_tokenize_restart = self.start_pos[0] + 1
self.gen = PushBackIterator(tokenize.generate_tokens(
self.gen = common.PushBackIterator(tokenize.generate_tokens(
self.buf.readline))
return self.next()
except StopIteration:
@@ -1667,7 +1636,7 @@ class PyFuzzyParser(object):
:raises: IndentationError
"""
self.buf = StringIO(self.code)
self.gen = PushBackIterator(tokenize.generate_tokens(
self.gen = common.PushBackIterator(tokenize.generate_tokens(
self.buf.readline))
extended_flow = ['else', 'elif', 'except', 'finally']