IJPL-159596 refactor ExternalResourceManagerExImpl

GitOrigin-RevId: d901d980d1c2f38143742d6669f95dec1a97c07e
This commit is contained in:
Vladimir Krivosheev
2024-08-04 09:23:06 +02:00
committed by intellij-monorepo-bot
parent a2b7af3b13
commit ddd74095aa
7 changed files with 482 additions and 546 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.intellij.lang.xpath.xslt.impl.references;
import com.intellij.codeInspection.LocalQuickFix;
@@ -36,40 +35,39 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
class ExternalResourceReference implements PsiReference, LocalQuickFixProvider {
private final XmlAttribute myAttribute;
private final ExternalResourceManager myResourceManager = ExternalResourceManager.getInstance();
final class ExternalResourceReference implements PsiReference, LocalQuickFixProvider {
private final XmlAttribute attribute;
private final ExternalResourceManager resourceManager = ExternalResourceManager.getInstance();
ExternalResourceReference(XmlAttribute attribute) {
myAttribute = attribute;
this.attribute = attribute;
}
@Override
public @NotNull LocalQuickFix @Nullable [] getQuickFixes() {
return new LocalQuickFix[] { new DownloadResourceFix(myAttribute.getValue()) };
return new LocalQuickFix[] { new DownloadResourceFix(attribute.getValue()) };
}
@Override
@NotNull
public PsiElement getElement() {
return myAttribute.getValueElement();
public @NotNull PsiElement getElement() {
return attribute.getValueElement();
}
@Override
@NotNull
public TextRange getRangeInElement() {
final XmlAttributeValue value = myAttribute.getValueElement();
public @NotNull TextRange getRangeInElement() {
final XmlAttributeValue value = attribute.getValueElement();
return value != null ? TextRange.from(1, value.getTextLength() - 2) : TextRange.from(0, 0);
}
@Override
@Nullable
public PsiElement resolve() {
final String value = myAttribute.getValue();
final String resourceLocation = myResourceManager.getResourceLocation(value);
public @Nullable PsiElement resolve() {
String value = attribute.getValue();
String resourceLocation = resourceManager.getResourceLocation(value, attribute.getProject());
if (Objects.equals(resourceLocation, value)) {
return null;
}
if (!Objects.equals(resourceLocation, value)) {
VirtualFile file;
try {
file = VfsUtil.findFileByURL(new URL(resourceLocation));
@@ -83,22 +81,20 @@ class ExternalResourceReference implements PsiReference, LocalQuickFixProvider {
}
}
if (file != null) {
return myAttribute.getManager().findFile(file);
}
return attribute.getManager().findFile(file);
}
return null;
}
@Override
@NotNull
public String getCanonicalText() {
return myAttribute.getValue();
public @NotNull String getCanonicalText() {
return attribute.getValue();
}
@Override
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
myAttribute.setValue(newElementName);
final XmlAttributeValue value = myAttribute.getValueElement();
attribute.setValue(newElementName);
final XmlAttributeValue value = attribute.getValueElement();
assert value != null;
return value;
}
@@ -115,7 +111,7 @@ class ExternalResourceReference implements PsiReference, LocalQuickFixProvider {
@Override
public Object @NotNull [] getVariants() {
return myResourceManager.getResourceUrls(null, false);
return resourceManager.getResourceUrls(null, false);
}
@Override

View File

@@ -38,27 +38,25 @@ import java.lang.reflect.InvocationTargetException;
import java.util.Set;
public abstract class DownloadManager {
private static final ExternalResourceManager resourceManager = ExternalResourceManager.getInstance();
private final Project myProject;
private final ProgressIndicator myProgress;
private final String myResourcePath;
private final Project project;
private final ProgressIndicator progress;
private final String resourcePath;
public DownloadManager(Project project, ProgressIndicator progress) {
myProject = project;
myProgress = progress;
this.project = project;
this.progress = progress;
myResourcePath = PathManager.getSystemPath() + File.separatorChar + "extResources";
resourcePath = PathManager.getSystemPath() + File.separatorChar + "extResources";
//noinspection ResultOfMethodCallIgnored
new File(myResourcePath).mkdirs();
new File(resourcePath).mkdirs();
}
public void fetch(@NotNull final String location) throws DownloadException {
if (!location.equals(resourceManager.getResourceLocation(location, myProject))) {
public void fetch(@NotNull String location) throws DownloadException {
if (!location.equals(ExternalResourceManager.getInstance().getResourceLocation(location, project))) {
return;
}
myProgress.setText(XPathBundle.message("progress.text.downloading", location));
progress.setText(XPathBundle.message("progress.text.downloading", location));
File file = null;
try {
@@ -68,32 +66,33 @@ public abstract class DownloadManager {
String name = Integer.toHexString(System.identityHashCode(this)) + "_" +
Integer.toHexString(location.hashCode()) + "_" +
location.substring(location.lastIndexOf('/') + 1);
return request.saveToFile(new File(myResourcePath, name.lastIndexOf('.') == -1 ? name + ".xml" : name), myProgress);
return request.saveToFile(new File(resourcePath, name.lastIndexOf('.') == -1 ? name + ".xml" : name), progress);
}
});
try {
//noinspection unchecked
final Set<String>[] resourceDependencies = new Set[1];
final VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
Set<String>[] resourceDependencies = new Set[1];
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
if (vf != null) {
PsiFile psiFile = PsiManager.getInstance(myProject).findFile(vf);
PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
if (psiFile != null && isAccepted(psiFile)) {
resourceDependencies[0] = getResourceDependencies(psiFile);
resourceManager.addResource(location, file.getAbsolutePath());
ExternalResourceManager.getInstance().addResource(location, file.getAbsolutePath());
}
else {
ApplicationManager.getApplication().invokeLater(
() -> Messages.showErrorDialog(myProject,
ApplicationManager.getApplication().invokeLater(() -> {
Messages.showErrorDialog(project,
XPathBundle.message("dialog.message.not.valid.file", vf.getPresentableUrl()),
XPathBundle.message("dialog.title.download.problem")), myProject.getDisposed());
XPathBundle.message("dialog.title.download.problem"));
}, project.getDisposed());
}
}
if (resourceDependencies[0] != null) {
for (String s : resourceDependencies[0]) {
myProgress.checkCanceled();
myProgress.setFraction(0);
progress.checkCanceled();
progress.setFraction(0);
fetch(s);
}
}
@@ -119,7 +118,7 @@ public abstract class DownloadManager {
throw new DownloadException(location, e);
}
finally {
if (file != null && location.equals(resourceManager.getResourceLocation(location, myProject))) {
if (file != null && location.equals(ExternalResourceManager.getInstance().getResourceLocation(location, project))) {
// something went wrong. get rid of the file
FileUtil.delete(file);
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.javaee;
import com.intellij.codeInsight.daemon.impl.quickfix.FetchExtResourceAction;
@@ -9,7 +9,6 @@ import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.util.CachedValueImpl;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.IndexableSetContributor;
import org.jetbrains.annotations.NotNull;
@@ -22,13 +21,16 @@ final class ExternalResourcesRootsProvider extends IndexableSetContributor {
ExternalResourceManagerExImpl manager = (ExternalResourceManagerExImpl)ExternalResourceManager.getInstance();
Set<String> duplicateCheck = new HashSet<>();
Set<VirtualFile> set = new HashSet<>();
for (Map<String, ExternalResourceManagerExImpl.Resource> map : manager.getStandardResources()) {
for (Map<String, ExternalResourceManagerExImpl.Resource> map : manager.getStandardResources$intellij_xml_psi_impl()) {
for (ExternalResourceManagerExImpl.Resource resource : map.values()) {
String url = resource.getResourceUrl();
if (url != null) {
url = url.substring(0, url.lastIndexOf('/') + 1);
if (duplicateCheck.add(url)) {
ContainerUtil.addIfNotNull(set, VfsUtilCore.findRelativeFile(url, null));
VirtualFile file = VfsUtilCore.findRelativeFile(url, null);
if (file != null) {
set.add(file);
}
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.javaee;
import com.intellij.openapi.application.ApplicationManager;
@@ -15,9 +15,9 @@ public abstract class ExternalResourceManager extends SimpleModificationTracker
return ApplicationManager.getApplication().getService(ExternalResourceManager.class);
}
public abstract void addResource(@NotNull @NonNls String url, @NonNls String location);
public abstract void addResource(@NotNull @NonNls String url, @NonNls @NotNull String location);
public abstract void addResource(@NotNull @NonNls String url, @NonNls @Nullable String version, @NonNls String location);
public abstract void addResource(@NotNull @NonNls String url, @NonNls @Nullable String version, @NotNull @NonNls String location);
public abstract void removeResource(@NotNull String url);

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.javaee;
import com.intellij.openapi.components.State;
@@ -11,9 +11,9 @@ import java.util.Map;
* @author Dmitry Avdeev
*/
@State(name = "ProjectResources")
public final class ProjectResources extends ExternalResourceManagerExImpl {
final class ProjectResources extends ExternalResourceManagerExImpl {
@Override
protected @NotNull Map<String, Map<String, Resource>> computeStdResources() {
public @NotNull Map<@NotNull String, @NotNull Map<@NotNull String, @NotNull Resource>> computeStdResources$intellij_xml_psi_impl() {
return Collections.emptyMap();
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.javaee;
import org.jetbrains.annotations.NonNls;
@@ -14,8 +14,8 @@ import java.util.Map;
* @author Dmitry Avdeev
*/
public final class ResourceRegistrarImpl implements ResourceRegistrar {
private final Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> myResources = new HashMap<>();
private final List<String> myIgnored = new ArrayList<>();
private final Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> resources = new HashMap<>();
private final List<String> ignored = new ArrayList<>();
@Override
public void addStdResource(@NonNls String resource, @NonNls String fileName) {
@@ -28,7 +28,7 @@ public final class ResourceRegistrarImpl implements ResourceRegistrar {
}
public void addStdResource(@NonNls String resource, @NonNls String version, @NonNls String fileName, @Nullable Class<?> klass, @Nullable ClassLoader classLoader) {
Map<String, ExternalResourceManagerExImpl.Resource> map = ExternalResourceManagerExImpl.getOrCreateMap(myResources, version);
Map<String, ExternalResourceManagerExImpl.Resource> map = ExternalResourceManagerExImpl.Companion.getOrCreateMap(resources, version);
map.put(resource, new ExternalResourceManagerExImpl.Resource(fileName, klass, classLoader));
}
@@ -39,7 +39,7 @@ public final class ResourceRegistrarImpl implements ResourceRegistrar {
@Override
public void addIgnoredResource(@NonNls String url) {
myIgnored.add(url);
ignored.add(url);
}
public void addInternalResource(@NonNls String resource, @NonNls String fileName) {
@@ -59,10 +59,10 @@ public final class ResourceRegistrarImpl implements ResourceRegistrar {
}
public @NotNull Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> getResources() {
return myResources;
return resources;
}
public @NotNull List<String> getIgnored() {
return myIgnored;
return ignored;
}
}