diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..dfe0770
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6017c92
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+build
+out
+.gradle
+.idea
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cf2a455
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2018 Runemoro
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..b54440a
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,30 @@
+plugins {
+ id 'java'
+ id 'org.jetbrains.intellij' version '0.3.12'
+}
+
+group 'org.dimdev'
+version '1.0'
+
+sourceCompatibility = 1.8
+
+repositories {
+ mavenCentral()
+ maven {
+ name = 'Fabric'
+ url = 'http://maven.modmuss50.me/'
+ }
+}
+
+dependencies {
+ testCompile 'junit:junit:4.12'
+ compile 'enigma-asm:enigma:0.12.0.33:lib'
+}
+
+jar {
+ from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
+}
+
+intellij {
+ version '2018.3'
+}
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..28861d2
Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..115e6ac
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/gradlew b/gradlew
new file mode 100644
index 0000000..cccdd3d
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,172 @@
+#!/usr/bin/env sh
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=$(save "$@")
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
+if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
+ cd "$(dirname "$0")"
+fi
+
+exec "$JAVACMD" "$@"
diff --git a/gradlew.bat b/gradlew.bat
new file mode 100644
index 0000000..f955316
--- /dev/null
+++ b/gradlew.bat
@@ -0,0 +1,84 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/settings.gradle b/settings.gradle
new file mode 100644
index 0000000..eebab9c
--- /dev/null
+++ b/settings.gradle
@@ -0,0 +1,2 @@
+rootProject.name = 'knit'
+
diff --git a/src/main/java/org/dimdev/knit/EnableRemappingAction.java b/src/main/java/org/dimdev/knit/EnableRemappingAction.java
new file mode 100644
index 0000000..abd3f80
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/EnableRemappingAction.java
@@ -0,0 +1,36 @@
+package org.dimdev.knit;
+
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.ToggleAction;
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.fileChooser.FileChooser;
+import com.intellij.openapi.fileChooser.FileChooserDescriptor;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.File;
+
+public class EnableRemappingAction extends ToggleAction {
+ private static final FileChooserDescriptor CHOOSE_FOLDER_DESCRIPTOR = new FileChooserDescriptor(false, true, false, false, false, false);
+ public final MappingsService mappingsService = ServiceManager.getService(MappingsService.class);
+
+ @Override
+ public boolean isSelected(@NotNull AnActionEvent e) {
+ return mappingsService.hasMappings();
+ }
+
+ @Override
+ public void setSelected(@NotNull AnActionEvent e, boolean state) {
+ if (state) {
+ // TODO: use custom dialog with "Select Enigma mappings folder" title
+ VirtualFile virtualFile = FileChooser.chooseFile(CHOOSE_FOLDER_DESCRIPTOR, null, null);
+
+ if (virtualFile != null) {
+ mappingsService.loadMappings(new File(virtualFile.getPath()));
+ }
+ } else {
+ mappingsService.saveMappings();
+ mappingsService.clearMappings();
+ }
+ }
+}
diff --git a/src/main/java/org/dimdev/knit/KnitApplicationInitializedListener.java b/src/main/java/org/dimdev/knit/KnitApplicationInitializedListener.java
new file mode 100644
index 0000000..e38e822
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/KnitApplicationInitializedListener.java
@@ -0,0 +1,46 @@
+package org.dimdev.knit;
+
+import com.intellij.ide.ApplicationInitializedListener;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.project.ProjectManager;
+import com.intellij.openapi.project.ProjectManagerListener;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiParameter;
+import com.intellij.refactoring.listeners.RefactoringEventData;
+import com.intellij.refactoring.listeners.RefactoringEventListener;
+import com.intellij.util.messages.MessageBus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public class KnitApplicationInitializedListener implements ApplicationInitializedListener {
+ @Override
+ public void componentsInitialized() {
+ MessageBus messageBus = ApplicationManager.getApplication().getMessageBus();
+ messageBus.connect().subscribe(ProjectManager.TOPIC, new ProjectManagerListener() {
+ @Override
+ public void projectOpened(@NotNull Project project) {
+ project.getMessageBus().connect().subscribe(RefactoringEventListener.REFACTORING_EVENT_TOPIC, new RefactoringEventListener() {
+ private RenameHandler renameHandler = new RenameHandler();
+
+ @Override
+ public void refactoringStarted(@NotNull String refactoringId, @Nullable RefactoringEventData beforeData) {}
+
+ @Override
+ public void refactoringDone(@NotNull String refactoringId, @Nullable RefactoringEventData afterData) {
+ PsiElement element = afterData.getUserData(RefactoringEventData.PSI_ELEMENT_KEY);
+ if (element instanceof PsiParameter) {
+ renameHandler.handleRename(null, element);
+ }
+ }
+
+ @Override
+ public void conflictsDetected(@NotNull String refactoringId, @NotNull RefactoringEventData conflictsData) {}
+
+ @Override
+ public void undoRefactoring(@NotNull String refactoringId) {}
+ });
+ }
+ });
+ }
+}
diff --git a/src/main/java/org/dimdev/knit/KnitRefactoringListenerProvider.java b/src/main/java/org/dimdev/knit/KnitRefactoringListenerProvider.java
new file mode 100644
index 0000000..18488a4
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/KnitRefactoringListenerProvider.java
@@ -0,0 +1,46 @@
+package org.dimdev.knit;
+
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.psi.*;
+import com.intellij.refactoring.listeners.RefactoringElementListener;
+import com.intellij.refactoring.listeners.RefactoringElementListenerProvider;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public class KnitRefactoringListenerProvider implements RefactoringElementListenerProvider {
+ private final MappingsService mappingsService = ServiceManager.getService(MappingsService.class);
+ private final RenameHandler renameHandler = new RenameHandler();
+
+ @Nullable
+ @Override
+ public RefactoringElementListener getListener(PsiElement element) {
+ if (!mappingsService.hasMappings()) {
+ return null;
+ }
+
+ String oldName;
+ if (element instanceof PsiClass) {
+ oldName = RenameHandler.getClassName((PsiClass) element);
+ } else if (element instanceof PsiMethod) {
+ oldName = ((PsiMethod) element).getName();
+ } else if (element instanceof PsiField) {
+ oldName = ((PsiField) element).getName();
+ } else {
+ oldName = null;
+ }
+
+ return new RefactoringElementListener() {
+ @Override
+ public void elementMoved(@NotNull PsiElement newElement) {
+ if (element instanceof PsiClass) {
+ renameHandler.handleRename(oldName, newElement);
+ }
+ }
+
+ @Override
+ public void elementRenamed(@NotNull PsiElement newElement) {
+ renameHandler.handleRename(oldName, newElement);
+ }
+ };
+ }
+}
diff --git a/src/main/java/org/dimdev/knit/MappingsService.java b/src/main/java/org/dimdev/knit/MappingsService.java
new file mode 100644
index 0000000..5ca2e0a
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/MappingsService.java
@@ -0,0 +1,59 @@
+package org.dimdev.knit;
+
+import com.intellij.openapi.ui.Messages;
+import cuchaz.enigma.mapping.Mappings;
+import cuchaz.enigma.mapping.MappingsEnigmaReader;
+import cuchaz.enigma.mapping.MappingsEnigmaWriter;
+import cuchaz.enigma.throwables.MappingParseException;
+
+import java.io.File;
+import java.io.IOException;
+
+public class MappingsService {
+ private final MappingsEnigmaReader mappingsReader = new MappingsEnigmaReader();
+ private final MappingsEnigmaWriter mappingsWriter = new MappingsEnigmaWriter();
+ private File mappingDirectory = null;
+ private Mappings mappings = null;
+
+ public Mappings getMappings() {
+ return mappings;
+ }
+
+ public void loadMappings(File mappingDirectory) {
+ this.mappingDirectory = mappingDirectory;
+ mappings = new Mappings();
+
+ try {
+ mappingsReader.readDirectory(mappings, mappingDirectory);
+ } catch (MappingParseException e) {
+ Messages.showErrorDialog("Exception occured while parsing mappings: " + e, "Mapping Parse Error");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void saveMappings() {
+ if (mappings == null) {
+ return;
+ }
+
+ try {
+ mappingsWriter.writeAsDirectory(mappingDirectory, mappings);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void clearMappings() {
+ mappingDirectory = null;
+ mappings = null;
+ }
+
+ public boolean hasMappings() {
+ return mappings != null;
+ }
+
+ static { // TODO: this should probably go somewhere else
+
+ }
+}
diff --git a/src/main/java/org/dimdev/knit/RenameHandler.java b/src/main/java/org/dimdev/knit/RenameHandler.java
new file mode 100644
index 0000000..eb8639c
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/RenameHandler.java
@@ -0,0 +1,162 @@
+package org.dimdev.knit;
+
+import com.intellij.debugger.engine.JVMNameUtil;
+import com.intellij.lang.jvm.JvmModifier;
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.psi.*;
+import cuchaz.enigma.mapping.*;
+
+public class RenameHandler {
+ private final MappingsService mappingsService = ServiceManager.getService(MappingsService.class);
+
+ public void handleRename(String oldName, PsiElement element) {
+ if (!mappingsService.hasMappings()) {
+ return;
+ }
+
+ if (element instanceof PsiClass) {
+ ClassMapping mapping = getClassMapping(oldName);
+ if (mapping != null) {
+ mappingsService.getMappings().setClassDeobfName(mapping, getClassName((PsiClass) element));
+ }
+ }
+
+ if (element instanceof PsiField) {
+ ClassMapping classMapping = getClassMapping(getClassName(((PsiField) element).getContainingClass()));
+ FieldMapping fieldMapping = getOrCreateFieldMapping((PsiField) element, oldName);
+ classMapping.setFieldName(fieldMapping.getObfName(), fieldMapping.getObfDesc(), ((PsiField) element).getName());
+ }
+
+ if (element instanceof PsiMethod) {
+ ClassMapping classMapping = getClassMapping(getClassName(((PsiMethod) element).getContainingClass()));
+ MethodMapping methodMapping = getOrCreateMethodMapping((PsiMethod) element, oldName);
+ classMapping.setMethodName(methodMapping.getObfName(), methodMapping.getObfDesc(), ((PsiMethod) element).getName());
+ }
+
+ if (element instanceof PsiParameter) {
+ PsiElement declarationScope = ((PsiParameter) element).getDeclarationScope();
+ if (declarationScope instanceof PsiMethod) {
+ MethodMapping mapping = getOrCreateMethodMapping((PsiMethod) declarationScope, ((PsiMethod) declarationScope).getName());
+
+ if (mapping != null) {
+ int index = ((PsiMethod) declarationScope).hasModifier(JvmModifier.STATIC) ? 0 : 1;
+ boolean found = false;
+ for (PsiParameter parameter : ((PsiMethod) declarationScope).getParameterList().getParameters()) {
+ if (parameter.equals(element)) {
+ found = true;
+ break;
+ }
+ index += parameter.getType().equals(PsiType.LONG) || parameter.getType().equals(PsiType.DOUBLE) ? 2 : 1;
+ }
+
+ if (found) {
+ mapping.setLocalVariableName(index, ((PsiParameter) element).getName());
+ }
+ }
+ }
+ }
+ }
+
+ private ClassMapping getClassMapping(String className) {
+ // Try getting deobfuscated class mapping
+ ClassMapping mapping = mappingsService.getMappings().getClassByDeobf(className);
+
+ // If that doesn't work, try getting obfuscated class mapping
+ if (mapping == null) {
+ mapping = mappingsService.getMappings().getClassByObf(className);
+ }
+
+ return mapping;
+ }
+
+ private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualName) {
+ // Get mapping for containing class
+ ClassMapping classMapping = getClassMapping(getClassName(method.getContainingClass()));
+ if (classMapping == null) {
+ return null;
+ }
+
+ // Try getting deobfuscated method mapping
+ MethodDescriptor descriptor = obfuscateDescriptor(new MethodDescriptor(getDescriptor(method)));
+ MethodMapping mapping = classMapping.getMethodByDeobf(actualName, descriptor);
+
+ // If that doesn't work, try getting obfuscated method mapping
+ if (mapping == null) {
+ mapping = classMapping.getMethodByObf(actualName, descriptor);
+ }
+
+ // If that doesn't work either, create a new method mapping
+ if (mapping == null) {
+ mapping = new MethodMapping(actualName, descriptor);
+ classMapping.addMethodMapping(mapping);
+ }
+
+ return mapping;
+ }
+
+ private FieldMapping getOrCreateFieldMapping(PsiField field, String actualName) {
+ // Get mapping for containing class
+ ClassMapping classMapping = getClassMapping(getClassName(field.getContainingClass()));
+ if (classMapping == null) {
+ return null;
+ }
+
+ // Try getting deobfuscated method mapping
+ TypeDescriptor descriptor = obfuscateDescriptor(new TypeDescriptor(getDescriptor(field.getType())));
+ FieldMapping mapping = classMapping.getFieldByDeobf(actualName, descriptor);
+
+ // If that doesn't work, try getting obfuscated method mapping
+ if (mapping == null) {
+ mapping = classMapping.getFieldByObf(actualName, descriptor);
+ }
+
+ // If that doesn't work either, create a new field mapping
+ if (mapping == null) {
+ mapping = new FieldMapping(actualName, descriptor, actualName, Mappings.EntryModifier.UNCHANGED);
+ classMapping.addFieldMapping(mapping);
+ }
+
+ return mapping;
+ }
+
+ public MethodDescriptor obfuscateDescriptor(MethodDescriptor descriptor) {
+ return descriptor.remap(className -> {
+ ClassMapping mapping = mappingsService.getMappings().getClassByDeobf(className);
+ return mapping != null ? mapping.getObfFullName() : className;
+ });
+ }
+
+ public TypeDescriptor obfuscateDescriptor(TypeDescriptor descriptor) {
+ return descriptor.remap(clazzName -> {
+ ClassMapping mapping = mappingsService.getMappings().getClassByDeobf(clazzName);
+ return mapping != null ? mapping.getObfFullName() : clazzName;
+ });
+ }
+
+ public static String getClassName(PsiClass element) {
+ return JVMNameUtil.getClassVMName(element).replace('.', '/');
+ }
+
+ public static String getDescriptor(PsiType type) {
+ StringBuilder descriptor = new StringBuilder();
+ while (type instanceof PsiArrayType) {
+ descriptor.append('[');
+ type = ((PsiArrayType) type).getComponentType();
+ }
+
+ String primitiveSignature = JVMNameUtil.getPrimitiveSignature(type.getCanonicalText());
+ if (primitiveSignature != null) {
+ descriptor.append(primitiveSignature);
+ } else {
+ descriptor.append('L')
+ .append(JVMNameUtil.getJVMQualifiedName(type).getDisplayName(null).replace('.', '/'))
+ .append(';');
+ }
+
+ return descriptor.toString();
+ }
+
+ public static String getDescriptor(PsiMethod method) {
+ return JVMNameUtil.getJVMSignature(method).getDisplayName(null);
+ }
+}
diff --git a/src/main/java/org/dimdev/knit/SaveMappingsAction.java b/src/main/java/org/dimdev/knit/SaveMappingsAction.java
new file mode 100644
index 0000000..c1e7ac2
--- /dev/null
+++ b/src/main/java/org/dimdev/knit/SaveMappingsAction.java
@@ -0,0 +1,15 @@
+package org.dimdev.knit;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.components.ServiceManager;
+import org.jetbrains.annotations.NotNull;
+
+public class SaveMappingsAction extends AnAction {
+ private final MappingsService mappingsService = ServiceManager.getService(MappingsService.class);
+
+ @Override
+ public void actionPerformed(@NotNull AnActionEvent e) {
+ mappingsService.saveMappings();
+ }
+}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
new file mode 100644
index 0000000..11e7943
--- /dev/null
+++ b/src/main/resources/META-INF/plugin.xml
@@ -0,0 +1,51 @@
+
Note: You may want to disable IntelliJ's "search and strings and comments" feature. It slows things down + and finds lots of false matches when renaming one letter names.
+ ]]> + + +