From b0651337f98ada40bdbd7ae3df6b6f7aaec23532 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 3 Jan 2023 17:04:16 +0100 Subject: [PATCH] RegExp: fall back to Java regex evaluation if JS is not available (IDEA-261269) GitOrigin-RevId: 7997dfe4a0f74d4a304285a3c1e0b17585e5649d --- .../lang/regexp/RegExpMatcherProvider.java | 20 +++---------------- .../EcmaScriptRegExpMatcherProvider.java | 12 ++++++++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/RegExpMatcherProvider.java b/RegExpSupport/src/org/intellij/lang/regexp/RegExpMatcherProvider.java index 296db34678e6..c43adbfbcb8e 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/RegExpMatcherProvider.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/RegExpMatcherProvider.java @@ -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, diff --git a/RegExpSupport/src/org/intellij/lang/regexp/ecmascript/EcmaScriptRegExpMatcherProvider.java b/RegExpSupport/src/org/intellij/lang/regexp/ecmascript/EcmaScriptRegExpMatcherProvider.java index fb76b1a61d33..3c16ea4ae643 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/ecmascript/EcmaScriptRegExpMatcherProvider.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/ecmascript/EcmaScriptRegExpMatcherProvider.java @@ -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 =