-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed strings, now compilation units are used instead across the co…
…de base
- Loading branch information
Showing
7 changed files
with
208 additions
and
45 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
src/main/java/org/ucombinator/jaam/visualizer/codeView/CodeAreaGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,157 @@ | ||
package org.ucombinator.jaam.visualizer.codeView; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.strobel.decompiler.languages.java.ast.*; | ||
import javafx.application.Application; | ||
import javafx.scene.Node; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.StackPane; | ||
import javafx.stage.Stage; | ||
|
||
import org.fxmisc.flowless.VirtualizedScrollPane; | ||
import org.fxmisc.richtext.CodeArea; | ||
import org.fxmisc.richtext.LineNumberFactory; | ||
import org.fxmisc.richtext.model.StyleSpans; | ||
import org.fxmisc.richtext.model.StyleSpansBuilder; | ||
|
||
public class CodeAreaGenerator { | ||
|
||
|
||
|
||
public StackPane generateCodeArea() | ||
{ | ||
String text = sampleCode; | ||
|
||
CodeArea codeArea = new CodeArea(); | ||
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); | ||
|
||
codeArea.richChanges() | ||
.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX | ||
.subscribe(change -> { | ||
codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText())); | ||
}); | ||
codeArea.replaceText(0, 0, sampleCode); | ||
|
||
codeArea.setMaxHeight(Double.MAX_VALUE); | ||
|
||
VirtualizedScrollPane scrollPane = new VirtualizedScrollPane(codeArea); | ||
scrollPane.setMaxHeight(Double.MAX_VALUE); | ||
|
||
StackPane result = new StackPane(scrollPane); | ||
|
||
result.setMaxWidth(Double.MAX_VALUE); | ||
result.setMaxHeight(Double.MAX_VALUE); | ||
|
||
return result; | ||
} | ||
|
||
public void addClass(CompilationUnit unit) | ||
{ | ||
AstNodeCollection<TypeDeclaration> types = unit.getTypes(); | ||
|
||
assert types.size() == 1; | ||
|
||
TypeDeclaration typeDeclaration = types.firstOrNullObject(); | ||
|
||
typeDeclaration.getText(); | ||
|
||
String className = typeDeclaration.getName(); | ||
|
||
System.out.println("JUAN " + className); | ||
|
||
System.out.println("PACK " + unit.getPackage().getName()); | ||
|
||
for(AstNode i: typeDeclaration.getChildrenByRole(Roles.TYPE_MEMBER)) | ||
{ | ||
EntityDeclaration entity = (EntityDeclaration)i; | ||
System.out.println(i.getRole() + " " + i.getClass() + " " + entity.getName() + " " + entity.getEntityType()); | ||
} | ||
|
||
//String className = getClassName(unit); | ||
//graph.addClass(className, unit.getText()); | ||
} | ||
|
||
private static final String[] KEYWORDS = new String[] { | ||
"abstract", "assert", "boolean", "break", "byte", | ||
"case", "catch", "char", "class", "const", | ||
"continue", "default", "do", "double", "else", | ||
"enum", "extends", "final", "finally", "float", | ||
"for", "goto", "if", "implements", "import", | ||
"instanceof", "int", "interface", "long", "native", | ||
"new", "package", "private", "protected", "public", | ||
"return", "short", "static", "strictfp", "super", | ||
"switch", "synchronized", "this", "throw", "throws", | ||
"transient", "try", "void", "volatile", "while" | ||
}; | ||
|
||
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b"; | ||
private static final String PAREN_PATTERN = "\\(|\\)"; | ||
private static final String BRACE_PATTERN = "\\{|\\}"; | ||
private static final String BRACKET_PATTERN = "\\[|\\]"; | ||
private static final String SEMICOLON_PATTERN = "\\;"; | ||
private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\""; | ||
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/"; | ||
|
||
private static final Pattern PATTERN = Pattern.compile( | ||
"(?<KEYWORD>" + KEYWORD_PATTERN + ")" | ||
+ "|(?<PAREN>" + PAREN_PATTERN + ")" | ||
+ "|(?<BRACE>" + BRACE_PATTERN + ")" | ||
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")" | ||
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")" | ||
+ "|(?<STRING>" + STRING_PATTERN + ")" | ||
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")" | ||
); | ||
|
||
private static final String sampleCode = String.join("\n", new String[] { | ||
"package com.example;", | ||
"", | ||
"import java.util.*;", | ||
"", | ||
"public class Foo extends Bar implements Baz {", | ||
"", | ||
" /*", | ||
" * multi-line comment", | ||
" */", | ||
" public static void main(String[] args) {", | ||
" // single-line comment", | ||
" for(String arg: args) {", | ||
" if(arg.length() != 0)", | ||
" System.out.println(arg);", | ||
" else", | ||
" System.err.println(\"Warning: empty string as argument\");", | ||
" }", | ||
" }", | ||
"", | ||
"}" | ||
}); | ||
|
||
private static StyleSpans<Collection<String>> computeHighlighting(String text) { | ||
Matcher matcher = PATTERN.matcher(text); | ||
int lastKwEnd = 0; | ||
StyleSpansBuilder<Collection<String>> spansBuilder | ||
= new StyleSpansBuilder<>(); | ||
while(matcher.find()) { | ||
String styleClass = | ||
matcher.group("KEYWORD") != null ? "keyword" : | ||
matcher.group("PAREN") != null ? "paren" : | ||
matcher.group("BRACE") != null ? "brace" : | ||
matcher.group("BRACKET") != null ? "bracket" : | ||
matcher.group("SEMICOLON") != null ? "semicolon" : | ||
matcher.group("STRING") != null ? "string" : | ||
matcher.group("COMMENT") != null ? "comment" : | ||
null; /* never happens */ assert styleClass != null; | ||
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); | ||
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start()); | ||
lastKwEnd = matcher.end(); | ||
} | ||
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); | ||
return spansBuilder.create(); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.