Skip to content

Commit

Permalink
Highlighter: added errorIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 3, 2019
1 parent 9e2741c commit c926b8b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
41 changes: 38 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public void setErrorPattern(Pattern errorPattern) {
reader.getHighlighter().setErrorPattern(errorPattern);
}

public void setErrorIndex(int errorIndex) {
reader.getHighlighter().setErrorIndex(errorIndex);
}

public void clearTailTip() {
reader.setTailTip("");
}
Expand Down Expand Up @@ -731,6 +735,7 @@ public boolean tailtipAcceptLine() {
}
clearDescription();
setErrorPattern(null);
setErrorIndex(-1);
cmdDescs.clearTemporaryDescs();
return clearTailTip(LineReader.ACCEPT_LINE);
}
Expand Down Expand Up @@ -773,6 +778,7 @@ private boolean doTailTip(String widget) {
} else {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
setErrorIndex(cmdDesc.getErrorIndex());
}
} else {
Pair<String,Boolean> cmdkey = cmdDescs.evaluateCommandLine(buffer.toString(), buffer.cursor());
Expand All @@ -783,6 +789,7 @@ private boolean doTailTip(String widget) {
} else if (!cmdkey.getV()) {
doDescription(cmdDesc.getMainDescription(descriptionSize));
setErrorPattern(cmdDesc.getErrorPattern());
setErrorIndex(cmdDesc.getErrorIndex());
}
}
return true;
Expand Down Expand Up @@ -1050,7 +1057,20 @@ public void clearTemporaryDescs() {
}

public static class CmdLine {
public enum DescriptionType {COMMAND, METHOD, SYNTAX};
public enum DescriptionType {
/**
* Cursor is at the end of line. The args[0] is completed, the line does not have unclosed opening parenthesis
* and does not end to the closing parenthesis.
*/
COMMAND,
/**
* The part of the line from beginning til cursor has unclosed opening parenthesis.
*/
METHOD,
/**
* The part of the line from beginning til cursor ends to the closing parenthesis.
*/
SYNTAX};
private String line;
private String head;
private String tail;
Expand All @@ -1062,7 +1082,7 @@ public enum DescriptionType {COMMAND, METHOD, SYNTAX};
* @param line Command line
* @param head Command line til cursor, method parameters and opening parenthesis before the cursor are removed.
* @param tail Command line after cursor, method parameters and closing parenthesis after the cursor are removed.
* @param args Parsed command line.
* @param args Parsed command line arguments.
* @param descType Request COMMAND, METHOD or SYNTAX description
*/
public CmdLine(String line, String head, String tail, List<String> args, DescriptionType descType) {
Expand Down Expand Up @@ -1129,6 +1149,7 @@ public static class CmdDesc {
private List<ArgDesc> argsDesc;
private TreeMap<String,List<AttributedString>> optsDesc;
private Pattern errorPattern;
private int errorIndex = -1;

public CmdDesc() {

Expand Down Expand Up @@ -1157,6 +1178,10 @@ public CmdDesc mainDesc(List<AttributedString> mainDesc) {
this.mainDesc = new ArrayList<>(mainDesc);
return this;
}

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

public void setErrorPattern(Pattern errorPattern) {
this.errorPattern = errorPattern;
Expand All @@ -1166,13 +1191,23 @@ public Pattern getErrorPattern() {
return errorPattern;
}

public void setErrorIndex(int errorIndex) {
this.errorIndex = errorIndex;
}

public int getErrorIndex() {
return errorIndex;
}

public List<ArgDesc> getArgsDesc() {
return argsDesc;
}

public List<AttributedString> getMainDescription(int descriptionSize) {
List<AttributedString> out = new ArrayList<>();
if (mainDesc.size() <= descriptionSize) {
if (mainDesc == null) {
// do nothing
} else if (mainDesc.size() <= descriptionSize) {
out = mainDesc;
} else {
int tabs = 0;
Expand Down
3 changes: 2 additions & 1 deletion reader/src/main/java/org/jline/reader/Highlighter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -16,4 +16,5 @@ public interface Highlighter {

AttributedString highlight(LineReader reader, String buffer);
public void setErrorPattern(Pattern errorPattern);
public void setErrorIndex(int errorIndex);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -20,12 +20,18 @@

public class DefaultHighlighter implements Highlighter {
private Pattern errorPattern;
private int errorIndex = -1;

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

@Override
public void setErrorIndex(int errorIndex) {
this.errorIndex = errorIndex;
}

@Override
public AttributedString highlight(LineReader reader, String buffer) {
int underlineStart = -1;
Expand Down Expand Up @@ -65,6 +71,10 @@ public AttributedString highlight(LineReader reader, String buffer) {
if (i == negativeStart) {
sb.style(AttributedStyle::inverse);
}
if (i == errorIndex) {
sb.style(AttributedStyle::inverse);
}

char c = buffer.charAt(i);
if (c == '\t' || c == '\n') {
sb.append(c);
Expand All @@ -85,6 +95,9 @@ public AttributedString highlight(LineReader reader, String buffer) {
if (i == negativeEnd) {
sb.style(AttributedStyle::inverseOff);
}
if (i == errorIndex) {
sb.style(AttributedStyle::inverseOff);
}
}
if (errorPattern != null) {
sb.styleMatches(errorPattern, AttributedStyle.INVERSE);
Expand Down

0 comments on commit c926b8b

Please sign in to comment.