chore: general cleanup of now useless RuntimeException wrapping

This commit is contained in:
zaaarf 2023-11-27 16:15:40 +01:00
parent 7ceb7eb998
commit e64939d156
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
13 changed files with 98 additions and 120 deletions

View file

@ -1,9 +1,5 @@
package com.codemp.intellij;
import com.codemp.intellij.exceptions.lib.ChannelException;
import com.codemp.intellij.exceptions.lib.DeadlockedException;
import com.codemp.intellij.exceptions.lib.InvalidStateException;
import com.codemp.intellij.exceptions.lib.TransportException;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.SystemInfo;
import cz.adamh.utils.NativeUtils;
@ -33,7 +29,6 @@ public class CodeMP {
LOGGER.info("Loaded CodeMP library!");
loadedLibrary = false;
}
}
}
}

View file

@ -9,7 +9,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class ConnectAction extends AnAction {
public static void connect(AnActionEvent e, String url, boolean silent) throws Exception {
public static void connect(AnActionEvent e, String url, boolean silent) {
CodeMP.loadLibrary(); //will only load it the first time
CodeMPHandler.connect(url);

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.actions;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.actions.buffer.BufferAttachAction;
import com.codemp.intellij.actions.workspace.WorkspaceJoinAction;
import com.intellij.openapi.actionSystem.AnAction;
@ -12,12 +13,9 @@ import org.jetbrains.annotations.NotNull;
public class FastForwardAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
ConnectAction.connect(e, "http://alemi.dev:50052", true);
WorkspaceJoinAction.join(e, "default", true);
BufferAttachAction.attach(e, "test", true);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
ConnectAction.connect(e, "http://alemi.dev:50052", true);
WorkspaceJoinAction.join(e, "default", true);
BufferAttachAction.attach(e, "fucl", true);
CodeMP.LOGGER.debug("Completed quick startup for testing!");
}
}

View file

@ -12,7 +12,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferAttachAction extends AnAction {
public static void attach(AnActionEvent e, String buffer, boolean silent) throws Exception {
public static void attach(AnActionEvent e, String buffer, boolean silent) {
BufferHandler bufferHandler = CodeMPHandler.attach(buffer);
if(!silent) ActionUtil.notify(e, "Success",
String.format("Successfully attached to buffer to buffer to %s!", buffer)

View file

@ -9,7 +9,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferCreateAction extends AnAction {
public static void create(AnActionEvent e, String buffer, boolean silent) throws Exception {
public static void create(AnActionEvent e, String buffer, boolean silent) {
CodeMPHandler.create(buffer);
if(!silent) ActionUtil.notify(e, "Success",
String.format("Created buffer %s!", buffer)

View file

@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
public class BufferCreateWithContentAction extends AnAction {
public static void createWithContent(AnActionEvent event, String buffer, boolean silent) throws Exception {
public static void createWithContent(AnActionEvent event, String buffer, boolean silent) {
String content = ActionUtil.getCurrentEditor(event).getDocument().getText();
CodeMPHandler.createWithContent(buffer, content);

View file

@ -12,7 +12,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferDetachAction extends AnAction {
public static void detach(AnActionEvent e, String buffer, boolean silent) throws Exception {
public static void detach(AnActionEvent e, String buffer, boolean silent) {
boolean res = CodeMPHandler.detach(buffer);
if(!res) throw new BufferDetachException(buffer);

View file

@ -14,7 +14,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class WorkspaceJoinAction extends AnAction {
public static void join(AnActionEvent e, String workspace, boolean silent) throws Exception {
public static void join(AnActionEvent e, String workspace, boolean silent) {
CursorHandler cursorHandler = CodeMPHandler.join(workspace);
if(!silent) ActionUtil.notify(e,

View file

@ -9,7 +9,7 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class WorkspaceLeaveAction extends AnAction {
public static void leave(AnActionEvent e, boolean silent) throws Exception {
public static void leave(AnActionEvent e, boolean silent) {
CodeMPHandler.leaveWorkspace();
if(!silent) ActionUtil.notify(e, "Success", "Left workspace");

View file

@ -1,6 +1,7 @@
package com.codemp.intellij.listeners;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.CodeMPException;
import com.codemp.intellij.jni.BufferHandler;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.event.DocumentEvent;
@ -16,7 +17,7 @@ public class BufferEventListener implements DocumentListener {
}
@Override
public void documentChanged(@NotNull DocumentEvent event) {
public void documentChanged(@NotNull DocumentEvent event) throws CodeMPException {
CodeMP.LOGGER.debug("Changed {} to {} at offset {}",
event.getOldFragment(), event.getNewFragment(), event.getOffset());
@ -24,14 +25,11 @@ public class BufferEventListener implements DocumentListener {
if(group instanceof String groupString && groupString.startsWith("codemp-buffer-receive"))
return;
try { //TODO move actions break
int changeOffset = event.getOffset();
CharSequence newFragment = event.getNewFragment();
this.bufferHandler.send(changeOffset,
changeOffset + event.getOldFragment().length(),
newFragment.toString());
} catch(Exception e) {
throw new RuntimeException(e);
}
//TODO move actions break
int changeOffset = event.getOffset();
CharSequence newFragment = event.getNewFragment();
this.bufferHandler.send(changeOffset,
changeOffset + event.getOldFragment().length(),
newFragment.toString());
}
}

View file

@ -22,18 +22,14 @@ public class CursorEventListener implements CaretListener {
if(caret == null)
return;
try {
VisualPosition startPos = caret.getSelectionStartPosition();
VisualPosition endPos = caret.getSelectionEndPosition();
CodeMP.LOGGER.debug("Caret moved from {}x {}y to {}x {}y",
startPos.line, startPos.column, endPos.line, endPos.column);
this.cursorHandler.send(
CodeMP.ACTIVE_BUFFERS_REVERSE.get(event.getEditor()),
startPos.line, startPos.column,
endPos.line, endPos.column
);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
VisualPosition startPos = caret.getSelectionStartPosition();
VisualPosition endPos = caret.getSelectionEndPosition();
CodeMP.LOGGER.debug("Caret moved from {}x {}y to {}x {}y",
startPos.line, startPos.column, endPos.line, endPos.column);
this.cursorHandler.send(
CodeMP.ACTIVE_BUFFERS_REVERSE.get(event.getEditor()),
startPos.line, startPos.column,
endPos.line, endPos.column
);
}
}

View file

@ -59,44 +59,40 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo
try {
while(true) {
try {
String buffer = CodeMPHandler.selectBuffer();
BufferHandler handler = CodeMPHandler.getBuffer(buffer);
String buffer = CodeMPHandler.selectBuffer();
BufferHandler handler = CodeMPHandler.getBuffer(buffer);
List<TextChangeWrapper> changeList = new ArrayList<>();
while(true) {
Optional<TextChangeWrapper> changeOptional;
try {
changeOptional = handler.tryRecv();
} catch(DeadlockedException e) {
CodeMP.LOGGER.error(e.getMessage());
continue;
}
if(changeOptional.isEmpty())
break;
TextChangeWrapper change = changeOptional.get();
CodeMP.LOGGER.debug("Received text change {} from offset {} to {}!",
change.getContent(), change.getStart(), change.getEnd());
changeList.add(change);
List<TextChangeWrapper> changeList = new ArrayList<>();
while(true) {
Optional<TextChangeWrapper> changeOptional;
try {
changeOptional = handler.tryRecv();
} catch(DeadlockedException e) {
CodeMP.LOGGER.error(e.getMessage());
continue;
}
Editor bufferEditor = CodeMP.ACTIVE_BUFFERS.get(buffer);
ApplicationManager.getApplication().invokeLaterOnWriteThread(() ->
ApplicationManager.getApplication().runWriteAction(() ->
CommandProcessor.getInstance().executeCommand(
this.myProject,
() -> changeList.forEach((change) ->
bufferEditor.getDocument().replaceString(
(int) change.getStart(), (int) change.getEnd(), change.getContent())
),
"CodeMPBufferReceive",
"codemp-buffer-receive", //TODO: mark this with the name
bufferEditor.getDocument()
)));
} catch(Exception ex) {
throw new RuntimeException(ex);
if(changeOptional.isEmpty())
break;
TextChangeWrapper change = changeOptional.get();
CodeMP.LOGGER.debug("Received text change {} from offset {} to {}!",
change.getContent(), change.getStart(), change.getEnd());
changeList.add(change);
}
Editor bufferEditor = CodeMP.ACTIVE_BUFFERS.get(buffer);
ApplicationManager.getApplication().invokeLaterOnWriteThread(() ->
ApplicationManager.getApplication().runWriteAction(() ->
CommandProcessor.getInstance().executeCommand(
this.myProject,
() -> changeList.forEach((change) ->
bufferEditor.getDocument().replaceString(
(int) change.getStart(), (int) change.getEnd(), change.getContent())
),
"CodeMPBufferReceive",
"codemp-buffer-receive", //TODO: mark this with the name
bufferEditor.getDocument()
)));
}
} catch(Exception ex) {
TaskManager.nullBufferTask();

View file

@ -1,6 +1,7 @@
package com.codemp.intellij.task;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.CodeMPException;
import com.codemp.intellij.jni.CursorEventWrapper;
import com.codemp.intellij.jni.CursorHandler;
import com.codemp.intellij.util.ColorUtil;
@ -41,54 +42,48 @@ public class CursorEventAwaiterTask extends Task.Backgroundable implements Dispo
assert myProject != null; //will never fail
try {
while(true) {
try {
CursorEventWrapper event = handler.recv();
CursorEventWrapper event = handler.recv();
Editor editor = CodeMP.ACTIVE_BUFFERS.get(event.getBuffer());
if(editor == null)
continue;
Editor editor = CodeMP.ACTIVE_BUFFERS.get(event.getBuffer());
if(editor == null)
continue;
CodeMP.LOGGER.debug(
"Cursor moved by user {}! Start pos: {}x {}y; end pos: {}x {}y in buffer {}!",
event.getUser(),
event.getStartCol(), event.getStartCol(),
event.getEndRow(), event.getEndCol(),
event.getBuffer());
CodeMP.LOGGER.debug(
"Cursor moved by user {}! Start pos: {}x {}y; end pos: {}x {}y in buffer {}!",
event.getUser(),
event.getStartCol(), event.getStartCol(),
event.getEndRow(), event.getEndCol(),
event.getBuffer());
int startOffset = editor.getDocument().getLineStartOffset(event.getStartRow()) + event.getStartCol();
int endOffset = editor.getDocument().getLineStartOffset(event.getEndRow()) + event.getEndCol();
int startOffset = editor.getDocument().getLineStartOffset(event.getStartRow()) + event.getStartCol();
int endOffset = editor.getDocument().getLineStartOffset(event.getEndRow()) + event.getEndCol();
ApplicationManager.getApplication().invokeLater(() -> {
try {
RangeHighlighter highlighter = this.highlighterMap.get(event.getUser());
if(highlighter != null)
highlighter.dispose();
ApplicationManager.getApplication().invokeLater(() -> {
try {
RangeHighlighter highlighter = this.highlighterMap.get(event.getUser());
if(highlighter != null)
highlighter.dispose();
this.highlighterMap.put(event.getUser(), editor
.getMarkupModel()
.addRangeHighlighter(
startOffset,
endOffset,
HighlighterLayer.SELECTION,
new TextAttributes(
null,
ColorUtil.colorFromUsername(event.getUser()),
null,
null,
Font.PLAIN
), HighlighterTargetArea.EXACT_RANGE
));
} catch(IllegalArgumentException ex) {
//suppress if the cursor only exceeds length by one, it's probably just him adding something at EOF
if(endOffset - editor.getDocument().getTextLength() != 1)
throw ex;
} catch(Exception ex) {
throw new RuntimeException(ex);
}
});
} catch(Exception ex) {
throw new RuntimeException(ex);
}
this.highlighterMap.put(event.getUser(), editor
.getMarkupModel()
.addRangeHighlighter(
startOffset,
endOffset,
HighlighterLayer.SELECTION,
new TextAttributes(
null,
ColorUtil.colorFromUsername(event.getUser()),
null,
null,
Font.PLAIN
), HighlighterTargetArea.EXACT_RANGE
));
} catch(IllegalArgumentException ex) {
//suppress if the cursor only exceeds length by one, it's probably just him adding something at EOF
if(endOffset - editor.getDocument().getTextLength() != 1)
throw ex;
}
});
}
} catch(Exception ex) { //exited
this.highlighterMap.forEach((s, r) -> r.dispose());