def on_modified(self, view): if sublime.score_selector(view.scope_name(view.sel()[0].b), 'source.php') == 0: return editor = PhpCoder().editor(view) doComplete = False line = editor.before() if len(line) == 0: return if re.match(r'.*[,\(]\s*$', line) or line[-1] == '$' or re.match( r'.*new [a-zA-Z0-9_]*$', line) or re.match( r'(^|\s+)[a-zA-Z]+$', line): doComplete = True else: expr = editor.expr() if expr and expr['prefix'][:2] in ['::', '->']: doComplete = True if doComplete: view.run_command( 'auto_complete', { 'disable_auto_insert': True, 'api_completions_only': False, 'next_completion_if_showing': False, 'auto_complete_commit_on_tab': True, })
def run(self, edit): if sublime.score_selector(self.view.scope_name(self.view.sel()[0].b), 'source.php') == 0: return if self.fromMouseEvent: old_sel = [r for r in self.view.sel()] self.view.run_command("drag_select", {'event': self.fromMouseEvent}) new_sel = self.view.sel()[0] editor = PhpCoder().editor(self.view) offset = 0 pos = editor.getPosition() while re.match(r'[a-zA-Z_][a-zA-Z0-9_]*', editor.substr(pos + offset, pos + offset + 1)): offset += 1 expr = editor.expr() if expr: expr['prefix'] += editor.substr(pos, pos + offset) if editor.substr(pos + offset, pos + offset + 1) == '(': expr['prefix'] += '()' else: wordRegion = self.view.word(self.view.sel()[0]) expr = {'prefix': '', 'expr': self.view.substr(wordRegion)} if editor.substr(wordRegion.begin() - 1, wordRegion.begin()) == '$': expr['expr'] = '$' + expr['expr'] if expr['expr']: resolver = phpcoder.resolver.Resolver(editor) self.prefix = None if expr['prefix']: parsedPrefix = resolver.parseExpression('Dummy' + expr['prefix']) if parsedPrefix and len(parsedPrefix) > 1: self.prefix = parsedPrefix[1]['name'] self.matches = [] for r in resolver.resolve(expr['expr']): if self.prefix == None or \ ('properties' in r and self.prefix in r['properties']) or \ ('methods' in r and self.prefix in r['methods']): self.matches.append(r) results = [] for r in self.matches: results.append([ r['name'] + expr['prefix'], r['path'], ]) if len(results) == 1: self.panel_on_select(0) elif len(results) > 0: self.view.window().show_quick_panel( results, self.panel_on_select, on_highlight=self.panel_on_highlight)
def _completeParams(self, editor, resolver): offset = 0 pos = editor.getPosition() - 1 open = 0 loop = 0 commas = 0 while not(open == 1 or pos == offset): c = editor.substr(pos - offset, pos - offset + 1) if c == '(': open += 1 elif c == ')': open -= 1 elif open == 0 and c == ',': commas += 1 offset += 1 m = re.match(r'.*new\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\($', editor.substr(0, pos - offset + 2), re.DOTALL) if m: klass = m.group(1) expr = {'expr': m.group(1), 'prefix': '->__construct()'} else: expr = editor.expr(-1 * offset) ret = [] if expr and expr['expr'] and expr['prefix']: parsedPrefix = resolver.parseExpression('Dummy' + expr['prefix']) if parsedPrefix and len(parsedPrefix) > 1: prefix = parsedPrefix[1]['name'] completions = [] for r in resolver.resolve(expr['expr']): try: method = r['methods'][prefix] name = '' value = '' if commas > 0: value = ' ' if 'params' in method: p = 1 for param in method['params']: if p > commas: if 'initial' in param: # Fix default values refering to self with correct class name initial = param['initial'] initial = re.sub(r'^self', r['name'], initial) name += '$' + param['name'] + ' = ' + initial value += '${' + str(p) + ':/* \\$' + param['name'] + ' = */ ' + initial + '}, ' else: name += '$' + param['name']; value += '${' + str(p) + ':\\$' + param['name'] + '}, ' name += ', ' ret.append([name.rstrip(', '), value.rstrip(', ')]) p += 1 except (Exception, ) as e: print(e) return ret
def run(self, edit): if sublime.score_selector(self.view.scope_name(self.view.sel()[0].b), 'source.php') == 0: return if self.fromMouseEvent: old_sel = [r for r in self.view.sel()] self.view.run_command("drag_select", {'event': self.fromMouseEvent}) new_sel = self.view.sel()[0] editor = PhpCoder().editor(self.view) offset = 0 pos = editor.getPosition() while re.match(r'[a-zA-Z_][a-zA-Z0-9_]*', editor.substr(pos + offset, pos + offset + 1)): offset += 1 expr = editor.expr() if expr: expr['prefix'] += editor.substr(pos, pos+offset) if editor.substr(pos + offset, pos + offset + 1) == '(': expr['prefix'] += '()' else: wordRegion = self.view.word(self.view.sel()[0]) expr = {'prefix': '', 'expr': self.view.substr(wordRegion)} if editor.substr(wordRegion.begin() - 1, wordRegion.begin()) == '$': expr['expr'] = '$' + expr['expr'] if expr['expr']: resolver = phpcoder.resolver.Resolver(editor) self.prefix = None if expr['prefix']: parsedPrefix = resolver.parseExpression('Dummy' + expr['prefix']) if parsedPrefix and len(parsedPrefix) > 1: self.prefix = parsedPrefix[1]['name'] self.matches = [] for r in resolver.resolve(expr['expr']): if self.prefix == None or \ ('properties' in r and self.prefix in r['properties']) or \ ('methods' in r and self.prefix in r['methods']): self.matches.append(r) results = [] for r in self.matches: results.append([ r['name'] + expr['prefix'], r['path'], ]) if len(results) == 1: self.panel_on_select(0) elif len(results) > 0: self.view.window().show_quick_panel(results, self.panel_on_select, on_highlight = self.panel_on_highlight)
def on_query_completions(self, view, prefix, locations): if sublime.score_selector(view.scope_name(view.sel()[0].b), 'source.php') == 0: return editor = PhpCoder().editor(view) resolver = phpcoder.resolver.Resolver(editor) line = editor.before() if re.match(r'.*[,\(]\s*$', line): return self._completeParams(editor, resolver) if re.match(r'.*new [a-zA-Z0-9_]*$', line): return self._completeNew(view) if line[-1] == '$': return editor.findLocals() expr = editor.expr() if expr: return self._completeExpr(expr, editor, resolver) return []
def on_query_completions(self, view, prefix, locations): if sublime.score_selector(view.scope_name(view.sel()[0].b), 'source.php') == 0: return editor = PhpCoder().editor(view) resolver = phpcoder.resolver.Resolver(editor) line = editor.before() if re.match(r'.*[,\(]\s*$', line): return self._completeParams(editor, resolver) if re.match(r'.*new [a-zA-Z0-9_]*$', line): return self._completeNew(view) if line[-1] == '$': return editor.findLocals() expr = editor.expr() if expr: return self._completeExpr(expr, editor, resolver) return []
def on_modified(self, view): if sublime.score_selector(view.scope_name(view.sel()[0].b), 'source.php') == 0: return editor = PhpCoder().editor(view) doComplete = False line = editor.before() if len(line) == 0: return if re.match(r'.*[,\(]\s*$', line) or line[-1] == '$' or re.match(r'.*new [a-zA-Z0-9_]*$', line) or re.match(r'(^|\s+)[a-zA-Z]+$', line): doComplete = True else: expr = editor.expr() if expr and expr['prefix'][:2] in ['::', '->']: doComplete = True if doComplete: view.run_command('auto_complete', { 'disable_auto_insert': True, 'api_completions_only': False, 'next_completion_if_showing': False, 'auto_complete_commit_on_tab': True, })
def _completeParams(self, editor, resolver): offset = 0 pos = editor.getPosition() - 1 open = 0 loop = 0 commas = 0 while not (open == 1 or pos == offset): c = editor.substr(pos - offset, pos - offset + 1) if c == '(': open += 1 elif c == ')': open -= 1 elif open == 0 and c == ',': commas += 1 offset += 1 m = re.match(r'.*new\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\($', editor.substr(0, pos - offset + 2), re.DOTALL) if m: klass = m.group(1) expr = {'expr': m.group(1), 'prefix': '->__construct()'} else: expr = editor.expr(-1 * offset) ret = [] if expr and expr['expr'] and expr['prefix']: parsedPrefix = resolver.parseExpression('Dummy' + expr['prefix']) if parsedPrefix and len(parsedPrefix) > 1: prefix = parsedPrefix[1]['name'] completions = [] for r in resolver.resolve(expr['expr']): try: method = r['methods'][prefix] name = '' value = '' if commas > 0: value = ' ' if 'params' in method: p = 1 for param in method['params']: if p > commas: if 'initial' in param: # Fix default values refering to self with correct class name initial = param['initial'] initial = re.sub( r'^self', r['name'], initial) name += '$' + param[ 'name'] + ' = ' + initial value += '${' + str( p ) + ':/* \\$' + param[ 'name'] + ' = */ ' + initial + '}, ' else: name += '$' + param['name'] value += '${' + str( p) + ':\\$' + param['name'] + '}, ' name += ', ' ret.append([ name.rstrip(', '), value.rstrip(', ') ]) p += 1 except (Exception, ) as e: print(e) return ret