Added: AS1/2 - highlight variable definition and all its instances on cursor place

Fixed: AS1/2 - Incorrect DefineFunction2 parameter names when parameter name is empty
This commit is contained in:
Jindra Petřík
2025-05-26 22:33:14 +02:00
parent 0faea44de8
commit 75c8639d77
16 changed files with 3350 additions and 557 deletions
+2 -1
View File
@@ -146,6 +146,7 @@ import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.regex.Pattern;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
@@ -2590,7 +2591,7 @@ public class Main {
initUiLang();
initLookAndFeel();
View.execInEventDispatch(() -> {
ErrorLogFrame.createNewInstance();
@@ -115,6 +115,7 @@ import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;
import javax.swing.tree.TreePath;
import jsyntaxpane.DefaultSyntaxKit;
import jsyntaxpane.SyntaxDocument;
import jsyntaxpane.Token;
import jsyntaxpane.TokenType;
@@ -639,6 +640,7 @@ public class ActionPanel extends JPanel implements SearchListener<ScriptSearchRe
editor.setShowMarkers(true);
decompiledEditor.setShowMarkers(Configuration.decompile.get());
decompiledEditor.setSwf(asm.getSwf());
lastASM = asm;
lastCode = actions;
lastDecompiled = decompiledText;
@@ -1030,6 +1032,8 @@ public class ActionPanel extends JPanel implements SearchListener<ScriptSearchRe
editor.setFont(Configuration.getSourceFont());
decompiledEditor.setFont(Configuration.getSourceFont());
decompiledEditor.changeContentType("text/actionscript");
DefaultSyntaxKit kit = (DefaultSyntaxKit) decompiledEditor.getEditorKit();
kit.installComponent(decompiledEditor, ActionVariableMarker.class.getName());
editor.addCaretListener(new CaretListener() {
@Override
@@ -0,0 +1,235 @@
/*
* Copyright (C) 2010-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.action;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.action.parser.ActionParseException;
import com.jpexs.decompiler.flash.action.parser.script.ActionScript2VariableParser;
import com.jpexs.decompiler.flash.gui.editor.DebuggableEditorPane;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import jsyntaxpane.SyntaxDocument;
import jsyntaxpane.Token;
import jsyntaxpane.TokenType;
import jsyntaxpane.actions.ActionUtils;
import jsyntaxpane.components.Markers;
import jsyntaxpane.components.SyntaxComponent;
import jsyntaxpane.util.Configuration;
/**
* This class highlights Variable tokens of ActionScript 1/2
*/
public class ActionVariableMarker implements SyntaxComponent, CaretListener, PropertyChangeListener, DocumentListener {
public static final String DEFAULT_TOKENTYPES = "IDENTIFIER, REGEX";
public static final String PROPERTY_COLOR = "TokenMarker.Color";
public static final String PROPERTY_TOKENTYPES = "ActionVariableMarker.TokenTypes";
private static final Color DEFAULT_COLOR = new Color(0xFFEE66);
private JEditorPane pane;
private final Set<TokenType> tokenTypes = new HashSet<>();
private Markers.SimpleMarker marker;
private Status status;
private Map<Integer, List<Integer>> definitionPosToReferences = new LinkedHashMap<>();
private Map<Integer, Integer> referenceToDefinition = new LinkedHashMap<>();
/**
* Constructs a new Token highlighter
*/
public ActionVariableMarker() {
}
@Override
public void caretUpdate(CaretEvent e) {
markTokenAt(e.getDot());
}
public void markTokenAt(int pos) {
SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
if (doc != null) {
Token token = getIdentifierTokenAt(doc, pos);
removeMarkers();
if (token != null && tokenTypes.contains(token.type)) {
addMarkers(token);
}
}
}
/**
* removes all markers from the pane.
*/
public void removeMarkers() {
Markers.removeMarkers(pane, marker);
}
private Token getIdentifierTokenAt(SyntaxDocument sDoc, int pos) {
Token thisToken = sDoc.getTokenAt(pos);
if (thisToken != null && (thisToken.type == TokenType.IDENTIFIER || thisToken.type == TokenType.REGEX)) {
return thisToken;
}
Token token = sDoc.getTokenAt(pos - 1);
if (token != null && (token.type == TokenType.IDENTIFIER || token.type == TokenType.REGEX) && (token.start + token.length == pos)) {
return token;
}
token = sDoc.getTokenAt(pos + 1);
if (token != null && (token.type == TokenType.IDENTIFIER || token.type == TokenType.REGEX)) {
return token;
}
return null;
}
/**
* add highlights for the given pattern
*
* @param pattern
*/
void addMarkers(Token tok) {
SyntaxDocument sDoc = (SyntaxDocument) pane.getDocument();
sDoc.readLock();
int definitionPos = tok.start;
if (referenceToDefinition.containsKey(tok.start)) {
definitionPos = referenceToDefinition.get(tok.start);
}
Token definitionToken = getIdentifierTokenAt(sDoc, definitionPos);
if (definitionToken != null) {
if (definitionPosToReferences.containsKey(definitionPos)) {
Markers.markToken(pane, definitionToken, marker);
for (int i : definitionPosToReferences.get(definitionPos)) {
Token referenceToken = getIdentifierTokenAt(sDoc, i);
if (referenceToken != null) {
Markers.markToken(pane, referenceToken, marker);
}
}
}
}
sDoc.readUnlock();
}
@Override
public void config(Configuration config) {
Color markerColor = config.getColor(
PROPERTY_COLOR, DEFAULT_COLOR);
this.marker = new Markers.SimpleMarker(markerColor);
String types = config.getString(
PROPERTY_TOKENTYPES, DEFAULT_TOKENTYPES);
for (String type : types.split("\\s*,\\s*")) {
try {
TokenType tt = TokenType.valueOf(type);
tokenTypes.add(tt);
} catch (IllegalArgumentException e) {
LOG.warning("Error in setting up TokenMarker "
+ " - Invalid TokenType: " + type);
}
}
}
@Override
public void install(JEditorPane editor) {
this.pane = editor;
editor.addCaretListener(this);
editor.addPropertyChangeListener(this);
editor.getDocument().addDocumentListener(this);
documentUpdated();
markTokenAt(editor.getCaretPosition());
status = Status.INSTALLING;
}
@Override
public void deinstall(JEditorPane editor) {
status = Status.DEINSTALLING;
removeMarkers();
pane.removePropertyChangeListener(this);
pane.getDocument().removeDocumentListener(this);
pane.removeCaretListener(this);
}
private static final Logger LOG = Logger.getLogger(ActionVariableMarker.class.getName());
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("document")) {
pane.removeCaretListener(this);
pane.getDocument().removeDocumentListener(this);
if (status.equals(Status.INSTALLING)) {
pane.addCaretListener(this);
pane.getDocument().addDocumentListener(this);
removeMarkers();
documentUpdated();
}
}
}
private void documentUpdated() {
try {
SyntaxDocument sDoc = (SyntaxDocument) pane.getDocument();
String fullText = sDoc.getText(0, sDoc.getLength());
SWF swf = null;
if (pane instanceof DebuggableEditorPane) {
DebuggableEditorPane dpane = (DebuggableEditorPane) pane;
swf = dpane.getSwf();
}
if (swf == null) {
return;
}
Map<Integer, List<Integer>> newDefinitionPosToReferences = new LinkedHashMap<>();
Map<Integer, Integer> newReferenceToDefinition = new LinkedHashMap<>();
ActionScript2VariableParser varParser = new ActionScript2VariableParser(swf);
varParser.parse(fullText, newDefinitionPosToReferences, newReferenceToDefinition);
definitionPosToReferences = newDefinitionPosToReferences;
referenceToDefinition = newReferenceToDefinition;
} catch (BadLocationException | ActionParseException | IOException | InterruptedException ex) {
definitionPosToReferences.clear();
referenceToDefinition.clear();
}
}
@Override
public void insertUpdate(DocumentEvent e) {
documentUpdated();
}
@Override
public void removeUpdate(DocumentEvent e) {
documentUpdated();
}
@Override
public void changedUpdate(DocumentEvent e) {
documentUpdated();
}
}
@@ -16,12 +16,14 @@
*/
package com.jpexs.decompiler.flash.gui.editor;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.gui.Main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Set;
import javax.swing.UIManager;
@@ -77,6 +79,8 @@ public class DebuggableEditorPane extends LineMarkedEditorPane implements BreakP
private LineNumbersBreakpointsRuler ruler;
private boolean showMarkers = true;
private WeakReference<SWF> swfRef = new WeakReference(null);
public DebuggableEditorPane() {
@@ -88,6 +92,14 @@ public class DebuggableEditorPane extends LineMarkedEditorPane implements BreakP
}
}
public void setSwf(SWF swf) {
this.swfRef = new WeakReference<>(swf);
}
public SWF getSwf() {
return swfRef.get();
}
public synchronized void setScriptName(String scriptName, String breakPointScriptName) {
this.scriptName = scriptName;
this.breakPointScriptName = breakPointScriptName;