Skip to content

Commit

Permalink
Show floating toolbar when results doesn't match source code
Browse files Browse the repository at this point in the history
  • Loading branch information
talarian1 committed Aug 15, 2023
1 parent 4e12dfd commit 07b389f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/com/jfrog/ide/idea/actions/StartLocalScanAction.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.jfrog.ide.idea.actions;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.util.messages.MessageBus;
import com.jfrog.ide.idea.events.ApplicationEvents;
import com.jfrog.ide.idea.scan.ScanManager;
import org.jetbrains.annotations.NotNull;

/**
* Created by romang on 3/6/17.
*/
public class StartLocalScanAction extends AnAction {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

@Override
public void actionPerformed(AnActionEvent e) {
Project project = e.getProject();
if (project == null) {
return;
}
MessageBus messageBus = ApplicationManager.getApplication().getMessageBus();
messageBus.syncPublisher(ApplicationEvents.ON_SCAN_LOCAL_STARTED).update();
ScanManager.getInstance(project).startScan();
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jfrog/ide/idea/events/AnnotationEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jfrog.ide.idea.events;

import com.intellij.util.messages.Topic;

public interface AnnotationEvents {
// Results expiry
Topic<AnnotationEvents> ON_IRRELEVANT_RESULT = Topic.create("Source code changed", AnnotationEvents.class);

/**
* Called when the selected file is modified.
*/
void update(String filePath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.ExternalAnnotator;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.util.messages.MessageBus;
import com.jfrog.ide.common.nodes.FileIssueNode;
import com.jfrog.ide.common.nodes.FileTreeNode;
import com.jfrog.ide.common.nodes.SortableChildrenTreeNode;
import com.jfrog.ide.idea.events.AnnotationEvents;
import com.jfrog.ide.idea.ui.ComponentsTree;
import com.jfrog.ide.idea.ui.LocalComponentsTree;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -85,6 +88,11 @@ public void apply(@NotNull PsiFile file, List<FileIssueNode> warnings, @NotNull
.gutterIconRenderer(iconRenderer)
.create();
}
// Notify outdated scan result
else {
MessageBus messageBus = ApplicationManager.getApplication().getMessageBus();
messageBus.syncPublisher(AnnotationEvents.ON_IRRELEVANT_RESULT).update(warning.getFilePath());
}
});
}
}
46 changes: 45 additions & 1 deletion src/main/java/com/jfrog/ide/idea/ui/JFrogFloatingToolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider;
import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.util.messages.MessageBusConnection;
import com.jfrog.ide.idea.events.AnnotationEvents;
import com.jfrog.ide.idea.events.ApplicationEvents;
import com.jfrog.ide.idea.utils.Descriptor;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

/**
* @author yahavi
**/
public class JFrogFloatingToolbar extends AbstractFloatingToolbarProvider {
public class JFrogFloatingToolbar extends AbstractFloatingToolbarProvider implements Disposable {
private final MessageBusConnection appBusConnection;
private Set<String> changedFiles;
private Set<FloatingToolbarComponent> components;


public JFrogFloatingToolbar() {
super("JFrog.Floating");
changedFiles = new HashSet<>();
components = new HashSet<>();
this.appBusConnection = ApplicationManager.getApplication().getMessageBus().connect(this);
registerOnChangeHandlers();
}

@Override
Expand All @@ -31,6 +46,13 @@ public void register(@NotNull DataContext dataContext, @NotNull FloatingToolbarC
if (fileEditor == null || fileEditor.getFile() == null) {
return;
}
if (changedFiles.contains(fileEditor.getFile().getPath())) {
component.scheduleShow();
components.add(component);
return;
} else {
component.scheduleHide();
}
Descriptor descriptor = Descriptor.fromFileName(fileEditor.getFile().getName());
if (descriptor == null) {
return;
Expand All @@ -43,6 +65,28 @@ public void register(@NotNull DataContext dataContext, @NotNull FloatingToolbarC
LocalComponentsTree localComponentsTree = LocalComponentsTree.getInstance(project);
if (localComponentsTree.isCacheEmpty() || localComponentsTree.isCacheExpired()) {
component.scheduleShow();
components.add(component);
}
}

private void registerOnChangeHandlers() {
appBusConnection.subscribe(AnnotationEvents.ON_IRRELEVANT_RESULT, (AnnotationEvents) this::updateChangedFiles);
appBusConnection.subscribe(ApplicationEvents.ON_SCAN_LOCAL_STARTED, (ApplicationEvents) this::clear);
}

private void clear() {
this.changedFiles = new HashSet<>();
components.forEach(FloatingToolbarComponent::scheduleHide);
components = new HashSet<>();
}

private void updateChangedFiles(String s) {
this.changedFiles.add(s);
}

@Override
public void dispose() {
// Disconnect and release resources from the application bus connection
appBusConnection.disconnect();
}
}

0 comments on commit 07b389f

Please sign in to comment.