Skip to content

Commit 9d6a8fb

Browse files
author
Liam Cain
committed
Big improvements. Should work faster and feel more fluid.
1 parent 2c35da2 commit 9d6a8fb

File tree

7 files changed

+162
-134
lines changed

7 files changed

+162
-134
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.pyc
22
*.html
33
*.css
4-
*.png
4+
*.png
5+
.DS_STORE

Default.sublime-keymap

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
[
2-
{ "keys": ["tab"], "command": "run_macro_file", "args": {"file": "Packages/AutoFileName/commit-and-trim.sublime-macro"},
2+
{ "keys": ["tab"], "command": "insert_dimensions",
33
"context":
44
[
55
{ "key": "setting.auto_complete_commit_on_tab" },
6-
{ "operand": true, "operator": "equal", "match_all": true, "key": "afn_commit-n-trim" }
6+
{ "key": "auto_complete_visible", "operator": "equal", "operand": true },
7+
{ "key": "afn_insert_dimensions", "operator": "equal", "operand": true }
78
]
89
},
9-
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/AutoFileName/commit-and-trim.sublime-macro"},
10+
{ "keys": ["enter"], "command": "insert_dimensions",
1011
"context":
1112
[
1213
{ "key": "setting.auto_complete_commit_on_tab", "operator": "equal", "operand": false },
13-
{ "operand": true, "operator": "equal", "match_all": true, "key": "afn_commit-n-trim" }
14+
{ "key": "auto_complete_visible", "operator": "equal", "operand": true },
15+
{ "key": "afn_insert_dimensions", "operator": "equal", "operand": true }
16+
]
17+
},
18+
{ "keys": ["backspace"], "command": "reload_auto_complete",
19+
"context":
20+
[
21+
{ "key": "afn_deleting_slash", "operator": "equal", "operand": true }
1422
]
1523
}
1624
]

afn_img_utils.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

autofilename.py

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,125 @@
11
import sublime
22
import sublime_plugin
33
import os
4-
import glob
5-
from afn_img_utils import get_image_size
4+
from getimageinfo import getImageInfo
65

7-
class AfnCommitCompCommand(sublime_plugin.TextCommand):
6+
class InsertDimensionsCommand(sublime_plugin.TextCommand):
87
this_dir = ''
98

109
def insert_dimension(self,edit,dim,name,tag_scope):
1110
view = self.view
1211
sel = view.sel()[0].a
1312
if name in view.substr(tag_scope):
1413
reg = view.find('(?<='+name+'\=)\s*\"\d{1,5}', tag_scope.a)
15-
view.replace(edit, reg, '"'+str(dim.get(name)))
14+
view.replace(edit, reg, '"'+str(dim))
1615
else:
17-
dimension = str(dim.get(name))
16+
dimension = str(dim)
1817
view.insert(edit, sel+1, ' '+name+'="'+dimension+'"')
1918

2019
def run(self, edit):
2120
view = self.view
21+
view.run_command("commit_completion")
2222
sel = view.sel()[0].a
23-
if not 'string' in view.scope_name(sel): return
23+
if not 'html' in view.scope_name(sel): return
2424
scope = view.extract_scope(sel-1)
2525
tag_scope = view.extract_scope(scope.a-1)
26-
region = sublime.Region(sel, scope.b-1)
27-
view.erase(edit, region)
2826

29-
path = view.substr(view.extract_scope(sel-1))
27+
path = view.substr(scope)
3028
if path.startswith(("'","\"","(")):
3129
path = path[1:-1]
3230

33-
path = path[path.rfind('/'):]
31+
path = path[path.rfind('/'):] if '/' in path else ''
3432
full_path = self.this_dir + path
33+
34+
print full_path
35+
3536
if '<img' in view.substr(tag_scope) and path.endswith(('.png','.jpg','.jpeg','.gif')):
3637
with open(full_path,'rb') as r:
37-
read_data = r.read()
38-
dim = get_image_size(read_data)
39-
self.insert_dimension(edit,dim,'width',tag_scope)
40-
self.insert_dimension(edit,dim,'height',tag_scope)
41-
38+
read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
39+
con_type, w, h = getImageInfo(read_data)
40+
self.insert_dimension(edit,w,'width',tag_scope)
41+
self.insert_dimension(edit,h,'height',tag_scope)
42+
43+
class ReloadAutoCompleteCommand(sublime_plugin.TextCommand):
44+
def complete(self):
45+
self.view.run_command('auto_complete',
46+
{'disable_auto_insert': True,
47+
'next_completion_if_showing': False})
48+
49+
def run(self,edit):
50+
self.view.run_command('hide_auto_complete')
51+
self.view.run_command('left_delete')
52+
sublime.set_timeout(self.complete, 50)
4253

4354
class FileNameComplete(sublime_plugin.EventListener):
44-
4555
committing_filename = False
4656

47-
def on_query_context(self, view, key, operator, operand, match_all):
48-
if key == "afn_commit-n-trim":
49-
return self.will_commit(view) == operand
57+
def on_activated(self,view):
58+
self.size = view.size()
59+
self.view = view
5060

51-
def scope(self,view,string):
52-
sel = view.sel()[0].a
53-
return string in view.scope_name(sel)
61+
def on_query_context(self, view, key, operator, operand, match_all):
62+
if key == "afn_insert_dimensions":
63+
settings = sublime.load_settings("autofilename.sublime-settings")
64+
return settings.get('afn_insert_dimensions') == operand
65+
if key == "afn_deleting_slash":
66+
sel = view.sel()[0]
67+
valid = sel.empty() and view.substr(sel.a-1) == '/'
68+
return valid == operand
69+
70+
def scope(self,string):
71+
sel = self.view.sel()[0].a
72+
return string in self.view.scope_name(sel)
73+
74+
def at_path_end(self,view):
75+
sel = view.sel()[0]
76+
return (sel.empty() and self.scope('string.end')) or (self.scope('.css') and view.substr(sel.a) == ')')
5477

5578
def on_selection_modified(self,view):
56-
sel = view.sel()[0].a
57-
v = view
58-
if self.scope(v,'string.end') or (self.scope(v,'.css') and ')' in view.substr(sel)):
59-
if view.substr(sel-1) == '/' or len(view.extract_scope(sel)) < 3:
60-
view.run_command('auto_complete',
79+
sel = view.sel()[0]
80+
if self.at_path_end(view):
81+
if view.substr(sel.a-1) == '/' or len(view.extract_scope(sel.a)) < 3:
82+
view.run_command('auto_complete',
6183
{'disable_auto_insert': True,
6284
'next_completion_if_showing': False})
6385

64-
def will_commit(self, view):
65-
if self.committing_filename:
66-
self.committing_filename = False
67-
return True
68-
return False
86+
def on_modified(self,view):
87+
sel = view.sel()[0]
88+
v = view
89+
if self.size > view.size():
90+
if self.at_path_end(view):
91+
if view.substr(sel.a-1) == '/':
92+
view.run_command("hide_auto_complete")
93+
sublime.set_timeout(self.complete, 50)
94+
95+
self.size = view.size()
96+
97+
def complete(self):
98+
self.view.run_command('auto_complete',
99+
{'disable_auto_insert': True,
100+
'next_completion_if_showing': False})
69101

70102
def fix_dir(self,sdir,fn):
71103
if fn.endswith(('.png','.jpg','.jpeg','.gif')):
72-
path = os.path.join(sdir + '/', fn)
104+
path = os.path.join(sdir, fn)
73105
with open(path,'rb') as r:
74-
read_data = r.read()
75-
dim = get_image_size(read_data)
76-
return fn + '\t' + 'w:'+str(dim.get('width'))+" h:"+str(dim.get('height'))
106+
read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
107+
con_type, w, h = getImageInfo(read_data)
108+
return fn+'\t'+'w:'+ str(w) +" h:" + str(h)
77109
return fn
78110

79111
def get_cur_path(self,view,sel):
80112
scope_contents = view.substr(view.extract_scope(sel-1))
81113
cur_path = scope_contents.replace('\r\n', '\n').split('\n')[0]
82114
if cur_path.startswith(("'","\"","(")):
83-
return cur_path[1:-1]
84-
return cur_path
115+
cur_path = cur_path[1:-1]
116+
117+
return cur_path[:cur_path.rfind('/')] if '/' in cur_path else ''
85118

86119
def on_query_completions(self, view, prefix, locations):
87-
SETTINGS = "autofilename.sublime-settings"
88-
is_proj_rel = sublime.load_settings(SETTINGS).get("auto_file_name_use_project_root")
89-
valid_scopes = ["string", "css", "sass", "scss", "less"]
120+
settings = sublime.load_settings("autofilename.sublime-settings")
121+
is_proj_rel = settings.get("afn_use_project_root")
122+
valid_scopes = settings.get("afn_valid_scopes")
90123
sel = view.sel()[0].a
91124
completions = []
92125
backup = []
@@ -99,42 +132,30 @@ def on_query_completions(self, view, prefix, locations):
99132

100133
cur_path = self.get_cur_path(view, sel)
101134

102-
if view.extract_scope(sel-1).b - sel > 1: # if the cursor is not at the end
103-
wild_pos = sel - view.extract_scope(sel-1).a - 1
104-
cur_path = cur_path[:wild_pos] + '*' + cur_path[wild_pos:] + '*'
105-
106-
if is_proj_rel and os.path.isabs(cur_path):
107-
this_dir = sublime.load_settings(SETTINGS).get("afn_proj_root")
108-
cur_path = cur_path[1:]
135+
if is_proj_rel:
136+
this_dir = settings.get("afn_proj_root")
109137
if len(this_dir) < 2:
110138
for f in sublime.active_window().folders():
111139
if f in view.file_name():
112140
this_dir = f
113141
else:
114142
if not view.file_name():
115-
print 'AutoFileName: File not saved.'
116143
backup.insert(0,('AutoFileName: File Not Saved',''))
117144
return backup
118145
this_dir = os.path.split(view.file_name())[0]
119146

120147
this_dir = os.path.join(this_dir, cur_path)
121148

122149
try:
123-
dir_files = []
124-
for f in glob.glob(this_dir):
125-
if os.path.isfile(f):
126-
dir_files.append(os.path.basename(f))
127-
else:
128-
dir_files.extend(os.listdir(f))
129-
130-
for d in list(set(dir_files)):
150+
dir_files = os.listdir(this_dir)
151+
152+
for d in dir_files:
131153
n = d.decode('utf-8')
132154
if n.startswith('.'): continue
133155
if not '.' in n: n += '/'
134156
completions.append((self.fix_dir(this_dir,n), n))
135-
if completions:
136-
self.committing_filename = True
137-
AfnCommitCompCommand.this_dir = this_dir
157+
if completions:
158+
InsertDimensionsCommand.this_dir = this_dir
138159
return completions
139160
except OSError:
140161
print "AutoFileName: could not find " + this_dir

autofilename.sublime-settings

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
//Changing this setting allows for absolute paths on a project level
44
//This is useful for web designers and developers who want to use the
55
//root of their site.
6-
"auto_file_name_use_project_root": false,
6+
"afn_use_project_root": true,
77

88
// Override the project root. Will only work
99
// if "auto_file_name_use_project_root" is true.
10-
"afn_proj_root": ""
10+
"afn_proj_root": "",
11+
12+
"afn_valid_scopes":["string","css","sass","less","scss"],
13+
14+
"afn_insert_dimensions": true
1115
}

commit-and-trim.sublime-macro

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)