Skip to content

Commit

Permalink
Count all newlines (#20)
Browse files Browse the repository at this point in the history
* Count all newlines

* Simply count '\n'

* formatting

* Use count method

* Count last line if not empty

* formatting

* count last line if not empty
  • Loading branch information
pstreef authored Apr 22, 2024
1 parent 4543304 commit d1464c2
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/main/java/org/openrewrite/LanguageComposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.yaml.tree.Yaml;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Value
@EqualsAndHashCode(callSuper = false)
Expand All @@ -57,9 +60,9 @@ public String getDisplayName() {
public String getDescription() {
//language=markdown
return "Counts the number of lines of the various kinds of source code and data formats parsed by OpenRewrite. " +
"Comments are not included in line counts. " +
"This recipe emits its results as two data tables, making no changes to any source file. " +
"One data table is per-file, the other is per-repository.";
"Comments are not included in line counts. " +
"This recipe emits its results as two data tables, making no changes to any source file. " +
"One data table is per-file, the other is per-repository.";
}

@Data
Expand Down Expand Up @@ -106,7 +109,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
0,
hasParseFailure));
} else {
int genericLineCount = genericLineCount(s);
int genericLineCount = LineCounter.count(s);
if (s.getClass().getName().startsWith("org.openrewrite.cobol.tree.CobolPreprocessor$Copybook")) {
Counts copybookCounts = acc.getFolderToLanguageToCounts()
.computeIfAbsent(folderPath, k -> new HashMap<>())
Expand Down Expand Up @@ -253,7 +256,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
genericLineCount,
hasParseFailure));
} else if (s instanceof PlainText) {
if(s.getSourcePath().endsWith(".js") || s.getSourcePath().endsWith(".jsx") || s.getSourcePath().endsWith(".mjs")) {
if (s.getSourcePath().endsWith(".js") || s.getSourcePath().endsWith(".jsx") || s.getSourcePath().endsWith(".mjs")) {
Counts javascriptCounts = acc.getFolderToLanguageToCounts()
.computeIfAbsent(folderPath, k -> new HashMap<>())
.computeIfAbsent("Javascript", k -> new Counts());
Expand All @@ -265,7 +268,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
s.getClass().getName(),
genericLineCount,
hasParseFailure));
} else if(s.getSourcePath().endsWith(".ts") || s.getSourcePath().endsWith(".tsx")) {
} else if (s.getSourcePath().endsWith(".ts") || s.getSourcePath().endsWith(".tsx")) {
Counts typescriptCounts = acc.getFolderToLanguageToCounts()
.computeIfAbsent(folderPath, k -> new HashMap<>())
.computeIfAbsent("TypeScript", k -> new Counts());
Expand All @@ -277,7 +280,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
s.getClass().getName(),
genericLineCount,
hasParseFailure));
} else if(s.getSourcePath().endsWith(".py")) {
} else if (s.getSourcePath().endsWith(".py")) {
Counts pythonCounts = acc.getFolderToLanguageToCounts()
.computeIfAbsent(folderPath, k -> new HashMap<>())
.computeIfAbsent("Python", k -> new Counts());
Expand Down Expand Up @@ -358,23 +361,27 @@ private static class Counts {
int fileCount;
}

private static int genericLineCount(SourceFile s) {
LineCounter counter = new LineCounter();
s.printAll(counter);
return counter.getLineCount();
}

private static class LineCounter extends PrintOutputCapture<Integer> {
private int count;
private boolean startedLine;

public LineCounter() {
super(0);
}

static int count(SourceFile s) {
LineCounter counter = new LineCounter();
s.printAll(counter);
return counter.getLineCount();
}

@Override
public PrintOutputCapture<Integer> append(char c) {
if (c == '\n') {
count++;
startedLine = false;
} else {
startedLine = true;
}
return this;
}
Expand All @@ -384,14 +391,15 @@ public PrintOutputCapture<Integer> append(@Nullable String text) {
if (text == null) {
return this;
}
if (text.contains("\n")) {
count += text.split("([\r\n]+)").length;
for (int i = 0; i < text.length(); i++) {
append(text.charAt(i));
}
return this;
}

int getLineCount() {
return count;
return count + (startedLine ? 1 : 0);
}
}

}

0 comments on commit d1464c2

Please sign in to comment.