sublime text 3插件改造之AutoFileName去掉.vue文件中img标签后面的width和height,完全去掉!!

在.vue文件中img标签使用autofilename提示引入文件时,会在文件后面插入宽度高度,如下图:

文件后面会自动插入height和width,其实这两玩意儿在大多数时候并没卵用,然后就开始了百度之旅...

一番百度,github之后,并没有找到自己想要的答案,github上说可以配置afn_insert_dimensions,afn_insert_width_first。参考github github

实际实验之后发现被坑得很惨,这两货怎么改变好像都没啥卵用。然后想到了修改插件源码...(本人不懂Python,所以未去查看为啥修改配置不生效的问题)

操作步骤:

1. 首选项>浏览插件目录,在该目录新建文件夹"AutoFileName"(与插件文件一致),

2. 选择当前目录上一级(Sublime Text 3)>Installed Packages找到"AutoFileName.sublime-package",复制一份该文件到任意目录,修改文件后缀为.zip,用软件解压。

3.找到解压目录中"autofilename.py",复制一份到第一步新建的目录AutoFileName中。

4.修改复制的autofilename.py(左侧是源码,右侧是修改之后

修改之后完整代码:

  1 import sublime
  2 import sublime_plugin
  3 import os
  4 from .getimageinfo import getImageInfo
  5 
  6 class AfnShowFilenames(sublime_plugin.TextCommand):
  7     def run(self, edit):
  8         FileNameComplete.is_active = True
  9         self.view.run_command('auto_complete',
 10                 {'disable_auto_insert': True,
 11                 'next_completion_if_showing': False})
 12 
 13 class AfnSettingsPanel(sublime_plugin.WindowCommand):
 14     def run(self):
 15         use_pr = '✗ Stop using project root' if self.get_setting('afn_use_project_root') else '✓ Use Project Root'
 16         use_dim = '✗ Disable HTML Image Dimension insertion' if self.get_setting('afn_insert_dimensions') else '✓ Auto-insert Image Dimensions in HTML'
 17         p_root = self.get_setting('afn_proj_root')
 18 
 19         menu = [
 20                 [use_pr, p_root],
 21                 [use_dim, '<img src="_path_" width = "x" height = "y" >']
 22                ]
 23         self.window.show_quick_panel(menu, self.on_done)
 24 
 25     def on_done(self, value):
 26         settings = sublime.load_settings('autofilename.sublime-settings')
 27         if value == 0:
 28             use_pr = settings.get('afn_use_project_root')
 29             settings.set('afn_use_project_root', not use_pr)
 30         if value == 1:
 31             use_dim = settings.get('afn_use_project_root')
 32             settings.set('afn_use_project_root', not use_dim)
 33 
 34     def get_setting(self,string,view=None):
 35         if view and view.settings().get(string):
 36             return view.settings().get(string)
 37         else:
 38             return sublime.load_settings('autofilename.sublime-settings').get(string)
 39 
 40 # Used to remove the / or \ when autocompleting a Windows drive (eg. /C:/path)
 41 class AfnDeletePrefixedSlash(sublime_plugin.TextCommand):
 42     def run(self, edit):
 43         sel = self.view.sel()[0].a
 44         reg = sublime.Region(sel-4,sel-3)
 45         self.view.erase(edit, reg)
 46 
 47 # inserts width and height dimensions into img tags. HTML only
 48 class InsertDimensionsCommand(sublime_plugin.TextCommand):
 49     this_dir = ''
 50 
 51     def insert_dimension(self,edit,dim,name,tag_scope):
 52         view = self.view
 53         sel = view.sel()[0].a
 54 
 55         if name in view.substr(tag_scope):
 56             reg = view.find('(?<='+name+'\=)\s*\"\d{1,5}', tag_scope.a)
 57             view.replace(edit, reg, '"'+str(dim))
 58         else:
 59             dimension = str(dim)
 60             view.insert(edit, sel+1, ' '+name+'="'+dimension+'"')
 61 
 62     def get_setting(self,string,view=None):
 63         if view and view.settings().get(string):
 64             return view.settings().get(string)
 65         else:
 66             return sublime.load_settings('autofilename.sublime-settings').get(string)
 67 
 68 
 69     def insert_dimensions(self, edit, scope, w, h):
 70         view = self.view
 71 
 72         if self.get_setting('afn_insert_width_first',view):
 73             self.insert_dimension(edit,h,'height', scope)
 74             self.insert_dimension(edit,w,'width', scope)
 75         else:
 76             self.insert_dimension(edit,w,'width', scope)
 77             self.insert_dimension(edit,h,'height', scope)
 78 
 79 
 80     # determines if there is a template tag in a given region.  supports HTML and template languages.
 81     def img_tag_in_region(self, region):
 82         view = self.view
 83 
 84         # handle template languages but template languages like slim may also contain HTML so
 85         # we do a check for that as well
 86         return view.substr(region).strip().startswith('img') | ('<img' in view.substr(region))
 87 
 88 
 89     def run(self, edit):
 90         view = self.view
 91         view.run_command("commit_completion")
 92         sel = view.sel()[0].a
 93 
 94         if not 'html' in view.scope_name(sel): return
 95         scope = view.extract_scope(sel-1)
 96 
 97         # if using a template language, the scope is set to the current line
 98         tag_scope = view.line(sel) if self.get_setting('afn_template_languages',view) else view.extract_scope(scope.a-1)
 99 
100         path = view.substr(scope)
101         if path.startswith(("'","\"","(")):
102             path = path[1:-1]
103 
104         path = path[path.rfind(FileNameComplete.sep):] if FileNameComplete.sep in path else path
105         full_path = self.this_dir + path
106 
107         if self.img_tag_in_region(tag_scope) and path.endswith(('.png','.jpg','.jpeg','.gif')):
108             with open(full_path,'rb') as r:
109                 read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
110             w, h = getImageInfo(read_data)
111 
112             self.insert_dimensions(edit, tag_scope)
113 
114 
115 # When backspacing through a path, selects the previous path component
116 class ReloadAutoCompleteCommand(sublime_plugin.TextCommand):
117     def run(self,edit):
118         view = self.view
119         view.run_command('hide_auto_complete')
120         view.run_command('left_delete')
121         sel = view.sel()[0].a
122 
123         scope = view.extract_scope(sel-1)
124         scope_text = view.substr(scope)
125         slash_pos = scope_text[:sel - scope.a].rfind(FileNameComplete.sep)
126         slash_pos += 1 if slash_pos < 0 else 0
127 
128         region = sublime.Region(scope.a+slash_pos+1,sel)
129         view.sel().add(region)
130 
131 
132 class FileNameComplete(sublime_plugin.EventListener):
133     def on_activated(self,view):
134         self.showing_win_drives = False
135         FileNameComplete.is_active = False
136         FileNameComplete.sep = '/'
137 
138     def get_drives(self):
139     # Search through valid drive names and see if they exist. (stolen from Facelessuser)
140         return [[d+":"+FileNameComplete.sep, d+":"+FileNameComplete.sep] for d in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if os.path.exists(d + ":")]
141 
142     def on_query_context(self, view, key, operator, operand, match_all):
143         if key == "afn_insert_dimensions":
144             return self.get_setting('afn_insert_dimensions',view) == operand
145         if key == "afn_deleting_slash":  # for reloading autocomplete
146             sel = view.sel()[0]
147             valid = self.at_path_end(view) and sel.empty() and view.substr(sel.a-1) == FileNameComplete.sep
148             return valid == operand
149         if key == "afn_use_keybinding":
150             return self.get_setting('afn_use_keybinding',view) == operand
151 
152     def at_path_end(self,view):
153         sel = view.sel()[0]
154         name = view.scope_name(sel.a)
155         if sel.empty() and 'string.end' in name:
156             return True
157         if '.css' in name and view.substr(sel.a) == ')':
158             return True
159         return False
160 
161     def on_modified(self, view):
162         sel = view.sel()[0].a
163         txt = view.substr(sublime.Region(sel-4,sel-3))
164         if (self.showing_win_drives and txt == FileNameComplete.sep):
165             self.showing_win_drives = False
166             view.run_command('afn_delete_prefixed_slash')
167 
168     def on_selection_modified_async(self,view):
169         if not view.window():
170             return
171         sel = view.sel()[0]
172         if sel.empty() and self.at_path_end(view):
173             scope_contents = view.substr(view.extract_scope(sel.a-1))
174             p = scope_contents.replace('\r\n', '\n').split('\n')[0]
175             if('\\' in p and not '/' in p):
176                 FileNameComplete.sep = '\\'
177             else:
178                 FileNameComplete.sep = '/'
179             if view.substr(sel.a-1) == FileNameComplete.sep or len(view.extract_scope(sel.a)) < 3:
180                 view.run_command('auto_complete',
181                 {'disable_auto_insert': True,
182                 'next_completion_if_showing': False})
183         else:
184             FileNameComplete.is_active = False
185 
186     def fix_dir(self,sdir,fn):
187         if fn.endswith(('.png','.jpg','.jpeg','.gif')):
188             path = os.path.join(sdir, fn)
189             with open(path,'rb') as r:
190                 read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24)
191             w, h = getImageInfo(read_data)
192             return fn+'\t'+'w:'+ str(w) +" h:" + str(h)
193         return fn
194 
195     def get_cur_path(self,view,sel):
196         scope_contents = view.substr(view.extract_scope(sel-1)).strip()
197         cur_path = scope_contents.replace('\r\n', '\n').split('\n')[0]
198         if cur_path.startswith(("'","\"","(")):
199             cur_path = cur_path[1:-1]
200 
201         return cur_path[:cur_path.rfind(FileNameComplete.sep)+1] if FileNameComplete.sep in cur_path else ''
202 
203     def get_setting(self,string,view=None):
204         if view and view.settings().get(string):
205             return view.settings().get(string)
206         else:
207             return sublime.load_settings('autofilename.sublime-settings').get(string)
208 
209     def on_query_completions(self, view, prefix, locations):
210         is_proj_rel = self.get_setting('afn_use_project_root',view)
211         valid_scopes = self.get_setting('afn_valid_scopes',view)
212         blacklist = self.get_setting('afn_blacklist_scopes', view)
213         uses_keybinding = self.get_setting('afn_use_keybinding', view)
214 
215         sel = view.sel()[0].a
216         this_dir = ""
217         completions = []
218 
219         if uses_keybinding and not FileNameComplete.is_active:
220             return
221         if not any(s in view.scope_name(sel) for s in valid_scopes):
222             return
223         if any(s in view.scope_name(sel) for s in blacklist):
224             return
225 
226         cur_path = os.path.expanduser(self.get_cur_path(view, sel))
227 
228 
229         if cur_path.startswith('/') or cur_path.startswith('\\'):
230             if is_proj_rel:
231                 proot = self.get_setting('afn_proj_root', view)
232                 if proot:
233                     if not view.file_name() and not os.path.isabs(proot):
234                         proot = "/"
235                     cur_path = os.path.join(proot, cur_path[1:])
236                 else:
237                     for f in sublime.active_window().folders():
238                         if f in view.file_name():
239                             cur_path = f
240         elif not view.file_name():
241             return
242         else:
243             this_dir = os.path.split(view.file_name())[0]
244         this_dir = os.path.join(this_dir, cur_path)
245 
246         try:
247             if sublime.platform() == "windows" and len(view.extract_scope(sel)) < 4 and os.path.isabs(cur_path):
248                 self.showing_win_drives = True
249                 return self.get_drives()
250             self.showing_win_drives = False
251             dir_files = os.listdir(this_dir)
252 
253             for d in dir_files:
254                 if d.startswith('.'): continue
255                 if not '.' in d: d += FileNameComplete.sep
256                 completions.append((self.fix_dir(this_dir,d), d))
257             if completions:
258                 InsertDimensionsCommand.this_dir = this_dir
259                 return completions
260             return
261         except OSError:
262             print("AutoFileName: could not find " + this_dir)
263             return
View Code

 

注意:修改这里代码之后,将在所有文件里面都不会再插入宽度高度!!!

 

posted @ 2017-03-15 17:12  极·简  Views(2202)  Comments(0Edit  收藏  举报