mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
WEB-20365 JSON Schema: do not remove schema silently on file move/delete
This commit is contained in:
@@ -2,9 +2,16 @@ package com.jetbrains.jsonSchema;
|
||||
|
||||
import com.intellij.openapi.components.*;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.xmlb.annotations.Transient;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Irina.Chernushina on 2/2/2016.
|
||||
@@ -17,6 +24,8 @@ import java.io.File;
|
||||
)
|
||||
public class JsonSchemaMappingsProjectConfiguration extends JsonSchemaMappingsConfigurationBase {
|
||||
private Project myProject;
|
||||
@Transient
|
||||
private final Map<VirtualFile, SchemaInfo> mySchemaFiles = new HashMap<>();
|
||||
|
||||
public static JsonSchemaMappingsProjectConfiguration getInstance(@NotNull final Project project) {
|
||||
return ServiceManager.getService(project, JsonSchemaMappingsProjectConfiguration.class);
|
||||
@@ -33,4 +42,48 @@ public class JsonSchemaMappingsProjectConfiguration extends JsonSchemaMappingsCo
|
||||
public File convertToAbsoluteFile(@NotNull String path) {
|
||||
return myProject.getBasePath() == null ? new File(path) : new File(myProject.getBasePath(), path);
|
||||
}
|
||||
|
||||
public boolean isRegisteredSchemaFile(@NotNull VirtualFile file) {
|
||||
return mySchemaFiles.containsKey(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setState(@NotNull Map<String, SchemaInfo> state) {
|
||||
super.setState(state);
|
||||
recalculateSchemaFiles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSchema(@NotNull SchemaInfo info) {
|
||||
super.addSchema(info);
|
||||
recalculateSchemaFiles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSchema(@NotNull SchemaInfo info) {
|
||||
super.removeSchema(info);
|
||||
recalculateSchemaFiles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadState(JsonSchemaMappingsConfigurationBase state) {
|
||||
super.loadState(state);
|
||||
recalculateSchemaFiles();
|
||||
}
|
||||
|
||||
private void recalculateSchemaFiles() {
|
||||
mySchemaFiles.clear();
|
||||
if (myProject == null || myProject.getBaseDir() == null) return;
|
||||
|
||||
for (JsonSchemaMappingsConfigurationBase.SchemaInfo info : myState.values()) {
|
||||
final String pathToSchema = FileUtil.toSystemIndependentName(info.getRelativePathToSchema());
|
||||
final VirtualFile schemaFile = VfsUtil.findRelativeFile(myProject.getBaseDir(), pathToSchema);
|
||||
if (schemaFile != null) mySchemaFiles.put(schemaFile, info);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public SchemaInfo getSchemaBySchemaFile(@NotNull final VirtualFile file) {
|
||||
return mySchemaFiles.get(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.jetbrains.jsonSchema;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListenerProvider;
|
||||
import com.intellij.refactoring.listeners.UndoRefactoringElementAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Irina.Chernushina on 2/17/2016.
|
||||
*/
|
||||
public class JsonSchemaRefactoringListenerProvider implements RefactoringElementListenerProvider {
|
||||
@Nullable
|
||||
@Override
|
||||
public RefactoringElementListener getListener(PsiElement element) {
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
VirtualFile fileAtElement = PsiUtilBase.asVirtualFile(element);
|
||||
if (fileAtElement == null) {
|
||||
return null;
|
||||
}
|
||||
Project project = element.getProject();
|
||||
final JsonSchemaMappingsProjectConfiguration configuration = JsonSchemaMappingsProjectConfiguration.getInstance(project);
|
||||
final JsonSchemaMappingsConfigurationBase.SchemaInfo schemaInfo = configuration.getSchemaBySchemaFile(fileAtElement);
|
||||
if (schemaInfo != null && project.getBaseDir() != null) {
|
||||
return new UndoRefactoringElementAdapter() {
|
||||
@Override
|
||||
protected void refactored(@NotNull PsiElement element, @Nullable String oldQualifiedName) {
|
||||
VirtualFile newFile = PsiUtilBase.asVirtualFile(element);
|
||||
if (newFile != null) {
|
||||
final String relativePath = VfsUtil.getRelativePath(newFile, project.getBaseDir());
|
||||
if (relativePath != null) {
|
||||
configuration.removeSchema(schemaInfo);
|
||||
final JsonSchemaMappingsConfigurationBase.SchemaInfo newSchema =
|
||||
new JsonSchemaMappingsConfigurationBase.SchemaInfo(schemaInfo.getName(), relativePath, schemaInfo.isApplicationLevel(),
|
||||
schemaInfo.getPatterns());
|
||||
configuration.addSchema(newSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,8 @@ package com.jetbrains.jsonSchema.extension;
|
||||
import com.intellij.json.JsonFileType;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.ResourceUtil;
|
||||
import com.jetbrains.jsonSchema.JsonSchemaMappingsConfigurationBase;
|
||||
import com.jetbrains.jsonSchema.JsonSchemaMappingsProjectConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -31,7 +28,6 @@ import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Irina.Chernushina on 2/16/2016.
|
||||
@@ -46,15 +42,7 @@ public class JsonSchemaSelfProviderFactory implements JsonSchemaProviderFactory
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull VirtualFile file) {
|
||||
if (project == null || !JsonFileType.INSTANCE.equals(file.getFileType())) return false;
|
||||
final JsonSchemaMappingsProjectConfiguration configuration = JsonSchemaMappingsProjectConfiguration.getInstance(project);
|
||||
final Map<String, JsonSchemaMappingsConfigurationBase.SchemaInfo> map = configuration.getStateMap();
|
||||
for (JsonSchemaMappingsConfigurationBase.SchemaInfo info : map.values()) {
|
||||
final String pathToSchema = FileUtil.toSystemIndependentName(info.getRelativePathToSchema());
|
||||
if (! file.getPath().endsWith(pathToSchema)) continue;
|
||||
final VirtualFile schemaFile = VfsUtil.findRelativeFile(project.getBaseDir(), pathToSchema);
|
||||
if (file.equals(schemaFile)) return true;
|
||||
}
|
||||
return false;
|
||||
return JsonSchemaMappingsProjectConfiguration.getInstance(project).isRegisteredSchemaFile(file);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
<completion.contributor order="last, before JsonSchemaCompletionContributor" language="JSON" implementationClass="com.jetbrains.jsonSchema.ide.JsonSchemaCompletionContributor"/>
|
||||
<lang.documentationProvider language="JSON"
|
||||
implementationClass="com.jetbrains.jsonSchema.ide.JsonSchemaDocumentationProvider"/>
|
||||
<refactoring.elementListenerProvider implementation="com.jetbrains.jsonSchema.JsonSchemaRefactoringListenerProvider"/>
|
||||
</extensions>
|
||||
<extensions defaultExtensionNs="JavaScript">
|
||||
<JsonSchema.ProviderFactory implementation="com.jetbrains.jsonSchema.extension.JsonSchemaImportedProviderFactory"/>
|
||||
|
||||
Reference in New Issue
Block a user