Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exception when hotswapping with IntelliJ 2023.1 using the new UI, version 2.5, closes #9 #10

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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