Skip to content

Commit

Permalink
fix hotswap of kotlin and inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
LabyStudio committed Jul 17, 2022
1 parent cda0521 commit b6d3baa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
Expand Up @@ -112,64 +112,70 @@ public void actionPerformed(@NotNull AnActionEvent event) {
this.notifyUser("Invalid file to hotswap: " + psiFile.getName(), NotificationType.WARNING);
}

HotSwapProgressImpl progress = new HotSwapProgressImpl(project);
progress.setTitle("Saving opened documents...");

// Save the opened documents
FileDocumentManager.getInstance().saveAllDocuments();

progress.setTitle("Initialize hotswap task...");

// Create compiler and progress
AbstractCompiler compiler = context.compiler(this.configuration);
ClassFile outputFile = context.getClassFile(psiFile);
VirtualFile sourceFile = psiFile.getVirtualFile();

// Get debugger session
DebuggerManagerEx debuggerManager = DebuggerManagerEx.getInstanceEx(project);
DebuggerSession debugger = debuggerManager.getContext().getDebuggerSession();

// Execute application thread
Application application = ApplicationManager.getApplication();
application.executeOnPooledThread(() -> {
ProgressManager.getInstance().runProcess(() -> {
progress.setTitle("Compile classes...");

try {
long start = System.currentTimeMillis();

// Compile the current opened file
List<ClassFile> classFiles = compiler.compile(sourceFile, outputFile);
if (classFiles.isEmpty()) {
String message = "Could not compile " + psiFile.getName();
HotSwapProgressImpl progress = new HotSwapProgressImpl(project);
try {
progress.setTitle("Saving opened documents...");

// Save the opened documents
FileDocumentManager.getInstance().saveAllDocuments();

progress.setTitle("Initialize hotswap task...");

// Create compiler and progress
AbstractCompiler compiler = context.compiler(this.configuration);
ClassFile outputFile = context.getClassFile(psiFile);
VirtualFile sourceFile = psiFile.getVirtualFile();

// Execute application thread
Application application = ApplicationManager.getApplication();
application.executeOnPooledThread(() -> {
ProgressManager.getInstance().runProcess(() -> {
progress.setTitle("Compile classes...");

try {
long start = System.currentTimeMillis();

// Compile the current opened file
List<ClassFile> classFiles = compiler.compile(sourceFile, outputFile);
if (classFiles.isEmpty()) {
String message = "Could not compile " + psiFile.getName();
progress.addMessage(debugger, MessageCategory.ERROR, message);
return;
}

// Show compile duration
long duration = System.currentTimeMillis() - start;
if (this.configuration.isShowCompileDuration()) {
String message = "Compiled " + classFiles.size() + " classes in " + duration + "ms";
progress.addMessage(debugger, MessageCategory.STATISTICS, message);
}
progress.setTitle("Hotswap classes...");

// Hotswap the file
if (!context.hotswap(debugger, progress, classFiles)) {
String message = "Could not hotswap " + psiFile.getName();
progress.addMessage(debugger, MessageCategory.ERROR, message);
}
} catch (Exception e) {
String message = "Error during hotswap: " + e.getMessage();
progress.addMessage(debugger, MessageCategory.ERROR, message);
return;
}

// Show compile duration
long duration = System.currentTimeMillis() - start;
if (this.configuration.isShowCompileDuration()) {
String message = "Compiled " + classFiles.size() + " classes in " + duration + "ms";
progress.addMessage(debugger, MessageCategory.STATISTICS, message);
}
progress.setTitle("Hotswap classes...");

// Hotswap the file
if (!context.hotswap(debugger, progress, classFiles)) {
String message = "Could not hotswap " + psiFile.getName();
progress.addMessage(debugger, MessageCategory.ERROR, message);
}
} catch (Exception e) {
String message = "Error during hotswap: " + e.getMessage();
progress.addMessage(debugger, MessageCategory.ERROR, message);
}

progress.setTitle("Hotswap completed");
progress.finished();
}, progress.getProgressIndicator());
});
progress.setTitle("Hotswap completed");
progress.finished();
}, progress.getProgressIndicator());
});
} catch (Exception e) {
String message = "Can't initialize hotswap task: " + e.getMessage();
progress.addMessage(debugger, MessageCategory.ERROR, message);
progress.finished();
}
} catch (Exception e) {
this.notifyUser("Can't initialize hotswap task: " + e.getMessage(), NotificationType.ERROR);
this.notifyUser("Can't setup hotswap task: " + e.getMessage(), NotificationType.ERROR);
}
}

Expand Down
Expand Up @@ -78,6 +78,17 @@ public List<ClassFile> getInnerClassFiles(ClassFile classFile) {
for (File fileInPackage : filesInPackage) {
String innerFullClassName = fileInPackage.getName();

// Check if it's a static kotlin file
if (innerFullClassName.equals(classFile.getClassName() + "Kt.class")) {
innerClasses.add(new ClassFile(
classFile.getProject(),
fileInPackage,
classFile.getPackageName(),
classFile.getClassName() + "Kt"
));
continue;
}

// Check if it's an inner class of the target class
if (!innerFullClassName.startsWith(classFile.getClassName() + "$")) {
continue;
Expand All @@ -90,7 +101,7 @@ public List<ClassFile> getInnerClassFiles(ClassFile classFile) {
}

String subClassName = innerFileName.substring(0, innerFileName.lastIndexOf(".class"));
String innerClassName = classFile.getClassPath() + "$" + subClassName;
String innerClassName = classFile.getClassName() + "$" + subClassName;

// Collect the inner class of the target class
innerClasses.add(new ClassFile(
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/META-INF/plugin.xml
@@ -1,7 +1,7 @@
<idea-plugin>
<id>net.labymod.intellij.singlehotswap</id>
<name>Single Hotswap</name>
<version>2.0</version>
<version>2.1</version>
<vendor email="labystudio@gmail.com" url="https://www.labymod.net">LabyMedia</vendor>

<idea-version since-build="203.000"/>
Expand Down Expand Up @@ -46,6 +46,10 @@

<change-notes>
<![CDATA[
v2.1 (17.07.2022):
<ul>
<li>Fixed support for kotlin & inner classes</li>
</ul>
v2.0 (16.07.2022):
<ul>
<li>Implemented the built-in java compiler to speed up the compile process</li>
Expand Down

0 comments on commit b6d3baa

Please sign in to comment.