Skip to content

Commit

Permalink
fix: Delete on patch review
Browse files Browse the repository at this point in the history
Allow the user to delete his comment pressing the keyboard delete button

Signed-off-by: stelios maurommatakis <steliosmavr@cytech.gr>
  • Loading branch information
Stelios123 committed Mar 26, 2024
1 parent 216bbbb commit d699658
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.intellij.collaboration.ui.codereview.ReturnToListComponent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.Disposer;
Expand Down Expand Up @@ -136,17 +137,21 @@ private JComponent infoComponent() {
borderPanel.add(projectSelect, BorderLayout.NORTH);
borderPanel.add(titleField, BorderLayout.CENTER);

descriptionField = new DragAndDropField(project);
descriptionField = new DragAndDropField(project, 0);
descriptionField.setBackground(UIUtil.getListBackground());
descriptionField.setBorder(JBUI.Borders.empty(8, 8, 0, 8));
descriptionField.setFont(JBFont.label());
descriptionField.getEmptyText().setText(RadicleBundle.message("description"));
descriptionField.setLineWrap(true);
descriptionField.getComponent().setBorder(JBUI.Borders.empty(8));
descriptionField.setPlaceholder(RadicleBundle.message("description"));
CollaborationToolsUIUtil.INSTANCE.registerFocusActions(descriptionField);

Check warning on line 145 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'INSTANCE' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

Check warning on line 145 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'registerFocusActions(javax.swing.JComponent)' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

Check warning on line 145 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.collaboration.ui.CollaborationToolsUIUtil' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

Check warning on line 145 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'INSTANCE' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

Check warning on line 145 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'registerFocusActions(javax.swing.JComponent)' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental

var textFieldListener = new TextFieldListener();
titleField.getDocument().addDocumentListener(textFieldListener);
descriptionField.getDocument().addDocumentListener(textFieldListener);
descriptionField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void documentChanged(com.intellij.openapi.editor.event.@NotNull DocumentEvent event) {
newIssueButton.setEnabled(!titleField.getText().isEmpty() && !descriptionField.getText().isEmpty());
}
});

var descriptionPane = new JBScrollPane(descriptionField, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.intellij.collaboration.ui.layout.SizeRestrictedSingleComponentLayout;
import com.intellij.collaboration.ui.util.ActionUtilKt;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.project.Project;
import com.intellij.ui.DocumentAdapter;
import com.intellij.util.Function;
import network.radicle.jetbrains.radiclejetbrainsplugin.RadicleBundle;
import network.radicle.jetbrains.radiclejetbrainsplugin.toolwindow.DragAndDropField;
Expand All @@ -18,7 +18,6 @@

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import java.awt.BorderLayout;
import java.util.List;

Expand Down Expand Up @@ -101,9 +100,9 @@ public void showAndFocusEditor() {
});
return null;
});
dragAndDropField.getDocument().addDocumentListener(new DocumentAdapter() {
dragAndDropField.addDocumentListener(new DocumentListener() {
@Override
protected void textChanged(@NotNull DocumentEvent e) {
public void documentChanged(com.intellij.openapi.editor.event.@NotNull DocumentEvent event) {
var enable = !dragAndDropField.getText().isEmpty();
prAction.setEnabled(enable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.toolwindow;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.dsl.builder.DslComponentProperty;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import network.radicle.jetbrains.radiclejetbrainsplugin.RadicleBundle;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadAction;
import network.radicle.jetbrains.radiclejetbrainsplugin.models.Embed;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.FileService;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.JComponent;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
Expand All @@ -22,9 +33,9 @@
import java.util.List;
import java.util.stream.Collectors;

public class DragAndDropField extends JBTextArea {
public class DragAndDropField extends EditorTextField {
private static final Logger logger = LoggerFactory.getLogger(DragAndDropField.class);

private int border = 0;
private final boolean enableDragAndDrop;
private final FileService fileService;
private final Project project;
Expand All @@ -35,14 +46,36 @@ public DragAndDropField(Project project, boolean allowDragAndDrop) {
this.fileService = project.getService(FileService.class);
this.embedList = new ArrayList<>();
this.project = project;
this.setLineWrap(true);
this.setDragAndDropTarget();
this.putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true);
this.addDocumentListener(new MyListener(this));
}

public DragAndDropField(Project project) {
public DragAndDropField(Project project, int border) {
this(project, true);
this.border = border;
}

@Override
protected @NotNull EditorEx createEditor() {
var editor = super.createEditor();
editor.getSettings().setUseSoftWraps(true);
editor.setBorder(JBUI.Borders.empty(border));
editor.setOneLineMode(false);
editor.setVerticalScrollbarVisible(true);
editor.getComponent().setOpaque(false);
editor.getScrollPane().setOpaque(false);
return editor;
}

@Override
protected void onEditorAdded(@NotNull Editor editor) {
this.putClientProperty(DslComponentProperty.INTERACTIVE_COMPONENT, editor);
if (!enableDragAndDrop || ApplicationManager.getApplication().isUnitTestMode()) {
return;
}
editor.getContentComponent().setDropTarget(new Target());
}

public void setEmbedList(List<Embed> list) {
this.embedList = list;
}
Expand All @@ -54,20 +87,30 @@ public List<Embed> getEmbedList() {
.collect(Collectors.toList());
}

private void setDragAndDropTarget() {
if (enableDragAndDrop && !ApplicationManager.getApplication().isUnitTestMode()) {
this.setDropTarget(new Target());
public static class MyListener implements DocumentListener {
private final EditorTextField myField;

public MyListener(EditorTextField myField) {
this.myField = myField;
}

@Override
public void documentChanged(@NotNull DocumentEvent event) {
var parent = myField.getParent();
if (parent != null) {
((JComponent) parent).scrollRectToVisible(new Rectangle(0, 0,
parent.getWidth(), parent.getHeight()));
}
}
}

private class Target extends DropTarget {
@Override
public void dragOver(DropTargetDragEvent dtde) {
Point cursorLocation = dtde.getLocation();
int offset = DragAndDropField.this.viewToModel2D(cursorLocation);
// Move the cursor to the calculated offset
var logicalPosition = DragAndDropField.this.getEditor().xyToLogicalPosition(cursorLocation);

Check warning on line 111 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/DragAndDropField.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `xyToLogicalPosition` may produce `NullPointerException`

Check warning on line 111 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/DragAndDropField.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `xyToLogicalPosition` may produce `NullPointerException`
int offset = DragAndDropField.this.getEditor().logicalPositionToOffset(logicalPosition);
DragAndDropField.this.setCaretPosition(offset);
// Set focus to the JBTextArea when dragging files over it
DragAndDropField.this.requestFocusInWindow();
dtde.acceptDrag(DnDConstants.ACTION_COPY);
}
Expand All @@ -92,9 +135,11 @@ public void drop(DropTargetDropEvent evt) {
embedList.add(new Embed(gitObjectId, fileName, base64));
var markDown = "![" + fileName + "](" + gitObjectId + ")";
var dropLocation = evt.getLocation();
int offset = DragAndDropField.this.viewToModel2D(dropLocation);
var logicalPosition = DragAndDropField.this.getEditor().xyToLogicalPosition(dropLocation);

Check warning on line 138 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/DragAndDropField.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `xyToLogicalPosition` may produce `NullPointerException`

Check warning on line 138 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/toolwindow/DragAndDropField.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `xyToLogicalPosition` may produce `NullPointerException`
int offset = DragAndDropField.this.getEditor().logicalPositionToOffset(logicalPosition);
if (offset >= 0 && offset <= DragAndDropField.this.getText().length()) {
DragAndDropField.this.getDocument().insertString(offset, markDown, null);
WriteCommandAction.runWriteCommandAction(project, () ->
DragAndDropField.this.getEditor().getDocument().insertString(offset, markDown));
}
}
} catch (Exception e) {
Expand All @@ -104,3 +149,5 @@ public void drop(DropTargetDropEvent evt) {
}
}



0 comments on commit d699658

Please sign in to comment.