SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

.NET Folding Strategy

Last post 01-05-2009 8:01 AM by EntityReborn. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 07-11-2008 8:28 PM

    • AndyB
    • Not Ranked
    • Joined on 07-11-2008
    • Posts 4

    .NET Folding Strategy

    Hey Guys, My first post here. I have been trying to develop a simple text/code editor using the #Develop TextEditor.

    I have got the Text editor working fine in my application but i am struggling to get Code Folding working. Well i have it working fine, just cant come up with an algorithm/method for implementing the IFoldingStrategy interface.

    I got a simple Strategy working fine. It worked by looping and creating a list of all the { and another list of all the } occurances in the code. It then looped through all the } and matched it up with the LAST occurance of } and then removed them both from the lists and moved on to the next.

    This doesnt exactly work sadly :(. I was hoping if someone could give me a nudge in the right direction for working out an IFoldingStrategy for .NET Languages such as C# or C++. I thought i may be able to find them in the source code download but no luck.

    Thanks for you time in reading, Any help would be greatly appriciated, Im sure i will have more questions soon enough but this one is getting to me.

    Andy

  • 07-12-2008 10:17 AM In reply to

    Re: .NET Folding Strategy

    SharpDevelop uses information from the parser and the ParserFoldingStrategy to work out where the folds should go. The ParserService calls the TextEditorDisplayBindingWrapper's ParseInformationUpdated. This method then updates the folds.
  • 07-12-2008 7:14 PM In reply to

    • AndyB
    • Not Ranked
    • Joined on 07-11-2008
    • Posts 4

    Re: .NET Folding Strategy

    Ah thank you.

     Didnt know that there was a ParserFoldingStrategy. Where abouts could i find out how to impliment it?

  • 07-12-2008 8:45 PM In reply to

    • AndyB
    • Not Ranked
    • Joined on 07-11-2008
    • Posts 4

    Re: .NET Folding Strategy

    Just an update.. I have found the ParserFoldingStrategy() interface, and i have copied it over into my app and am using it and setting it as the TextEditor.Document.FoldingManager.FoldingStrategy property.

    Unfortunately.. It compiles ok and no runtime errors, but it doesnt work.. there are no fold markers?
    Any tips or hints?

  • 07-12-2008 10:06 PM In reply to

    Re: .NET Folding Strategy

    SharpDevelop has a ParserService that runs a thread in the background. This would thread would update the folds and do the parsing. You could use the ParserService but it requires several interfaces to be implemented in your code before it will work. The SharpSnippetCompiler sample works in this way. A simpler way is to just use the parser directly. The CSharpCodeCompletion does this but it does not update the folds. To update the folds you would need to call UpdateFoldings on the FoldingManager. If you look at the TextEditorDisplayBindingWrapper's ParseInformationUpdatedInvoked method you'll see what you need to do.
  • 08-28-2008 8:14 PM In reply to

    • AndyB
    • Not Ranked
    • Joined on 07-11-2008
    • Posts 4

    Re: .NET Folding Strategy

    Wow, Okay firstly i would like to apologise that its been so long i left this topic go dead.
    Had exams followed by holiday followed by several other issues before i could get back to coding.

    That is an interesting method of doing it.
    I have looked in the samples and the source code, and i have googled, but i cannot seem to find any expamples of how to impliment this ParserService to work with my text editor control.
    Could you point me in the right direction?
    I would be really greatful.

    Andy
  • 01-05-2009 8:01 AM In reply to

    Re: .NET Folding Strategy

     Im looking for the same thing. I found this snippit from demo code on codeproject, which basicaly takes the region idea into effect, but I couldn't get it to work for { and }, just replacing the #xxx tags.

        public class RegionFoldingStrategy : IFoldingStrategy
        {
            /// <summary>
            /// Generates the foldings for our document.
            /// </summary>
            /// <param name="document">The current document.</param>
            /// <param name="fileName">The filename of the document.</param>
            /// <param name="parseInformation">Extra parse information, not used in this sample.</param>
            /// <returns>A list of FoldMarkers.</returns>
            public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
            {
                List<FoldMarker> list = new List<FoldMarker>();

                Stack<int> startLines = new Stack<int>();
               
                // Create foldmarkers for the whole document, enumerate through every line.
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    LineSegment seg = document.GetLineSegment(i);
                    int offs, end = document.TextLength;
                    char c;
                    for (offs = seg.Offset; offs < end && ((c = document.GetCharAt(offs)) == ' ' || c == '\t'); offs++)
                        {}
                    if (offs == end)
                        break;
                    int spaceCount = offs - seg.Offset;

                    // now offs points to the first non-whitespace char on the line
                    if (document.GetCharAt(offs) == '#') {
                        string text = document.GetText(offs, seg.Length - spaceCount);
                        if (text.StartsWith("#begin"))
                            startLines.Push(i);
                        if (text.StartsWith("#end") && startLines.Count > 0) {
                            // Add a new FoldMarker to the list.
                            int start = startLines.Pop();
                            list.Add(new FoldMarker(document, start,
                                document.GetLineSegment(start).Length,
                                i, spaceCount + "#end".Length));
                        }
                    }   
                }
                return list;
            }
        }
    }

Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.