-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test - jdk/amber/string tapas #28
base: master
Are you sure you want to change the base?
Changes from all commits
1d019a1
c190cec
05ab2e0
38cd491
4ced4f2
418f7aa
1e71abf
6d54032
8361443
4c3f57e
d53ac3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,12 +65,14 @@ | |
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.swing.text.Document; | ||
import org.netbeans.api.java.lexer.JavaTokenId; | ||
import org.netbeans.api.java.source.CompilationInfo; | ||
import org.netbeans.api.java.source.ElementHandle; | ||
import org.netbeans.api.java.source.JavaParserResultTask; | ||
import org.netbeans.api.java.source.JavaSource.Phase; | ||
import org.netbeans.api.java.source.TreeUtilities; | ||
import org.netbeans.api.java.source.support.CancellableTreePathScanner; | ||
import org.netbeans.api.lexer.PartType; | ||
import org.netbeans.api.lexer.Token; | ||
import org.netbeans.api.lexer.TokenHierarchy; | ||
//import org.netbeans.modules.editor.NbEditorUtilities; | ||
|
@@ -81,6 +83,8 @@ | |
import org.netbeans.modules.parsing.spi.SchedulerEvent; | ||
import org.netbeans.modules.parsing.spi.TaskIndexingMode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jackpot: |
||
import org.openide.filesystems.FileUtil; | ||
import org.openide.util.Exceptions; | ||
import org.openide.util.Pair; | ||
|
||
|
||
/** | ||
|
@@ -249,23 +253,25 @@ protected boolean process(CompilationInfo info, final Document doc, ErrorDescrip | |
|
||
boolean computeUnusedImports = "text/x-java".equals(FileUtil.getMIMEType(info.getFileObject())); | ||
|
||
List<int[]> imports = computeUnusedImports ? new ArrayList<int[]>() : null; | ||
List<Pair<int[], Coloring>> extraColoring = computeUnusedImports ? new ArrayList<>(v.extraColoring) : v.extraColoring; | ||
|
||
if (computeUnusedImports) { | ||
Collection<TreePath> unusedImports = UnusedImports.process(info, cancel); | ||
|
||
if (unusedImports == null) return true; | ||
|
||
Coloring unused = collection2Coloring(Arrays.asList(ColoringAttributes.UNUSED)); | ||
|
||
for (TreePath tree : unusedImports) { | ||
if (cancel.get()) { | ||
return true; | ||
} | ||
|
||
//XXX: finish | ||
imports.add(new int[] { | ||
extraColoring.add(Pair.of(new int[] { | ||
(int) info.getTrees().getSourcePositions().getStartPosition(cu, tree.getLeaf()), | ||
(int) info.getTrees().getSourcePositions().getEndPosition(cu, tree.getLeaf()) | ||
}); | ||
}, unused)); | ||
} | ||
} | ||
|
||
|
@@ -320,7 +326,7 @@ protected boolean process(CompilationInfo info, final Document doc, ErrorDescrip | |
return true; | ||
|
||
if (computeUnusedImports) { | ||
setter.setHighlights(doc, imports, v.preText); | ||
setter.setHighlights(doc, extraColoring, v.preText); | ||
} | ||
|
||
setter.setColorings(doc, newColoring); | ||
|
@@ -407,6 +413,7 @@ private static class DetectorVisitor extends CancellableTreePathScanner<Void, Vo | |
private Map<Element, List<Use>> type2Uses; | ||
private Map<Tree, List<Token>> tree2Tokens; | ||
private List<Token> contextKeywords; | ||
private List<Pair<int[], Coloring>> extraColoring; | ||
private Map<int[], String> preText; | ||
private TokenList tl; | ||
private long memberSelectBypass = -1; | ||
|
@@ -421,6 +428,7 @@ private DetectorVisitor(org.netbeans.api.java.source.CompilationInfo info, final | |
type2Uses = new HashMap<Element, List<Use>>(); | ||
tree2Tokens = new IdentityHashMap<Tree, List<Token>>(); | ||
contextKeywords = new ArrayList<>(); | ||
extraColoring = new ArrayList<>(); | ||
preText = new HashMap<>(); | ||
|
||
tl = new TokenList(info, doc, cancel); | ||
|
@@ -1063,8 +1071,36 @@ public Void visitMemberReference(MemberReferenceTree node, Void p) { | |
return null; | ||
} | ||
|
||
private static final Coloring UNINDENTED_TEXT_BLOCK = | ||
ColoringAttributes.add(ColoringAttributes.empty(), ColoringAttributes.UNINDENTED_TEXT_BLOCK); | ||
|
||
@Override | ||
public Void visitLiteral(LiteralTree node, Void p) { | ||
int startPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), node); | ||
tl.moveToOffset(startPos); | ||
Token t = tl.currentToken(); | ||
if (t != null && t.id() == JavaTokenId.MULTILINE_STRING_LITERAL && t.partType() == PartType.COMPLETE) { | ||
String tokenText = t.text().toString(); | ||
String[] lines = tokenText.split("\n"); | ||
int indent = Arrays.stream(lines, 1, lines.length) | ||
.mapToInt(this::leadingIndent) | ||
.min() | ||
.orElse(0); | ||
int pos = startPos + lines[0].length() + 1; | ||
for (int i = 1; i < lines.length; i++) { | ||
String line = lines[i]; | ||
if (i == lines.length - 1) { | ||
line = line.substring(0, line.length() - 3); | ||
} | ||
String strippendLine = line.replaceAll("[\t ]+$", ""); | ||
int indentedStart = pos + indent; | ||
int indentedEnd = pos + strippendLine.length(); | ||
if (indentedEnd > indentedStart) | ||
extraColoring.add(Pair.of(new int[] {indentedStart, indentedEnd}, UNINDENTED_TEXT_BLOCK)); | ||
pos += line.length() + 1; | ||
} | ||
} | ||
|
||
TreePath pp = getCurrentPath().getParentPath(); | ||
if (pp.getLeaf() != null && | ||
pp.getLeaf().getKind() == Kind.METHOD_INVOCATION) { | ||
|
@@ -1085,11 +1121,24 @@ public Void visitLiteral(LiteralTree node, Void p) { | |
return super.visitLiteral(node, p); | ||
} | ||
|
||
private int leadingIndent(String line) { | ||
int indent = 0; | ||
|
||
for (int i = 0; i < line.length(); i++) { //TODO: code points | ||
if (Character.isWhitespace(line.charAt(i))) | ||
indent++; | ||
else | ||
break; | ||
} | ||
|
||
return indent; | ||
} | ||
|
||
} | ||
|
||
public static interface ErrorDescriptionSetter { | ||
|
||
public void setHighlights(Document doc, Collection<int[]> highlights, Map<int[], String> preText); | ||
public void setHighlights(Document doc, Collection<Pair<int[], Coloring>> highlights, Map<int[], String> preText); | ||
public void setColorings(Document doc, Map<Token, Coloring> colorings); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,30 @@ public void run() { | |
}); | ||
} | ||
|
||
public Token currentToken() { | ||
Token[] res = new Token[1]; | ||
doc.render(new Runnable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jackpot: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jackpot: |
||
@Override | ||
public void run() { | ||
if (cancel.get()) { | ||
return ; | ||
} | ||
|
||
if (ts != null && !ts.isValid()) { | ||
cancel.set(true); | ||
return ; | ||
} | ||
|
||
if (ts == null) { | ||
return ; | ||
} | ||
|
||
res[0] = ts.token(); | ||
} | ||
}); | ||
return res[0]; | ||
} | ||
|
||
public void moduleNameHere(final ExpressionTree tree, final Map<Tree, List<Token>> tree2Tokens) { | ||
doc.render(new Runnable() { | ||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jackpot:
warning: Unused Import