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

View File

@@ -38,27 +38,25 @@ import java.lang.reflect.InvocationTargetException;
import java.util.Set; import java.util.Set;
public abstract class DownloadManager { public abstract class DownloadManager {
private static final ExternalResourceManager resourceManager = ExternalResourceManager.getInstance(); private final Project project;
private final ProgressIndicator progress;
private final Project myProject; private final String resourcePath;
private final ProgressIndicator myProgress;
private final String myResourcePath;
public DownloadManager(Project project, ProgressIndicator progress) { public DownloadManager(Project project, ProgressIndicator progress) {
myProject = project; this.project = project;
myProgress = progress; this.progress = progress;
myResourcePath = PathManager.getSystemPath() + File.separatorChar + "extResources"; resourcePath = PathManager.getSystemPath() + File.separatorChar + "extResources";
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(myResourcePath).mkdirs(); new File(resourcePath).mkdirs();
} }
public void fetch(@NotNull final String location) throws DownloadException { public void fetch(@NotNull String location) throws DownloadException {
if (!location.equals(resourceManager.getResourceLocation(location, myProject))) { if (!location.equals(ExternalResourceManager.getInstance().getResourceLocation(location, project))) {
return; return;
} }
myProgress.setText(XPathBundle.message("progress.text.downloading", location)); progress.setText(XPathBundle.message("progress.text.downloading", location));
File file = null; File file = null;
try { try {
@@ -68,32 +66,33 @@ public abstract class DownloadManager {
String name = Integer.toHexString(System.identityHashCode(this)) + "_" + String name = Integer.toHexString(System.identityHashCode(this)) + "_" +
Integer.toHexString(location.hashCode()) + "_" + Integer.toHexString(location.hashCode()) + "_" +
location.substring(location.lastIndexOf('/') + 1); 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 { try {
//noinspection unchecked //noinspection unchecked
final Set<String>[] resourceDependencies = new Set[1]; Set<String>[] resourceDependencies = new Set[1];
final VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
if (vf != null) { if (vf != null) {
PsiFile psiFile = PsiManager.getInstance(myProject).findFile(vf); PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
if (psiFile != null && isAccepted(psiFile)) { if (psiFile != null && isAccepted(psiFile)) {
resourceDependencies[0] = getResourceDependencies(psiFile); resourceDependencies[0] = getResourceDependencies(psiFile);
resourceManager.addResource(location, file.getAbsolutePath()); ExternalResourceManager.getInstance().addResource(location, file.getAbsolutePath());
} }
else { else {
ApplicationManager.getApplication().invokeLater( ApplicationManager.getApplication().invokeLater(() -> {
() -> Messages.showErrorDialog(myProject, Messages.showErrorDialog(project,
XPathBundle.message("dialog.message.not.valid.file", vf.getPresentableUrl()), 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) { if (resourceDependencies[0] != null) {
for (String s : resourceDependencies[0]) { for (String s : resourceDependencies[0]) {
myProgress.checkCanceled(); progress.checkCanceled();
myProgress.setFraction(0); progress.setFraction(0);
fetch(s); fetch(s);
} }
} }
@@ -119,7 +118,7 @@ public abstract class DownloadManager {
throw new DownloadException(location, e); throw new DownloadException(location, e);
} }
finally { 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 // something went wrong. get rid of the file
FileUtil.delete(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; package com.intellij.javaee;
import com.intellij.codeInsight.daemon.impl.quickfix.FetchExtResourceAction; 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.CachedValue;
import com.intellij.psi.util.CachedValueProvider; import com.intellij.psi.util.CachedValueProvider;
import com.intellij.util.CachedValueImpl; import com.intellij.util.CachedValueImpl;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.IndexableSetContributor; import com.intellij.util.indexing.IndexableSetContributor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -22,13 +21,16 @@ final class ExternalResourcesRootsProvider extends IndexableSetContributor {
ExternalResourceManagerExImpl manager = (ExternalResourceManagerExImpl)ExternalResourceManager.getInstance(); ExternalResourceManagerExImpl manager = (ExternalResourceManagerExImpl)ExternalResourceManager.getInstance();
Set<String> duplicateCheck = new HashSet<>(); Set<String> duplicateCheck = new HashSet<>();
Set<VirtualFile> set = 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()) { for (ExternalResourceManagerExImpl.Resource resource : map.values()) {
String url = resource.getResourceUrl(); String url = resource.getResourceUrl();
if (url != null) { if (url != null) {
url = url.substring(0, url.lastIndexOf('/') + 1); url = url.substring(0, url.lastIndexOf('/') + 1);
if (duplicateCheck.add(url)) { 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; package com.intellij.javaee;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
@@ -15,9 +15,9 @@ public abstract class ExternalResourceManager extends SimpleModificationTracker
return ApplicationManager.getApplication().getService(ExternalResourceManager.class); 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); 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; package com.intellij.javaee;
import com.intellij.openapi.components.State; import com.intellij.openapi.components.State;
@@ -11,9 +11,9 @@ import java.util.Map;
* @author Dmitry Avdeev * @author Dmitry Avdeev
*/ */
@State(name = "ProjectResources") @State(name = "ProjectResources")
public final class ProjectResources extends ExternalResourceManagerExImpl { final class ProjectResources extends ExternalResourceManagerExImpl {
@Override @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(); 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; package com.intellij.javaee;
import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NonNls;
@@ -14,8 +14,8 @@ import java.util.Map;
* @author Dmitry Avdeev * @author Dmitry Avdeev
*/ */
public final class ResourceRegistrarImpl implements ResourceRegistrar { public final class ResourceRegistrarImpl implements ResourceRegistrar {
private final Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> myResources = new HashMap<>(); private final Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> resources = new HashMap<>();
private final List<String> myIgnored = new ArrayList<>(); private final List<String> ignored = new ArrayList<>();
@Override @Override
public void addStdResource(@NonNls String resource, @NonNls String fileName) { 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) { 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)); map.put(resource, new ExternalResourceManagerExImpl.Resource(fileName, klass, classLoader));
} }
@@ -39,7 +39,7 @@ public final class ResourceRegistrarImpl implements ResourceRegistrar {
@Override @Override
public void addIgnoredResource(@NonNls String url) { public void addIgnoredResource(@NonNls String url) {
myIgnored.add(url); ignored.add(url);
} }
public void addInternalResource(@NonNls String resource, @NonNls String fileName) { 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() { public @NotNull Map<String, Map<String, ExternalResourceManagerExImpl.Resource>> getResources() {
return myResources; return resources;
} }
public @NotNull List<String> getIgnored() { public @NotNull List<String> getIgnored() {
return myIgnored; return ignored;
} }
} }