Skip to content

Commit 7406eeb

Browse files
committed
Apply replace rules and replace for files
1 parent 2f5277f commit 7406eeb

File tree

14 files changed

+115
-48
lines changed

14 files changed

+115
-48
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public IReadOnlyDictionary<string, IPlaceholder> SwitchOnNew()
2626
throw new System.NotImplementedException();
2727
}
2828

29-
public void Set(IReadOnlyDictionary<string, IPlaceholder> placeholders)
29+
public void Fill(IReadOnlyDictionary<string, IPlaceholder> placeholders)
3030
{
3131
throw new System.NotImplementedException();
3232
}

src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,25 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex
2323
var matches = parser.Parse(ref context);
2424
Assert.Equal(matches.Count(), matchesCount);
2525
}
26+
27+
[Theory]
28+
// [InlineData("AssignmentNullUnionOperator")]
29+
[InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)]
30+
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)]
31+
public static void StructuralSearchShouldBe(string exampleName, string exampleFilePath, int matchesCount)
32+
{
33+
var config = ConfigurationMock.GetConfigurationFromFiles(exampleName);
34+
var parser = new StructuralSearchParser(config);
35+
36+
var fileInfo = new FileInfo(exampleFilePath);
37+
var input = Input.File(fileInfo);
38+
IParsingContext context = new ParsingContext(input);
39+
var matches = parser.Parse(ref context);
40+
matches = parser.ApplyFindRule(ref context, matches);
41+
matches = parser.ApplyReplaceRule(ref context, matches);
42+
parser.Replace(ref context, matches);
43+
44+
Assert.Equal(matches.Count(), matchesCount);
45+
}
2646
}
2747
}

src/SimpleStateMachine.StructuralSearch/FindParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public FindParser(SeriesParser parser)
1717
Parser = parser;
1818
}
1919

20-
public IEnumerable<FindParserMatch> Parse(ref IParsingContext context)
20+
public IEnumerable<FindParserResult> Parse(ref IParsingContext context)
2121
{
22-
List<FindParserMatch> matches = new();
22+
List<FindParserResult> matches = new();
2323
StringBuilder res = new();
2424
Parser.SetContext(ref context);
2525

@@ -29,7 +29,7 @@ public IEnumerable<FindParserMatch> Parse(ref IParsingContext context)
2929
.ThenInvoke(match =>
3030
{
3131
var placeholders= parsingContext.SwitchOnNew();
32-
matches.Add(new FindParserMatch(match, placeholders));
32+
matches.Add(new FindParserResult(match, placeholders));
3333
})
3434
.ThenReturn(Unit.Value)
3535
.Try();

src/SimpleStateMachine.StructuralSearch/FindParserResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace SimpleStateMachine.StructuralSearch;
44

5-
public readonly record struct FindParserMatch(Match<string> Match, IReadOnlyDictionary<string, IPlaceholder> Placeholders);
5+
public readonly record struct FindParserResult(Match<string> Match, IReadOnlyDictionary<string, IPlaceholder> Placeholders);

src/SimpleStateMachine.StructuralSearch/IFindParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace SimpleStateMachine.StructuralSearch
77
{
88
public interface IFindParser
99
{
10-
IEnumerable<FindParserMatch> Parse(ref IParsingContext context);
10+
IEnumerable<FindParserResult> Parse(ref IParsingContext context);
1111
}
1212
}

src/SimpleStateMachine.StructuralSearch/IParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IParsingContext
1313
IPlaceholder GetPlaceholder(string name);
1414

1515
IReadOnlyDictionary<string, IPlaceholder> SwitchOnNew();
16-
void Set(IReadOnlyDictionary<string, IPlaceholder>placeholders);
16+
void Fill(IReadOnlyDictionary<string, IPlaceholder>placeholders);
1717
void Clear();
1818
}
1919
}

src/SimpleStateMachine.StructuralSearch/EmptyInput.cs renamed to src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
99
throw new System.NotImplementedException();
1010
}
1111

12-
public void Replace(Match<string> match, string value)
12+
public void ReplaceAsync(Match<string> match, string value)
1313
{
1414
throw new System.NotImplementedException();
1515
}

src/SimpleStateMachine.StructuralSearch/FileInput.cs renamed to src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
1919
return parser.Parse(FileInfo.OpenText());
2020
}
2121

22-
public void Replace(Match<string> match, string value)
22+
public void ReplaceAsync(Match<string> match, string value)
2323
{
24-
using var valueReader = new StringReader(value);
25-
using var streamWriter = new StreamWriter(FileInfo.FullName);
26-
var emptyStart = match.Offset.Start + match.Lenght;
27-
var emptyEnd = match.Offset.End;
28-
valueReader.CopyPartTo(streamWriter, match.Offset.Start, match.Lenght);
29-
24+
var text = File.ReadAllText(Path);
25+
text = text.Replace(match.Value, value);
26+
File.WriteAllText(Path, text);
27+
28+
// using var valueReader = new StringReader(value);
29+
// using var streamWriter = new StreamWriter(FileInfo.FullName);
30+
// var emptyStart = match.Offset.Start + match.Lenght;
31+
// var emptyEnd = match.Offset.End;
32+
// valueReader.CopyPartTo(streamWriter, match.Offset.Start, match.Lenght);
33+
3034
//return (emptyStart, emptyEnd);
3135
}
3236

src/SimpleStateMachine.StructuralSearch/IInput.cs renamed to src/SimpleStateMachine.StructuralSearch/Input/IInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SimpleStateMachine.StructuralSearch
55
public interface IInput
66
{
77
Result<char, T> ParseBy<T>(Parser<char, T> parser);
8-
void Replace(Match<string> match, string value);
8+
void ReplaceAsync(Match<string> match, string value);
99

1010
string Extension { get; }
1111
string Path { get; }

src/SimpleStateMachine.StructuralSearch/StringInput.cs renamed to src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
1616
return parser.Parse(Input);
1717
}
1818

19-
public void Replace(Match<string> match, string value)
19+
public void ReplaceAsync(Match<string> match, string value)
2020
{
2121
throw new System.NotImplementedException();
2222
}

0 commit comments

Comments
 (0)