2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi;
18 import com.intellij.lang.Language;
19 import com.intellij.lang.LanguageExtension;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.application.ModalityState;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.util.Key;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.util.FileContentUtilCore;
27 import com.intellij.util.ObjectUtils;
28 import org.jetbrains.annotations.NotNull;
33 public final class LanguageSubstitutors extends LanguageExtension<LanguageSubstitutor> {
34 public static final LanguageSubstitutors INSTANCE = new LanguageSubstitutors();
35 private static final Logger LOG = Logger.getInstance(LanguageSubstitutors.class);
36 private static final Key<Language> SUBSTITUTED_LANG_KEY = Key.create("SUBSTITUTED_LANG_KEY");
38 private boolean myReparsingInProgress;
40 private LanguageSubstitutors() {
41 super("com.intellij.lang.substitutor");
45 public Language substituteLanguage(@NotNull Language lang, @NotNull VirtualFile file, @NotNull Project project) {
46 for (LanguageSubstitutor substitutor : forKey(lang)) {
47 Language language = substitutor.getLanguage(file, project);
48 if (language != null) {
49 processLanguageSubstitution(file, lang, language);
57 private void processLanguageSubstitution(@NotNull final VirtualFile file,
58 @NotNull Language originalLang,
59 @NotNull final Language substitutedLang) {
60 Language prevSubstitutedLang = SUBSTITUTED_LANG_KEY.get(file);
61 final Language prevLang = ObjectUtils.notNull(prevSubstitutedLang, originalLang);
62 if (!substitutedLang.equals(prevLang)) {
63 if (ApplicationManager.getApplication().isDispatchThread() && myReparsingInProgress) {
64 return; // avoid recursive reparsing
66 if (file.replace(SUBSTITUTED_LANG_KEY, prevSubstitutedLang, substitutedLang)) {
67 invokeLaterIfNeeded(new Runnable() {
70 LOG.info("Reparsing " + file.getPath() + " because of language substitution " +
71 prevLang.getID() + "->" + substitutedLang.getID());
72 myReparsingInProgress = true;
73 FileContentUtilCore.reparseFiles(file);
74 myReparsingInProgress = false;
76 }, ModalityState.defaultModalityState());
81 private static void invokeLaterIfNeeded(@NotNull Runnable runnable, @NotNull ModalityState modalityState) {
82 if (ApplicationManager.getApplication().isDispatchThread()) {
86 ApplicationManager.getApplication().invokeLater(runnable, modalityState);