[spelling] IJPL-159316 Spelling: make Splitter implementations cancellable

GitOrigin-RevId: c192e1f540e26061634e3bc219da127f7f685b4d
This commit is contained in:
Yuriy Artamonov
2024-07-29 19:16:42 +02:00
committed by intellij-monorepo-bot
parent 7e75c193eb
commit c52bceef8e
7 changed files with 30 additions and 64 deletions

View File

@@ -4,6 +4,7 @@ package com.intellij.spellchecker.inspections;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Consumer;
@@ -29,6 +30,8 @@ public abstract class BaseSplitter implements Splitter {
if (tooShort) {
return;
}
ProgressManager.checkCanceled();
consumer.consume(found);
}
@@ -109,19 +112,27 @@ public abstract class BaseSplitter implements Splitter {
}
}
private static final int PROCESSING_TIME_LIMIT = 500;
/**
* @throws TooLongBombedMatchingException in case processing is longer than {@link #PROCESSING_TIME_LIMIT}
*/
protected static CharSequence newBombedCharSequence(String text, TextRange range) {
return newBombedCharSequence(range.substring(text));
}
protected static CharSequence newBombedCharSequence(final String substring) {
final long myTime = System.currentTimeMillis() + 500;
/**
* @throws TooLongBombedMatchingException in case processing is longer than {@link #PROCESSING_TIME_LIMIT}
*/
protected static CharSequence newBombedCharSequence(String substring) {
return new StringUtil.BombedCharSequence(substring) {
final long myTime = System.currentTimeMillis() + PROCESSING_TIME_LIMIT;
@Override
protected void checkCanceled() {
//todo[anna] if (ApplicationManager.getApplication().isHeadlessEnvironment()) return;
long l = System.currentTimeMillis();
if (l >= myTime) {
throw new ProcessCanceledException();
throw new TooLongBombedMatchingException();
}
}
};
@@ -132,4 +143,7 @@ public abstract class BaseSplitter implements Splitter {
ProgressIndicatorProvider.checkCanceled();
}
}
}
public static class TooLongBombedMatchingException extends ProcessCanceledException {
}
}

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.spellchecker.inspections;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.TextRange;
import com.intellij.spellchecker.util.Strings;
import com.intellij.util.Consumer;
@@ -22,8 +21,6 @@ public class IdentifierSplitter extends BaseSplitter {
}
private static final @NonNls Pattern WORD = Pattern.compile("\\b\\p{L}*'?\\p{L}*");
private static final @NonNls Pattern WORD_IN_QUOTES = Pattern.compile("'([^']*)'");
@Override
@@ -37,7 +34,7 @@ public class IdentifierSplitter extends BaseSplitter {
for (TextRange textRange : extracted) {
List<TextRange> words = splitByCase(text, textRange);
if (words.size() == 0) {
if (words.isEmpty()) {
continue;
}
@@ -65,7 +62,7 @@ public class IdentifierSplitter extends BaseSplitter {
addWord(consumer, flag, found);
}
}
catch (ProcessCanceledException e) {
catch (TooLongBombedMatchingException e) {
return;
}
}
@@ -138,7 +135,7 @@ public class IdentifierSplitter extends BaseSplitter {
private static void add(String text, List<TextRange> result, int i, int s) {
if (i - s > 3) {
final TextRange textRange = new TextRange(s, i);
TextRange textRange = new TextRange(s, i);
//System.out.println("textRange = " + textRange + " = "+ textRange.substring(text));
result.add(textRange);
}

View File

@@ -131,7 +131,7 @@ public class PlainTextSplitter extends BaseSplitter {
if (matcher.hitEnd()) break;
}
}
catch (ProcessCanceledException ignored) {
catch (TooLongBombedMatchingException ignored) {
}
}

View File

@@ -1,21 +1,6 @@
/*
* Copyright 2000-2010 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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.spellchecker.inspections;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Consumer;
@@ -51,7 +36,7 @@ public final class PropertiesSplitter extends BaseSplitter {
splitter.split(text, found, consumer);
}
}
catch (ProcessCanceledException ignored) {
catch (TooLongBombedMatchingException ignored) {
}
}
}

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2010 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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.spellchecker.inspections;
import com.intellij.openapi.util.TextRange;
@@ -20,7 +6,6 @@ import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface Splitter {
void split(@Nullable String text, @NotNull TextRange range, Consumer<TextRange> consumer);
}

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.spellchecker.inspections;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Consumer;
@@ -51,7 +50,7 @@ public class TextSplitter extends BaseSplitter {
ws.split(text, found, consumer);
}
}
catch (ProcessCanceledException ignored) {
catch (TooLongBombedMatchingException ignored) {
}
}

View File

@@ -1,21 +1,6 @@
/*
* Copyright 2000-2013 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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.spellchecker.inspections;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.TextRange;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NonNls;
@@ -39,6 +24,7 @@ public final class WordSplitter extends BaseSplitter {
if (text == null || range.getLength() <= 1) {
return;
}
try {
Matcher specialMatcher = SPECIAL.matcher(newBombedCharSequence(text));
specialMatcher.region(range.getStartOffset(), range.getEndOffset());
@@ -50,7 +36,7 @@ public final class WordSplitter extends BaseSplitter {
IdentifierSplitter.getInstance().split(text, range, consumer);
}
}
catch (ProcessCanceledException ignored) {
catch (TooLongBombedMatchingException ignored) {
}
}
}