From 736dca4f2862a64f9aaa49d1df2b9a4f1564d415 Mon Sep 17 00:00:00 2001 From: Santiago Herrera Date: Mon, 21 Aug 2017 10:57:09 -0500 Subject: [PATCH 1/3] Select line number --- open_include.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/open_include.py b/open_include.py index f62f08b..1c57067 100644 --- a/open_include.py +++ b/open_include.py @@ -419,6 +419,16 @@ def try_open(self, window, maybe_path): debug_info('Trying to open: ' + maybe_path) + row_regex = re.compile(r':(\d+)(?:-(\d+))?$') + row_search_result = row_regex.search(maybe_path) + rows = None + if row_search_result is not None: + maybe_path = row_regex.sub('', maybe_path) + row_start = row_end = int(row_search_result.group(1)) + if row_search_result.group(2) is not None: + row_end = int(row_search_result.group(2)) + rows = [row_start, row_end] + path_normalized = normalize(maybe_path) if path_normalized in cache['checked']: return False @@ -448,7 +458,7 @@ def try_open(self, window, maybe_path): desktop.open(maybe_path) else: # Open within ST - self.open(window, maybe_path) + self.open(window, maybe_path, rows = rows) elif maybe_path and ( os_is_dir(maybe_path) or os_is_dir('\\' + maybe_path) ) and not cache['folder'] and cache['folder_save']: # Walkaround for UNC path @@ -521,12 +531,24 @@ def read_url_on_done(self, content, content_type): elif content_type == 'text/xml' or content_type == 'application/xml': view.settings().set('syntax', 'Packages/XML/XML.tmLanguage') - def open(self, window, path): + def open(self, window, path, rows = None): if get_setting('in_secondary_colum', False): window.run_command('set_layout', {"cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]}) window.focus_group(1) window.open_file(path) + if rows is not None: + start_row, end_row = min(rows), max(rows) + + view = window.active_view() + start_point = view.text_point(start_row - 1, 0) + end_point = view.text_point(end_row, 0) - 1 + + selection = view.sel() + selection.clear() + selection.add(sublime.Region(start_point, end_point)) + view.show(view.text_point(start_row - 1, 0)) + class OpenIncludeFindInFileGoto(): def run(self, view): line_no = self.get_line_no(view) From aeba0c47c56c1f74a037bbf61466300250d7a1c7 Mon Sep 17 00:00:00 2001 From: Santiago Date: Sun, 23 May 2021 09:37:50 -0500 Subject: [PATCH 2/3] Open word immediately under cursor --- open_include.py | 80 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/open_include.py b/open_include.py index 1c57067..ff30557 100644 --- a/open_include.py +++ b/open_include.py @@ -121,6 +121,34 @@ class OpenIncludeThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) + def word_from_line(self, line, col): + left_string = '' + if col > 0: + left_string = line[:col] + + right_string = '' + if col < len(line): + right_string = line[col:] + + left_string_len = len(left_string) + right_string_len = len(right_string) + + if left_string_len <= 0 and right_string_len <= 0: + return '' + elif left_string_len > 0 and right_string_len <= 0: + return re.search(r'([^\s]+)$', left_string).group(1) + elif left_string_len <= 0 and right_string_len > 0: + return re.search(r'^([^\s]+)', right_string).group(1) + elif left_string_len > 0 and right_string_len > 0: + if re.search(r'\s', left_string[-1]) and re.search(r'\s', right_string[0]): + return '' + elif not re.search(r'\s', left_string[-1]) and re.search(r'\s', right_string[0]): + return re.search(r'([^\s]+)$', left_string).group(1) + elif re.search(r'\s', left_string[-1]) and not re.search(r'\s', right_string[0]): + return re.search(r'^([^\s]+)', right_string).group(1) + else: + return re.search(r'([^\s]+)$', left_string).group(1) + re.search(r'^([^\s]+)', right_string).group(1) + def run(self): global cache debug_info('-- running --') @@ -169,6 +197,17 @@ def run(self): self.open(window, file_to_open) opened = True + # immediately under cursor if there is no selected text + if not opened and region.begin() == region.end(): + (row,col) = view.rowcol(region.begin()) + line = view.substr(view.line(region.begin())) or '' + word = self.word_from_line(line, col) + if len(word) > 0: + opened = self.resolve_path(window, view, word) + + if not opened: + opened = self.try_open_folder(word) + # selection expanded to full lines if not opened: expanded_lines = view.substr(sublime.Region(view.line(region.begin()).begin(), view.line(region.end()).end())) @@ -245,15 +284,24 @@ def expand_paths_with_extensions(self, window, view, paths): for path in paths: if path.startswith('http'): continue + + path, row_start, row_end = self.rows_in_path(path) + + row_append_str = '' + if row_start is not None: + row_append_str += ':%s' % row_start + if row_end is not None and row_end != row_start: + row_append_str += '-%s' % row_end + extension_original = os.path.splitext(path)[1] for extension in extensions: if not extension_original: subs = path.replace('\\', '/').split('/') subs[-1] = re.sub('"|\'', '', subs[-1]); subs[-1] = extension.get('prefix', '') + subs[-1] + extension.get('extension', '') - path_add.append(os.path.join(*subs).replace('\\', '/')) + path_add.append(os.path.join(*subs).replace('\\', '/') + row_append_str) - path_add.append(extension.get('prefix', '') + path + extension.get('extension', '')) + path_add.append(extension.get('prefix', '') + path + extension.get('extension', '') + row_append_str) return unique(paths + path_add) @@ -315,7 +363,8 @@ def resolve_path(self, window, view, paths, skip_folders = False): except: try: paths_decoded = urllib.parse.unquote(paths) - paths += '\n' + paths_decoded + if paths != paths_decoded: + paths += '\n' + paths_decoded except: pass @@ -327,8 +376,6 @@ def resolve_path(self, window, view, paths, skip_folders = False): if not path.startswith('http'): # remove quotes paths += '\n' + re.sub(';', '', path) - # remove :row:col - paths += '\n' + re.sub('(\:[0-9]*)+$', '', path).strip() # replace . for / paths += '\n' + path.replace('./', '.').replace('.', '/') # replace :: for / @@ -340,7 +387,7 @@ def resolve_path(self, window, view, paths, skip_folders = False): paths += '\n' + os.path.expandvars(path) - paths = paths.strip().split('\n') + paths = list(set(paths.strip().split('\n'))) if paths[0].startswith('http'): return self.try_open(window, paths[0]) @@ -413,20 +460,27 @@ def create_path_relative_to_folder(self, window, view, maybe_path, path): maybe_path_tpm = self.resolve_relative(maybe_path, path) return self.try_open(window, maybe_path_tpm) + def rows_in_path(self, path): + row_regex = re.compile(r':(\d+)(?:-(\d+))?$') + row_search_result = row_regex.search(path) + row_start = row_end = None + if row_search_result is not None: + path = row_regex.sub('', path) + row_start = row_end = int(row_search_result.group(1)) + if row_search_result.group(2) is not None: + row_end = int(row_search_result.group(2)) + + return [path, row_start, row_end] + # try opening the resouce def try_open(self, window, maybe_path): global cache debug_info('Trying to open: ' + maybe_path) - row_regex = re.compile(r':(\d+)(?:-(\d+))?$') - row_search_result = row_regex.search(maybe_path) + maybe_path, row_start, row_end = self.rows_in_path(maybe_path) rows = None - if row_search_result is not None: - maybe_path = row_regex.sub('', maybe_path) - row_start = row_end = int(row_search_result.group(1)) - if row_search_result.group(2) is not None: - row_end = int(row_search_result.group(2)) + if row_start is not None: rows = [row_start, row_end] path_normalized = normalize(maybe_path) From 063dd1b0be19fe3f721e47cda58491793f5ecbc1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Sun, 23 May 2021 10:29:09 -0500 Subject: [PATCH 3/3] Open at end_point --- open_include.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/open_include.py b/open_include.py index ff30557..7dbfadb 100644 --- a/open_include.py +++ b/open_include.py @@ -601,7 +601,7 @@ def open(self, window, path, rows = None): selection = view.sel() selection.clear() selection.add(sublime.Region(start_point, end_point)) - view.show(view.text_point(start_row - 1, 0)) + view.show(end_point) class OpenIncludeFindInFileGoto(): def run(self, view):