Skip to content

Commit

Permalink
Merge pull request #599 from shuzijun/gradle
Browse files Browse the repository at this point in the history
fix #598
  • Loading branch information
shuzijun authored Dec 7, 2022
2 parents 10da661 + 24e3daa commit 190f5da
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
Expand All @@ -21,6 +20,7 @@
import com.shuzijun.leetcode.plugin.manager.NoteManager;
import com.shuzijun.leetcode.plugin.model.LeetcodeEditor;
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import com.shuzijun.leetcode.plugin.utils.FileEditorProviderReflection;
import com.shuzijun.leetcode.plugin.utils.URLUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -74,7 +74,7 @@ private void initComponent() {
myComponent.addToCenter(new JBLabel("No note"));
} else {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
FileEditorProvider[] editorProviders = FileEditorProviderManager.getInstance().getProviders(project, vf);
FileEditorProvider[] editorProviders = FileEditorProviderReflection.getProviders(project, vf);

if (editorProviders != null && editorProviders.length > 0) {
fileEditor = editorProviders[0].createEditor(project, vf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.UserDataHolderBase;
Expand All @@ -21,6 +20,7 @@
import com.shuzijun.leetcode.plugin.manager.ArticleManager;
import com.shuzijun.leetcode.plugin.manager.QuestionManager;
import com.shuzijun.leetcode.plugin.model.*;
import com.shuzijun.leetcode.plugin.utils.FileEditorProviderReflection;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nls;
Expand Down Expand Up @@ -177,7 +177,7 @@ private void openArticle() throws InterruptedException, java.util.concurrent.Exe
mySplitter.setSecondComponent(new JBLabel("no solution"));
} else {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
FileEditorProvider[] editorProviders = FileEditorProviderManager.getInstance().getProviders(project, vf);
FileEditorProvider[] editorProviders = FileEditorProviderReflection.getProviders(project, vf);
FileEditor newEditor = editorProviders[0].createEditor(project, vf);
if (newEditor == fileEditor) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.UserDataHolderBase;
Expand All @@ -26,6 +25,7 @@
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import com.shuzijun.leetcode.plugin.model.Question;
import com.shuzijun.leetcode.plugin.model.Submission;
import com.shuzijun.leetcode.plugin.utils.FileEditorProviderReflection;
import com.shuzijun.leetcode.plugin.window.dialog.SubmissionsPanel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -186,7 +186,7 @@ private void openSubmission(Submission submission) throws InterruptedException,
mySplitter.setSecondComponent(new JBLabel("no submission"));
} else {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
FileEditorProvider[] editorProviders = FileEditorProviderManager.getInstance().getProviders(project, vf);
FileEditorProvider[] editorProviders = FileEditorProviderReflection.getProviders(project, vf);
FileEditor newEditor = editorProviders[0].createEditor(project, vf);
if (newEditor == fileEditor) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.shuzijun.leetcode.plugin.utils;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorProvider;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ReflectionUtil;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//https://youtrack.jetbrains.com/issue/IDEA-302416
public class FileEditorProviderReflection {
public static FileEditorProvider[] getProviders(@NotNull Project project, @NotNull VirtualFile virtualFile) {
try {
Method getProvidersMethod = ReflectionUtil.getMethod(
FileEditorProviderManager.class,
"getProviders",
Project.class, VirtualFile.class
);
if (getProvidersMethod != null) {
getProvidersMethod.setAccessible(true);
// NOTE: Have to get a reference to FileEditorProviderManager this way instead of via getInstance() to
// avoid a different IncompatibleClassChangeError in pre-2022.3 vs. 2022.3+
FileEditorProviderManager fileEditorProviderManager = ApplicationManager.getApplication().getService(FileEditorProviderManager.class);
if (fileEditorProviderManager != null) {
Object result = getProvidersMethod.invoke(fileEditorProviderManager, project, virtualFile);
if (result instanceof FileEditorProvider[]) {
return (FileEditorProvider[]) result;
}
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
LogUtils.LOG.warn("Failed to get file editor providers for project '" + project.getName() + "', file '" + virtualFile.getPath() + "'.", e);
}

return new FileEditorProvider[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void change(Config oldConfig, Config newConfig) {
}
}
});
messageBusConnection.subscribe(QuestionStatusNotifier.QUESTION_STATUS_TOPIC, question -> StatisticsData.refresh(project));
messageBusConnection.subscribe(QuestionStatusNotifier.QUESTION_STATUS_TOPIC, (QuestionStatusNotifier) question -> StatisticsData.refresh(project));

for (SimpleToolWindowPanel n : navigatorPanels) {
if (n != null && navigatorPanel instanceof Disposable) {
Expand Down Expand Up @@ -171,13 +171,7 @@ public User getUser() {
} else if (userCache.containsKey(config.getUrl())) {
return userCache.get(config.getUrl());
} else {
String otherKey = null;
for (Object key : NAVIGATOR_TABS_PANEL_DISPOSABLE_MAP.keySet()) {
if (!key.equals(id)) {
otherKey = (String) key;
break;
}
}
String otherKey = NAVIGATOR_TABS_PANEL_DISPOSABLE_MAP.getOtherKey(id);
if (otherKey == null || !((NavigatorTabsPanel) NAVIGATOR_TABS_PANEL_DISPOSABLE_MAP.get(otherKey)).userCache.containsKey(config.getUrl())) {
User user = QuestionManager.getUser();
userCache.put(config.getUrl(), user);
Expand Down Expand Up @@ -248,6 +242,22 @@ public static synchronized void loadUser(boolean login) {
}

public static class DisposableMap<K, V> extends HashMap implements Disposable {
@Override
public synchronized Object put(Object key, Object value) {
return super.put(key,value);
}

public synchronized K getOtherKey(K key){
K otherKey = null;
for (Object k : this.keySet()) {
if (!k.equals(key)) {
otherKey = key;
break;
}
}
return otherKey;
}

@Override
public void dispose() {
for (Object value : values()) {
Expand Down

0 comments on commit 190f5da

Please sign in to comment.