Question

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?

Was it helpful?

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.

OTHER TIPS

I solved it with the following code, However, I want to add the bookmarks content to the end of the file, so I can know where to load the bookmarks when I open a new file. How can I edit scintilla1.Lines[scintilla1.Lines.Count]?

List<int> bookmarks = new List<int>();

while(true)
{
    try
    {
        Line next = scintilla1.Markers.FindNextMarker();
        scintilla1.Caret.Position = next.StartPosition;
        scintilla1.Caret.Goto(next.EndPosition);
        scintilla1.Scrolling.ScrollToCaret();
        scintilla1.Focus();
        bookmarks.Add(next.Number);
    }
    catch(Exception ex)
    {
        break;
    }
}

string Marks="";
for(int i =0;i<bookmarks.Count;i++)
    Marks += bookmarks[i]+ ",";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top