Fix commenting issues

This commit is contained in:
Oystein Kristoffer Tveit 2021-02-24 22:24:22 +01:00
parent 83309ea8d1
commit 80ebb93f5c
2 changed files with 37 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import app.model.ProgrammingLanguage; import app.model.ProgrammingLanguage;
@ -106,8 +107,21 @@ public class Java implements ProgrammingLanguage {
} }
public boolean isCommentedSelection(String selection) { public boolean isCommentedSelection(String selection) {
// TODO: Fix var lines = selection.split("\n");
return selection.startsWith("/*");
boolean midPartStartsWithStar =
lines.length < 2 ||
Arrays.asList(Arrays.copyOfRange(lines, 1, lines.length - 1))
.stream()
.map(l -> l.startsWith(" * "))
.allMatch(b -> b);
return
Stream.of(
selection.startsWith("/*"),
midPartStartsWithStar,
selection.endsWith(" */")
).allMatch(b -> b);
} }
} }

View File

@ -60,46 +60,49 @@ public class Markdown implements ProgrammingLanguage {
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// TODO: fix spacing of arrows String cStart = "<!-- ";
String cEnd = " -->";
String cStartRegex = "<!-- ?";
String cEndRegex = " ?-->\\s{0,}";
public String commentLine(String line) { public String commentLine(String line) {
return "<!-- " + line + " -->"; return cStart + line + cEnd;
} }
public String unCommentLine(String line) { public String unCommentLine(String line) {
return line return line
.replaceFirst("<!-- ?", "") .replaceFirst(cStartRegex, "")
.replaceAll("-->\\s+?", ""); .replaceAll(cEndRegex, "");
} }
public boolean isCommentedLine(String line) { public boolean isCommentedLine(String line) {
return line.startsWith("<!--") && line.endsWith("-->"); return line.startsWith(cStart) && line.endsWith(cEnd);
} }
public String commentSelection(String selection) { public String commentSelection(String selection) {
return return
"<!-- " + cStart + selection + cEnd;
selection.lines()
.map(l -> " * " + l)
.collect(Collectors.joining("\n"))
+ " -->";
} }
public String unCommentSelection(String selection) { public String unCommentSelection(String selection) {
var lines = selection.split("\n"); String[] lines = selection.split("\n");
var firstLine = lines[0]; String firstLine = lines[0];
var lastLine = lines[lines.length - 1]; String lastLine = lines[lines.length - 1];
var midlines = Arrays.copyOfRange(lines, 1, lines.length - 1);
String midLineString =
lines.length < 3 ?
"" :
String.join("\n", Arrays.copyOfRange(lines, 1, lines.length - 1)) + "\n";
return return
firstLine.replaceFirst("<!-- ", "") firstLine.replaceFirst(cStartRegex, "") + "\n"
+ String.join("\n", midlines) + midLineString
+ lastLine.replaceAll(" -->\\s+?", ""); + lastLine.replaceAll(cEndRegex, "");
} }
public boolean isCommentedSelection(String selection) { public boolean isCommentedSelection(String selection) {
return selection.startsWith("<!--") && selection.endsWith("-->"); return selection.startsWith(cStart) && selection.endsWith(cEnd);
} }
} }