RegExp: fall back to Java regex evaluation if JS is not available (IDEA-261269)

GitOrigin-RevId: 7997dfe4a0f74d4a304285a3c1e0b17585e5649d
This commit is contained in:
Bas Leijdekkers
2023-01-03 17:04:16 +01:00
committed by intellij-monorepo-bot
parent 3e69177c43
commit b0651337f9
2 changed files with 12 additions and 20 deletions

View File

@@ -1,25 +1,11 @@
/*
* 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.
*/
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.lang.regexp;
import com.intellij.lang.LanguageExtension;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Use an RegExpMatcherProvider implementation to replace the regexp matcher used for the Check RegExp intention.
@@ -41,7 +27,7 @@ public interface RegExpMatcherProvider {
* (see e.g. {@link StringUtil#newBombedCharSequence(java.lang.CharSequence, long)}
* @return the result of the match
*/
@NotNull
@Nullable
RegExpMatchResult matches(String regExp,
PsiFile regExpFile,
PsiElement elementInHost,

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.lang.regexp.ecmascript;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
@@ -11,7 +12,7 @@ import org.intellij.lang.regexp.RegExpMatch;
import org.intellij.lang.regexp.RegExpMatchResult;
import org.intellij.lang.regexp.RegExpMatcherProvider;
import org.intellij.lang.regexp.intention.CheckRegExpForm;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
@@ -22,8 +23,9 @@ import java.util.List;
* @author Bas Leijdekkers
*/
public class EcmaScriptRegExpMatcherProvider implements RegExpMatcherProvider {
private static final Logger LOG = Logger.getInstance(EcmaScriptRegExpMatcherProvider.class);
@NotNull
@Nullable
@Override
public RegExpMatchResult matches(String regExp, PsiFile regExpFile, PsiElement elementInHost, String sampleText, long timeoutMillis) {
String modifiers = "";
@@ -43,6 +45,10 @@ public class EcmaScriptRegExpMatcherProvider implements RegExpMatcherProvider {
}
}
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
if (engine == null) {
LOG.warn("Nashorn JS scripting engine not found, falling back to Java regex evaluation");
return null;
}
try {
@Language("Nashorn JS")
final String script =