def run(self, edit): if len(self.view.sel()) == 0: return sel = self.view.sel()[0] if not sel.empty(): variable = [sel.a, sel.b] else: line_region = self.view.line(sel.b) line_start, line_end = line_region.begin(), line_region.end() start = expression.find_match(self.view, sel.b, '[^\w$@]|^', {'backward': True, 'range': [line_start, sel.b], 'string': True, 'nesting': True, 'comment': True}) end = expression.find_match(self.view, sel.b, '[^\w?!]|$', {'range': [sel.b, line_end], 'string': True, 'nesting': True, 'comment': True}) if start == None or end == None or start == end: return variable = [start.end(0) + line_start, end.start(0) + sel.b] entries = find_entries(self.view, variable) if len(entries) == 0: return self.view.sel().clear() for entry in entries: self.view.sel().add(sublime.Region(*entry))
def _set_method_starts(view, edit, current_method): header = [current_method['start'], current_method['body_start']] match = expression.find_match(view, current_method['body_start'], r'(\s*)$', {'range': header, 'backward': True}) if match == None: return match shift = match.start(1) + current_method['start'] header[1] = shift match = expression.find_match(view, shift, r'^(\s+)\n[ \t]*\S', {'nesting': True, 'range': [shift, current_method['end']]}) if match == None: return replacement = '' if "\n" in view.substr(sublime.Region(*header)): replacement = "\n" region = sublime.Region(match.start(1) + shift, match.end(1) + shift) if match.group(1) == replacement: return view.replace(edit, region, replacement)
def _get_args(self, method, snippet_index): match_options = { 'range': [method['start'], method['end']], 'nesting': True, } args_point = expression.find_match(self.view, method['start'], r'(?:\()', match_options) args = [] if args_point != None: tokens_point = method['start'] + args_point.end(0) tokens = statement.get_tokens(self.view, tokens_point) for token in tokens: args.append(self.view.substr(sublime.Region(*token))) # f*****g self if 'python' in self.view.scope_name(method['start']) and 'self' in args: args.remove('self') args_prepared = [] for arg in args: snippet_index += 1 args_prepared.append('${' + str(snippet_index) + ':' + re.search(r'^\S*', arg).group(0) + '}') return args_prepared
def is_lesser_indentation_required(self, entries): if len(entries) == 1: return False start_line, _ = self.view.rowcol(entries[0][0]) lines = self.view.substr( self.view.line( sublime.Region(entries[0][0], entries[len(entries) - 1][1]))) indentation = None for index, line in enumerate(lines.split("\n")): if line.strip() == '': continue point = self.view.text_point(start_line + index, 0) in_nesting = expression.find_match(self.view, point, r'[\])]', {'range': [point, point + 512]}) if in_nesting: continue if statement.is_arguments(self.view, point): continue current = len(self.get_indentation(line)) if indentation == None: # first overlap indentation = current if current < indentation: return True return False
def get(view, sel, values): scope = None method = method_parser.extract_method(view, sel.b) if method != None: scope = [method['start'], method['end']] else: scope = [0, view.size()] match = expression.find_match(view, sel.b, r'[^=][\*\/+\-]?=(?!=)\s*', { 'backward': True, 'range': [scope[0], sel.b] }) if match == None: return None container = statement.get_statement(view, scope[0] + match.start(0)) if container == None: return None variables = local_variable.find_variables(view, container[0], False, container) if variables == None or len(variables) == 0: return return view.substr(sublime.Region(*variables[0]))
def _get_definition_info(self): selection = self.view.sel()[0] if selection.empty(): token_info = statement.get_token(self.view, selection.b) token = None if token_info != None: _, token = token_info if token == None or '(' not in self.view.substr(sublime.Region(*token)): token = statement.get_parent_token(self.view, selection.b, r'\(') if token == None: return 'name', None return 'name', token else: statement_1 = statement.get_statement(self.view, selection.begin()) statement_2 = statement.get_statement(self.view, selection.end()) if statement_1 == None or statement_2 == None: return None if statement_1 != statement_2 or expression.find_match(self.view, statement_1[0], r'\{', {'range': statement_1, 'string': False}) != None: return 'body', [statement_1[0], statement_2[1]] else: return 'name', statement_1
def get(view, sel, values): scope = None method = method_parser.extract_method(view, sel.b) if method != None: _, method = method scope = [method['start'], method['end']] else: scope = [0, view.size()] match = expression.find_match(view, sel.b, r'[^=][\*\/+\-]?=(?!=)\s*', {'backward': True, 'range': [scope[0], sel.b]}) if match == None: return None container = statement.get_statement(view, scope[0] + match.start(0)) if container == None: return None variables = local_variable.find_variables(view, container[0], False, container) if variables == None or len(variables) == 0: return return view.substr(sublime.Region(*variables[0]))
def is_lesser_indentation_required(self, entries): if len(entries) == 1: return False start_line, _ = self.view.rowcol(entries[0][0]) lines = self.view.substr(self.view.line(sublime.Region(entries[0][0], entries[len(entries) - 1][1]))) indentation = None for index, line in enumerate(lines.split("\n")): if line.strip() == '': continue point = self.view.text_point(start_line + index, 0) in_nesting = expression.find_match(self.view, point, r'[\])]', {'range': [point, point + 512]}) if in_nesting: continue if statement.is_arguments(self.view, point): continue current = len(self.get_indentation(line)) if indentation == None : # first overlap indentation = current if current < indentation: return True return False
def get_partial_statement(view, point): nesting = expression.get_nesting(view, point) as_argument = (nesting != None and statement.get_statement( view, nesting[0]) == statement.get_statement(view, nesting[1])) if as_argument: argument = statement.get_argument(view, point) if argument != None: _, argument = argument argument = prepare_argument(view, argument, point) if argument == None or is_variable(view, argument): token = statement.get_parent_token(view, point) return prepare_argument(view, token, point) return argument else: tokens = statement.get_tokens(view, point) if tokens == None: return None _, token = statement.get_token(view, point, tokens) if token == None or not is_variable(view, token): return token tokens_range = [tokens[0][0], tokens[len(tokens) - 1][1]] assignment_match = expression.find_match(view, tokens_range[0], r'=[^=]\s*', {'range': tokens_range}) if assignment_match != None: tokens_range[0] += assignment_match.end(0) return tokens_range
def _skip_parenthesis(view, point): if '(' not in view.substr(sublime.Region(point, view.line(point).b)): return point match = expression.find_match(view, point, r'\)', {'range': [point, point + 512], 'nesting': 'end'}) if match == None: return point return match.end(0) + point
def _set_method_endings(view, edit, current_method): if current_method['end'] == current_method['body_end']: return shift = current_method['body_start'] options = {'nesting': True, 'range': [shift, current_method['end']]} match = expression.find_match(view, shift, r'(\s+)\n\s*\S+$', options) if match == None: return region = sublime.Region(match.start(1) + shift, match.end(1) + shift) view.replace(edit, region, "")
def extract_query(view, sel): if not sel.empty(): query_start, query_end = sel.begin(), sel.end() else: point = sel.a if view.substr(sublime.Region(point - 1, point)) == ';': point -= 1 if view.substr(sublime.Region(point - 2, point)) == '\G': point -= 2 query_start = (expression.find_match(view, point, r'(\\g|\\G|;|^)', {'backward': True}).end(1)) query_end = (expression.find_match(view, point, r'(\\g|\\G|;|$)'). end(1)) query = view.substr(sublime.Region(query_start, query_end)) query_start += len(re.search('^(\s*)', query).group(1)) return query, query_start, query_end
def _get_new_line(view, container, ruler, bad_case = False): position = _get_new_line_for_string(view, ruler) if position != None: return position if bad_case: match = expression.find_match(view, ruler, r'[{(]', {'backward': True, 'range': container, 'nesting': True}) if match != None: if match.group(1) != '' and match.group(1) != None: return container[0] + match.end(1) else: return container[0] + match.end(0) else: match = expression.find_match(view, ruler, DELIMETER, { 'backward': True, 'range': container, 'nesting': True}) if match != None: return container[0] + match.end(0) return None
def _get_class_name(self, view, point): expr = r'(?:\n|^)\s*((class|module|interface).*)\n' text = view.substr(sublime.Region(0, view.size())) match = expression.find_match(view, point.begin(), expr, { 'backward': True, 'nesting': True, 'options': re.IGNORECASE, }) if match == None: return None return match.group(1)
def find_this_arg(view, star=None): expr = "func\s*\((.*?)\)" result = expression.find_match( view, view.sel()[0].a, expr, {"backward": True, "scope": "keyword.(control|other).go"} ) result = result or expression.find_match( view, view.sel()[0].a, expr, {"backward": False, "scope": "keyword.(control|other).go"} ) result = result or expression.find_match( view, 0, "type (\w+)", {"backward": False, "scope": "keyword.(control|other).go"} ) if result != None: result = result.group(1) else: result = "" if star == True: result = re.sub(r"(\s)(\w+)$", "\\1*\\2", result) elif star == False: result = re.sub(r"(\s)\*(\w+)$", "\\1\\2", result) return result
def toggle(view, edit, selection): token = _get_token(view, selection) if token == None: return delimeter = expression.find_match(view, token[0], r'(?:\.|->)\s*', {'range': token}) if delimeter == None: return if "\n" in delimeter.group(0): join(view, edit, selection) else: unjoin(view, edit, selection)
def _get_parts(view, point): container = statement.get_root_statement(view, point) if container == None: return None assignment_match = expression.find_match(view, container[0], r'\s+=\s+', {'range': container}) if assignment_match == None: return None part_1 = [container[0], container[0] + assignment_match.start(0)] part_2 = [container[0] + assignment_match.end(0), container[1]] return part_1, part_2
def _skip_parenthesis_or_return_args(view, point): new_point = point while True: new_point = _skip_parenthesis(view, point) if new_point == point: break point = new_point if 'source.go' in view.scope_name(point): match = expression.find_match(view, point, r'{', {'range': [point, point + 128]}) if match != None: new_point += match.end(0) return new_point
def _is_definition_exist(self, snippets, places = None): for snippet in snippets: definition = re.escape(snippet['contents']) if places == None: places = self.get_places() for place in places: shift = place['range'][0] match = expression.find_match(self.view, place['range'][0], definition, {'range': place['range']}) if match != None: return True return False
def prepare_argument(view, argument, point): if argument == None: return None hash_match = expression.find_match(view, argument[0], r'((\S|^)(?<!:):(?!:)|=>|=)\s*', {'range': argument}) if hash_match == None: return argument delimeter_point = argument[0] + hash_match.end(0) if point >= delimeter_point: return None argument[0] = delimeter_point return argument
def on_query_context(self, view, key, operator, operand, match_all): if key != 'in_nesting': return None result = True for sel in view.sel(): in_nesting = expression.find_match(view, sel.b, r'[\)\}\]]', {'range': [sel.b, min(sel.b + 1024 * 5, view.size())]}) != None if operator == sublime.OP_EQUAL: result = result and in_nesting == operand elif operator == sublime.OP_NOT_EQUAL: result = result and in_nesting != operand else: raise Exception('Operator "' + operator + '" is not supported') if not match_all: return result return result
def on_query_context(self, view, key, operator, operand, match_all): if key != 'in_nesting': return None result = True for sel in view.sel(): in_nesting = expression.find_match( view, sel.b, r'[\)\}\]]', {'range': [sel.b, min(sel.b + 1024 * 5, view.size())]}) != None if operator == sublime.OP_EQUAL: result = result and in_nesting == operand elif operator == sublime.OP_NOT_EQUAL: result = result and in_nesting != operand else: raise Exception('Operator "' + operator + '" is not supported') if not match_all: return result return result