1
1
import sublime
2
2
import sublime_plugin
3
3
import os
4
- import glob
5
- from afn_img_utils import get_image_size
4
+ from getimageinfo import getImageInfo
6
5
7
- class AfnCommitCompCommand (sublime_plugin .TextCommand ):
6
+ class InsertDimensionsCommand (sublime_plugin .TextCommand ):
8
7
this_dir = ''
9
8
10
9
def insert_dimension (self ,edit ,dim ,name ,tag_scope ):
11
10
view = self .view
12
11
sel = view .sel ()[0 ].a
13
12
if name in view .substr (tag_scope ):
14
13
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 ))
16
15
else :
17
- dimension = str (dim . get ( name ) )
16
+ dimension = str (dim )
18
17
view .insert (edit , sel + 1 , ' ' + name + '="' + dimension + '"' )
19
18
20
19
def run (self , edit ):
21
20
view = self .view
21
+ view .run_command ("commit_completion" )
22
22
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
24
24
scope = view .extract_scope (sel - 1 )
25
25
tag_scope = view .extract_scope (scope .a - 1 )
26
- region = sublime .Region (sel , scope .b - 1 )
27
- view .erase (edit , region )
28
26
29
- path = view .substr (view . extract_scope ( sel - 1 ) )
27
+ path = view .substr (scope )
30
28
if path .startswith (("'" ,"\" " ,"(" )):
31
29
path = path [1 :- 1 ]
32
30
33
- path = path [path .rfind ('/' ):]
31
+ path = path [path .rfind ('/' ):] if '/' in path else ''
34
32
full_path = self .this_dir + path
33
+
34
+ print full_path
35
+
35
36
if '<img' in view .substr (tag_scope ) and path .endswith (('.png' ,'.jpg' ,'.jpeg' ,'.gif' )):
36
37
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 )
42
53
43
54
class FileNameComplete (sublime_plugin .EventListener ):
44
-
45
55
committing_filename = False
46
56
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
50
60
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 ) == ')' )
54
77
55
78
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' ,
61
83
{'disable_auto_insert' : True ,
62
84
'next_completion_if_showing' : False })
63
85
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 })
69
101
70
102
def fix_dir (self ,sdir ,fn ):
71
103
if fn .endswith (('.png' ,'.jpg' ,'.jpeg' ,'.gif' )):
72
- path = os .path .join (sdir + '/' , fn )
104
+ path = os .path .join (sdir , fn )
73
105
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 )
77
109
return fn
78
110
79
111
def get_cur_path (self ,view ,sel ):
80
112
scope_contents = view .substr (view .extract_scope (sel - 1 ))
81
113
cur_path = scope_contents .replace ('\r \n ' , '\n ' ).split ('\n ' )[0 ]
82
114
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 ''
85
118
86
119
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" )
90
123
sel = view .sel ()[0 ].a
91
124
completions = []
92
125
backup = []
@@ -99,42 +132,30 @@ def on_query_completions(self, view, prefix, locations):
99
132
100
133
cur_path = self .get_cur_path (view , sel )
101
134
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" )
109
137
if len (this_dir ) < 2 :
110
138
for f in sublime .active_window ().folders ():
111
139
if f in view .file_name ():
112
140
this_dir = f
113
141
else :
114
142
if not view .file_name ():
115
- print 'AutoFileName: File not saved.'
116
143
backup .insert (0 ,('AutoFileName: File Not Saved' ,'' ))
117
144
return backup
118
145
this_dir = os .path .split (view .file_name ())[0 ]
119
146
120
147
this_dir = os .path .join (this_dir , cur_path )
121
148
122
149
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 :
131
153
n = d .decode ('utf-8' )
132
154
if n .startswith ('.' ): continue
133
155
if not '.' in n : n += '/'
134
156
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
138
159
return completions
139
160
except OSError :
140
161
print "AutoFileName: could not find " + this_dir
0 commit comments