From c1fd57421dfd5343ce869eb123007479b133f692 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Fri, 16 Aug 2024 02:42:15 +0200 Subject: [PATCH] feat: effectively restored to before glue migration --- build.gradle | 2 + gradle.properties | 2 - .../code/intellij/actions/ConnectAction.java | 41 ++++---- .../intellij/actions/FastForwardAction.java | 24 ----- .../workspace/WorkspaceLeaveAction.java | 3 +- .../listeners/BufferEventListener.java | 19 ++-- .../listeners/CursorEventListener.java | 23 ++--- .../WorkspaceFileClosedListener.java | 2 +- .../intellij/settings/CodeMPSettings.java | 48 ++++++++++ .../settings/CodeMPSettingsConfigurable.java | 96 +++++++++++++++++++ .../intellij/task/BufferEventAwaiterTask.java | 9 +- .../intellij/task/CursorEventAwaiterTask.java | 11 +-- src/main/resources/META-INF/plugin.xml | 11 ++- 13 files changed, 207 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/mp/code/intellij/actions/FastForwardAction.java create mode 100644 src/main/java/mp/code/intellij/settings/CodeMPSettings.java create mode 100644 src/main/java/mp/code/intellij/settings/CodeMPSettingsConfigurable.java diff --git a/build.gradle b/build.gradle index a55f393..242e763 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,8 @@ dependencies { implementation 'org.slf4j:slf4j-api:2.0.9' implementation 'ch.qos.logback:logback-classic:1.4.12' implementation files('../../lib/dist/java/build/libs/codemp-0.6.2.jar') + compileOnly 'org.projectlombok:lombok:1.18.34' + annotationProcessor 'org.projectlombok:lombok:1.18.34' } intellij { diff --git a/gradle.properties b/gradle.properties index 0fd4233..24630b3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,5 @@ # Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib kotlin.stdlib.default.dependency=false -# TODO temporary workaround for Kotlin 1.8.20+ (https://jb.gg/intellij-platform-kotlin-oom) -kotlin.incremental.useClasspathSnapshot=false # Enable Gradle Configuration Cache -> https://docs.gradle.org/current/userguide/configuration_cache.html org.gradle.configuration-cache=true # Enable Gradle Build Cache -> https://docs.gradle.org/current/userguide/build_cache.html diff --git a/src/main/java/mp/code/intellij/actions/ConnectAction.java b/src/main/java/mp/code/intellij/actions/ConnectAction.java index 4fa6cee..c435e5b 100644 --- a/src/main/java/mp/code/intellij/actions/ConnectAction.java +++ b/src/main/java/mp/code/intellij/actions/ConnectAction.java @@ -1,35 +1,40 @@ package mp.code.intellij.actions; +import com.intellij.credentialStore.Credentials; import mp.code.intellij.CodeMP; +import mp.code.intellij.settings.CodeMPSettings; import mp.code.intellij.util.ActionUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import mp.code.exceptions.CodeMPException; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + public class ConnectAction extends AnAction { - public static void connect(AnActionEvent e, String url, String username, String password, boolean silent) throws CodeMPException { - System.out.printf("%s %s %s", url, username, password); - CodeMP.connect(url, username, password); + public static void connect(AnActionEvent e, boolean silent) throws NullPointerException, CodeMPException { + CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); + Credentials creds = Objects.requireNonNull(state.getCredentials()); + CodeMP.connect( + Objects.requireNonNull(state.getServerUrl()), + Objects.requireNonNull(creds.getUserName()), + Objects.requireNonNull(creds.getPasswordAsString()) + ); + if(!silent) ActionUtil.notify(e, - "Success", String.format("Connected to %s!", url)); - CodeMP.LOGGER.debug("Connected to {}!", url); + "Success", String.format("Connected to %s!", state.getServerUrl())); + CodeMP.LOGGER.debug("Connected to {}!", state.getServerUrl()); } @Override public void actionPerformed(@NotNull AnActionEvent e) { - /* - LoginDialog dialog = new LoginDialog(e.getProject(), "Please input your login credentials!", "Connect to CodeMP server"); - if(dialog.showAndGet()) { - try { - connect(e, dialog.urlField.getText(), dialog.usernameField.getText(), dialog.passwordField.getText(), false); - } catch(Exception exception) { - ActionUtil.notifyError( - e, - String.format("Failed to connect to %s!", dialog.urlField.getText()), - exception - ); - } - }*/ + try { + connect(e, false); + } catch(NullPointerException ex) { + ActionUtil.notifyError(e, "Invalid credentials!", "Please configure your credentials before connecting."); + } catch(Exception exception) { + ActionUtil.notifyError(e, "Failed to connect to server!", exception); + } } } diff --git a/src/main/java/mp/code/intellij/actions/FastForwardAction.java b/src/main/java/mp/code/intellij/actions/FastForwardAction.java deleted file mode 100644 index 8dcb21e..0000000 --- a/src/main/java/mp/code/intellij/actions/FastForwardAction.java +++ /dev/null @@ -1,24 +0,0 @@ -package mp.code.intellij.actions; - -import mp.code.intellij.CodeMP; -import mp.code.intellij.actions.workspace.WorkspaceJoinAction; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import mp.code.exceptions.CodeMPException; -import org.jetbrains.annotations.NotNull; - -/** - * Used exclusively to streamline debugging. - */ -public class FastForwardAction extends AnAction { - @Override - public void actionPerformed(@NotNull AnActionEvent e) { - try { - ConnectAction.connect(e, "http://alemi.dev:50053", "", "", true); - WorkspaceJoinAction.join(e, "glue", true); - CodeMP.LOGGER.debug("Completed quick startup for testing!"); - } catch(CodeMPException ex) { - throw new RuntimeException(ex); - } - } -} diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java index 4841239..c1d7ea8 100644 --- a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java @@ -10,8 +10,7 @@ import org.jetbrains.annotations.NotNull; public class WorkspaceLeaveAction extends AnAction { public static void leave(AnActionEvent e, String workspaceId, boolean silent) { - //CodeMP.getClient("leave workspace").leaveWorkspace(workspaceId); - // TODO + CodeMP.getClient("leave workspace").leaveWorkspace(workspaceId); Disposer.dispose(CodeMP.ACTIVE_WORKSPACES.remove(workspaceId)); if(!silent) ActionUtil.notify(e, "Success", String.format("Left workspace %s!", workspaceId)); diff --git a/src/main/java/mp/code/intellij/listeners/BufferEventListener.java b/src/main/java/mp/code/intellij/listeners/BufferEventListener.java index 6121aa1..26bfa87 100644 --- a/src/main/java/mp/code/intellij/listeners/BufferEventListener.java +++ b/src/main/java/mp/code/intellij/listeners/BufferEventListener.java @@ -1,5 +1,6 @@ package mp.code.intellij.listeners; +import lombok.SneakyThrows; import mp.code.intellij.CodeMP; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.event.DocumentEvent; @@ -10,6 +11,7 @@ import mp.code.exceptions.CodeMPException; import org.jetbrains.annotations.NotNull; import java.util.Optional; +import java.util.OptionalLong; public class BufferEventListener implements DocumentListener { @@ -20,6 +22,7 @@ public class BufferEventListener implements DocumentListener { } @Override + @SneakyThrows public void documentChanged(@NotNull DocumentEvent event) { CodeMP.LOGGER.debug("Changed {} to {} at offset {}", event.getOldFragment(), event.getNewFragment(), event.getOffset()); @@ -32,15 +35,11 @@ public class BufferEventListener implements DocumentListener { //TODO move actions break int changeOffset = event.getOffset(); CharSequence newFragment = event.getNewFragment(); - try { - this.controller.send(new TextChange( - changeOffset, - changeOffset + event.getOldFragment().length(), - newFragment.toString(), - 0L - )); - } catch(CodeMPException ignored) { - // TODO actually give a shit - } + this.controller.send(new TextChange( + changeOffset, + changeOffset + event.getOldFragment().length(), + newFragment.toString(), + OptionalLong.empty() + )); } } diff --git a/src/main/java/mp/code/intellij/listeners/CursorEventListener.java b/src/main/java/mp/code/intellij/listeners/CursorEventListener.java index c12cf15..71c378e 100644 --- a/src/main/java/mp/code/intellij/listeners/CursorEventListener.java +++ b/src/main/java/mp/code/intellij/listeners/CursorEventListener.java @@ -1,5 +1,6 @@ package mp.code.intellij.listeners; +import lombok.SneakyThrows; import mp.code.intellij.CodeMP; import mp.code.intellij.util.FileUtil; import com.intellij.openapi.editor.Caret; @@ -9,7 +10,6 @@ import com.intellij.openapi.editor.event.CaretEvent; import com.intellij.openapi.editor.event.CaretListener; import mp.code.CursorController; import mp.code.data.Cursor; -import mp.code.exceptions.CodeMPException; import org.jetbrains.annotations.NotNull; public class CursorEventListener implements CaretListener { @@ -21,6 +21,7 @@ public class CursorEventListener implements CaretListener { } @Override + @SneakyThrows public void caretPositionChanged(@NotNull CaretEvent event) { Caret caret = event.getCaret(); if(caret == null) @@ -33,17 +34,13 @@ public class CursorEventListener implements CaretListener { ); Editor editor = event.getEditor(); - try { - this.controller.send(new Cursor( - startPos.line, - startPos.column, - endPos.line, - endPos.column, - FileUtil.getRelativePath(editor.getProject(), editor.getVirtualFile()), - null - )); - } catch(CodeMPException e) { - // TODO zzzzz - } + this.controller.send(new Cursor( + startPos.line, + startPos.column, + endPos.line, + endPos.column, + FileUtil.getRelativePath(editor.getProject(), editor.getVirtualFile()), + null + )); } } diff --git a/src/main/java/mp/code/intellij/listeners/WorkspaceFileClosedListener.java b/src/main/java/mp/code/intellij/listeners/WorkspaceFileClosedListener.java index 82e440c..4b82204 100644 --- a/src/main/java/mp/code/intellij/listeners/WorkspaceFileClosedListener.java +++ b/src/main/java/mp/code/intellij/listeners/WorkspaceFileClosedListener.java @@ -27,7 +27,7 @@ public class WorkspaceFileClosedListener implements FileEditorManagerListener.Be Disposable disp = this.task.activeBuffers.remove(path); if(disp == null) return; - // TODO : this.handler.detachFromBuffer(path); + this.handler.detachFromBuffer(path); Disposer.dispose(disp); } } diff --git a/src/main/java/mp/code/intellij/settings/CodeMPSettings.java b/src/main/java/mp/code/intellij/settings/CodeMPSettings.java new file mode 100644 index 0000000..b6c8823 --- /dev/null +++ b/src/main/java/mp/code/intellij/settings/CodeMPSettings.java @@ -0,0 +1,48 @@ +package mp.code.intellij.settings; + +import com.intellij.credentialStore.CredentialAttributes; +import com.intellij.credentialStore.CredentialAttributesKt; +import com.intellij.credentialStore.Credentials; +import com.intellij.ide.passwordSafe.PasswordSafe; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.PersistentStateComponent; +import com.intellij.openapi.components.State; +import com.intellij.openapi.components.Storage; +import lombok.Getter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@State( + name = "mp.code.intellij.settings.CodeMPSettings", + storages = @Storage("codemp.xml") +) +public class CodeMPSettings implements PersistentStateComponent { + private State currentState = new State(); + public static CodeMPSettings getInstance() { + return ApplicationManager.getApplication() + .getService(CodeMPSettings.class); + } + + @Override + public State getState() { + return this.currentState; + } + + @Override + public void loadState(@NotNull State state) { + this.currentState = state; + } + + private static final String KEY = "cred"; + static CredentialAttributes createCredentialAttributes() { + return new CredentialAttributes(CredentialAttributesKt.generateServiceName("CodeMP", KEY)); + } + + public static class State { + @Getter String serverUrl; + public @Nullable Credentials getCredentials() { + CredentialAttributes attr = createCredentialAttributes(); + return PasswordSafe.getInstance().get(attr); + } + } +} diff --git a/src/main/java/mp/code/intellij/settings/CodeMPSettingsConfigurable.java b/src/main/java/mp/code/intellij/settings/CodeMPSettingsConfigurable.java new file mode 100644 index 0000000..a7a5e27 --- /dev/null +++ b/src/main/java/mp/code/intellij/settings/CodeMPSettingsConfigurable.java @@ -0,0 +1,96 @@ +package mp.code.intellij.settings; + +import com.intellij.credentialStore.CredentialAttributes; +import com.intellij.credentialStore.Credentials; +import com.intellij.credentialStore.OneTimeString; +import com.intellij.ide.passwordSafe.PasswordSafe; +import com.intellij.openapi.options.Configurable; +import com.intellij.ui.components.JBLabel; +import com.intellij.ui.components.JBPasswordField; +import com.intellij.ui.components.JBTextField; +import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.Objects; + +final class CodeMPSettingsConfigurable implements Configurable { + + private Component component; + + @Nls(capitalization = Nls.Capitalization.Title) + @Override + public String getDisplayName() { + return "CodeMP"; + } + + @Override + public JComponent getPreferredFocusedComponent() { + return this.component.serverUrlField; + } + + @Nullable + @Override + public JComponent createComponent() { + this.component = new Component(); + return component.mainPanel; + } + + @Override + public boolean isModified() { + CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); + Credentials creds = state.getCredentials(); + return !this.component.serverUrlField.getText().equals(state.serverUrl) + || (creds == null && (this.component.userNameField.getText() != null || this.component.passwordField.getPassword() != null)) + || creds != null && ( + !Objects.equals(creds.getUserName(), this.component.userNameField.getText()) + || !Objects.equals(creds.getPassword(), new OneTimeString(this.component.passwordField.getPassword())) + ); + } + + @Override + public void apply() { + CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); + state.serverUrl = this.component.serverUrlField.getText(); + CredentialAttributes attributes = CodeMPSettings.createCredentialAttributes(); + PasswordSafe.getInstance().set(attributes, new Credentials( + this.component.userNameField.getText(), + this.component.passwordField.getPassword() + )); + } + + @Override + public void reset() { + CodeMPSettings.State state = Objects.requireNonNull(CodeMPSettings.getInstance().getState()); + this.component.serverUrlField.setText(state.serverUrl); + + Credentials cred = state.getCredentials(); + if(cred != null) { + this.component.userNameField.setText(cred.getUserName()); + this.component.passwordField.setText(cred.getPasswordAsString()); + } + + } + + @Override + public void disposeUIResources() { + this.component = null; + } + + private static class Component { + final JPanel mainPanel; + final JBTextField serverUrlField = new JBTextField(); + final JBTextField userNameField = new JBTextField(); + final JBPasswordField passwordField = new JBPasswordField(); + + Component() { + this.mainPanel = FormBuilder.createFormBuilder() + .addLabeledComponent(new JBLabel("Server address:"), this.serverUrlField, 1, false) + .addLabeledComponent(new JBLabel("Username:"), this.userNameField, 1, false) + .addLabeledComponent(new JBLabel("Password:"), this.passwordField, 1, false) + .addComponentFillVertically(new JPanel(), 0) + .getPanel(); + } + } +} diff --git a/src/main/java/mp/code/intellij/task/BufferEventAwaiterTask.java b/src/main/java/mp/code/intellij/task/BufferEventAwaiterTask.java index f6649fd..437a29c 100644 --- a/src/main/java/mp/code/intellij/task/BufferEventAwaiterTask.java +++ b/src/main/java/mp/code/intellij/task/BufferEventAwaiterTask.java @@ -1,5 +1,6 @@ package mp.code.intellij.task; +import lombok.SneakyThrows; import mp.code.intellij.CodeMP; import mp.code.intellij.util.FileUtil; import com.intellij.openapi.Disposable; @@ -32,15 +33,13 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo } @Override + @SneakyThrows @SuppressWarnings("InfiniteLoopStatement") public void run(@NotNull ProgressIndicator indicator) { while(true) { Optional bufferOptional; - try { - bufferOptional = this.handler.selectBuffer(100L); - } catch(CodeMPException e) { - bufferOptional = Optional.empty(); // TODO error handling - } + bufferOptional = this.handler.selectBuffer(100L); + if(bufferOptional.isEmpty()) continue; BufferController buffer = bufferOptional.get(); diff --git a/src/main/java/mp/code/intellij/task/CursorEventAwaiterTask.java b/src/main/java/mp/code/intellij/task/CursorEventAwaiterTask.java index 3f34f13..ed7dd8f 100644 --- a/src/main/java/mp/code/intellij/task/CursorEventAwaiterTask.java +++ b/src/main/java/mp/code/intellij/task/CursorEventAwaiterTask.java @@ -1,5 +1,7 @@ package mp.code.intellij.task; +import lombok.SneakyThrows; +import mp.code.exceptions.DeadlockedException; import mp.code.intellij.CodeMP; import mp.code.intellij.util.ColorUtil; import mp.code.intellij.util.FileUtil; @@ -23,7 +25,7 @@ import java.awt.*; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -//TODO this is janky as it shows a progress bar it doesn't use tbh +//TODO this is janky as it shows a progress bar it doesn't use //implements disposable so i can use it as lifetime ig public class CursorEventAwaiterTask extends Task.Backgroundable implements Disposable { private final CursorController handler; @@ -35,15 +37,12 @@ public class CursorEventAwaiterTask extends Task.Backgroundable implements Dispo } @Override + @SneakyThrows @SuppressWarnings("InfiniteLoopStatement") public void run(@NotNull ProgressIndicator indicator) { while(true) { Cursor event; - try { - event = this.handler.recv(); - } catch(CodeMPException ex) { - continue; // TODO proper handling - } + event = this.handler.recv(); Editor editor = FileUtil.getActiveEditorByPath(this.myProject, event.buffer); if(editor == null) continue; diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 59f26ac..247578a 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -10,8 +10,7 @@ - - + @@ -22,5 +21,11 @@ - + + +