Skip to content

Commit

Permalink
vhdl regex folding
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Jul 11, 2023
1 parent 7ccce57 commit 97fbb3c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 66 deletions.
82 changes: 29 additions & 53 deletions src/OneWare.Shared/EditorExtensions/RegexFoldingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,14 @@ namespace OneWare.Shared.EditorExtensions;

public class RegexFoldingStrategy : IFoldingStrategy
{
private const string FoldingStartPattern = @"(?x)
# From the start of the line make sure we are not going into a comment ...
^(
([^-]-?(?!-))*?
(
# Check for keyword ... is
(\b(?i:architecture|case|entity|function|package|procedure)\b(.+?)(?i:\bis)\b)
private readonly Regex FoldingStart;
private readonly Regex FoldingEnd;

# Check for if statements
|(\b(?i:if)\b(.+?)(?i:generate|then)\b)
# Check for and while statements
|(\b(?i:for|while)(.+?)(?i:loop|generate)\b)
# Check for keywords that do not require an is after it
|(\b(?i:component|process|record)\b[^;]*?$)
# From the beginning of the line, check for instantiation maps
|(^\s*\b(?i:port|generic)\b(?i:\s+map\b)?\s*\()
)
)
";

private const string FoldingEndPattern = @"(?x)
# From the start of the line ...
^(
(
(
# Make sure we are not going into a comment ...
([^-]-?(?!-))*?
(
# The word end to the end of the line
(?i:\bend\b).*$\n?
)
)
)
# ... a close paren followed by an optional semicolon as the only thing on the line
|(\s*?\)\s*?;?\s*?$\n?
)
)
";

private static readonly Regex FoldingStart = new(FoldingStartPattern, RegexOptions.Multiline);
private static readonly Regex FoldingEnd = new(FoldingEndPattern, RegexOptions.Multiline);
public RegexFoldingStrategy(Regex foldingStart, Regex foldingEnd)
{
FoldingStart = foldingStart;
FoldingEnd = foldingEnd;
}

public void UpdateFoldings(FoldingManager manager, TextDocument document)
{
Expand All @@ -60,14 +23,27 @@ public void UpdateFoldings(FoldingManager manager, TextDocument document)

public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
{
var start = FoldingStart.Match(document.Text);
var end = FoldingEnd.Match(document.Text, start.Index);

Console.WriteLine(start.Index);
Console.WriteLine(end.Index);

firstErrorOffset = -1;

return new NewFolding[] { new NewFolding(start.Index + start.Length, end.Index + end.Length)};
firstErrorOffset = -1;
var newFoldings = new List<NewFolding>();

var startOffsets = new Stack<int>();
foreach (var line in document.Lines)
{
var lineText = document.GetText(line.Offset, line.Length);
var start = FoldingStart.Match(lineText);
var end = FoldingEnd.Match(lineText);

if (start.Success && !end.Success)
{
startOffsets.Push(line.Offset + start.Index + start.Length);
}

if (end.Success && !start.Success && startOffsets.Any())
{
newFoldings.Add(new NewFolding(startOffsets.Pop(), line.Offset + end.Index));
}
}

return newFoldings.OrderBy(x => x.StartOffset);
}
}
53 changes: 53 additions & 0 deletions src/OneWare.Vhdl/Folding/FoldingRegexVhdl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Text.RegularExpressions;

namespace OneWare.Vhdl.Folding;

public class FoldingRegexVhdl
{
private const string FoldingStartPattern = @"(?x)
# From the start of the line make sure we are not going into a comment ...
^(
([^-]-?(?!-))*?
(
# Check for keyword ... is
(\b(?i:architecture|case|entity|function|package|procedure)\b(.+?)(?i:\bis)\b)
# Check for if statements
|(\b(?i:if)\b(.+?)(?i:generate|then)\b)
# Check for and while statements
|(\b(?i:for|while)(.+?)(?i:loop|generate)\b)
# Check for keywords that do not require an is after it
|(\b(?i:component|process|record)\b[^;]*?$)
# From the beginning of the line, check for instantiation maps
|(^\s*\b(?i:port|generic)\b(?i:\s+map\b)?\s*\()
)
)
";

private const string FoldingEndPattern = @"(?x)
# From the start of the line ...
^(
(
(
# Make sure we are not going into a comment ...
([^-]-?(?!-))*?
(
# The word end to the end of the line
(?i:\bend\b).*$\n?
)
)
)
# ... a close paren followed by an optional semicolon as the only thing on the line
|(\s*?\)\s*?;?\s*?$\n?
)
)
";

public static Regex FoldingStart = new(FoldingStartPattern, RegexOptions.Multiline);

public static Regex FoldingEnd = new(FoldingEndPattern, RegexOptions.Multiline);
}
12 changes: 0 additions & 12 deletions src/OneWare.Vhdl/FoldingStrategyVhdl.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/OneWare.Vhdl/TypeAssistanceVhdl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OneWare.Shared.EditorExtensions;
using OneWare.Shared.LanguageService;
using OneWare.Shared.Services;
using OneWare.Vhdl.Folding;
using OneWare.Vhdl.Indentation;
using Prism.Ioc;

Expand All @@ -17,7 +18,7 @@ internal class TypeAssistanceVhdl : TypeAssistanceLsp
public TypeAssistanceVhdl(IEditor editor, LanguageServiceVhdl ls) : base(editor, ls)
{
CodeBox.TextArea.IndentationStrategy = IndentationStrategy = new VhdlIndentationStrategy(CodeBox.Options);
FoldingStrategy = new RegexFoldingStrategy();
FoldingStrategy = new RegexFoldingStrategy(FoldingRegexVhdl.FoldingStart, FoldingRegexVhdl.FoldingEnd);
}

public override string LineCommentSequence => "--";
Expand Down

0 comments on commit 97fbb3c

Please sign in to comment.