/* * 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.intellij.psi.templateLanguages; import com.intellij.lang.Language; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.impl.FilePropertyPusher; import com.intellij.openapi.roots.impl.PushedFilePropertiesUpdater; import com.intellij.openapi.util.Conditions; import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.FileAttribute; import com.intellij.openapi.vfs.newvfs.persistent.VfsDependentEnum; import com.intellij.util.io.DataInputOutputUtil; import com.intellij.util.io.EnumeratorStringDescriptor; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; /** * @author Konstantin.Ulitin */ public class TemplateDataLanguagePusher implements FilePropertyPusher<Language> { public static final Key<Language> KEY = Key.create("TEMPLATE_DATA_LANGUAGE"); private static final VfsDependentEnum<String> ourLanguagesEnumerator = new VfsDependentEnum<>( "languages", EnumeratorStringDescriptor.INSTANCE, 1 ); @Override public void initExtra(@NotNull Project project, @NotNull MessageBus bus, @NotNull Engine languageLevelUpdater) { } @NotNull @Override public Key<Language> getFileDataKey() { return KEY; } @Override public boolean pushDirectoriesOnly() { return false; } @NotNull @Override public Language getDefaultValue() { return Language.ANY; } @Nullable @Override public Language getImmediateValue(@NotNull Project project, @Nullable VirtualFile file) { return TemplateDataLanguageMappings.getInstance(project).getImmediateMapping(file); } @Nullable @Override public Language getImmediateValue(@NotNull Module module) { return null; } @Override public boolean acceptsFile(@NotNull VirtualFile file) { return true; } @Override public boolean acceptsDirectory(@NotNull VirtualFile file, @NotNull Project project) { return true; } private static final FileAttribute PERSISTENCE = new FileAttribute("template_language", 2, true); @Override public void persistAttribute(@NotNull Project project, @NotNull VirtualFile fileOrDir, @NotNull Language value) throws IOException { final DataInputStream iStream = PERSISTENCE.readAttribute(fileOrDir); if (iStream != null) { try { final int oldLanguage = DataInputOutputUtil.readINT(iStream); String oldLanguageId = ourLanguagesEnumerator.getById(oldLanguage); if (value.getID().equals(oldLanguageId)) return; } finally { //noinspection ThrowFromFinallyBlock iStream.close(); } } if (value != Language.ANY || iStream != null) { final DataOutputStream oStream = PERSISTENCE.writeAttribute(fileOrDir); DataInputOutputUtil.writeINT(oStream, ourLanguagesEnumerator.getId(value.getID())); oStream.close(); PushedFilePropertiesUpdater.getInstance(project).filePropertiesChanged(fileOrDir, Conditions.alwaysTrue()); } } @Override public void afterRootsChanged(@NotNull Project project) { } }