Sublime Forum

Hightlight changed row

#1

is there a way to highlight the rows that have changed since last save and not yet stored (for example, near the line number)?

0 Likes

Change Tracker - highlights changes
Highlighting lines changed from the last commit
#2

No, but it could definitely but done very easily with a plugin. I’ll try to make one for you.

0 Likes

#3

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]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)[/code]

0 Likes

#4

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

0 Likes

#5

I’ll give it a try.

0 Likes

#6

This took a bit of work but here it is:

[code]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)[/code]

Same thing as before: save as a plugin in User directory (or you can just overwrite the last plugin).

0 Likes

#7

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!

0 Likes

#8

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 :astonished: )

I just hope I helped.

0 Likes

#9

Indeed, thanks for giving me a good starting point!

0 Likes

#10

Hi :stuck_out_tongue:

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: github.com/JuDelCo/Highlight-Changes
Sublime text 3 branch: github.com/JuDelCo/Highlight-Ch … ime-text-3

Example (screenshot):

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

Thanks :wink:

0 Likes

Possible to put line highlights?