Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
feat: ignored warnings now work in modularized projects
Browse files Browse the repository at this point in the history
  • Loading branch information
L1nc0ln committed Sep 27, 2022
1 parent 549f828 commit 94fa0c2
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 92 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public class IgnoredWarningsFacade
{

private static final IgnoredWarningsCache warningsCache = IgnoredWarningsCache.getInstance();

private IgnoredWarningsFacade()
{
Expand All @@ -26,7 +25,9 @@ private IgnoredWarningsFacade()
@NotNull
public static Observable<Set<WarningsItem>> getIgnoredWarnings(@NotNull Project pProject)
{
return warningsCache.get(pProject);
return Optional.ofNullable(pProject.getLookup().lookup(IgnoredWarningsProvider.class))
.map(IgnoredWarningsProvider::get)
.orElse(Observable.just(Set.of()));
}

public static void addIgnoredWarning(@NotNull Project pProject, int pId, @NotNull String pDescription) throws IOException
Expand Down Expand Up @@ -70,12 +71,12 @@ private static void writeToFile(@NotNull Project pProject, Stream<WarningsItem>
fileContent.content = pWarningsItemStream
.distinct()
.collect(Collectors.toMap(pWarningsItem -> String.valueOf(pWarningsItem.getId()), WarningsItem::getDescription));
try (FileWriter writer = new FileWriter(IgnoredWarningsCache.getIgnoredWarningsFile(pProject)))
try (FileWriter writer = new FileWriter(IgnoredWarningsProvider.getIgnoredWarningsFile(pProject)))
{
writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(fileContent));
writer.flush();
}
FileUtil.toFileObject(IgnoredWarningsCache.getIgnoredWarningsFile(pProject)).refresh();
FileUtil.toFileObject(IgnoredWarningsProvider.getIgnoredWarningsFile(pProject)).refresh();
}

public static class WarningsItem
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package de.adito.aditoweb.nbm.nodejs.impl.ls;

import com.google.gson.Gson;
import de.adito.aditoweb.nbm.nbide.nbaditointerface.project.IProjectVisibility;
import de.adito.notification.INotificationFacade;
import de.adito.observables.netbeans.FileObservable;
import io.reactivex.rxjava3.core.Observable;
import org.jetbrains.annotations.*;
import org.netbeans.api.project.*;
import org.openide.filesystems.FileUtil;
import org.openide.util.lookup.ServiceProvider;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author m.kaspera, 26.08.2022
*/
@ServiceProvider(service = IgnoredWarningsProvider.class, path = "Projects/de-adito-project/Lookup")
public class IgnoredWarningsProvider
{

private Observable<Set<IgnoredWarningsFacade.WarningsItem>> warningsObs = null;
private final Project project;

public IgnoredWarningsProvider()
{
project = null;
}

public IgnoredWarningsProvider(Project pProject)
{
project = pProject;
}

/**
* IMPORTANT: only use this method if the instance of this serviceProvider was obtained by querying the lookup of the actual project,
* if you got obtained this instance by querying the default lookup use the get(Project pProject) method.
*
* @return Observable of the Set of WarningsItems that are ignored for the given project. Contains items of the parent project(s) if this project
* is only used as a module, or an empty Observable if this serviceProvider did not get a project
*/
@NotNull
public Observable<Set<IgnoredWarningsFacade.WarningsItem>> get()
{
if (project != null)
return get(project);
else
return Observable.just(Set.of());
}

/**
* @param pProject Project whose warningsItems should be obtained
* @return Observable of the Set of WarningsItems that are ignored for the given project. Contains items of the parent project(s) if this project
* is only used as a module
*/
@NotNull
public Observable<Set<IgnoredWarningsFacade.WarningsItem>> get(@NotNull Project pProject)
{
if (warningsObs == null)
{
try
{
if (Boolean.TRUE.equals(pProject.getLookup().lookup(IProjectVisibility.class).isVisible()))
{
File ignoredWarningsFile = getIgnoredWarningsFile(pProject);
warningsObs = FileObservable.createForPlainFile(ignoredWarningsFile)
.map(pFile -> readIgnoredWarnings(ignoredWarningsFile));
}
else
{
List<Observable<Set<IgnoredWarningsFacade.WarningsItem>>> warningsItemsObs = new ArrayList<>();
Project currentProj = pProject;
boolean breakLoop = false;
while (currentProj != null && !breakLoop)
{
breakLoop = (Boolean.TRUE.equals(currentProj.getLookup().lookup(IProjectVisibility.class).isVisible()));
File ignoredWarningsFile = getIgnoredWarningsFile(currentProj);
warningsItemsObs.add(FileObservable.createForPlainFile(ignoredWarningsFile)
.map(pFile -> readIgnoredWarnings(ignoredWarningsFile)));
currentProj = FileOwnerQuery.getOwner(currentProj.getProjectDirectory().getParent());
}
warningsObs = Observable.combineLatest(warningsItemsObs, IgnoredWarningsProvider::combineSetArray);
}
}
catch (IOException pE)
{
INotificationFacade.INSTANCE.error(pE);
warningsObs = Observable.just(Set.of());
}
}
return warningsObs;
}

/**
* Combine an array containing sets of warningsItems to a single set
*
* @param setSet array of sets of warningsItems
* @return combined set
*/
@NotNull
private static Set<IgnoredWarningsFacade.WarningsItem> combineSetArray(@NotNull Object[] setSet)
{
//noinspection unchecked array contains sets of WarningsItems, but has to be object array to satisfy signatures
return Arrays.stream(setSet)
.map(pX -> ((Set<IgnoredWarningsFacade.WarningsItem>) pX))
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}

@SuppressWarnings("ResultOfMethodCallIgnored") // ignore for mkdirs and createNewFile
@NotNull
public static File getIgnoredWarningsFile(@NotNull Project pProject) throws IOException
{
File ignoredWarnings = new File(FileUtil.toFile(pProject.getProjectDirectory()), ".aditoprj/ignoredWarnings");
if (!ignoredWarnings.exists())
{
ignoredWarnings.getParentFile().mkdirs();
ignoredWarnings.createNewFile();
}
return ignoredWarnings;
}

private Set<IgnoredWarningsFacade.WarningsItem> readIgnoredWarnings(@Nullable File pIgnoreFile) throws FileNotFoundException
{
if (pIgnoreFile == null)
return Set.of();
IgnoreWarningFix.FileContent fileContent = new Gson().fromJson(new FileReader(pIgnoreFile), IgnoreWarningFix.FileContent.class);
HashSet<IgnoredWarningsFacade.WarningsItem> warningsSet = new HashSet<>();
Set<Map.Entry<String, String>> entrySet = Optional.ofNullable(fileContent)
.map(pFileContent -> pFileContent.content)
.map(Map::entrySet)
.orElse(Set.of());
for (Map.Entry<String, String> warningItem : entrySet)
{
warningsSet.add(new IgnoredWarningsFacade.WarningsItem(Integer.parseInt(warningItem.getKey()), warningItem.getValue()));
}
return warningsSet;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import de.adito.aditoweb.nbm.nbide.nbaditointerface.lsp.ILSPHintsFilter;
import org.jetbrains.annotations.*;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.*;
import org.openide.filesystems.FileObject;
import org.openide.text.PositionBounds;
import org.openide.util.lookup.ServiceProvider;
Expand Down Expand Up @@ -31,9 +31,10 @@ public boolean canFilter(@NotNull FileObject pFileObject)
@Override
public boolean filter(@NotNull FileObject pFileObject, @Nullable String pId, @NotNull String pDescription, @NotNull String pSeverity, @Nullable PositionBounds pRange)
{
Project project = FileOwnerQuery.getOwner(pFileObject);
// Filter hints that were set as ignored by the user
Set<IgnoredWarningsFacade.WarningsItem> warningsItems = IgnoredWarningsCache.getInstance()
.get(FileOwnerQuery.getOwner(pFileObject))
Set<IgnoredWarningsFacade.WarningsItem> warningsItems = project.getLookup().lookup(IgnoredWarningsProvider.class)
.get()
.blockingFirst();
if (pId != null && warningsItems.stream().anyMatch(pWarningsItem -> pWarningsItem.getId() == Integer.parseInt(pId)))
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ private static class IgnoredWarningsTableModel implements TableModel

public IgnoredWarningsTableModel(@NotNull Project pOpenProject, @NotNull JTable pTable)
{
IgnoredWarningsCache cache = IgnoredWarningsCache.getInstance();
IgnoredWarningsProvider provider = pOpenProject.getLookup().lookup(IgnoredWarningsProvider.class);
IDisposerService disposerService = Lookup.getDefault().lookup(IDisposerService.class);
disposerService.register(pOpenProject, cache.get(pOpenProject).subscribe(pWarningsItems -> {
disposerService.register(pOpenProject, provider.get().subscribe(pWarningsItems -> {
warningsItems = pWarningsItems.stream()
.sorted(Comparator.comparing(IgnoredWarningsFacade.WarningsItem::getId))
.collect(Collectors.toList());
Expand Down Expand Up @@ -183,7 +183,7 @@ public void actionPerformed(ActionEvent e)
try
{
IgnoredWarningsFacade.unIgnoreWarnings(project, itemsToRemove);
FileUtil.toFileObject(IgnoredWarningsCache.getIgnoredWarningsFile(project)).refresh();
FileUtil.toFileObject(IgnoredWarningsProvider.getIgnoredWarningsFile(project)).refresh();
}
catch (IOException pE)
{
Expand Down

0 comments on commit 94fa0c2

Please sign in to comment.