Skip to content

Commit

Permalink
Syntax error highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 25, 2019
1 parent bbdfc0e commit 3911e26
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
28 changes: 27 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.regex.Pattern;

import org.jline.keymap.KeyMap;
import org.jline.reader.Binding;
Expand All @@ -34,7 +35,7 @@
import org.jline.utils.Status;

/**
* Brackets/quotes autopairing and command autosuggestion widgets for jline applications.
* Brackets/quotes autopairing and command autosuggestion widgets for jline applications.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
Expand Down Expand Up @@ -155,6 +156,10 @@ public void setTailTip(String tailTip) {
reader.setTailTip(tailTip);
}

public void setErrorPattern(Pattern errorPattern) {
reader.getHighlighter().setErrorPattern(errorPattern);
}

public void clearTailTip() {
reader.setTailTip("");
}
Expand Down Expand Up @@ -725,6 +730,7 @@ public boolean tailtipAcceptLine() {
setSuggestionType(SuggestionType.COMPLETER);
}
clearDescription();
setErrorPattern(null);
cmdDescs.clearTemporaryDescs();
return clearTailTip(LineReader.ACCEPT_LINE);
}
Expand Down Expand Up @@ -766,6 +772,7 @@ private boolean doTailTip(String widget) {
doCommandTailTip(widget, cmdDesc, args);
} else {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
}
} else {
Pair<String,Boolean> cmdkey = cmdDescs.evaluateCommandLine(buffer.toString(), buffer.cursor());
Expand All @@ -775,6 +782,7 @@ private boolean doTailTip(String widget) {
resetTailTip();
} else if (!cmdkey.getV()) {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
}
}
return true;
Expand Down Expand Up @@ -1112,6 +1120,11 @@ public static class CmdDesc {
private List<AttributedString> mainDesc;
private List<ArgDesc> argsDesc;
private TreeMap<String,List<AttributedString>> optsDesc;
private Pattern errorPattern;

public CmdDesc() {

}

public CmdDesc(List<ArgDesc> argsDesc) {
this(new ArrayList<>(), argsDesc, new HashMap<>());
Expand All @@ -1132,6 +1145,19 @@ public CmdDesc(List<AttributedString> mainDesc, List<ArgDesc> argsDesc, Map<Stri
}
}

public CmdDesc mainDesc(List<AttributedString> mainDesc) {
this.mainDesc = new ArrayList<>(mainDesc);
return this;
}

public void setErrorPattern(Pattern errorPattern) {
this.errorPattern = errorPattern;
}

public Pattern getErrorPattern() {
return errorPattern;
}

public List<ArgDesc> getArgsDesc() {
return argsDesc;
}
Expand Down
3 changes: 3 additions & 0 deletions reader/src/main/java/org/jline/reader/Highlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
*/
package org.jline.reader;

import java.util.regex.Pattern;

import org.jline.utils.AttributedString;

public interface Highlighter {

AttributedString highlight(LineReader reader, String buffer);
public void setErrorPattern(Pattern errorPattern);
}
11 changes: 11 additions & 0 deletions reader/src/main/java/org/jline/reader/impl/DefaultHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package org.jline.reader.impl;

import java.util.regex.Pattern;

import org.jline.reader.LineReader;
import org.jline.reader.LineReader.RegionType;
import org.jline.reader.Highlighter;
Expand All @@ -17,6 +19,12 @@
import org.jline.utils.WCWidth;

public class DefaultHighlighter implements Highlighter {
private Pattern errorPattern;

@Override
public void setErrorPattern(Pattern errorPattern) {
this.errorPattern = errorPattern;
}

@Override
public AttributedString highlight(LineReader reader, String buffer) {
Expand Down Expand Up @@ -78,6 +86,9 @@ public AttributedString highlight(LineReader reader, String buffer) {
sb.style(AttributedStyle::inverseOff);
}
}
if (errorPattern != null) {
sb.styleMatches(errorPattern, AttributedStyle.INVERSE);
}
return sb.toAttributedString();
}

Expand Down

0 comments on commit 3911e26

Please sign in to comment.