Sublime Forum

[ST3] Bug: toggle_bookmark does not work sometimes

#1

When there are a large number of bookmarks - toggle_bookmark does not work on some of the bookmarks. On others it does.

Steps to reproduce:

  1. Load a large file such as “Key Bindings - Default”
  2. In the console enter (this will result in many matches which is what we want):
    RegionsResult = sublime.active_window().active_view().find_all("{", sublime.IGNORECASE | sublime.LITERAL)
  3. In the console enter (this will add bookmarks to all the matches):
    sublime.active_window().active_view().add_regions(“bookmarks”, RegionsResult, “bookmarks”, “bookmark”, sublime.HIDDEN | sublime.PERSISTENT)
  4. Try to do “toggle_bookmark” on the lines with bookmarks - as you will see on some of them will work, on some of them will not.

However, if I do the same thing with a smaller file in which it doesn’t have that many results, it works fine. So it seems to be a problem when there are a large number of bookmarks set - then “toggle_bookmark” stops working on some of the lines.

0 Likes

Dev Build 3017
Highlight/change background color of selected text
#2

I don’t think there is a bug, i think it’s by design. You can have multiple bookmarks per line. You have to jump to each individual bookmark and toggle it off at that position. Personally I would prefer to have only one bookmark per line, because I can’t see the exact bookmark positions. I would like to delete that bookmark with the cursor somewhere in that line (without having to jump to the exact bookmark position in that line).

0 Likes

#3

I think that it’s relatively easy to write a plugin that replace the current behavior with the one you want.
Something like this plugin: https://github.com/artkorsun/DelphiStyleBookmarks

0 Likes

#4

Thanks bizoo!

It can even be done by a macro:

Bookmark Line.sublime-macro


	{
		"args": null,
		"command": "set_mark"
	},
	{
		"args":
		{
			"to": "line"
		},
		"command": "expand_selection"
	},
	{
		"args": null,
		"command": "toggle_bookmark"
	},
	{
		"args":
		{
			"by": "characters",
			"forward": false
		},
		"command": "move"
	},
	{
		"args": null,
		"command": "select_to_mark"
	},
	{
		"args":
		{
			"by": "characters",
			"forward": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"name": "mark"
		},
		"command": "clear_bookmarks"
	}
]

Update: Macro keeps Cursor Position.

0 Likes

#5

Clever, I never think to use macro instead of plugin.
But Python is so fun in ST :smiley:

0 Likes

#6

Oh! Thank you for the explanation for why this is happening. You are right it is because there is more than one bookmark on that line. Here is a command called toggle_bookmark_wholeline that gives desired functionality. It will find and remove all bookmarks that intersect with the line contained by the keyboard caret/first selection. This command is now included in mark_from_findpanel as a complete solution for processing (marking/unmarking) search results one-by-one via bookmarks.

[code]class ToggleBookmarkWholelineCommand(sublime_plugin.TextCommand):
def run(self, edit):
caretLine = self.view.line(self.view.sel()[0])
oldBookmarks = self.view.get_regions(“bookmarks”)

	newBookmarks = ]
	bookmarkFoundOnCaretLine = False

	for thisbookmark in oldBookmarks:
		# if thisbookmark.intersects(caretLine):
		if ( (thisbookmark.begin() >= caretLine.begin()) and (thisbookmark.begin() <= caretLine.end()) or
		     (thisbookmark.end() >= caretLine.begin()) and (thisbookmark.end() <= caretLine.end()) ):
			bookmarkFoundOnCaretLine = True
		else:
			newBookmarks.append(thisbookmark)

	if not bookmarkFoundOnCaretLine:
			newBookmarks.append(self.view.sel()[0])

	sublime.active_window().active_view().add_regions("bookmarks", newBookmarks, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)

[/code]

0 Likes

#7

Very nice!

While I was testing you changed it to exactly what I wanted (add cursor position instead of line to bookmark selection) :smile:

You just need to put “import sublime, sublime_plugin” back in (if this code is used as a stand-alone plugin).

Thank you very much!!

0 Likes

#8

I’m still pretty amazed that this is a “feature”, and not a bug. Are people really bookmarking multiple spots on a line and jumping between them? I would be surprised to find a single person doing this, let alone the majority. Maybe we could call this “charmarking”?

Also, visually, there’s no indication of the multiple markers.

0 Likes