Skip to content

Commit

Permalink
Add support for interfaces to objects (#39)
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
kdorsel authored Jul 11, 2020
1 parent c822eaa commit 846613b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/TcBlack/CompositeCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public CompositeCode Tokenize()
else if (
line.StartsWith("FUNCTION")
|| line.StartsWith("METHOD")
|| line.StartsWith("PROPERTY"))
|| line.StartsWith("PROPERTY")
|| line.StartsWith("INTERFACE"))
{
Add(new ObjectDefinition(
unformattedCode: line,
Expand Down
30 changes: 30 additions & 0 deletions src/TcBlack/ObjectDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,42 @@ private TcObject Tokenize()
{
return TokenizeFunctionBlock();
}
else if (_unformattedCode.Contains("INTERFACE"))
{
return TokenizeInterface();
}
else
{
return TokenizeMethodOrProperty();
}
}

private TcObject TokenizeInterface()
{
string pattern = @"INTERFACE\s+(\w+)\s*(?:EXTENDS((?:[\s,]+[\w\.]+)+))?";

MatchCollection matches = Regex.Matches(
_unformattedCode, pattern, RegexOptions.IgnoreCase
);
if (matches.Count > 0)
{
Match match = matches[0];
string[] parents = Regex.Split(match.Groups[2].Value, @"[\s,]+");
return new TcObject(
objectType: "INTERFACE",
accessModifier: "",
name: match.Groups[1].Value,
dataType: "",
extends: string.Join(", ", parents.Skip(1)),
implements: ""
);
}
else
{
return new TcObject("", "", "", "", "", "");
}
}

private TcObject TokenizeFunctionBlock()
{
string[] splitDefinition = Regex
Expand Down
11 changes: 6 additions & 5 deletions src/TcBlack/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using CommandLine;

Expand All @@ -14,7 +15,7 @@ class Program
class Options
{
[Option(
HelpText = "TcPOU file(s) to format.",
HelpText = "TcPOU/TcIO file(s) to format.",
SetName = "FilesToFormat"
)]
public IEnumerable<string> Filenames { get; set; }
Expand Down Expand Up @@ -92,11 +93,11 @@ static string[] FilesToFormat(Options options)
{
if (options.Project.Length > 0)
{
Regex extensions = new Regex(@"(TcPOU|TcIO)$");
string projectDirectory = Path.GetDirectoryName(options.Project);

return Directory.GetFiles(
projectDirectory, "*.TcPOU", SearchOption.AllDirectories
);
return Directory.EnumerateFiles(
projectDirectory, "*.*", SearchOption.AllDirectories
).Where(f => extensions.IsMatch(f)).ToArray();
}
else
{
Expand Down
35 changes: 35 additions & 0 deletions src/TcBlackTests/ObjectDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,40 @@ public void MethodsWithReturnTypesWithFullPath(
Assert.Equal(expectedCode, var.Format(ref indents));
}

[Theory]
[InlineData(
"INTERFACE ITF_1",
"INTERFACE ITF_1"
)]
[InlineData(
"INTERFACE ITF_1 EXTENDS I_TcArguments",
"INTERFACE ITF_1 EXTENDS I_TcArguments"
)]
[InlineData(
"INTERFACE ITF_1 EXTENDS I_TcArguments , I_Number2",
"INTERFACE ITF_1 EXTENDS I_TcArguments, I_Number2"
)]
[InlineData(
"INTERFACE ITF_1 EXTENDS I_TcArguments , I_Number2, I_A",
"INTERFACE ITF_1 EXTENDS I_TcArguments, I_Number2, I_A"
)]
[InlineData(
"INTERFACE ITF_1 EXTENDS I_TcArguments,I_Number2,I_A",
"INTERFACE ITF_1 EXTENDS I_TcArguments, I_Number2, I_A"
)]
[InlineData(
"INTERFACE I_Test EXTENDS __SYSTEM.IQueryInterface",
"INTERFACE I_Test EXTENDS __SYSTEM.IQueryInterface"
)]
public void FormatInterfaces(
string originalCode, string expectedCode
)
{
ObjectDefinition var =
new ObjectDefinition(originalCode, " ", "\n");
uint indents = 0;
Assert.Equal(expectedCode, var.Format(ref indents));
}

}
}

0 comments on commit 846613b

Please sign in to comment.