Persist folding between sessions?
-
When I edit a large Swagger file using Notepad++, I often collapse (fold?) sections that I’m not interested in by clicking on the - on the far left. Is there some way I can save this folding so that when I open the file again, it remains, and I don’t have to refold all of the sections?
-
If you exit Notepad++ and reload, the fold state persists. If you close a particular file and re-open, the fold state does not persist.
However, you could use View > Fold All, and then just unfold the specific area(s) you are looking at.
If that’s not sufficient, there is a PythonScript method. Assuming you have PythonScript plugin installed, you will need to create two PythonScript scripts:
saveFolds.py
# encoding=utf-8 """in response to https://notepad-plus-plus.org/community/topic/19077/ Saves current fold state """ from Npp import * #console.show() #console.clear() foldstr = "" def saveFoldingLevel(contents, lineNumber, totalLines): global foldstr if editor.getFoldLevel(lineNumber)>0: foldstr += "{},{},{}\n".format( lineNumber, editor.getFoldLevel(lineNumber), editor.getFoldExpanded(lineNumber) ) #console.write("#{}/{}# foldLevel={} ({})\n".format(lineNumber, totalLines, editor.getFoldLevel(lineNumber)&FOLDLEVEL.NUMBERMASK-0x400, editor.getFoldLevel(lineNumber))) editor.forEachLine(saveFoldingLevel) #console.write(foldstr) notepad.new() editor.setText(foldstr) notepad.messageBox("About to save the folding file\nPlease enter appropriate path/filename in SaveAs dialog") notepad.menuCommand(MENUCOMMAND.FILE_SAVEAS) notepad.close()
loadFolds.py
# encoding=utf-8 """in response to https://notepad-plus-plus.org/community/topic/19077/ Loads current fold state """ from Npp import * console.show() console.clear() currentID = notepad.getCurrentBufferID() notepad.messageBox("About to load the folding file\nPlease enter appropriate path/filename in Open dialog") notepad.menuCommand(MENUCOMMAND.FILE_OPEN) #notepad.menuCommand(MENUCOMMAND.VIEW_GOTO_ANOTHER_VIEW) # toggle to other view foldList = dict() def readFoldingLevel(contents, lineNumber, totalLines): global foldList #console.write(contents) if contents.strip(): # if it's not a blank line lstr, fstr, fbool = contents.strip().split(',') #console.write("{}/{}/{}\n".format(lstr,fstr,fbool)) foldList[ int(lstr) ] = (fbool == 'True') editor.forEachLine(readFoldingLevel) notepad.close() #print(foldList) notepad.activateBufferID(currentID) def applyFoldingLevel(contents, lineNumber, totalLines): global foldList #key = "{}".format(lineNumber) if lineNumber in foldList: # make sure it exists state = foldList[ lineNumber ] editor.setFoldExpanded(lineNumber, state) editor.forEachLine(applyFoldingLevel)
Run the first script to save a file with a CSV that holds line number, fold level and flag information, and fold state (it only uses the first and third from each line)
Run the second script to load a file from a CSV that holds line number, fold level and flag info, and fold state. It will apply the given fold state to each line.Please note that if your original file changes between the save and the load, the state is unpredictable.
-
@PeterJones said in Persist folding between sessions?:
If you exit Notepad++ and reload, the fold state persists.
It seems like you would need the Preference setting Remember current session for next launch to be ticked for this state to persist. Maybe the OP is operating without that.
Also “reload” is probably not the right term here; how about “restart” or “re-run”?
“Reload” implies File > Reload ; totally different thing. -
@Alan-Kilborn said in Persist folding between sessions?:
Also “reload” is probably not the right term here; how about “restart” or “re-run”?
Of those choices, “re-run” is probably the best.
“reload the program” is common in my mental parlance; I’m sorry if that caused confusion to others. In my defense, I was pretty explicit about exiting Notepad++ itself before doing the reload, so I hope that would help alleviate the confusion.
-
“Reload”: It was a minor quibble with your post, and I’m sure most readers understood.
More important was my comment about “Remember current session”. I hope we hear back from the OP on that aspect.
-
Do note that for the general case when you reload a file (with or without Notepad++ restart) there is no guarantee that the file was not modified by another application in-between. Saving folding state of a file to be restored in a modified file is not possible for every case - the file content could be COMPLETELY different.
Whoever implemented this feature should have been very careful not to do stupid things.
The safest approach would be to save the hash of the files content and do not try to restore folding at all if this hash was changed. -
Fortunately, if you try to fold an unfoldable line, nothing happens (at least in my experiments). As far as I know, no data can be lost from improper folding (View > Unfold All should always show everything again). And I did try to make it clear that “if [the] original file changes between the save and the load, the state is unpredictable.”
In case I wasn’t clear enough:
warning / disclaimer
@Sal-Ricciardi, the PythonScript solution shown is only a hack for solving a very limited subset of the problem, as a proof of concept – specifically, re-applying the same fold levels to the exact same state of a particular file. You are responsible for keeping proper backups of important data. You are responsible for ensuring you load the correct fold-save file for a given file. You are responsible for making sure that the file doesn’t change between loads.
If you would like more features, you may feel free to develop the above script into a more feature-rich version of the PythonScript, or use the PythonScript shown as a basis for a full-fledged plugin.
I make no guarantees or warranties. But I do hope this helps you.