I use the prettier extention to format my files on save.
If I put a bookmark on a line that gets moved on format it gets removed
Is there any way to prevent this?
Hi @SSmale ,
Prettier does like any other Formatter extension, which in the end, does not _notify_ the editor _how_ the lines has been moved. It just updates the editor file content and reload the text/code block. To be able to stick the bookmarks to lines of code, the extension needs to be aware of line movement (move up/down, insert, delete) and it does not happen in this case.
So, unfortunately, I don't see how to fix this in Bookmarks side, unless VS Code make changes on how Formatter extensions works. I will try to find more info in VS Code repo about this.
Hope this helps
Hi @alefragnani,
Would it be possible to attach the bookmark to a line number or a setting to stop them being removed on update. My use case is the jump around longer files when I need to check how/what/why I did something elsewhere on the page
Thanks for the speedy and thorough reply
Hi @SSmale ,
The bookmarks are _attached_ to the line number. It's what I call _sticky_.
If you have a bookmark at line 10 and hit Enter at any line above, the bookmark is moved one line down. If you delete any line above, it will move the bookmark one line up. I can do this based on events (notifications) that the VS Code editor fires.
The problem is that the Prettier extension, just like other formatters, does not _notify_ that the lines has been rearranged, and so, there is not way to move the bookmarks up or down. That's the kind of API that I need to be able to do this.
Hope this helps.
The problem of prettier extension, and several others, the formatter replace all document with new content (See prettier)
Some extensions, replace only changed data, example the shell-format
As a temporary or makeshift solution, how about forcing bookmarks.saveBookmarksInProject to true upon detection of formatter extensions, and keeping a backup of the bookmarks.json file every few minutes or so, and then replacing the wiped out data due to formatting with the most recent backup? Or some other way of keeping the bookmarks after formatting? (I am assuming a loss of the bookmark data upon formatting, but if this is not the problem, then I guess I am asking if the bookmarks could be kept after formatting). Of course this would not be ideal since the positions of the bookmarks may well be knocked off of their original positions after formatting, but it is better than losing all of your bookmarks whenever the formatting happens.
Just wanted to say that the Go extension apparently does not suffer from this issue. I have it set to format the document on save and bookmarks get properly moved around if the reformatting changes line breaks.
I am using Go extension and I can confirm the bookmarks are still removed. It is quite annoying I would prefer slightly shifted wrong bookmarks to nothing. At least even though the lines are shifted they will be still within the vicinity of the old code.
same here. I'm using prettier in vscode and formatOnSave is turned on. So whenever prettier formats the code, every bookmarks are removed.
I just gone through this thread. What I'm suggesting is these bookmarks doesn't have to be _sticky_.
Just store the line number bookmarked. If prettier format the code, it's okay. I can bookmark it again.
So that I can at least use this extension. At present, when I hit save, the editor formats the code, and all bookmarks are gone.
Hi @vajahath ,
That's a good (and easy to do) alternative for this issue. Let's see how others react 馃憤
In fact, the initial releases didn't support _sticky_ bookmarks. This was introduced by a contribution, and now, its the oficial (and only) behavior
I'm having exact the same setting and problem as @vajahath. Currently, the bookmark almost always disappear when I save the file, making the bookmarks useless in my situation. I have tried another plugin Numbered Bookmark also made by @alefragnani, but it suffers the same problem.
It would be great if this could be fixed. However, I am not an expert with this sticky behavior. For me, it looks like a design choice. The problem is, if we change the bookmarks from sticky to not sticky, will it break somebody's workflow just like it broke ours?
Maybe implement a variable so that the user can change the sticky option in their preference file?
@johnyluyte ,
The sticky behavior, in concept, is very simple. It tries to attach the bookmark to the _line of code_, not the _line number_. When you change your source code files, it works in two ways:
The problem with prettier happens because (prettier) does not _notifies_ the VS Code editor which lines it changed. It simply _replaces_ the file contents. So, I don't know if I must move the bookmark Up or Down. Why it, sometimes, clear the bookmarks, I still don't know. At least, this was my initial findings.
When the extensions (Bookmarks and Numbered Bookmarks) where first released, it didn't support Sticky. It was added latter, in Apr/2016 by PR #20 as an _option_ , and in Apr/2017 it became the default/sole behavior. So, it would be fairly easy to recreate this option, and allow you to _don't use sticky_, until this issue is fixed.
The question is: Is it a _good_ alternative? I must know from you, the users. Simply 馃憤 on this comments so I can priorize this update.
Hope this helps
Just like @johnyluyte said, I would vote to make this _sticky_ ness a choice to users via settings.
Suggestion:
If you do choose to make the sticky behavior a toggle, it would be great if it was a clickable on/off toggle somewhere on the BOOKMARKS EXPLORER panel or on the VSCODE footer bar.
I'm not sure if some fix has been deployed for this problem or not yet?
@PerpetualWar no, not yet.
I would like to release an update with the _workaround_ before the new years end, but I can't promisse you that.
Perhaps, if Santa is generous to me, I can give back to all of you 馃榿
I notice that the editor itself can track the cursor position very well under formatting.
The mechanic is quite similar to the desired sticky behavior.
Although, maybe it involves some magic not exposed to extension?
Hi @sirius0xff ,
Haha... It doesn鈥檛 involves magic (I hope), just a complex algorithm that I didn鈥檛 have free time to deal with, yet.
I鈥檓 focusing on other areas of the extension, and in fact, this is low priority for me, basically because I don鈥檛 use prettier, or any other auto-formatted solution. Before that, I still want to revisit the _sticky_ behavior, to fix #116 and #168, which maybe could also fix this prettier issue.
Hope this helps
I recently face this issue.
Luckily, there is already an option to work with prettier.
just in case someone has the same issue, please add this line in the setting of VScode.
"bookmarks.useWorkaroundForFormatters": true
Most helpful comment
@PerpetualWar no, not yet.
I would like to release an update with the _workaround_ before the new years end, but I can't promisse you that.
Perhaps, if Santa is generous to me, I can give back to all of you 馃榿