/*
 * Copyright 2000-2014 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.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.psi.PsiElement;
import org.intellij.lang.regexp.DefaultRegExpPropertiesProvider;
import org.intellij.lang.regexp.RegExpLanguageHost;
import org.intellij.lang.regexp.psi.RegExpChar;
import org.intellij.lang.regexp.psi.RegExpGroup;
import org.intellij.lang.regexp.psi.RegExpNamedGroupRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author yole
 */
public class JavaRegExpHost implements RegExpLanguageHost {

  private final DefaultRegExpPropertiesProvider myPropertiesProvider;

  public JavaRegExpHost() {
    myPropertiesProvider = DefaultRegExpPropertiesProvider.getInstance();
  }

  @Override
  public boolean characterNeedsEscaping(char c) {
    return c == ']' || c == '}';
  }

  @Override
  public boolean supportsPerl5EmbeddedComments() {
    return false;
  }

  @Override
  public boolean supportsPossessiveQuantifiers() {
    return true;
  }

  @Override
  public boolean supportsPythonConditionalRefs() {
    return false;
  }

  @Override
  public boolean supportsNamedGroupSyntax(RegExpGroup group) {
    if (group.isNamedGroup()) {
      final JavaSdkVersion version = getJavaVersion(group);
      return version != null && version.isAtLeast(JavaSdkVersion.JDK_1_7);
    }
    return false;
  }

  @Override
  public boolean supportsNamedGroupRefSyntax(RegExpNamedGroupRef ref) {
    if (ref.isNamedGroupRef()) {
      final JavaSdkVersion version = getJavaVersion(ref);
      return version != null && version.isAtLeast(JavaSdkVersion.JDK_1_7);
    }
    return false;
  }

  @Override
  public boolean supportsExtendedHexCharacter(RegExpChar regExpChar) {
    final JavaSdkVersion version = getJavaVersion(regExpChar);
    return version != null && version.isAtLeast(JavaSdkVersion.JDK_1_7);
  }

  @Nullable
  private static JavaSdkVersion getJavaVersion(PsiElement element) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      return JavaSdkVersion.JDK_1_9;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module != null) {
      final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
      if (sdk != null && sdk.getSdkType() instanceof JavaSdk) {
        return JavaSdk.getInstance().getVersion(sdk);
      }
    }
    return null;
  }

  @Override
  public boolean isValidCategory(@NotNull String category) {
    return myPropertiesProvider.isValidCategory(category);
  }

  @NotNull
  @Override
  public String[][] getAllKnownProperties() {
    return myPropertiesProvider.getAllKnownProperties();
  }

  @Nullable
  @Override
  public String getPropertyDescription(@Nullable String name) {
    return myPropertiesProvider.getPropertyDescription(name);
  }

  @NotNull
  @Override
  public String[][] getKnownCharacterClasses() {
    return myPropertiesProvider.getKnownCharacterClasses();
  }
}

@@ -21,9 +21,9 @@
 -201
-4
+6
  Jet
@@ -978,32 +978,86 @@
 uleRootManager;%0A
+import com.intellij.openapi.roots.ProjectRootManager;%0A
 import com.intel
@@ -3314,24 +3314,234 @@
     %7D%0A    %7D%0A
+    final Sdk sdk = ProjectRootManager.getInstance(element.getProject()).getProjectSdk();%0A    if (sdk != null && sdk.getSdkType() instanceof JavaSdk) %7B%0A      return JavaSdk.getInstance().getVersion(sdk);%0A    %7D%0A
     return n

check project for sdk when not found in module (IDEA-153417)