Skip to content

Commit

Permalink
Merge pull request ninja-ide#683 from diegosarmentero/master
Browse files Browse the repository at this point in the history
improves in completion update
  • Loading branch information
diegosarmentero committed Jun 30, 2012
2 parents 5b1efd3 + 4af6734 commit 5b4beed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 3 additions & 1 deletion ninja_ide/tools/completion/analyzer.py
Expand Up @@ -85,7 +85,7 @@ def _get_valid_module(self, source, retry=0):
new_line = ''
#This is failing sometimes, it should remaing commented
#until we find the proper fix.
indent = re.match('^\s+', unicode(reason.text))
indent = re.match('^\s+', reason.text)
if indent is not None:
new_line = indent.group() + 'pass'
split_source = source.splitlines()
Expand Down Expand Up @@ -224,6 +224,8 @@ def _process_class(self, symbol):
clazz.add_attributes(assigns)
elif sym.__class__ is ast.FunctionDef:
clazz.add_function(self._process_function(sym, clazz))
clazz.update_bases()
clazz.update_with_parent_data()
return clazz

def _process_function(self, symbol, parent=None):
Expand Down
9 changes: 6 additions & 3 deletions ninja_ide/tools/completion/code_completion.py
Expand Up @@ -46,6 +46,9 @@ def __init__(self):
model.MODULES = self.cdaemon.modules
self.module_id = None
self.patIndent = re.compile('^\s+')
self.patClass = re.compile("class (\w+?)\(")
self.patFunction = re.compile("(\w+?)\(")
self.patWords = re.compile('\W+')
self._valid_op = (')', '}', ']')
self._invalid_op = ('(', '{', '[')
self._invalid_words = ('if', 'elif', 'for', 'while', 'in', 'return',
Expand Down Expand Up @@ -226,9 +229,9 @@ def get_completion(self, code, offset):
data = {'attributes': result['type']['attributes'],
'functions': result['type']['functions']}
else:
clazzes = sorted(set(re.findall("class (\w+?)\(", code)))
funcs = sorted(set(re.findall("(\w+?)\(", code)))
attrs = sorted(set(re.split('\W+', code)))
clazzes = sorted(set(self.patClass.findall(code)))
funcs = sorted(set(self.patFunction.findall(code)))
attrs = sorted(set(self.patWords.split(code)))
if final_word in attrs:
attrs.remove(final_word)
if attr_name in attrs:
Expand Down
18 changes: 12 additions & 6 deletions ninja_ide/tools/completion/completer_widget.py
Expand Up @@ -37,6 +37,8 @@ def __init__(self, editor):
super(CodeCompletionWidget, self).__init__(
None, Qt.FramelessWindowHint | Qt.ToolTip)
self._editor = editor
self._revision = 0
self._block = 0
self.stack_layout = QStackedLayout(self)
self.stack_layout.setContentsMargins(0, 0, 0, 0)
self.stack_layout.setSpacing(0)
Expand Down Expand Up @@ -78,9 +80,9 @@ def __init__(self, editor):
self.connect(self.completion_list,
SIGNAL("itemClicked(QListWidgetItem*)"),
self.pre_key_insert_completion)
self.connect(self._editor.document(), SIGNAL("blockCountChanged(int)"),
self.connect(self._editor.document(),
SIGNAL("cursorPositionChanged(QTextCursor)"),
self.update_metadata)
self.update_metadata()

def _select_next_row(self, move=1):
new_row = self.completion_list.currentRow() + move
Expand All @@ -99,11 +101,15 @@ def _select_previous_row(self, move=1):
self.completion_list.count() - move)
return True

def update_metadata(self):
def update_metadata(self, cursor):
if settings.CODE_COMPLETION:
source = self._editor.get_text()
source = source.encode(self._editor.encoding)
self.cc.analyze_file(self._editor.ID, source)
if self._editor.document().revision() != self._revision and \
cursor.block().blockNumber() != self._block:
source = self._editor.get_text()
source = source.encode(self._editor.encoding)
self.cc.analyze_file(self._editor.ID, source)
self._revision = self._editor.document().revision()
self._block = cursor.block().blockNumber()

def insert_completion(self, insert, type_=ord('a')):
if insert != self._prefix:
Expand Down
18 changes: 16 additions & 2 deletions ninja_ide/tools/completion/model.py
Expand Up @@ -282,7 +282,8 @@ def need_resolution(self):
for cla in self.classes:
clazz = self.classes[cla]
for parent in clazz.bases:
return True
if clazz.bases[parent] is None:
return True
if self._check_attr_func_resolution(clazz):
return True
return False
Expand Down Expand Up @@ -312,10 +313,22 @@ def __init__(self, name):
super(Clazz, self).__init__()
self.name = name
self.bases = {}
self._update_bases = []
# self.decorators = []

def add_parent(self, parent):
self.bases[parent] = None
self._update_bases.append(parent)
value = self.bases.get(parent, None)
if value is None:
self.bases[parent] = None

def update_bases(self):
to_remove = []
for parent in self.bases:
if parent not in self._update_bases:
to_remove.append(parent)
for remove in to_remove:
self.bases.pop(parent)

def get_completion_items(self):
attributes = [a for a in self.attributes]
Expand All @@ -332,6 +345,7 @@ def update_with_parent_data(self):
if parent.__class__ is Clazz:
self.attributes.update(parent.attributes)
self.functions.update(parent.functions)
self.bases[base] = None
elif isinstance(parent, tuple):
parent_name = parent[0]
data = parent[1]
Expand Down

0 comments on commit 5b4beed

Please sign in to comment.