Skip to content

Commit

Permalink
Merge pull request #10 from LabyStudio/develop
Browse files Browse the repository at this point in the history
fix exception when hotswapping with IntelliJ 2023.1 using the new UI, version 2.5, closes #9
  • Loading branch information
LabyStudio committed Apr 17, 2023
2 parents 8856d72 + be9da62 commit 5d42c64
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Expand Up @@ -12,7 +12,7 @@ repositories {
// Configure Gradle IntelliJ Plugin
intellij {
pluginName.set("Single Hotswap")
version.set("2022.3")
version.set("2023.1")
type.set("IC") // Target IDE Platform
plugins.set(listOf("Kotlin", "Groovy", "java", "properties"))

Expand Down
Expand Up @@ -15,8 +15,10 @@
import com.intellij.openapi.compiler.CompilerManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.util.ui.MessageCategory;
Expand Down Expand Up @@ -110,6 +112,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {
// Check if it is possible to hotswap the opened file
if (!context.isPossible(psiFile)) {
this.notifyUser("Invalid file to hotswap: " + psiFile.getName(), NotificationType.WARNING);
return;
}

// Get debugger session
Expand All @@ -127,6 +130,12 @@ public void actionPerformed(@NotNull AnActionEvent event) {
ClassFile outputFile = context.getClassFile(psiFile);
VirtualFile sourceFile = psiFile.getVirtualFile();

Module module = ProjectFileIndex.getInstance(project).getModuleForFile(sourceFile);
if (module == null) {
this.notifyUser("Could not find module for file: " + sourceFile.getName(), NotificationType.WARNING);
return;
}

// Execute progress
HotSwapProgressImpl progress = new HotSwapProgressImpl(project);
Application application = ApplicationManager.getApplication();
Expand All @@ -139,7 +148,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {
long start = System.currentTimeMillis();

// Compile the current opened file
List<ClassFile> classFiles = compiler.compile(sourceFile, outputFile);
List<ClassFile> classFiles = compiler.compile(module, sourceFile, outputFile);
if (classFiles.isEmpty()) {
String message = "Could not compile " + psiFile.getName();
progress.addMessage(debugger, MessageCategory.ERROR, message);
Expand Down
@@ -1,5 +1,6 @@
package net.labymod.intellij.singlehotswap.compiler;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFile;
import net.labymod.intellij.singlehotswap.hotswap.ClassFile;
import net.labymod.intellij.singlehotswap.hotswap.Context;
Expand Down Expand Up @@ -30,5 +31,5 @@ public AbstractCompiler(Context context) {
* @return A list of class files that were compiled. (More than one class file can be compiled if the source file contains inner classes.)
* @throws Exception If an error occurs while compiling the source file.
*/
public abstract List<ClassFile> compile(VirtualFile sourceFile, ClassFile outputFile) throws Exception;
public abstract List<ClassFile> compile(Module module, VirtualFile sourceFile, ClassFile outputFile) throws Exception;
}
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
Expand Down Expand Up @@ -35,15 +34,11 @@ public BuiltInJavaCompiler(Context context) {
}

@Override
public List<ClassFile> compile(VirtualFile sourceFile, ClassFile outputFile) throws Exception {
public List<ClassFile> compile(Module module, VirtualFile sourceFile, ClassFile outputFile) throws Exception {
File file = VfsUtil.virtualToIoFile(sourceFile);

// Find current module
Project project = outputFile.getProject();
Module module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(sourceFile);
if (module == null) {
return new ArrayList<>();
}

// Find the class files and the class version
// We take the java version from the previously compiled class file by reading the header
Expand Down
Expand Up @@ -3,15 +3,13 @@
import com.intellij.debugger.settings.DebuggerSettings;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.task.ProjectTaskManager;
import com.intellij.task.ProjectTaskManager.Result;
import com.intellij.task.impl.ModuleFilesBuildTaskImpl;
import net.labymod.intellij.singlehotswap.compiler.AbstractCompiler;
import net.labymod.intellij.singlehotswap.hotswap.ClassFile;
import net.labymod.intellij.singlehotswap.hotswap.Context;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,7 +28,7 @@ public DefaultCompiler(Context context) {
}

@Override
public List<ClassFile> compile(VirtualFile sourceFile, ClassFile outputFile) throws Exception {
public List<ClassFile> compile(Module module, VirtualFile sourceFile, ClassFile outputFile) throws Exception {
List<ClassFile> classFiles = new ArrayList<>();

Project project = outputFile.getProject();
Expand All @@ -44,13 +42,6 @@ public List<ClassFile> compile(VirtualFile sourceFile, ClassFile outputFile) thr
// IntelliJ always reloads every single file that is referenced by the target class.
settings.RUN_HOTSWAP_AFTER_COMPILE = DebuggerSettings.RUN_HOTSWAP_NEVER;

// Create task
ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
@Nullable Module module = index.getModuleForFile(sourceFile, false);
if (module == null) {
return classFiles;
}

// Compile virtual file
ModuleFilesBuildTaskImpl task = new ModuleFilesBuildTaskImpl(module, true, sourceFile);
Result result = projectTaskManager.run(task).blockingGet(3, TimeUnit.MINUTES);
Expand Down
8 changes: 6 additions & 2 deletions 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.4</version>
<version>2.5</version>
<vendor email="labystudio@gmail.com" url="https://www.labymod.net">LabyMedia</vendor>

<idea-version since-build="203.000"/>
Expand Down Expand Up @@ -53,7 +53,11 @@

<change-notes>
<![CDATA[
v2.4 (2022-03-06):
v2.5 (2023-04-17):
<ul>
<li>Fixed exception when hotswapping with IntelliJ 2023.1 using the new UI</li>
</ul>
v2.4 (2023-03-06):
<ul>
<li>Fixed an issue where the background task wouldn't finish if an error occurred</li>
</ul>
Expand Down

0 comments on commit 5d42c64

Please sign in to comment.