Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Created May 16, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rapptz/c92205921cad7be0de74 to your computer and use it in GitHub Desktop.
Save Rapptz/c92205921cad7be0de74 to your computer and use it in GitHub Desktop.
import sublime, sublime_plugin
import os.path
import platform
def compare_file_names(x, y):
if platform.system() == 'Windows' or platform.system() == 'Darwin':
return x.lower() == y.lower()
else:
return x == y
class AdvancedSwitchFileCommand(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(AdvancedSwitchFileCommand, self).__init__(*args, **kwargs)
self.directories = [ '.', 'src', 'include', 'source' ]
self.sources = [ '.cpp', '.c++', '.cxx', '.c', '.cc' ]
self.headers = [ '.hpp', '.h++', '.hxx', '.h', '.hh', '.inl', '.ipp' ]
def is_header(self, ext):
return ext.lower() in self.headers
def is_source(self, ext):
return ext.lower() in self.sources
def run(self, directories=[]):
if not self.window.active_view():
return
fname = self.window.active_view().file_name()
if not fname:
return
print(fname)
path = os.path.dirname(fname)
filename = os.path.basename(fname)
root = os.path.dirname(path)
base, ext = os.path.splitext(filename)
dirs = [os.path.abspath(os.path.join(root, p)) if p != '.' else path for p in self.directories]
done = False # exit out of nested loop
search = []
if self.is_source(ext):
search = self.headers
elif self.is_header(ext):
search = self.sources
if len(search) != 0:
for d in dirs:
if done:
break
for extension in search:
p = os.path.join(d, base + extension)
if os.path.exists(p):
self.window.open_file(p)
done = True
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment