public
Last active

Autocomplete Filenames in Sublime Text 2

  • Download Gist
autofilename.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import sublime, sublime_plugin, os, re
 
class FileNameComplete(sublime_plugin.EventListener):
 
def on_query_completions(self, view, prefix, locations):
completions = []
sel = view.sel()[0].a
 
if "string" in view.syntax_name(sel):
pass
elif "./" in view.substr(sublime.Region(sel-3,sel)):
pass
else:
return []
 
this_file = view.file_name()
dir_len = this_file.rfind('/') #(for OSX)
 
if not dir_len > 0:
dir_len = this_file.rfind('\\') #(for Windows)
this_dir = this_file[:(dir_len + 1)] # + 1 for the '/'
this_dir += view.substr(view.extract_scope(sel-1)).replace('\"','') # strings are quoted
 
print this_dir
 
dir_files = os.listdir(this_dir)
for d in dir_files:
if not '.' in d:
d += '/'
completions.append(d)
 
return [(x, x) for x in list(set(completions))]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.