def run(self, edit): context = Context.get_context(self.view) current_scope = self.view.scope_name(Selection.get_position(self.view)) trigger = resolve_trigger(self.view, Query) print("FOUND TRIGGER", trigger) self.content = "<h4>FuzzyFilepath - context evaluation</h4>" self.add("filepath valid", context.get("valid_needle")) self.add("context valid", context.get("is_valid")) self.content += "<br>" self.add("path", context.get("needle")) self.add("prefix", context.get("prefix")) self.add("property", context.get("style")) self.add("tag", context.get("tagName")) self.add("word", context.get("word")) self.content += "<br>" self.add("scope", current_scope) self.content += "<br>" self.content += "<h5>Trigger</h5>" if trigger is False: self.content += "no trigger could be found matching the given context" else: for key in trigger: self.add(key, trigger.get(key)) self.show()
def run(self, edit): print("show context") context = Context.get_context(self.view) current_scope = self.view.scope_name(Selection.get_position(self.view)) self.content = "<h4>FuzzyFilepath - context evaluation</h4>" self.add("filepath valid", context.get("valid_needle")) self.add("context valid", context.get("is_valid")) self.content += "<br>" self.add("path", context.get("needle")) self.add("prefix", context.get("prefix")) self.add("property", context.get("style")) self.add("tag", context.get("tagName")) self.add("word", context.get("word")) self.content += "<br>" self.add("scope", current_scope) self.show()
def run(self, edit): current_directory = os.path.dirname(self.view.file_name()) context = Context.get_context(self.view) if context.get("valid") is False: return log(ID, "abort, no valid path given:", context.get("needle")) path = context.get("needle") project_folder = state.get_project_directory() if not (path and project_folder): return log(ID, "path or project invalid", path, project_folder) is_relative = Path.is_relative(path) if is_relative: path = Path.get_absolute_path(current_directory, path) path = re.sub(project_folder, "", path) path = re.sub("^[\\\\/\.]", "", path) realpath = path # cleanup string, in case there are special characters for a path # e.g. webpack uses ~, which is not part of path path = re.sub("[^A-Za-z0-9 \-_\\\\/.%?#]*", "", path) files = state.find_file(path) if len(files) == 0: # it may be an uncached file, try to open it by using the real path if self.filepath_exists(os.path.join(project_folder, realpath)): return self.open_file(project_folder, realpath) return log(ID, "failed finding file", path, "it is not within the cache and not a realpath") if len(files) == 1: self.open_file(project_folder, files[0]) else: # if javascript, search for index.js current_scope = self.view.scope_name(Selection.get_position(self.view)) if re.search("\.js ", current_scope): for file in files: if "index.js" in file: return self.open_file(project_folder, file) self.files = files self.project_folder = project_folder self.view.show_popup_menu(files, self.select_file)
def run(self, edit): current_directory = os.path.dirname(self.view.file_name()) context = Context.get_context(self.view) if context.get("valid") is False: return log(ID, "abort, no valid path given:", context.get("needle")) path = context.get("needle") project_folder = state.get_project_directory() if not (path and project_folder): return log(ID, "path or project invalid", path, project_folder) is_relative = Path.is_relative(path) if is_relative: path = Path.get_absolute_path(current_directory, path) path = re.sub(project_folder, "", path) path = re.sub("^[\\\\/\.]", "", path) files = state.find_file(path) if len(files) == 0: return log(ID, "failed finding file", path) if len(files) == 1: self.open_file(project_folder, files[0]) else: # if javascript, search for index.js current_scope = self.view.scope_name( Selection.get_position(self.view)) if re.search("\.js ", current_scope): for file in files: if "index.js" in file: return self.open_file(project_folder, file) self.files = files self.project_folder = project_folder self.view.show_popup_menu(files, self.select_file)
def run(self, edit): current_directory = os.path.dirname(self.view.file_name()) context = Context.get_context(self.view) if context.get("valid") is False: return log(ID, "abort, no valid path given:", context.get("needle")) path = context.get("needle") project = ProjectManager.get_current_project() if not (path and project): return log(ID, "path or project invalid", path, project) is_relative = Path.is_relative(path) if is_relative: path = Path.get_absolute_path(current_directory, path) path = re.sub(project.get_directory(), "", path) path = re.sub("^[\\\\/\.]", "", path) files = project.find_file(path) if len(files) == 0: return log(ID, "failed finding file", path) if len(files) == 1: self.open_file(project.get_directory(), files[0]) else: # if javascript, search for index.js current_scope = self.view.scope_name(Selection.get_position(self.view)) if re.search("\.js ", current_scope): for file in files: if "index.js" in file: return self.open_file(project.get_directory(), file) self.files = files self.project_folder = project.get_directory() self.view.show_popup_menu(files, self.select_file)
def get_context(view): error = False valid = True valid_needle = True position = Selection.get_position(view) # regions word_region = view.word(position) line_region = view.line(position) pre_region = sublime.Region(line_region.a, word_region.a) post_region = sublime.Region(word_region.b, line_region.b) # text line = view.substr(line_region) word = view.substr(word_region) pre = view.substr(pre_region) post = view.substr(post_region) error = re.search("[" + NEEDLE_INVALID_CHARACTERS + "]", word) needle_region = view.word(position) # grab everything in 'separators' needle = "" separator = False pre_match = "" # search for a separator before current word, i.e. <">path/to/<position> pre_quotes = re.search( "([" + NEEDLE_SEPARATOR_BEFORE + "])([^" + NEEDLE_SEPARATOR + "]*)$", pre) if pre_quotes: needle += pre_quotes.group(2) + word separator = pre_quotes.group(1) pre_match = pre_quotes.group(2) needle_region.a -= len(pre_quotes.group(2)) else: # use whitespace as separator pre_quotes = re.search("(\s)([^" + NEEDLE_SEPARATOR + "\s]*)$", pre) if pre_quotes: needle = pre_quotes.group(2) + word separator = pre_quotes.group(1) pre_match = pre_quotes.group(2) needle_region.a -= len(pre_quotes.group(2)) if pre_quotes: post_quotes = re.search("^([" + NEEDLE_SEPARATOR_AFTER + "]*)", post) if post_quotes: needle += post_quotes.group(1) needle_region.b += len(post_quotes.group(1)) else: logger.verbose(ID, "no post quotes found => invalid") valid = False elif not re.search("[" + NEEDLE_INVALID_CHARACTERS + "]", needle): needle = pre + word needle_region.a = pre_region.a else: needle = word # grab prefix prefix_region = sublime.Region(line_region.a, pre_region.b - len(pre_match) - 1) prefix_line = view.substr(prefix_region) # # print("prefix line", prefix_line) #define? (["...", "..."]) -> before? # before: ABC =:([ prefix = re.search( "\s*([" + NEEDLE_CHARACTERS + "]+)[" + DELIMITER + "]*$", prefix_line) if prefix is None: # validate array, like define(["...", ".CURSOR."]) prefix = re.search( "^\s*([" + NEEDLE_CHARACTERS + "]+)[" + DELIMITER + "]+", prefix_line) if prefix: # print("prefix:", prefix.group(1)) prefix = prefix.group(1) tag = re.search("<\s*([" + NEEDLE_CHARACTERS + "]*)\s*[^>]*$", prefix_line) if tag: tag = tag.group(1) # print("tag:", tag) propertyName = re.search( "[\s\"\'']*([" + NEEDLE_CHARACTERS + "]*)[\s\"\']*\:[^\:]*$", prefix_line) if propertyName: propertyName = propertyName.group(1) # print("style:", style) if separator is False: logger.verbose(ID, "separator undefined => invalid", needle) valid_needle = False valid = False elif re.search("[" + NEEDLE_INVALID_CHARACTERS + "]", needle): logger.verbose(ID, "invalid characters in needle => invalid", needle) valid_needle = False valid = False elif prefix is None and separator.strip() == "": logger.verbose(ID, "prefix undefined => invalid", needle) valid = False return { "is_valid": valid, "valid_needle": valid_needle, "needle": needle, "prefix": prefix, "tagName": tag, "style": propertyName, "region": needle_region, "word": word, # really do not use any of this "error": error }
def get_context(view): error = False valid = True valid_needle = True position = Selection.get_position(view) # regions word_region = view.word(position) line_region = view.line(position) pre_region = sublime.Region(line_region.a, word_region.a) post_region = sublime.Region(word_region.b, line_region.b) # text line = view.substr(line_region) word = view.substr(word_region) pre = view.substr(pre_region) post = view.substr(post_region) error = re.search("[" + NEEDLE_INVALID_CHARACTERS + "]", word) needle_region = view.word(position) # grab everything in 'separators' needle = "" separator = False pre_match = "" # search for a separator before current word, i.e. <">path/to/<position> pre_quotes = re.search("(["+NEEDLE_SEPARATOR_BEFORE+"])([^"+NEEDLE_SEPARATOR+"]*)$", pre) if pre_quotes: needle += pre_quotes.group(2) + word separator = pre_quotes.group(1) pre_match = pre_quotes.group(2) needle_region.a -= len(pre_quotes.group(2)) else: # use whitespace as separator pre_quotes = re.search("(\s)([^"+NEEDLE_SEPARATOR+"\s]*)$", pre) if pre_quotes: needle = pre_quotes.group(2) + word separator = pre_quotes.group(1) pre_match = pre_quotes.group(2) needle_region.a -= len(pre_quotes.group(2)) if pre_quotes: post_quotes = re.search("^(["+NEEDLE_SEPARATOR_AFTER+"]*)", post) if post_quotes: needle += post_quotes.group(1) needle_region.b += len(post_quotes.group(1)) else: print("no post quotes found => invalid") valid = False elif not re.search("["+NEEDLE_INVALID_CHARACTERS+"]", needle): needle = pre + word needle_region.a = pre_region.a else: needle = word # grab prefix prefix_region = sublime.Region(line_region.a, pre_region.b - len(pre_match) - 1) prefix_line = view.substr(prefix_region) # # print("prefix line", prefix_line) #define? (["...", "..."]) -> before? # before: ABC =:([ prefix = re.search("\s*(["+NEEDLE_CHARACTERS+"]+)["+DELIMITER+"]*$", prefix_line) if prefix is None: # validate array, like define(["...", ".CURSOR."]) prefix = re.search("^\s*(["+NEEDLE_CHARACTERS+"]+)["+DELIMITER+"]+", prefix_line) if prefix: # print("prefix:", prefix.group(1)) prefix = prefix.group(1) tag = re.search("<\s*(["+NEEDLE_CHARACTERS+"]*)\s*[^>]*$", prefix_line) if tag: tag = tag.group(1) # print("tag:", tag) propertyName = re.search("[\s\"\'']*(["+NEEDLE_CHARACTERS+"]*)[\s\"\']*\:[^\:]*$", prefix_line) if propertyName: propertyName = propertyName.group(1) # print("style:", style) if separator is False: # print("context", "separator undefined => invalid", needle) valid_needle = False valid = False elif re.search("["+NEEDLE_INVALID_CHARACTERS+"]", needle): # print("context", "invalid characters in needle => invalid", needle) valid_needle = False valid = False elif prefix is None and separator.strip() == "": # print("context", "prefix undefined => invalid", needle) valid = False return { "is_valid": valid, "valid_needle": valid_needle, "needle": needle, "prefix": prefix, "tagName": tag, "style": propertyName, "region": needle_region, "word": word, # really do not use any of this "error": error }