3

I use the BraceFoldingStrategy by Daniel Grünwald:

public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
        {
            List<NewFolding> newFoldings = new List<NewFolding>();

            Stack<int> startOffsets = new Stack<int>();
            int lastNewLineOffset = 0;
            char openingBrace = this.OpeningBrace;
            char closingBrace = this.ClosingBrace;
            for (int i = 0; i < document.TextLength; i++) {
                char c = document.GetCharAt(i);
                if (c == openingBrace) {
                    startOffsets.Push(i);
                } else if (c == closingBrace && startOffsets.Count > 0) {
                    int startOffset = startOffsets.Pop();
                    // don't fold if opening and closing brace are on the same line
                    if (startOffset < lastNewLineOffset) {
                        newFoldings.Add(new NewFolding(startOffset, i + 1));
                    }
                } else if (c == '\n' || c == '\r') {
                    lastNewLineOffset = i + 1;
                }
            }
            newFoldings.Sort((a,b) => a.StartOffset.CompareTo(b.StartOffset));
            return newFoldings;
        }

This works as expected but one issue is remaining enter image description here

There is always a last - icon on the last brace. I tried to remove that,but when I look at the list newFoldings I can see that there are only 3 foldings there. So where is the fourth coming from?

EDIT

According to Daniel Grünwald the problem is that I have to be using to different folding managers. However I can't find any statement in my initialisation code where I set a second FoldingManager. Maybe any hints?

private void textEditor_Loaded(object sender, RoutedEventArgs e)
{
   LoadSourceCode();
   textEditor.Background = Brushes.Green;  
   textEditor.IsReadOnly = true;    
   textEditor.ShowLineNumbers = true;

   using (Stream s = this.GetType().Assembly.GetManifestResourceStream("Prototype_Concept_2.View.Layouts.CustomHighlighting.xshd"))
   {
      using (XmlTextReader reader = new XmlTextReader(s))
      {    
         textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
      }
    }

   (textEditor.TextArea as IScrollInfo).ScrollOwner.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
   (textEditor.TextArea as IScrollInfo).ScrollOwner.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;

   FoldingManager foldingManager = FoldingManager.Install(textEditor.TextArea);    
   BraceFoldingStrategy foldingStrategy = new BraceFoldingStrategy();    
   foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document);

   TextArea txt = textEditor.TextArea;    
   ObservableCollection<UIElement> margins = txt.LeftMargins;

   foreach (UIElement margin in margins)    
   {    
      if ((margin as FoldingMargin) != null)
      {
         Contacts.AddPreviewContactDownHandler(margin as FoldingMargin, OnDown);
      }
   }

   Main.Children.Remove(textEditor);
   ScrollHost.Width = textEditor.Width;    
   ScrollHost.Height = textEditor.Height;    
   ScrollHost.Content = textEditor;
   textEditor.Background = Brushes.Transparent;
   ScrollHost.Background = Brushes.Transparent;    
}

EDIT 2

I checked that the currently installed FoldingManager has 3 foldings, but I still don't know where the fourth folding is defined.

1 Answers1

6

The simple BraceFoldingStrategy is in my experience too buggy to be really useful. There are multiple problems like broken indenting (I was getting extra folding margin every time tab was changed in AvalonDock - it may have been the very same problem you are facing), braces in strings being parsed as normal braces (it is even able to pair a string { with real }) etc., so I decided to drop it.

Real code folding support requires real lexical (or better - syntactic) analysis information, use that if you can (you can find usage example in SharpDevelop 4.0 sources), but you have to have access to a parser for the language you are writing the editor for for that.

I think code folding is not IDEs most important feature and I personally seldom ever use it (only with really big file with regions/large methods).

I know this is not the answer you are hoping for (I would post this as a comment if it the post didn't exceed the length limit). I will keep watching this (and hoping somebody provides a real answer) though.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • Thanks for your answer. It is true it is a little bit buggy, especially if braces are used in comments. However, this is not the main feature of my application, it just one more thing that is nice to have. That's why it isn't too bad but nevertheless a little bit annoying. I also updated my question since Daniel Grünwald (the developer of it) told me that my problem is that I'm using two different FoldingManagers. However I can't find any place where I add a second FoldingManager in my code. Maybe you can give me a hint were I added a second FoldingManager. – RoflcoptrException Mar 10 '11 at 09:31
  • I awarded the bounty to you because otherwise it would have ended without an answer. Nevertheless I hope somebody will be able to answer this question anytime. – RoflcoptrException Mar 18 '11 at 20:29
  • @roflcoptr Thanks. I will try to figure it out sometime, I'm having a more pressing issues with AvalonDock in front of me though :) – Matěj Zábský Mar 18 '11 at 21:27
  • OK I just solved the problem. Somehow the loaded event of the TextEditor is called twice. So the FoldingManager is also installed twice. Thanks for the hints. – RoflcoptrException Apr 13 '11 at 13:26