Skip to content

Commit

Permalink
TextMatches
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Mar 15, 2024
1 parent 2977329 commit a1c88db
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
40 changes: 40 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/table/TextMatches.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.table;

import lombok.Value;
import org.openrewrite.Column;
import org.openrewrite.DataTable;
import org.openrewrite.Recipe;

public class TextMatches extends DataTable<TextMatches.Row> {

public TextMatches(Recipe recipe) {
super(recipe, "Text matches",
"Lines matching simple text search.");
}

@Value
public static class Row {
@Column(displayName = "Source path",
description = "The path to the source file.")
String sourcePath;

@Column(displayName = "Match",
description = "The text of the match.")
String match;
}
}
14 changes: 14 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/text/Find.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.marker.SearchResult;
import org.openrewrite.quark.Quark;
import org.openrewrite.remote.Remote;
import org.openrewrite.table.TextMatches;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -36,6 +37,7 @@
@Value
@EqualsAndHashCode(callSuper = false)
public class Find extends Recipe {
transient TextMatches textMatches = new TextMatches(this);

@Override
public String getDisplayName() {
Expand Down Expand Up @@ -128,6 +130,18 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
snippets.add(snippet(rawText.substring(previousEnd, matchStart)));
snippets.add(SearchResult.found(snippet(rawText.substring(matchStart, matcher.end()))));
previousEnd = matcher.end();

int startLine = Math.max(0, rawText.substring(0, matchStart).lastIndexOf('\n') + 1);
int endLine = rawText.indexOf('\n', matcher.end());
if (endLine == -1) {
endLine = rawText.length();
}

textMatches.insertRow(ctx, new TextMatches.Row(
sourceFile.getSourcePath().toString(),
rawText.substring(startLine, matcher.start()) + "~~>" +
rawText.substring(matcher.start(), endLine)
));
}
snippets.add(snippet(rawText.substring(previousEnd)));
return plainText.withText("").withSnippets(snippets);
Expand Down
25 changes: 25 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/text/FindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,38 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.table.TextMatches;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.test.SourceSpecs.dir;
import static org.openrewrite.test.SourceSpecs.text;

class FindTest implements RewriteTest {

@Test
void dataTable() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, null))
.dataTable(TextMatches.Row.class, rows -> {
assertThat(rows).hasSize(1);
assertThat(rows.get(0).getMatch()).isEqualTo("This is ~~>text.");
}),
text(
"""
This is a line above.
This is text.
This is a line below.
""",
"""
This is a line above.
This is ~~>text.
This is a line below.
"""
)
);
}

@DocumentExample
@Test
void regex() {
Expand Down

0 comments on commit a1c88db

Please sign in to comment.