Clean up ProgrammingLanguage code

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-26 14:41:47 +02:00
parent ad9ee1c84e
commit de8d1e9756
3 changed files with 18 additions and 12 deletions

View File

@ -5,22 +5,21 @@ import java.util.regex.Pattern;
/** /**
* An interface describing functions required for a class to * An interface describing functions required for a class to
* provide language specific details and functionality to the * provide language specific details and functionality.
* editor
*/ */
public interface ProgrammingLanguage { public interface ProgrammingLanguage {
/** /**
* The name of the programming language * @return The name of the programming language
*/ */
public String getName(); public String getName();
/** /**
* The map containing the regex and corresponding style-classes to be used for syntax highlighting * @return The map containing the regexes and corresponding style-classes to be used for syntax highlighting
*/ */
public Map<Pattern,String> getPatternMap(); public Map<Pattern,String> getPatternMap();
/** /**
* The pattern containing all regexes for syntax highlighting * @return A combined regex for syntax highlighting
*/ */
public Pattern getPattern(); public Pattern getPattern();
@ -39,8 +38,8 @@ public interface ProgrammingLanguage {
public String unCommentLine(String line); public String unCommentLine(String line);
/** /**
* Whether or not a line is commented
* @param line The text of the line * @param line The text of the line
* @return Whether or not a line is commented
*/ */
public boolean isCommentedLine(String line); public boolean isCommentedLine(String line);
@ -52,15 +51,15 @@ public interface ProgrammingLanguage {
public String commentSelection(String selection); public String commentSelection(String selection);
/** /**
* Uncomment a line * Uncomment an area of text
* @param selection The text of the line to uncomment * @param selection The text of the area to uncomment
* @return The uncommented area * @return The uncommented area
*/ */
public String unCommentSelection(String selection); public String unCommentSelection(String selection);
/** /**
* Whether or not an area of text is commented
* @param selection The content of the area * @param selection The content of the area
* @return Whether or not an area of text is commented
*/ */
public boolean isCommentedSelection(String selection); public boolean isCommentedSelection(String selection);

View File

@ -15,6 +15,7 @@ import app.model.ProgrammingLanguage;
public class Java implements ProgrammingLanguage { public class Java implements ProgrammingLanguage {
private String name = "Java"; private String name = "Java";
private static Map<Pattern, String> pattern;
private static final String[] keywords = new String[] { private static final String[] keywords = new String[] {
"abstract", "assert", "boolean", "break", "byte", "abstract", "assert", "boolean", "break", "byte",
@ -52,9 +53,11 @@ public class Java implements ProgrammingLanguage {
e("(?://.*)|/\\*(?:\\n|.)*?\\*/", "comment") e("(?://.*)|/\\*(?:\\n|.)*?\\*/", "comment")
); );
private static Map<Pattern, String> pattern;
public Java() { public Java() {
this.initializePatternMap();
}
private void initializePatternMap() {
pattern = new LinkedHashMap<>(); pattern = new LinkedHashMap<>();
patternList patternList
.forEach(e -> pattern.put(e.getKey(), e.getValue())); .forEach(e -> pattern.put(e.getKey(), e.getValue()));

View File

@ -14,6 +14,7 @@ import app.model.ProgrammingLanguage;
public class Markdown implements ProgrammingLanguage { public class Markdown implements ProgrammingLanguage {
private String name = "Markdown"; private String name = "Markdown";
private static Map<Pattern, String> pattern;
private static Entry<Pattern, String> e(String k, String v) { private static Entry<Pattern, String> e(String k, String v) {
return new AbstractMap.SimpleEntry<>(Pattern.compile(k), v); return new AbstractMap.SimpleEntry<>(Pattern.compile(k), v);
@ -38,9 +39,12 @@ public class Markdown implements ProgrammingLanguage {
e("\\[\\d+\\]: .*", "source") e("\\[\\d+\\]: .*", "source")
); );
private static Map<Pattern, String> pattern;
public Markdown() { public Markdown() {
this.initializePatternMap();
}
private void initializePatternMap() {
pattern = new LinkedHashMap<>(); pattern = new LinkedHashMap<>();
patternList patternList
.forEach(e -> pattern.put(e.getKey(), e.getValue())); .forEach(e -> pattern.put(e.getKey(), e.getValue()));