Skip to content

fix compatibility #365

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

Merged
merged 1 commit into from
Aug 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ public class PluginConstant {

public static final String LEETCODE_EDITOR_GROUP = ACTION_PREFIX + ".editor.group";


public static final String LEETCODE_EDITOR_TIMER_STATUS_BAR_ID = PLUGIN_ID + "-TimerStatusBar";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.shuzijun.leetcode.plugin.setting;

import com.intellij.credentialStore.CredentialAttributes;
import com.intellij.credentialStore.Credentials;
import com.intellij.ide.passwordSafe.PasswordSafe;
import com.intellij.ide.passwordSafe.PasswordSafeException;
import com.intellij.openapi.components.*;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.shuzijun.leetcode.plugin.model.Config;
Expand All @@ -18,7 +19,7 @@
/**
* @author shuzijun
*/
@State(name = "PersistentConfig" + PluginConstant.ACTION_SUFFIX, storages = {@Storage(value = PluginConstant.ACTION_PREFIX+"-config.xml", roamingType = RoamingType.DISABLED)})
@State(name = "PersistentConfig" + PluginConstant.ACTION_SUFFIX, storages = {@Storage(value = PluginConstant.ACTION_PREFIX + "-config.xml", roamingType = RoamingType.DISABLED)})
public class PersistentConfig implements PersistentStateComponent<PersistentConfig> {

public static String PATH = "leetcode" + File.separator + "editor";
Expand Down Expand Up @@ -67,24 +68,16 @@ public String getTempFilePath() {
return getConfig().getFilePath() + File.separator + PATH + File.separator + initConfig.get(INITNAME).getAlias() + File.separator;
}

public void savePassword(String password) {
try {
PasswordSafe.getInstance().storePassword
(null, this.getClass(), PluginConstant.PLUGIN_ID, password != null ? password : "");
} catch (PasswordSafeException exception) {
MessageUtils.showAllWarnMsg("warning", "Failed to save password");
public void savePassword(String password, String username) {
if(username == null || password == null){
return;
}
PasswordSafe.getInstance().set(new CredentialAttributes(PluginConstant.PLUGIN_ID, username, this.getClass()), new Credentials(username, password==null?"":password));
}

public String getPassword() {
if (getConfig().getVersion() != null) {
try {
return PasswordSafe.getInstance().getPassword(null, this.getClass(), PluginConstant.PLUGIN_ID);
} catch (PasswordSafeException exception) {
MessageUtils.showAllWarnMsg("warning", "Password acquisition failed");
return null;
}

public String getPassword(String username) {
if (getConfig().getVersion() != null && username != null) {
return PasswordSafe.getInstance().getPassword(new CredentialAttributes(PluginConstant.PLUGIN_ID, username, this.getClass()));
}
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
* @author shuzijun
*/
@State(name = "LeetcodeEditor" + PluginConstant.ACTION_SUFFIX, storages = {@Storage(value = PluginConstant.ACTION_PREFIX+"/editor.xml")})
public class ProjectConfig implements ProjectComponent, PersistentStateComponent<ProjectConfig.InnerState> {
public class ProjectConfig implements PersistentStateComponent<ProjectConfig.InnerState> {

public Map<String, LeetcodeEditor> idProjectConfig = new HashMap<>();

public ProjectConfig(Project project) {

}

@Nullable
public static ProjectConfig getInstance(Project project) {
return ServiceManager.getService(project, ProjectConfig.class);
return project.getService(ProjectConfig.class);
}

private InnerState innerState = new InnerState();
Expand Down Expand Up @@ -81,15 +77,4 @@ public String getComponentName() {
return this.getClass().getName();
}

public void initComponent() {
}

public void disposeComponent() {
}

public void projectOpened() {
}

public void projectClosed() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void loadSetting() {
Config config = PersistentConfig.getInstance().getInitConfig();
if (config != null) {
userNameField.setText(config.getLoginName());
passwordField.setText(PersistentConfig.getInstance().getPassword());
passwordField.setText(PersistentConfig.getInstance().getPassword(config.getLoginName()));
if (StringUtils.isNotBlank(config.getFilePath())) {
fileFolderBtn.setText(config.getFilePath());
}
Expand Down Expand Up @@ -216,7 +216,7 @@ public boolean isModified() {
Config currentState = new Config();
process(currentState);
if (currentState.isModified(config)) {
if (passwordField.getText() != null && passwordField.getText().equals(PersistentConfig.getInstance().getPassword())) {
if (passwordField.getText() != null && passwordField.getText().equals(PersistentConfig.getInstance().getPassword(config.getLoginName()))) {
return false;
} else {
return true;
Expand All @@ -239,7 +239,7 @@ public void apply() {
file.mkdirs();
}
PersistentConfig.getInstance().setInitConfig(config);
PersistentConfig.getInstance().savePassword(passwordField.getText());
PersistentConfig.getInstance().savePassword(passwordField.getText(),config.getLoginName());
CustomTreeCellRenderer.loaColor();
TimerBarWidget.loaColor();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.shuzijun.leetcode.plugin.timer;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.CustomStatusBarWidget;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import com.shuzijun.leetcode.plugin.model.Config;
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -147,15 +144,5 @@ public void reset() {
label.setVisible(false);
}

@Nullable
public StatusBarWidget.WidgetPresentation getPresentation() {
return this.getPresentation(SystemInfo.isMac ? StatusBarWidget.PlatformType.MAC : StatusBarWidget.PlatformType.DEFAULT);
}

@Nullable
public StatusBarWidget.WidgetPresentation getPresentation(@NotNull StatusBarWidget.PlatformType type) {
return null;
}


}
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
package com.shuzijun.leetcode.plugin.timer;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import com.intellij.openapi.wm.StatusBarWidgetProvider;
import com.intellij.openapi.wm.StatusBarWidgetFactory;
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


/**
* @author shuzijun
*/
public class TimerStatusBarWidgetProvider implements StatusBarWidgetProvider {
@Nullable
public class TimerStatusBarWidgetProvider implements StatusBarWidgetFactory {

private TimerBarWidget timerBarWidget;

@Override
public @NonNls @NotNull String getId() {
return PluginConstant.LEETCODE_EDITOR_TIMER_STATUS_BAR_ID;
}

@Override
public @Nls @NotNull String getDisplayName() {
return PluginConstant.LEETCODE_EDITOR_TIMER_STATUS_BAR_ID;
}

@Override
public boolean isAvailable(@NotNull Project project) {
return true;
}

@Override
public @NotNull StatusBarWidget createWidget(@NotNull Project project) {
if (timerBarWidget == null) {
timerBarWidget = new TimerBarWidget(project);
}
return timerBarWidget;
}

@Override
public void disposeWidget(@NotNull StatusBarWidget widget) {
if (timerBarWidget != null) {
Disposer.dispose(timerBarWidget);
}
}

@Override
public StatusBarWidget getWidget(@NotNull Project project) {
return new TimerBarWidget(project);
public boolean canBeEnabledOn(@NotNull StatusBar statusBar) {
return false;
}
}
41 changes: 3 additions & 38 deletions src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.shuzijun.leetcode.plugin.utils;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.TransactionGuard;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
import com.intellij.util.ExceptionUtil;
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
import com.shuzijun.leetcode.plugin.model.Constant;
import com.shuzijun.leetcode.plugin.model.LeetcodeEditor;
Expand All @@ -25,7 +20,6 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;

/**
Expand Down Expand Up @@ -244,40 +238,11 @@ public static void openFileEditorAndSaveState(File file, Project project, Questi
public static void saveEditDocument(VirtualFile file){
if (FileDocumentManager.getInstance().isFileModified(file)) {
try {
ThrowableComputable<Boolean, Throwable> action = new ThrowableComputable<Boolean, Throwable>() {
@Override
public Boolean compute() throws Throwable {
ApplicationManager.getApplication().invokeLaterOnWriteThread((() -> {
ApplicationManager.getApplication().runWriteAction(() -> {
FileDocumentManager.getInstance().saveDocument(FileDocumentManager.getInstance().getDocument(file));
return true;
}
};


Application application = ApplicationManager.getApplication();
if (application.isDispatchThread()) {
ApplicationManager.getApplication().runWriteAction(action);
} else {
if (application.isReadAccessAllowed()) {
LogUtils.LOG.error("Must not start write action from within read action in the other thread - deadlock is coming");
}

AtomicReference<Boolean> result = new AtomicReference();
AtomicReference<Throwable> exception = new AtomicReference();
TransactionGuard.getInstance().submitTransactionAndWait(() -> {
try {
result.set(WriteAction.compute(action));
} catch (Throwable var4) {
exception.set(var4);
}

});
Throwable t = (Throwable) exception.get();
if (t != null) {
t.addSuppressed(new RuntimeException());
ExceptionUtil.rethrowUnchecked(t);
throw t;
}
}
}));
} catch (Throwable ignore) {
LogUtils.LOG.error("自动保存文件错误", ignore);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shuzijun.leetcode.plugin.utils;

import com.shuzijun.leetcode.plugin.utils.io.HttpRequests;
import com.intellij.util.io.HttpRequests;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.jetbrains.annotations.NotNull;
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/shuzijun/leetcode/plugin/utils/MTAUtils.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
package com.shuzijun.leetcode.plugin.utils;

import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.util.SystemInfo;
import com.shuzijun.leetcode.plugin.model.Config;
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import com.shuzijun.leetcode.plugin.utils.io.HttpRequests;
import org.apache.http.client.utils.URIBuilder;

import java.awt.*;
import java.net.URI;
import java.util.Calendar;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -59,7 +49,7 @@ public ClickTask(Config config, String actionsId) {

@Override
public void run() {
try {
/* try {
if (version == null) {
version = PluginManager.getPlugin(PluginId.getId(PluginConstant.PLUGIN_ID)).getVersion()
.replace("v", "").replaceAll("-|_", ".");
Expand Down Expand Up @@ -98,7 +88,7 @@ public void run() {
HttpRequests.request(uri.toURL().toString()).userAgent(userAgent).tryConnect();

} catch (Exception e) {
}
}*/
}
}
}
19 changes: 4 additions & 15 deletions src/main/java/com/shuzijun/leetcode/plugin/utils/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.JBColor;
import com.intellij.ui.awt.RelativePoint;
import com.shuzijun.leetcode.plugin.model.Constant;
import com.shuzijun.leetcode.plugin.model.PluginConstant;
import org.jetbrains.annotations.Nullable;

Expand All @@ -22,7 +20,8 @@
/**
* @author shuzijun
*/
public class MessageUtils implements ProjectComponent {
@Service
public final class MessageUtils {

private Project project;

Expand All @@ -32,7 +31,7 @@ public MessageUtils(Project project) {

@Nullable
public static MessageUtils getInstance(Project project) {
return ServiceManager.getService(project, MessageUtils.class);
return project.getService(MessageUtils.class);
}


Expand Down Expand Up @@ -67,15 +66,5 @@ public String getComponentName() {
return this.getClass().getName();
}

public void initComponent() {
}

public void disposeComponent() {
}

public void projectOpened() {
}

public void projectClosed() {
}
}
Loading