don't reparse file if a language is substituted with a dialect of the language (in... phpstorm/163.28
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Wed, 8 Jun 2016 12:12:54 +0000 (15:12 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Wed, 8 Jun 2016 12:12:54 +0000 (15:12 +0300)
platform/core-api/src/com/intellij/psi/LanguageSubstitutors.java

index bdca79ca28b05c57e8ad1540d59488bf0c015751..1a1a1c96f3b10c40ef5476ed28829316cd266c14 100644 (file)
@@ -59,11 +59,11 @@ public final class LanguageSubstitutors extends LanguageExtension<LanguageSubsti
                                            @NotNull final Language substitutedLang) {
     Language prevSubstitutedLang = SUBSTITUTED_LANG_KEY.get(file);
     final Language prevLang = ObjectUtils.notNull(prevSubstitutedLang, originalLang);
-    if (!substitutedLang.equals(prevLang)) {
-      if (ApplicationManager.getApplication().isDispatchThread() && myReparsingInProgress) {
-        return; // avoid recursive reparsing
-      }
+    if (!haveCommonAncestorLanguage(substitutedLang, prevLang)) {
       if (file.replace(SUBSTITUTED_LANG_KEY, prevSubstitutedLang, substitutedLang)) {
+        if (ApplicationManager.getApplication().isDispatchThread() && myReparsingInProgress) {
+          return; // avoid recursive reparsing
+        }
         invokeLaterIfNeeded(new Runnable() {
           @Override
           public void run() {
@@ -78,6 +78,22 @@ public final class LanguageSubstitutors extends LanguageExtension<LanguageSubsti
     }
   }
 
+  private static boolean haveCommonAncestorLanguage(@NotNull Language lang1, @NotNull Language lang2) {
+    Language rootLang1 = findRootLanguage(lang1);
+    Language rootLang2 = findRootLanguage(lang2);
+    return rootLang1.is(rootLang2);
+  }
+
+  @NotNull
+  private static Language findRootLanguage(@NotNull Language lang) {
+    Language parent = lang.getBaseLanguage();
+    while (parent != null) {
+      lang = parent;
+      parent = lang.getBaseLanguage();
+    }
+    return lang;
+  }
+
   private static void invokeLaterIfNeeded(@NotNull Runnable runnable, @NotNull ModalityState modalityState) {
     if (ApplicationManager.getApplication().isDispatchThread()) {
       runnable.run();