Skip to content

Commit

Permalink
Actions for refreshing managed dependencies and clearing haxelib cache
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed May 18, 2023
1 parent ba48c83 commit 9b09707
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.intellij.plugins.haxe.buildsystem.lime;

import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

Expand All @@ -10,4 +14,29 @@ public class LimeOpenFlUtil {
public boolean isOpenfl(XmlTag rootTag) {
return Arrays.stream(rootTag.findSubTags("haxelib")).anyMatch(xmlTag -> "openfl".equalsIgnoreCase(xmlTag.getAttribute("name").getValue()));
}

public boolean isOpenFlFile(@NotNull XmlFile file) {
if(!FileUtilRt.extensionEquals(file.getName(), "xml")) return false;

XmlDocument document = file.getDocument();
if(document == null) return false;

XmlTag rootTag = document.getRootTag();
if(rootTag == null) return false;

return LimeOpenFlUtil.isOpenfl(rootTag);
}

public boolean isLimeFile(@NotNull XmlFile file) {
if(!FileUtilRt.extensionEquals(file.getName(), "xml")) return false;

XmlDocument document = file.getDocument();
if (document == null) return false;

XmlTag rootTag = document.getRootTag();
if (rootTag == null) return false;

return !LimeOpenFlUtil.isOpenfl(rootTag);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@ public Icon getFileIcon(@Iconable.IconFlags int flags) {

@Override
public boolean isMyFile(@NotNull XmlFile file, @Nullable Module module) {
if(!FileUtilRt.extensionEquals(file.getName(), "xml")) return false;

XmlDocument document = file.getDocument();
if (document == null) return false;

XmlTag rootTag = document.getRootTag();
if (rootTag == null) return false;

return !LimeOpenFlUtil.isOpenfl(rootTag);
return LimeOpenFlUtil.isLimeFile(file);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ public Icon getFileIcon(@Iconable.IconFlags int flags) {

@Override
public boolean isMyFile(@NotNull XmlFile file, @Nullable Module module) {
if(!FileUtilRt.extensionEquals(file.getName(), "xml")) return false;

XmlDocument document = file.getDocument();
if(document == null) return false;

XmlTag rootTag = document.getRootTag();
if(rootTag == null) return false;

return LimeOpenFlUtil.isOpenfl(rootTag);
return LimeOpenFlUtil.isOpenFlFile(file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static HaxelibCache getInstance() {
return instance;
}

public void reload() {
load();
}
private void load() {

Module haxeModule = getHaxeModule();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.intellij.plugins.haxe.haxelib;

import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.haxe.HaxeBundle;
import org.jetbrains.annotations.Nullable;

public class HaxelibNotifier {

public static void notifyMissingLib(@Nullable Project project, String content) {
NotificationGroupManager.getInstance()
.getNotificationGroup("haxe.haxelib.warning")
.createNotification(content, NotificationType.WARNING)
.setTitle(HaxeBundle.message("haxe.haxelib.library.dependencies"))
.setContent(HaxeBundle.message("haxe.haxelib.library.missing", content))
//.addAction(new DumbAwareAction() {
// //TODO create haxelib install logic
// @Override
// public void actionPerformed(@NotNull AnActionEvent e) {
//
// }
//})
.notify(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ public class HaxelibProjectUpdater {
* It overrides myRunInForeground. The UI is blocked with no updates.
*/
private static final boolean myTestInForeground = false;
/**
* Set this true to put up a modal dialog and run in the foreground thread
* (locking up the UI.)
* Set it false to run in a background thread. Progress is updated in the
* status bar and the UI is usable.
*/
private static final boolean myRunInForeground = false;

public static final HaxelibProjectUpdater INSTANCE = new HaxelibProjectUpdater();

Expand Down Expand Up @@ -146,7 +139,6 @@ public boolean closeProject(Project project) {

ProjectTracker tracker = myProjects.get(project);
removed = myProjects.remove(project);
tracker.dispose();
if (removed) {
myQueue.remove(tracker);
if (tracker.equals(myQueue.getUpdatingProject())) {
Expand Down Expand Up @@ -636,6 +628,7 @@ private static void syncOpenFLModule(@NotNull Module module,
else {
// TODO: Figure out how to report this to the user.
log.warn("Required library 'openfl' is not known to haxelib.");
HaxelibNotifier.notifyMissingLib(project, "openfl");
}

// TODO: Pull libs off of the command line, too.
Expand Down Expand Up @@ -765,7 +758,7 @@ private void syncModuleClasspaths(final ProjectTracker tracker) {
}


private void synchronizeClasspaths(@NotNull ProjectTracker tracker) {
public void synchronizeClasspaths(@NotNull ProjectTracker tracker) {

//
// Either of these commented-out sections will cause indexing to not be attempted
Expand Down Expand Up @@ -1229,7 +1222,7 @@ public void setPropertiesList(HaxeLibraryList propertiesList) {
/**
* Tracks the state of a project for updating class paths.
*/
public final class ProjectTracker implements Disposable {
public final class ProjectTracker {
final Project myProject;
boolean myIsDirty;
boolean myIsUpdating;
Expand All @@ -1255,7 +1248,7 @@ public ProjectTracker(Project project) {
mySdkManager = new HaxelibLibraryCacheManager();

VirtualFileManager mgr = VirtualFileManager.getInstance();
mgr.addAsyncFileListener(this::lookForLibChanges, this);
mgr.addAsyncFileListener(this::lookForLibChanges, project);
}

/**
Expand Down Expand Up @@ -1385,12 +1378,6 @@ public boolean equalsName(@Nullable ProjectTracker tracker) {
return myProject.getName().equals(tracker.getProject().getName());
}

@Override
public void dispose() {


}

private AsyncFileListener.ChangeApplier lookForLibChanges(List<? extends VFileEvent> events) {
if(getProject().isDisposed()) return null;
List<String> list = events.stream()
Expand Down Expand Up @@ -1641,31 +1628,15 @@ private void runUpdate() {

if (myTestInForeground) {
doUpdateWork();
} else if (myRunInForeground) {
// TODO: Put this string in a resource bundle.
ProgressManager.getInstance().run(new Task.Modal(project, "Synchronizing with haxelib libraries...", false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
indicator.startNonCancelableSection();
doUpdateWork();
indicator.finishNonCancelableSection();
}
});
} else {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
ProgressManager.getInstance().run(
// TODO: Put this string in a resource bundle.
new Task.Backgroundable(project, "Synchronizing with haxelib libraries...", false, PerformInBackgroundOption.ALWAYS_BACKGROUND) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
doUpdateWork();
}
});
}
});
ApplicationManager.getApplication().invokeLater(() -> ProgressManager.getInstance().run(
// TODO: Put this string in a resource bundle.
new Task.Backgroundable(project, "Synchronizing with haxelib libraries...", false, PerformInBackgroundOption.ALWAYS_BACKGROUND) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
doUpdateWork();
}
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ public static HaxeLibraryList createHaxelibsFromHaxeLibData(@NotNull List<HaxeLi
}
else {
log.warn("Library specified in XML file is not known to haxelib: " + data.name);
HaxelibNotifier.notifyMissingLib(null, data.name);

}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class HXMLHaxelibCompletionContributor extends CompletionContributor {
private HaxelibCache haxelibCache;

public HXMLHaxelibCompletionContributor() {
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> haxelibCache = HaxelibCache.getInstance(), "sdadsds", false, null);
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> haxelibCache = HaxelibCache.getInstance(), "Fetching haxelib list", false, null);

// intelliJ 2018 and older
extend(CompletionType.BASIC, psiElement(HXMLTypes.VALUE).withParent(HXMLLib.class).withLanguage(HXMLLanguage.INSTANCE), getProvider());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.intellij.plugins.haxe.ide.actions.haxelib;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.haxe.buildsystem.hxml.psi.HXMLFile;
import com.intellij.plugins.haxe.buildsystem.lime.LimeOpenFlUtil;
import com.intellij.plugins.haxe.haxelib.HaxelibCache;
import com.intellij.psi.xml.XmlFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ClearHaxeLibCacheAction extends AnAction implements DumbAware {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

@Override
public void update(@NotNull AnActionEvent event) {
super.update(event);
Presentation p = event.getPresentation();
p.setEnabled(isAvailable(event));
p.setVisible(isVisible(event));
}

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
HaxelibCache.getInstance().reload();
}

protected boolean isAvailable(AnActionEvent e) {
return getProject(e) != null && isVisible(e);
}

protected boolean isVisible(AnActionEvent e) {
return switch (e.getPlace()) {
case "EditorPopup" -> isProjectFile(e);
case "ProjectViewPopup" -> true;
default -> false;
};
}

private static boolean isProjectFile(AnActionEvent e) {
Object file = e.getDataContext().getData("psi.File");
if (file instanceof XmlFile xmlFile) {
return LimeOpenFlUtil.isLimeFile(xmlFile) || LimeOpenFlUtil.isOpenFlFile(xmlFile);
}
return file instanceof HXMLFile;
}

@Nullable
private static Project getProject(AnActionEvent e) {
DataContext context = e.getDataContext();
return CommonDataKeys.PROJECT.getData(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.intellij.plugins.haxe.ide.actions.haxelib;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.haxe.buildsystem.hxml.psi.HXMLFile;
import com.intellij.plugins.haxe.buildsystem.lime.LimeOpenFlUtil;
import com.intellij.plugins.haxe.haxelib.HaxelibProjectUpdater;
import com.intellij.psi.xml.XmlFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class SyncProjectLibraryListAction extends AnAction implements DumbAware {

@Override
public void update(@NotNull AnActionEvent event) {
super.update(event);
Presentation p = event.getPresentation();
p.setEnabled(isAvailable(event));
p.setVisible(isVisible(event));
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
HaxelibProjectUpdater instance = HaxelibProjectUpdater.INSTANCE;
Project project = getProject(e);
HaxelibProjectUpdater.ProjectTracker tracker = instance.findProjectTracker(project);
if(tracker!= null) instance.synchronizeClasspaths(tracker);
}

protected boolean isAvailable(AnActionEvent e) {
return getProject(e) != null && isVisible(e);
}

protected boolean isVisible(AnActionEvent e) {
return switch (e.getPlace()) {
case "EditorPopup" -> isProjectFile(e);
case "ProjectViewPopup" -> true;
default -> false;
};
}

private static boolean isProjectFile(AnActionEvent e) {
Object file = e.getDataContext().getData("psi.File");
if (file instanceof XmlFile xmlFile) {
return LimeOpenFlUtil.isLimeFile(xmlFile) || LimeOpenFlUtil.isOpenFlFile(xmlFile);
}
return file instanceof HXMLFile;
}

@Nullable
private static Project getProject(AnActionEvent e) {
DataContext context = e.getDataContext();
return CommonDataKeys.PROJECT.getData(context);
}

}
21 changes: 21 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@

<postStartupActivity implementation="com.intellij.plugins.haxe.haxelib.HaxelibModuleManagerService"/>

<notificationGroup id="haxe.haxelib.warning" displayType="BALLOON" bundle="messages.HaxeBundle" key="haxe.haxelib.library.missing"/>

</extensions>

<applicationListeners>
Expand Down Expand Up @@ -344,5 +346,24 @@
text="Getter and Setter">
<add-to-group anchor="after" relative-to-action="Generate.SetAccessor.Haxe" group-id="GenerateGroup"/>
</action>


<action id="Haxe.project.sync-libs" class="com.intellij.plugins.haxe.ide.actions.haxelib.SyncProjectLibraryListAction"
text="Sync Libraries"
icon="AllIcons.Actions.Refresh">
</action>
<action id="Haxe.haxlib.clear-cache" class="com.intellij.plugins.haxe.ide.actions.haxelib.ClearHaxeLibCacheAction"
text="Clear Haxelib Cache"
icon="AllIcons.Actions.Refresh">
</action>

<group id="Haxe.projectUtils" icon="HaxeIcons.HAXE_LOGO" popup="true" text="Haxe">
<reference id="Haxe.project.sync-libs"/>
<reference id="Haxe.haxlib.clear-cache"/>
<add-to-group group-id="ProjectViewPopupMenu"/>
<add-to-group group-id="EditorPopupMenu"/>
</group>


</actions>
</idea-plugin>
Loading

0 comments on commit 9b09707

Please sign in to comment.