diff --git a/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguageMappings.java b/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguageMappings.java index ad3ac2611d81..7460ece74572 100644 --- a/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguageMappings.java +++ b/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguageMappings.java @@ -23,12 +23,15 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.impl.FilePropertyPusher; import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Map; /** * @author peter @@ -54,8 +57,20 @@ public class TemplateDataLanguageMappings extends LanguagePerFileMappings myPropertyPusher = new TemplateDataLanguagePusher(); + + @NotNull + @Override + protected FilePropertyPusher getFilePropertyPusher() { + return myPropertyPusher; + } } diff --git a/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguagePusher.java b/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguagePusher.java new file mode 100644 index 000000000000..cae3f6e1d059 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/templateLanguages/TemplateDataLanguagePusher.java @@ -0,0 +1,113 @@ +/* + * 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.Key; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.newvfs.FileAttribute; +import com.intellij.util.io.DataInputOutputUtil; +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 { + + public static final Key KEY = Key.create("TEMPLATE_DATA_LANGUAGE"); + + @Override + public void initExtra(@NotNull Project project, @NotNull MessageBus bus, @NotNull Engine languageLevelUpdater) { + + } + + @NotNull + @Override + public Key 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", 1, 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); + if (value.getID().hashCode() == oldLanguage) return; + } + finally { + //noinspection ThrowFromFinallyBlock + iStream.close(); + } + } + + if (value != Language.ANY || iStream != null) { + final DataOutputStream oStream = PERSISTENCE.writeAttribute(fileOrDir); + DataInputOutputUtil.writeINT(oStream, value.getID().hashCode()); + oStream.close(); + PushedFilePropertiesUpdater.getInstance(project).filePropertiesChanged(fileOrDir); + } + } + + @Override + public void afterRootsChanged(@NotNull Project project) { + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 02489b6d7dd9..eb839b34b1e2 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -589,6 +589,7 @@ serviceImplementation="com.intellij.psi.templateLanguages.TemplateDataLanguageMappings"/> +