/// <summary> /// Updates the foldings in this <see cref="FoldingManager"/> using the given new foldings. /// This method will try to correlate new foldings with existing foldings and will attempt /// to keep the state (<see cref="FoldingSection.IsFolded"/>) of existing foldings. /// </summary> /// <remarks> /// Based on original code from: https://github.com/icsharpcode/AvalonEdit/blob/master/ICSharpCode.AvalonEdit/Folding/FoldingManager.cs /// This custom version was created in order to fix an issue where only the first collapsed folding was preserved. /// See: https://github.com/icsharpcode/AvalonEdit/issues/86 /// </remarks> public static void UpdateFoldings(this FoldingManager manager, IEnumerable <NewFolding> newFoldings) { if (newFoldings == null) { throw new ArgumentNullException(nameof(newFoldings)); } var oldFoldings = manager.AllFoldings.ToArray(); int oldFoldingIndex = 0; int previousStartOffset = 0; // Merge new foldings into old foldings so that sections keep being collapsed. // Both oldFoldings and newFoldings are sorted by start offset. foreach (var newFolding in newFoldings) { // ensure newFoldings are sorted correctly if (newFolding.StartOffset < previousStartOffset) { throw new ArgumentException(nameof(newFoldings) + " must be sorted by start offset"); } previousStartOffset = newFolding.StartOffset; if (newFolding.StartOffset == newFolding.EndOffset) { continue; // ignore zero-length foldings } // Remove old foldings that were skipped. while (oldFoldingIndex < oldFoldings.Length && newFolding.StartOffset > oldFoldings[oldFoldingIndex].StartOffset) { manager.RemoveFolding(oldFoldings[oldFoldingIndex++]); } // Reuse current folding if its matching. FoldingSection section; if (oldFoldingIndex < oldFoldings.Length && newFolding.StartOffset == oldFoldings[oldFoldingIndex].StartOffset) { section = oldFoldings[oldFoldingIndex++]; section.Length = newFolding.EndOffset - newFolding.StartOffset; } else { // No matching current folding; create a new one. section = manager.CreateFolding(newFolding.StartOffset, newFolding.EndOffset); section.IsFolded = newFolding.DefaultClosed; section.Tag = newFolding; } section.Title = newFolding.Name; } // Remove all outstanding old foldings. while (oldFoldingIndex < oldFoldings.Length) { var oldSection = oldFoldings[oldFoldingIndex++]; manager.RemoveFolding(oldSection); } }
private void UpdateFoldings() { if (Foldings == null) { return; } if (_foldingManager == null) { return; } _foldingManager.Clear(); foreach (var folding in Foldings) { var foldingSection = _foldingManager.CreateFolding(folding.StartOffset, folding.EndOffset); foldingSection.Title = folding.Name; foldingSection.IsFolded = folding.IsFolded; } }
void CreateDefaultEntries(string language, out IHighlightingItem defaultText, IList <IHighlightingItem> items) { // Create entry for "default text/background" defaultText = new SimpleHighlightingItem(CustomizingHighlighter.DefaultTextAndBackground, ta => ta.Document.Text = "Normal text") { Foreground = SystemColors.WindowTextColor, Background = SystemColors.WindowColor }; defaultText = new CustomizedHighlightingItem(customizationList, defaultText, language, canSetFont: false); defaultText.PropertyChanged += item_PropertyChanged; items.Add(defaultText); // Create entry for "Selected text" IHighlightingItem selectedText = new SimpleHighlightingItem( CustomizingHighlighter.SelectedText, ta => { ta.Document.Text = "Selected text"; ta.Selection = Selection.Create(ta, 0, 13); }) { Foreground = SystemColors.HighlightTextColor, Background = SystemColors.HighlightColor }; selectedText = new CustomizedHighlightingItem(customizationList, selectedText, language, canSetFont: false); selectedText.PropertyChanged += item_PropertyChanged; items.Add(selectedText); // Create entry for "Non-printable characters" IHighlightingItem nonPrintChars = new SimpleHighlightingItem( CustomizingHighlighter.NonPrintableCharacters, ta => { ta.Document.Text = " \r \r\n \n"; }) { Foreground = Colors.LightGray }; nonPrintChars = new CustomizedHighlightingItem(customizationList, nonPrintChars, language, canSetFont: false, canSetBackground: false); nonPrintChars.PropertyChanged += item_PropertyChanged; items.Add(nonPrintChars); // Create entry for "Line numbers" IHighlightingItem lineNumbers = new SimpleHighlightingItem( CustomizingHighlighter.LineNumbers, ta => { ta.Document.Text = "These are just" + Environment.NewLine + "multiple" + Environment.NewLine + "lines of" + Environment.NewLine + "text"; }) { Foreground = Colors.Gray }; lineNumbers = new CustomizedHighlightingItem(customizationList, lineNumbers, language, canSetFont: false, canSetBackground: false); lineNumbers.PropertyChanged += item_PropertyChanged; items.Add(lineNumbers); // Create entry for "Bracket highlight" IHighlightingItem bracketHighlight = new SimpleHighlightingItem( BracketHighlightRenderer.BracketHighlight, ta => { ta.Document.Text = "(simple) example"; XshdSyntaxDefinition xshd = (XshdSyntaxDefinition)languageComboBox.SelectedItem; if (xshd == null) { return; } var customizationsForCurrentLanguage = customizationList.Where(c => c.Language == null || c.Language == xshd.Name); BracketHighlightRenderer.ApplyCustomizationsToRendering(bracketHighlighter, customizationsForCurrentLanguage); bracketHighlighter.SetHighlight(new BracketSearchResult(0, 1, 7, 1)); }) { Foreground = BracketHighlightRenderer.DefaultBorder, Background = BracketHighlightRenderer.DefaultBackground }; bracketHighlight = new CustomizedHighlightingItem(customizationList, bracketHighlight, language, canSetFont: false); bracketHighlight.PropertyChanged += item_PropertyChanged; items.Add(bracketHighlight); // Create entry for "Current Line highlight" IHighlightingItem currentLineHighlight = new SimpleHighlightingItem( CustomizingHighlighter.CurrentLineHighlighter, ta => { ta.Document.Text = "example text line"; ta.TextView.Options.HighlightCurrentLine = true; }) { Foreground = Color.FromArgb(52, 0, 255, 110), Background = Color.FromArgb(22, 20, 220, 224) }; currentLineHighlight = new CustomizedHighlightingItem(customizationList, currentLineHighlight, language, canSetFont: false); currentLineHighlight.PropertyChanged += item_PropertyChanged; items.Add(currentLineHighlight); // Create entry for "Folding controls" IHighlightingItem foldingControls = new SimpleHighlightingItem( FoldingControls, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10); }) { Foreground = Colors.Gray, Background = Colors.White }; foldingControls = new CustomizedHighlightingItem(customizationList, foldingControls, language, canSetFont: false); foldingControls.PropertyChanged += item_PropertyChanged; items.Add(foldingControls); // Create entry for "Selected folding controls" IHighlightingItem selectedFoldingControls = new SimpleHighlightingItem( FoldingSelectedControls, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10); }) { Foreground = Colors.Black, Background = Colors.White }; selectedFoldingControls = new CustomizedHighlightingItem(customizationList, selectedFoldingControls, language, canSetFont: false); selectedFoldingControls.PropertyChanged += item_PropertyChanged; items.Add(selectedFoldingControls); // Create entry for "Folding text markers" IHighlightingItem foldingTextMarker = new SimpleHighlightingItem( FoldingTextMarkers, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10).IsFolded = true; }) { Foreground = Colors.Gray }; foldingTextMarker = new CustomizedHighlightingItem(customizationList, foldingTextMarker, language, canSetFont: false, canSetBackground: false); foldingTextMarker.PropertyChanged += item_PropertyChanged; items.Add(foldingTextMarker); IHighlightingItem linkText = new SimpleHighlightingItem( CustomizingHighlighter.LinkText, ta => { ta.Document.Text = "http://icsharpcode.net" + Environment.NewLine + "*****@*****.**"; }) { Foreground = Colors.Blue, Background = Colors.Transparent }; linkText = new CustomizedHighlightingItem(customizationList, linkText, language, canSetFont: false); linkText.PropertyChanged += item_PropertyChanged; items.Add(linkText); IHighlightingItem errorMarker = new SimpleHighlightingItem( ErrorPainter.ErrorColorName, ta => { ta.Document.Text = "some error"; ITextMarker marker = textMarkerService.Create(0, 5); marker.Tag = (Action <IHighlightingItem, ITextMarker>) delegate(IHighlightingItem item, ITextMarker m) { m.MarkerTypes = TextMarkerTypes.SquigglyUnderline; m.MarkerColor = item.Foreground; }; }) { Foreground = Colors.Red }; errorMarker = new CustomizedHighlightingItem(customizationList, errorMarker, language, canSetFont: false, canSetBackground: false); errorMarker.PropertyChanged += item_PropertyChanged; items.Add(errorMarker); IHighlightingItem warningMarker = new SimpleHighlightingItem( ErrorPainter.WarningColorName, ta => { ta.Document.Text = "some warning"; ITextMarker marker = textMarkerService.Create(0, 5); marker.Tag = (Action <IHighlightingItem, ITextMarker>) delegate(IHighlightingItem item, ITextMarker m) { m.MarkerTypes = TextMarkerTypes.SquigglyUnderline; m.MarkerColor = item.Foreground; }; }) { Foreground = Colors.Orange }; warningMarker = new CustomizedHighlightingItem(customizationList, warningMarker, language, canSetFont: false, canSetBackground: false); warningMarker.PropertyChanged += item_PropertyChanged; items.Add(warningMarker); IHighlightingItem messageMarker = new SimpleHighlightingItem( ErrorPainter.MessageColorName, ta => { ta.Document.Text = "some message"; ITextMarker marker = textMarkerService.Create(0, 5); marker.Tag = (Action <IHighlightingItem, ITextMarker>) delegate(IHighlightingItem item, ITextMarker m) { m.MarkerTypes = TextMarkerTypes.SquigglyUnderline; m.MarkerColor = item.Foreground; }; }) { Foreground = Colors.Blue }; messageMarker = new CustomizedHighlightingItem(customizationList, messageMarker, language, canSetFont: false, canSetBackground: false); messageMarker.PropertyChanged += item_PropertyChanged; items.Add(messageMarker); IHighlightingItem breakpointMarker = new SimpleHighlightingItem( BookmarkBase.BreakpointMarkerName, ta => { ta.Document.Text = "some code with a breakpoint"; ITextMarker marker = textMarkerService.Create(0, ta.Document.TextLength); marker.Tag = (Action <IHighlightingItem, ITextMarker>) delegate(IHighlightingItem item, ITextMarker m) { m.BackgroundColor = item.Background; m.ForegroundColor = item.Foreground; }; }) { Background = BookmarkBase.BreakpointDefaultBackground, Foreground = BookmarkBase.BreakpointDefaultForeground }; breakpointMarker = new CustomizedHighlightingItem(customizationList, breakpointMarker, language, canSetFont: false); breakpointMarker.PropertyChanged += item_PropertyChanged; items.Add(breakpointMarker); IHighlightingItem currentStatementMarker = new SimpleHighlightingItem( BookmarkBase.CurrentLineBookmarkName, ta => { ta.Document.Text = "current statement line"; ITextMarker marker = textMarkerService.Create(0, ta.Document.TextLength); marker.Tag = (Action <IHighlightingItem, ITextMarker>) delegate(IHighlightingItem item, ITextMarker m) { m.BackgroundColor = item.Background; m.ForegroundColor = item.Foreground; }; }) { Background = BookmarkBase.CurrentLineDefaultBackground, Foreground = BookmarkBase.CurrentLineDefaultForeground }; currentStatementMarker = new CustomizedHighlightingItem(customizationList, currentStatementMarker, language, canSetFont: false); currentStatementMarker.PropertyChanged += item_PropertyChanged; items.Add(currentStatementMarker); IHighlightingItem columnRuler = new SimpleHighlightingItem( CustomizingHighlighter.ColumnRuler, ta => { ta.Document.Text = "some line with a lot of text"; ta.TextView.Options.ColumnRulerPosition = 15; ta.TextView.Options.ShowColumnRuler = true; }) { Foreground = Colors.LightGray }; columnRuler = new CustomizedHighlightingItem(customizationList, columnRuler, language, canSetFont: false, canSetBackground: false); columnRuler.PropertyChanged += item_PropertyChanged; items.Add(columnRuler); }
void CreateDefaultEntries(string language, out IHighlightingItem defaultText) { // Create entry for "default text/background" defaultText = new SimpleHighlightingItem(CustomizableHighlightingColorizer.DefaultTextAndBackground, ta => ta.Document.Text = "Normal text") { Foreground = SystemColors.WindowTextColor, Background = SystemColors.WindowColor }; defaultText = new CustomizedHighlightingItem(customizationList, defaultText, null, canSetFont: false); if (language != null) { defaultText = new CustomizedHighlightingItem(customizationList, defaultText, language, canSetFont: false); } defaultText.PropertyChanged += item_PropertyChanged; listBox.Items.Add(defaultText); // Create entry for "Selected text" IHighlightingItem selectedText = new SimpleHighlightingItem( CustomizableHighlightingColorizer.SelectedText, ta => { ta.Document.Text = "Selected text"; ta.Selection = Selection.Create(ta, 0, 13); }) { Foreground = SystemColors.HighlightTextColor, Background = SystemColors.HighlightColor }; selectedText = new CustomizedHighlightingItem(customizationList, selectedText, null, canSetFont: false); if (language != null) { selectedText = new CustomizedHighlightingItem(customizationList, selectedText, language, canSetFont: false); } selectedText.PropertyChanged += item_PropertyChanged; listBox.Items.Add(selectedText); // Create entry for "Non-printable characters" IHighlightingItem nonPrintChars = new SimpleHighlightingItem( CustomizableHighlightingColorizer.NonPrintableCharacters, ta => { ta.Document.Text = " \r \r\n \n"; }) { Foreground = Colors.LightGray }; nonPrintChars = new CustomizedHighlightingItem(customizationList, nonPrintChars, null, canSetFont: false, canSetBackground: false); if (language != null) { nonPrintChars = new CustomizedHighlightingItem(customizationList, nonPrintChars, language, canSetFont: false); } nonPrintChars.PropertyChanged += item_PropertyChanged; listBox.Items.Add(nonPrintChars); // Create entry for "Line numbers" IHighlightingItem lineNumbers = new SimpleHighlightingItem( CustomizableHighlightingColorizer.LineNumbers, ta => { ta.Document.Text = "These are just" + Environment.NewLine + "multiple" + Environment.NewLine + "lines of" + Environment.NewLine + "text"; }) { Foreground = Colors.Gray }; lineNumbers = new CustomizedHighlightingItem(customizationList, lineNumbers, null, canSetFont: false, canSetBackground: false); if (language != null) { lineNumbers = new CustomizedHighlightingItem(customizationList, lineNumbers, language, canSetFont: false); } lineNumbers.PropertyChanged += item_PropertyChanged; listBox.Items.Add(lineNumbers); // Create entry for "Bracket highlight" IHighlightingItem bracketHighlight = new SimpleHighlightingItem( BracketHighlightRenderer.BracketHighlight, ta => { ta.Document.Text = "(simple) example"; XshdSyntaxDefinition xshd = (XshdSyntaxDefinition)languageComboBox.SelectedItem; if (xshd == null) { return; } var customizationsForCurrentLanguage = customizationList.Where(c => c.Language == null || c.Language == xshd.Name); BracketHighlightRenderer.ApplyCustomizationsToRendering(bracketHighlighter, customizationsForCurrentLanguage); bracketHighlighter.SetHighlight(new BracketSearchResult(0, 1, 7, 1)); }) { Foreground = BracketHighlightRenderer.DefaultBorder, Background = BracketHighlightRenderer.DefaultBackground }; bracketHighlight = new CustomizedHighlightingItem(customizationList, bracketHighlight, null, canSetFont: false); if (language != null) { bracketHighlight = new CustomizedHighlightingItem(customizationList, bracketHighlight, language, canSetFont: false); } bracketHighlight.PropertyChanged += item_PropertyChanged; listBox.Items.Add(bracketHighlight); // Create entry for "Folding controls" IHighlightingItem foldingControls = new SimpleHighlightingItem( FoldingControls, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10); }) { Foreground = Colors.Gray, Background = Colors.White }; foldingControls = new CustomizedHighlightingItem(customizationList, foldingControls, null, canSetFont: false); if (language != null) { foldingControls = new CustomizedHighlightingItem(customizationList, foldingControls, language, canSetFont: false); } foldingControls.PropertyChanged += item_PropertyChanged; listBox.Items.Add(foldingControls); // Create entry for "Selected folding controls" IHighlightingItem selectedFoldingControls = new SimpleHighlightingItem( FoldingSelectedControls, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10); }) { Foreground = Colors.Black, Background = Colors.White }; selectedFoldingControls = new CustomizedHighlightingItem(customizationList, selectedFoldingControls, null, canSetFont: false); if (language != null) { selectedFoldingControls = new CustomizedHighlightingItem(customizationList, selectedFoldingControls, language, canSetFont: false); } selectedFoldingControls.PropertyChanged += item_PropertyChanged; listBox.Items.Add(selectedFoldingControls); // Create entry for "Folding text markers" IHighlightingItem foldingTextMarker = new SimpleHighlightingItem( FoldingTextMarkers, ta => { ta.Document.Text = "This" + Environment.NewLine + "is a folding" + Environment.NewLine + "example"; foldingManager.CreateFolding(0, 10).IsFolded = true; }) { Foreground = Colors.Gray }; foldingTextMarker = new CustomizedHighlightingItem(customizationList, foldingTextMarker, null, canSetFont: false, canSetBackground: false); if (language != null) { foldingControls = new CustomizedHighlightingItem(customizationList, foldingTextMarker, language, canSetFont: false, canSetBackground: false); } foldingTextMarker.PropertyChanged += item_PropertyChanged; listBox.Items.Add(foldingTextMarker); }