Home Download Buy Blog Forum Support

hightlight changed row

hightlight changed row

Postby sbarex on Fri Nov 18, 2011 8:48 pm

is there a way to highlight the rows that have changed since last save and not yet stored (for example, near the line number)?
sbarex
 
Posts: 5
Joined: Fri Nov 18, 2011 8:30 pm

Re: hightlight changed row

Postby C0D312 on Fri Nov 18, 2011 9:05 pm

No, but it could definitely but done very easily with a plugin. I'll try to make one for you.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: hightlight changed row

Postby C0D312 on Fri Nov 18, 2011 10:05 pm

Here's your plugin. Just go to Tools > New Plugin. Paste this into the new file. Save the file as whatever you want (I called it HighlightUnsaved.py), just make sure you save it in your User folder in Sublime Text's Packages directory (should be the default when you press save as but I'm not positive).
Code: Select all
import sublime, sublime_plugin

class ClearChangesCommand(sublime_plugin.EventListener):
   def on_post_save(self, view):
      view.erase_regions('unsaved')

class HighlightUnsavedCommand(sublime_plugin.EventListener):
   def on_modified(self, view):
      unsaved = view.get_regions('unsaved') + [view.line(s) for s in view.sel()]
      view.add_regions("unsaved", unsaved, "unsaved", "dot", sublime.HIDDEN | sublime.PERSISTENT)
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: hightlight changed row

Postby sbarex on Fri Nov 18, 2011 10:13 pm

wow fantastic and fast, thanks!
There is also a way that when you undo a change in a row, the system removes the marker?
Thanks
Simon
sbarex
 
Posts: 5
Joined: Fri Nov 18, 2011 8:30 pm

Re: hightlight changed row

Postby C0D312 on Fri Nov 18, 2011 10:30 pm

I'll give it a try.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: hightlight changed row

Postby C0D312 on Fri Nov 18, 2011 11:38 pm

This took a bit of work but here it is:
Code: Select all
import sublime, sublime_plugin

class ClearChangesCommand(sublime_plugin.EventListener):
   def on_post_save(self, view):
      view.erase_regions('unsaved')

class HighlightUnsavedCommand(sublime_plugin.EventListener):
   def on_modified(self, view):

      unsaved = view.get_regions('unsaved') + [view.line(s) for s in view.sel()]

      with open(view.file_name(), 'r') as f:
         read_data = f.read()

      for sel in view.sel():
         print view.line(sel)

         if read_data[view.line(sel).begin():view.line(sel).end()] == view.substr(view.line(sel)):
            unsaved[:] = [x for x in unsaved if x != view.line(sel)]
      
      view.add_regions("unsaved", unsaved, "mark", "dot", sublime.HIDDEN | sublime.PERSISTENT)

Same thing as before: save as a plugin in User directory (or you can just overwrite the last plugin).
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: hightlight changed row

Postby sbarex on Sat Nov 19, 2011 9:25 am

Interesting approach.
How I understand the API that is not possible to intercept an event before the user changes the contents of a row to save its state and compare it after each change to see if it was restored to its original state.
An alternative might be to save all rows in a buffer when the file is loaded (but consume a lot of memory) and use that for comparison instead of opening the original file for each change.
The problem with the comparison between the original file or a buffer is that the addition of a line means that all subsequent lines will appear to be changed.
Anyway thanks for this code!
sbarex
 
Posts: 5
Joined: Fri Nov 18, 2011 8:30 pm

Re: hightlight changed row

Postby C0D312 on Sat Nov 19, 2011 1:34 pm

I'm new to Python/Plugin Development so there is probably a better way to make the plugin. This way was the first way that I thought of. The addition of new lines will cause trouble to the plugin... and I can't seem to think of a workaround that wouldn't take a lot of memory. (I didn't realize you'd be judging my code so much :o )

I just hope I helped.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: hightlight changed row

Postby sbarex on Sat Nov 19, 2011 4:18 pm

Indeed, thanks for giving me a good starting point!
sbarex
 
Posts: 5
Joined: Fri Nov 18, 2011 8:30 pm

Highlight Changes

Postby JuDelCo on Mon Apr 22, 2013 7:22 pm

Hi :P

A long time ago, I changed this plugin to work with sublime text 2 because it stopped working.
When I switched to Sublime text 3, I fixed it again.
Now I just uploaded it to github to everyone use.

Of course all credits go to C0D312


Sublime text 2 branch: https://github.com/JuDelCo/Highlight-Changes
Sublime text 3 branch: https://github.com/JuDelCo/Highlight-Ch ... ime-text-3

Example (screenshot):

Image


EDIT: Okay, i just noticed the thread is in general forum. Maybe there's better in Plugin Announcements or Plugin Development ? Nevermind... xD


Thanks ;)
JuDelCo
 
Posts: 4
Joined: Sun Apr 21, 2013 7:27 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 12 guests