Collapse / expand all regions in a file

Hi there,
How do I go about collapsing / expanding all regions in a file?
Thanks,
Rob
Hi Rob,
You should use FullExpand\FullCollapse methods like this:
syntaxEdit1.Outlining.FullExpand();
syntaxEdit1.Outlining.FullCollapse();
Regards,
Andrew
Hi Andrew, That collapsing everything. I am looking to just collapse the regions.
Below is what I thought may work. Do you have any other suggestions?
Thanks, Rob
List ranges = new List();
var count = _syntaxEdit.Outlining.GetOutlineRanges(ranges);
var scriptTextLines = ScriptText.Split("\r\n");
for (var i = 0; i < ranges.Count; i++)
{
var range = ranges[i];
var lineOfInterest = scriptTextLines[range.StartPoint.Y];
if (lineOfInterest.Contains("#region"))
{
_syntaxEdit.Outlining.Collapse(i);
}
}
Hi Rob,
As I understand you wish to collapse only regions contains specific text.
I think it will be better to use such code:
IList ranges = new List();
syntaxEdit1.Find("#region", Alternet.Editor.TextSource.SearchOptions.EntireScope, null, ranges);
if (ranges.Count > 0)
foreach (IRange range in ranges)
syntaxEdit1.Outlining.Collapse(range.StartPoint.Y);
Regards,
Andrew
Hi Andrew,
By "regions" I was referring to C# regions. Which happens to have #region as the keyword, so using range.StartPoint.Y as the argument for the collapse method does the job. 👍
My follow up problem after what event can I call collapse? At the moment I am trying
After Form.Shown && After csParser.TextReparsed then => CollapseRegions();
The Form.Shown => csParser.TextReparsed works for the CodeUtils.FillMethods method.
Hi Rob,
yes this is correct way, as only after parser fully parsed text content, editor has information about outlining regions.
Regards,
Andrew
Hi Andrew, Unfortunately "Form.Shown => csParser.TextReparsed" doesn't work, hence why I asked.
Stepping through the code after CodeUtils.Fill have successfully populated the ComboBoxes, then _syntaxEdit.Outlining.GetOutlineRanges(ranges) produces an empty list.
I also tried calling _syntaxEdit.Outlining.OutlineText(); before the GetOutlineRanges method, without any success.
If I spawn a thread, wait for 2 seconds and then call GetOutlineRanges then the ranges list is populated.
Interestingly when I set breakpoint, I can see the editor however there is no syntax highlighting.
Also tried calling Application.DoEvents() prior to the GetOutlineRanges() without success.
So after I successfully run the CodeUtils.Fill* methods, I then do a one time listen on _syntaxEdit.StatusChanged += RunOnceCollapse;
private void RunOnceCollapse(object? sender, EventArgs e)
{
if (e is NotifyEventArgs args && args.State == (NotifyState.BlockChanged | NotifyState.TextParsed | NotifyState.Outline))
{
CollapseRegions();
_syntaxEdit.StatusChanged -= RunOnceCollapse;
}
}
Does the trick. Is there a nicer way to do all this?
Hi Rob,
For me the following code works just fine, as Outline information should be already available when TextReparsed is called.
csParser1.TextReparsed += CsParser1_TextReparsed;

private void CsParser1_TextReparsed(object sender, EventArgs e)
{
IList ranges = new List();
//csParser1.Outline(ranges);
syntaxEdit1.Outlining.GetOutlineRanges(ranges);
syntaxEdit1.Outlining.FullCollapse();
MessageBox.Show(ranges.Count.ToString());
}
I've tested it with our CodeOutlining quick start project, the latest 6.2 version.
You can also force parser to do all parsing synchronously and then query information related to the outlining with the code like this:
csParser1.Prepare(fileInfo.FullName, syntaxEdit1.Lines, null, false, PrepareReason.TextChanged);
csParser1.ForceReparseText();
var ranges = new List();
csParser1.Outline(ranges);
MessageBox.Show(ranges.Count.ToString());

Finally, you can derive your own class from CsParser, override Outline method and get information about all ranges from there.
Let me know if it helps. If it does not, I'm happy to look at your project to figure out what might have gone wrong.
Regards,
Dmitry