Search in sources :

Example 1 with UndoRefactoringElementAdapter

use of com.intellij.refactoring.listeners.UndoRefactoringElementAdapter in project intellij-community by JetBrains.

the class JsonSchemaRefactoringListenerProvider method getListener.

@Nullable
@Override
public RefactoringElementListener getListener(PsiElement element) {
    if (element == null) {
        return null;
    }
    VirtualFile fileAtElement = PsiUtilBase.asVirtualFile(element);
    if (fileAtElement == null || !(fileAtElement.getFileType() instanceof LanguageFileType) || !(((LanguageFileType) fileAtElement.getFileType()).getLanguage().isKindOf(JsonLanguage.INSTANCE))) {
        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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) UndoRefactoringElementAdapter(com.intellij.refactoring.listeners.UndoRefactoringElementAdapter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 UndoRefactoringElementAdapter (com.intellij.refactoring.listeners.UndoRefactoringElementAdapter)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1