Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 84 additions & 8 deletions open_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 --')
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand All @@ -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 /
Expand All @@ -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])
Expand Down Expand Up @@ -413,12 +460,29 @@ 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)

maybe_path, row_start, row_end = self.rows_in_path(maybe_path)
rows = None
if row_start is not None:
rows = [row_start, row_end]

path_normalized = normalize(maybe_path)
if path_normalized in cache['checked']:
return False
Expand Down Expand Up @@ -448,7 +512,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
Expand Down Expand Up @@ -521,12 +585,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(end_point)

class OpenIncludeFindInFileGoto():
def run(self, view):
line_no = self.get_line_no(view)
Expand Down