sublimetext2 - Perform action before save (`on_pre_save`) -


import sublime_plugin  class test(sublime_plugin.eventlistener):      def on_pre_save(self, view):         view.set_syntax_file("packages/python/python.tmlanguage") 

here simple example. logically (from point of view), should change syntax before saving, , so, file should saved <filename>.py.

but actually, syntax changed after save operation. so, if worked js file, saved js, not py.

i'm wondering why on_pre_save works strange, or, in other words, there difference between on_pre_save , on_post_save. also, , that's practical interest, how can perform arbitrary(1) action right before saving?

(1) i've use word "arbitrary", because don't mean syntax changes. may different. example, change font consolas times new roman.

the on_pre_save event happens before file buffer written disk, , allows take action might want take before file on disk changes, example making change content of buffer (e.g. "reformat on save").

the on_post_save event happens after file buffer has been written disk, allowing take action might want take after save operation, example examining contents of buffer once it's "final" (e.g. "lint on save", if done through external tool requires changes on disk , not in memory).

in either case file name of file has been selected user @ time event happens. new file, means on_pre_save doesn't happen until after they've selected name , location of file. existing file, save resaves same filename.

to answer question, can "arbitrary" thing want in on_pre_save have happen prior when save happens. it's possible change filename in situation if want to.

note changing filename out under user without asking them first decidedly bad ux. additionally, if change filename file exists within on_pre_save sublime blindly overwrite file no warnings, bad mojo.

for that's going alter name , location of file on disk, more appropriate way go have command user has explicitly invoke make happen they're aware of what's going on.


as requested in comment , completeness, here's example wanted example code above do.

the important thing note here have extremely careful situation trigger event in. written above, plugin make impossible ever save kind of file ever due swapping on python file instead.

in example it's constrained take effect on text file, turning python file. note if there python file name in location, overwrite without warning it's happen.

be extremely wary code; it's quite easy accidentally stop being able save files correct name, example stop being able use sublime fix code, amongst other nasty issues.

import sublime_plugin import os  class testlistener(sublime_plugin.eventlistener):     def on_pre_save(self, view):         # part extremely important because mentioned above it's         # entirely disconcerting save operation gank         # filename , make else without         # warning. if you're not careful might destroy ability         # use sublime fix plugin, example.         if not view.file_name().endswith(".txt"):             print("doing nothing for: ", view.file_name())             return          # huge warning: can , willfully clobber on file         # happens exist without warning         # whatsoever, , decidedly bad idea(tm)         python_name = os.path.splitext(view.file_name())[0] + ".py"         view.retarget(python_name) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -