Search code examples
c#windowsformsscintilla

Scintilla Control Saving bookmarks to file


I am trying to save the indicators (bookmarks) for the file being edited in scintilla so that they are reloaded next time you open a file.

This is my code snippet:

List<int> bookmarks = new List<int>();
for (int i = 0; i < scintilla1.Lines.Count; i++)
{
    if (!bookmarks.Contains(scintilla1.Markers.FindNextMarker(i).Number))
        bookmarks.Add(scintilla1.Markers.FindNextMarker(i).Number);
}

for (int j=0;j<bookmarks.Count;j++)
    MessageBox.Show(bookmarks[j].ToString());

However, it seems that the index is out side its bounds, any help?


Solution

  • Can you try this:

    HashMap<int> bookmarks = new HashMap<int>();
    for (int i = 0; i < scintilla1.Lines.Count; i++)
    {
        bookmarks.Add(scintilla1.Markers.FindNextMarker(i).Number);
    }
    
    foreach (var bookmark in bookmarks)
    {
        MessageBox.Show(bookmark.ToString());
    }
    

    Also, it should be noted that FindNextMarker will return the next line that has a marker (see implementation here). So I think you approach is wrong. It should probably be more like this:

    HashMap<int> bookmarks = new HashMap<int>();
    int nextBookmark = 0;
    
    while (nextBookmark != UInt32.MaxValue)
    {
        nextBookmark = scintilla1.Markers.FindNextMarker(nextBookmark).Line;
        if (nextBookmark != UInt32.MaxValue)
        {
            bookmarks.Add(nextBookmark);
        }
    }
    
    foreach (var bookmark in bookmarks)
    {
        MessageBox.Show(bookmark.ToString());
    }
    

    Better yet, you can get ALL the markers using public List<Marker> GetMarkers(int line):

    foreach (var bookmark in scintilla1.Markers.GetMarkers(0))
    {
        MessageBox.Show(bookmark.Line.ToString());
    }
    

    To be noted, there appears to be a maximum of 32 markers per file. See the markers documentation on the Scintilla site.