AS1/2/3 - highlight variables and errors on panel next to vertical scrollbar

This commit is contained in:
Jindra Petřík
2025-05-30 01:45:36 +02:00
parent 7556250385
commit 0cfcd9dc25
10 changed files with 344 additions and 178 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ All notable changes to this project will be documented in this file.
- AS1/2/3 - highlight variable definition and all its instances on cursor place
(also in edit mode)
- AS1/2/3 - underline errors in the code (also in edit mode)
- AS1/2/3 - highlight variables and errors on scrollbar
- AS1/2/3 - highlight variables and errors on panel next to vertical scrollbar
### Changed
- AS1/2 - Single DoAction tag inside frame is now displayed directly as frame node
Binary file not shown.
@@ -45,7 +45,7 @@ public abstract class ParseException extends Exception {
* @param line Line number where the exception occurred
*/
public ParseException(String text, long line) {
super("ParseException:" + text + " on line " + line);
super(text + " on line " + line);
this.line = line;
this.text = text;
this.position = -1;
@@ -58,7 +58,7 @@ public abstract class ParseException extends Exception {
* @param line Line number where the exception occurred
*/
public ParseException(String text, long line, long position) {
super("ParseException:" + text + " on line " + line);
super(text + " on line " + line);
this.line = line;
this.text = text;
this.position = position;
@@ -212,7 +212,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
s = lex();
} else {
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.MULTIPLY);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.MULTIPLY)) {
break;
}
String propName = s.value.toString(); //Can be *
int propPosition = s.position;
s = lex();
@@ -246,38 +248,38 @@ public class ActionScript3SimpleParser implements SimpleParser {
private boolean name(List<SimpleParseException> errors, TypeItem thisType, Reference<Boolean> needsActivation, List<NamespaceItem> openedNamespaces, HashMap<String, Integer> registerVars, boolean inFunction, boolean inMethod, List<VariableOrScope> variables, List<DottedChain> importedClasses, ABC abc) throws IOException, AVM2ParseException, SimpleParseException, InterruptedException {
ParsedSymbol s = lex();
DottedChain name = new DottedChain(new String[]{}, new String[]{""});
boolean attribute = false;
String name2 = "";
String lastName = "";
if (s.type == SymbolType.ATTRIBUTE) {
attribute = true;
lastName = s.value.toString();
s = lex();
}
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP);
name2 += s.value.toString();
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP)) {
return true;
}
lastName += s.value.toString();
int identPos = s.position;
s = lex();
boolean attrBracket = false;
String nsSuffix = "";
if (s.type == SymbolType.NAMESPACESUFFIX) {
s = lex();
nsSuffix = "#" + s.value;
lastName += "#" + s.value;
}
name = name.add(attribute, name2, nsSuffix);
String fullName = lastName;
while (s.isType(SymbolType.DOT)) {
variables.add(new Variable(false, name.toPrintableString(true), identPos));
variables.add(new Variable(false, fullName, identPos));
s = lex();
name2 = "";
attribute = false;
lastName = "";
if (s.type == SymbolType.ATTRIBUTE) {
attribute = true;
lastName += s.value.toString();
s = lex();
if (s.type == SymbolType.MULTIPLY) {
name2 += s.value.toString();
lastName += s.value.toString();
identPos = s.position;
} else if (s.group == SymbolGroup.IDENTIFIER) {
name2 += s.value.toString();
lastName += s.value.toString();
identPos = s.position;
} else {
if (s.type != SymbolType.BRACKET_OPEN) {
@@ -287,39 +289,34 @@ public class ActionScript3SimpleParser implements SimpleParser {
continue;
}
} else {
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.NAMESPACE, SymbolType.MULTIPLY);
name2 += s.value.toString();
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.NAMESPACE, SymbolType.MULTIPLY)) {
return true;
}
lastName = s.value.toString();
identPos = s.position;
}
s = lex();
nsSuffix = "";
if (s.type == SymbolType.NAMESPACESUFFIX) {
nsSuffix = "#" + s.value;
lastName += "#" + s.value;
s = lex();
}
name = name.add(attribute, name2, nsSuffix);
fullName += "." + lastName;
}
if (s.type == SymbolType.NAMESPACE_OP) {
String nsname = name.getLast();
boolean nsAtribute = name.isLastAttribute();
s = lex();
if (s.group == SymbolGroup.IDENTIFIER) {
String nsprop = s.value.toString();
variables.add(new Variable(false, (nsAtribute ? "@" : "") + nsname + "::" + nsprop, s.position));
variables.add(new Variable(false, lastName + "::" + nsprop, s.position));
} else if (s.type == SymbolType.BRACKET_OPEN) {
expression(errors, thisType, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, true, variables, false, abc);
expectedType(errors, SymbolType.BRACKET_CLOSE);
}
name = name.getWithoutLast();
s = lex();
} else {
variables.add(new Variable(false, name.toPrintableString(true), identPos));
variables.add(new Variable(false, fullName, identPos));
}
boolean ret = false;
if (!name.isEmpty()) {
ret = true;
}
boolean ret = true;
if (s.type == SymbolType.BRACKET_OPEN) {
lexer.pushback(s);
if (attrBracket) {
@@ -333,7 +330,7 @@ public class ActionScript3SimpleParser implements SimpleParser {
return ret;
}
private void expected(List<SimpleParseException> errors, ParsedSymbol symb, int line, Object... expected) throws IOException, AVM2ParseException, SimpleParseException {
private boolean expected(List<SimpleParseException> errors, ParsedSymbol symb, int line, Object... expected) throws IOException, AVM2ParseException, SimpleParseException {
boolean found = false;
for (Object t : expected) {
if (symb.type == t) {
@@ -355,6 +352,7 @@ public class ActionScript3SimpleParser implements SimpleParser {
}
errors.add(new SimpleParseException("" + expStr + " expected but " + symb.type + " found", line, symb.position));
}
return found;
}
private ParsedSymbol expectedType(List<SimpleParseException> errors, Object... type) throws IOException, AVM2ParseException, SimpleParseException, InterruptedException {
@@ -383,7 +381,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
}
expression(errors, thisType, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, true, variables, false, abc);
s = lex();
expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE);
if (!expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE)) {
break;
}
}
}
@@ -408,7 +408,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
hasRest = true;
s = lex();
}
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
paramNames.add(s.value.toString());
paramPositions.add(s.position);
@@ -513,7 +515,7 @@ public class ActionScript3SimpleParser implements SimpleParser {
return metadata;
}
private void classTraits(List<SimpleParseException> errors, boolean outsidePackage, Reference<Boolean> cinitNeedsActivation, List<DottedChain> importedClasses, List<NamespaceItem> openedNamespaces, String classNameStr, boolean isInterface, Reference<Boolean> iinitNeedsActivation, ABC abc, List<VariableOrScope> classVariables) throws AVM2ParseException, SimpleParseException, IOException, CompilationException, InterruptedException {
private void classTraits(List<SimpleParseException> errors, boolean outsidePackage, Reference<Boolean> cinitNeedsActivation, List<DottedChain> importedClasses, List<NamespaceItem> openedNamespaces, DottedChain pkg, String classNameStr, boolean isInterface, Reference<Boolean> iinitNeedsActivation, ABC abc, List<VariableOrScope> classVariables) throws AVM2ParseException, SimpleParseException, IOException, CompilationException, InterruptedException {
Stack<Loop> cinitLoops = new Stack<>();
Map<Loop, String> cinitLoopLabels = new HashMap<>();
@@ -626,6 +628,8 @@ public class ActionScript3SimpleParser implements SimpleParser {
s = lex();
}
String prefix = isStatic ? pkg.addWithSuffix(classNameStr).toPrintableString(true) + "." : "this.";
switch (s.type) {
case FUNCTION:
s = lex();
@@ -636,8 +640,10 @@ public class ActionScript3SimpleParser implements SimpleParser {
isSetter = true;
s = lex();
}
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.PARENT_OPEN);
String fname = null;
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.PARENT_OPEN)) {
break;
}
String fname;
int fnamePos = s.position;
//fix for methods with name "get" or "set" - they are not getters/setters!
@@ -651,10 +657,12 @@ public class ActionScript3SimpleParser implements SimpleParser {
//isSetter = false;
} else {
errors.add(new SimpleParseException("Missing method name", lexer.yyline(), s.position));
break;
}
} else {
fname = s.value.toString();
}
classVariables.add(new Variable(true, prefix + fname, fnamePos));
if (fname.equals(classNameStr)) { //constructor
if (isStatic) {
errors.add(new SimpleParseException("Constructor cannot be static", lexer.yyline(), s.position));
@@ -709,9 +717,13 @@ public class ActionScript3SimpleParser implements SimpleParser {
errors.add(new SimpleParseException("Interface cannot have namespace fields", lexer.yyline(), s.position));
}
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
String nname = s.value.toString();
int npos = s.position;
classVariables.add(new Variable(true, prefix + nname, npos));
s = lex();
if (s.type == SymbolType.ASSIGN) {
@@ -739,8 +751,10 @@ public class ActionScript3SimpleParser implements SimpleParser {
}
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
classVariables.add(new Variable(true, s.value.toString(), s.position));
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
classVariables.add(new Variable(true, prefix + s.value.toString(), s.position));
s = lex();
if (s.type == SymbolType.NAMESPACESUFFIX) {
@@ -909,8 +923,12 @@ public class ActionScript3SimpleParser implements SimpleParser {
String subNameStr;
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
subNameStr = s.value.toString();
int subNamePos = s.position;
sinitVariables.add(new Variable(true, pkgName.addWithSuffix(subNameStr).toPrintableString(true), subNamePos));
s = lex();
if (s.type == SymbolType.NOT) {
@@ -946,7 +964,7 @@ public class ActionScript3SimpleParser implements SimpleParser {
Reference<Boolean> iinitNeedsActivation = new Reference<>(false);
List<VariableOrScope> classVariables = new ArrayList<>();
classTraits(errors, !inPackage, cinitNeedsActivation, importedClasses, subOpenedNamespaces, subNameStr, isInterface, iinitNeedsActivation, abc, classVariables);
classTraits(errors, !inPackage, cinitNeedsActivation, importedClasses, subOpenedNamespaces, pkgName, subNameStr, isInterface, iinitNeedsActivation, abc, classVariables);
sinitVariables.add(new ClassScope(classVariables));
@@ -969,7 +987,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
}
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
sinitVariables.add(new Variable(true, s.value.toString(), s.position));
s = lex();
if (s.type == SymbolType.COLON) {
@@ -993,8 +1013,11 @@ public class ActionScript3SimpleParser implements SimpleParser {
errors.add(new SimpleParseException("Final flag not allowed for namespaces", lexer.yyline(), s.position));
}
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
sinitVariables.add(new Variable(true, s.value.toString(), s.position));
s = lex();
if (s.type == SymbolType.ASSIGN) {
@@ -1205,7 +1228,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
case VAR:
case CONST:
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER)) {
break;
}
String varIdentifier = s.value.toString();
int varPos = s.position;
s = lex();
@@ -1410,8 +1435,9 @@ public class ActionScript3SimpleParser implements SimpleParser {
while (s.type == SymbolType.CATCH) {
expectedType(errors, SymbolType.PARENT_OPEN);
s = lex();
expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP);
if (!expected(errors, s, lexer.yyline(), SymbolGroup.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP)) {
break;
}
String enamestr = s.value.toString();
int ePos = s.position;
expectedType(errors, SymbolType.COLON);
@@ -153,7 +153,9 @@ public class ActionScript2SimpleParser implements SimpleParser {
}
expression(errors, inFunction, inMethod, inTellTarget, true, variables, false, hasEval);
s = lex();
expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE);
if (!expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE)) {
break;
}
}
return ret;
}
@@ -181,7 +183,9 @@ public class ActionScript2SimpleParser implements SimpleParser {
}
if (!s.isType(SymbolType.COMMA, SymbolType.PARENT_CLOSE)) {
expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE);
if (!expected(errors, s, lexer.yyline(), SymbolType.COMMA, SymbolType.PARENT_CLOSE)) {
break;
}
}
}
List<VariableOrScope> subvariables = new ArrayList<>();
@@ -1620,16 +1624,15 @@ public class ActionScript2SimpleParser implements SimpleParser {
} else {
lexer.pushback(symb);
}
Reference<Boolean> hasEval = new Reference<>(false);
commands(errors, false, false, 0, false, vars, hasEval);
if (inOnHandler) {
expectedType(errors, SymbolType.CURLY_CLOSE);
}
if (lexer.lex().type != SymbolType.EOF) {
errors.add(new SimpleParseException("Parsing finished before end of the file", lexer.yyline(), lexer.yychar()));
}
Reference<Boolean> hasEval = new Reference<>(false);
commands(errors, false, false, 0, false, vars, hasEval);
} catch (ActionParseException ex) {
errors.add(new SimpleParseException(ex.getMessage(), ex.line, ex.position));
}
@@ -0,0 +1,208 @@
/*
* Copyright (C) 2025 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.gui.editor;
import com.jpexs.decompiler.flash.gui.AppStrings;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ScrollBarUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import jsyntaxpane.actions.ActionUtils;
import jsyntaxpane.components.Markers;
/**
*
* @author JPEXS
*/
public class HighlightsPanel extends JPanel {
private final LineMarkedEditorPane editorPane;
private Cursor HAND_CURSOR = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
private Cursor DEFAULT_CURSOR = Cursor.getDefaultCursor();
private Map<Integer, String> errors = new LinkedHashMap<>();
private int scrollBarButtonSize = 0;
private JScrollPane scrollPane;
public HighlightsPanel(LineMarkedEditorPane editorPane) {
this.editorPane = editorPane;
scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, editorPane);
JScrollBar bar = new JScrollBar(JScrollBar.VERTICAL, 0, 1, 0, 100);
scrollBarButtonSize = bar.getPreferredSize().width;
setPreferredSize(new Dimension(16, Integer.MAX_VALUE));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!SwingUtilities.isLeftMouseButton(e)) {
return;
}
int totalLineCount = ActionUtils.getLineCount(editorPane);
Rectangle r = getScrollbarTrackRect();
int line = (e.getY() - r.y) * totalLineCount / r.height;
int linesCountPer1Px = totalLineCount / r.height;
if (!scrollPane.getVerticalScrollBar().isVisible()) {
linesCountPer1Px = 0;
}
for (Highlighter.Highlight highlight : editorPane.getHighlighter().getHighlights()) {
Highlighter.HighlightPainter painter = highlight.getPainter();
if (painter instanceof Markers.SimpleMarker) {
try {
int lineNum = ActionUtils.getLineNumber(editorPane, highlight.getStartOffset());
if (lineNum >= line - linesCountPer1Px && lineNum <= line + linesCountPer1Px) {
editorPane.setCaretPosition(highlight.getStartOffset());
return;
}
} catch (BadLocationException ex) {
//ignore
}
}
}
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int totalLineCount = ActionUtils.getLineCount(editorPane);
Rectangle r = getScrollbarTrackRect();
int line = (e.getY() - r.y) * totalLineCount / r.height;
int linesCountPer1Px = totalLineCount / r.height;
if (!scrollPane.getVerticalScrollBar().isVisible()) {
linesCountPer1Px = 0;
}
int lineStart = line * r.height / totalLineCount;
int lineEnd = (line + 1) * r.height / totalLineCount;
if (e.getY() - r.y < lineStart - 1 || e.getY() - r.y > lineStart + 2) {
if (getCursor() != DEFAULT_CURSOR) {
setCursor(DEFAULT_CURSOR);
}
setToolTipText(null);
return;
}
int currentLine = editorPane.getLine();
for (Highlighter.Highlight highlight : editorPane.getHighlighter().getHighlights()) {
Highlighter.HighlightPainter painter = highlight.getPainter();
if (painter instanceof Markers.SimpleMarker) {
try {
int lineNum = ActionUtils.getLineNumber(editorPane, highlight.getStartOffset());
if (lineNum >= line - linesCountPer1Px && lineNum <= line + linesCountPer1Px) {
if (line != currentLine) {
if (getCursor() != HAND_CURSOR) {
setCursor(HAND_CURSOR);
}
}
if (errors.containsKey(highlight.getStartOffset())) {
setToolTipText(AppStrings.translate("highlighter.error").replace("%error%", errors.get(highlight.getStartOffset())));
return;
}
if (line != currentLine && painter instanceof OccurencesMarker) {
setToolTipText(AppStrings.translate("highlighter.occurences"));
return;
}
if (line == currentLine) {
setToolTipText(AppStrings.translate("highlighter.currentLine"));
}
return;
}
} catch (BadLocationException ex) {
//ignore
}
}
}
if (getCursor() != DEFAULT_CURSOR) {
setCursor(DEFAULT_CURSOR);
}
setToolTipText(null);
}
});
}
public void setErrors(Map<Integer, String> errors) {
this.errors = errors;
}
private Rectangle getScrollbarTrackRect() {
return new Rectangle(0, scrollBarButtonSize, 16, getHeight() + - 2 * scrollBarButtonSize);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle r = getScrollbarTrackRect();
int totalLineCount = ActionUtils.getLineCount(editorPane);
int h = 3;
Highlighter.Highlight[] highlights = editorPane.getHighlighter().getHighlights();
Set<Integer> ignoredLines = new HashSet<>();
int currentLine = editorPane.getLine();
for (Highlighter.Highlight highlight : highlights) {
Highlighter.HighlightPainter painter = highlight.getPainter();
if (painter instanceof Markers.SimpleMarker) {
Markers.SimpleMarker simpleMarker = (Markers.SimpleMarker) painter;
g.setColor(simpleMarker.getColor());
try {
int line = ActionUtils.getLineNumber(editorPane, highlight.getStartOffset());
//prefer error lines
if (painter instanceof WavyUnderLinePainter) {
ignoredLines.add(line);
} else if (ignoredLines.contains(line)) {
continue;
}
float ratio = (float) line / totalLineCount;
int y = r.y + (int) (ratio * r.height);
g.fillRect(0, y, getWidth(), h);
} catch (BadLocationException ex) {
//ignore
}
}
}
if (currentLine >= 0) {
float ratio = (float) currentLine / totalLineCount;
int y = r.y + (int) (ratio * r.height) + 1;
g.setColor(Color.gray);
g.fillRect(0, y, getWidth(), 1);
g.fillOval(16 / 2 - 2, y - 1, 3, 3);
}
}
}
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2025 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.gui.editor;
import java.awt.Color;
import jsyntaxpane.components.Markers;
/**
*
* @author JPEXS
*/
public class OccurencesMarker extends Markers.SimpleMarker {
public OccurencesMarker(Color color) {
super(color);
}
}
@@ -1,89 +0,0 @@
/*
* Copyright (C) 2025 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.gui.editor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JLayer;
import javax.swing.JScrollBar;
import javax.swing.plaf.LayerUI;
import javax.swing.plaf.ScrollBarUI;
import jsyntaxpane.actions.ActionUtils;
/**
*
* @author JPEXS
*/
public class ScrollbarOverlay extends LayerUI<JScrollBar> {
private final LineMarkedEditorPane editorPane;
private final Map<Integer, Color> markedLines = new LinkedHashMap<>();
public ScrollbarOverlay(LineMarkedEditorPane editorPane) {
this.editorPane = editorPane;
}
public void removeMarkers(Color color) {
Set<Integer> lines = new HashSet<>(markedLines.keySet());
for (int key : lines) {
if (markedLines.get(key).equals(color)) {
markedLines.remove(key);
}
}
}
public void clearMarkers() {
markedLines.clear();
}
public void addMarker(int line, Color color) {
markedLines.put(line, color);
}
public void setMarkedLines(Map<Integer, Color> markedLines) {
this.markedLines.clear();
this.markedLines.putAll(markedLines);
}
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
JScrollBar bar = (JScrollBar) ((JLayer<?>) c).getView();
ScrollBarUI ui = bar.getUI();
Rectangle r;
if (ui instanceof TrackRectSubstanceScrollbarUI) {
r = ((TrackRectSubstanceScrollbarUI) ui).getTrackBounds();
} else {
r = new Rectangle(0, 16, 16, bar.getHeight() - 32);
}
int totalLineCount = ActionUtils.getLineCount(editorPane);
for (Map.Entry<Integer, Color> entry : markedLines.entrySet()) {
g.setColor(entry.getValue());
float ratio = (float) entry.getKey() / totalLineCount;
int y = r.y + (int) (ratio * r.height);
g.drawLine(0, y, bar.getWidth(), y);
}
}
}
@@ -75,7 +75,7 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
private static final Color SCROLLBAR_ERROR_COLOR = new Color(0xff0000);
private JEditorPane pane;
private final Set<TokenType> tokenTypes = new HashSet<>();
private Markers.SimpleMarker marker;
private OccurencesMarker marker;
private Markers.SimpleMarker errorMarker;
private Status status;
private Map<Integer, String> errors = new LinkedHashMap<>();
@@ -87,9 +87,7 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
private MouseMotionAdapter mouseMotionAdapter;
private JLayer<JScrollBar> layer;
private ScrollbarOverlay scrollbarOverlay;
private HighlightsPanel highlightsPanel;
private ScrollPaneUI originalScrollPaneUI;
@@ -120,7 +118,7 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
if (token != null && tokenTypes.contains(token.type)) {
addMarkers(token);
}
layer.repaint();
highlightsPanel.repaint();
}
}
@@ -129,12 +127,10 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
*/
public void removeMarkers() {
Markers.removeMarkers(pane, marker);
scrollbarOverlay.removeMarkers(SCROLLBAR_VARIABLE_COLOR);
}
public void removeErrorMarkers() {
Markers.removeMarkers(pane, errorMarker);
scrollbarOverlay.removeMarkers(SCROLLBAR_ERROR_COLOR);
}
private Token getIdentifierTokenAt(SyntaxDocument sDoc, int pos) {
@@ -188,6 +184,8 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
}
void addErrorMarkers() {
highlightsPanel.setErrors(errors);
SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
if (doc == null) {
return;
@@ -197,10 +195,9 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
Token token = getNearestTokenAt(doc, position);
if (token != null) {
Markers.markToken(pane, token, errorMarker);
markPositionOnScrollbar(position, SCROLLBAR_ERROR_COLOR);
}
}
layer.repaint();
highlightsPanel.repaint();
doc.readUnlock();
}
@@ -220,12 +217,10 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
if (definitionToken != null) {
if (definitionPosToReferences.containsKey(definitionPos)) {
Markers.markToken(pane, definitionToken, marker);
markPositionOnScrollbar(definitionToken.start, SCROLLBAR_VARIABLE_COLOR);
for (int i : definitionPosToReferences.get(definitionPos)) {
Token referenceToken = getIdentifierTokenAt(sDoc, i);
if (referenceToken != null) {
Markers.markToken(pane, referenceToken, marker);
markPositionOnScrollbar(referenceToken.start, SCROLLBAR_VARIABLE_COLOR);
}
}
}
@@ -233,19 +228,11 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
sDoc.readUnlock();
}
private void markPositionOnScrollbar(int position, Color color) {
try {
scrollbarOverlay.addMarker(ActionUtils.getLineNumber(pane, position), color);
} catch (BadLocationException ex) {
//ignore
}
}
@Override
public void config(Configuration config) {
Color markerColor = config.getColor(PROPERTY_COLOR, DEFAULT_COLOR);
Color errorColor = config.getColor(PROPERTY_ERRORCOLOR, DEFAULT_ERRORCOLOR);
this.marker = new Markers.SimpleMarker(markerColor);
this.marker = new OccurencesMarker(markerColor);
this.errorMarker = new WavyUnderLinePainter(errorColor); //Markers.SimpleMarker(errorColor);
String types = config.getString(
PROPERTY_TOKENTYPES, DEFAULT_TOKENTYPES);
@@ -290,13 +277,10 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
verticalScrollBar.setUI(new TrackRectSubstanceScrollbarUI(verticalScrollBar));
}
scrollbarOverlay = new ScrollbarOverlay((LineMarkedEditorPane) pane);
layer = new JLayer<>(verticalScrollBar, scrollbarOverlay);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
highlightsPanel = new HighlightsPanel((LineMarkedEditorPane) pane);
//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel panel = (JPanel) SwingUtilities.getAncestorOfClass(JPanel.class, scrollPane);
panel.add(layer, BorderLayout.EAST);
panel.add(highlightsPanel, BorderLayout.EAST);
documentUpdated();
markTokenAt(editor.getCaretPosition());
@@ -335,8 +319,8 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
}
JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, editor);
JPanel panel = (JPanel) SwingUtilities.getAncestorOfClass(JPanel.class, scrollPane);
panel.remove(layer);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.remove(highlightsPanel);
//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.revalidate();
panel.repaint();
scrollPane.setUI(originalScrollPaneUI);
@@ -362,7 +346,7 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
private void documentUpdated() {
errors.clear();
removeErrorMarkers();
layer.repaint();
highlightsPanel.repaint();
try {
SyntaxDocument sDoc = (SyntaxDocument) pane.getDocument();
@@ -393,7 +377,6 @@ public class VariableMarker implements SyntaxComponent, CaretListener, PropertyC
definitionPosToReferences.clear();
referenceToDefinition.clear();
errors.put((int) ex.position, ex.getMessage());
ex.printStackTrace();
}
Timer tim = errorsTimer;
if (tim != null) {
@@ -1058,3 +1058,7 @@ work.debugging.start = Starting Flash content debugger
menu.file.view.alwaysOnTop = Always on top
contextmenu.showDetail = Show detail
highlighter.occurences = Mark occurences
highlighter.currentLine = Current line
highlighter.error = Error: %error%