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.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import app.model.ProgrammingLanguage;
@ -106,8 +107,21 @@ public class Java implements ProgrammingLanguage {
}
public boolean isCommentedSelection(String selection) {
// TODO: Fix
return selection.startsWith("/*");
var lines = selection.split("\n");
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) {
return "<!-- " + line + " -->";
return cStart + line + cEnd;
}
public String unCommentLine(String line) {
return line
.replaceFirst("<!-- ?", "")
.replaceAll("-->\\s+?", "");
.replaceFirst(cStartRegex, "")
.replaceAll(cEndRegex, "");
}
public boolean isCommentedLine(String line) {
return line.startsWith("<!--") && line.endsWith("-->");
return line.startsWith(cStart) && line.endsWith(cEnd);
}
public String commentSelection(String selection) {
return
"<!-- " +
selection.lines()
.map(l -> " * " + l)
.collect(Collectors.joining("\n"))
+ " -->";
cStart + selection + cEnd;
}
public String unCommentSelection(String selection) {
var lines = selection.split("\n");
var firstLine = lines[0];
var lastLine = lines[lines.length - 1];
var midlines = Arrays.copyOfRange(lines, 1, lines.length - 1);
String[] lines = selection.split("\n");
String firstLine = lines[0];
String lastLine = lines[lines.length - 1];
String midLineString =
lines.length < 3 ?
"" :
String.join("\n", Arrays.copyOfRange(lines, 1, lines.length - 1)) + "\n";
return
firstLine.replaceFirst("<!-- ", "")
+ String.join("\n", midlines)
+ lastLine.replaceAll(" -->\\s+?", "");
firstLine.replaceFirst(cStartRegex, "") + "\n"
+ midLineString
+ lastLine.replaceAll(cEndRegex, "");
}
public boolean isCommentedSelection(String selection) {
return selection.startsWith("<!--") && selection.endsWith("-->");
return selection.startsWith(cStart) && selection.endsWith(cEnd);
}
}