Commit 0a245dca authored by Alexey Kudravtsev's avatar Alexey Kudravtsev Committed by intellij-monorepo-bot
Browse files

use more race-free RangeMarker.getTextRange() to fix stuff similar to...

use more race-free RangeMarker.getTextRange() to fix stuff similar to "IDEA-312518 GeneralHighlightingPass: Invalid range specified", fix usages, add SSR

GitOrigin-RevId: 45fa7d2e2ae70c4427153b1833f8860dd770004b
parent a67d375c
Showing with 29 additions and 23 deletions
+29 -23
......@@ -1771,6 +1771,14 @@
<searchConfiguration name="SwingWorker generates too many threads; use Application.execute*() instead" text="javax.swing.SwingWorker" recursive="true" caseInsensitive="true" type="Kotlin" pattern_context="default">
<constraint name="__context__" within="" contains="" />
</searchConfiguration>
<replaceConfiguration name="TextRange can be simplified" text="com.intellij.openapi.util.TextRange.create($t$)" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" search_injected="false" reformatAccordingToStyle="true" shortenFQN="true" replacement="$t$">
<constraint name="__context__" script="&quot;__context__ instanceof com.intellij.psi.PsiExpression &#10; &amp;&amp; ((com.intellij.psi.PsiExpression)__context__).getType() != null &#10; &amp;&amp; ((com.intellij.psi.PsiExpression)__context__).getType().getCanonicalText().equals(&quot;com.intellij.openapi.util.TextRange&quot;)&quot;" within="" contains="" />
<constraint name="t" nameOfExprType="com\.intellij\.openapi\.util\.TextRange" exprTypeWithinHierarchy="true" within="" contains="" />
</replaceConfiguration>
<replaceConfiguration name="TextRange can be simplified" text="TextRange.create($t$)" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" search_injected="false" reformatAccordingToStyle="true" shortenFQN="true" replacement="$t$.getTextRange()">
<constraint name="__context__" within="" contains="" />
<constraint name="t" nameOfExprType="com\.intellij\.openapi\.editor" exprTypeWithinHierarchy="true" within="" contains="" />
</replaceConfiguration>
<searchConfiguration name="Timer creates/leaks threads" description="java.util.Timer eagerly creates a Thread on its start, which is too expensive. Please use ApplicationExecutionUtil.getScheduledExecutorService() instead &#10;&#10;" text="new java.util.Timer($p$)" recursive="true" caseInsensitive="true" type="JAVA" pattern_context="default" search_injected="false">
<constraint name="__context__" within="" contains="" />
<constraint name="p" minCount="0" maxCount="2147483647" within="" contains="" />
......
......@@ -592,7 +592,7 @@ public class JavaDocCompletionContributor extends CompletionContributor implemen
PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), paramListMarker.getStartOffset(), PsiDocComment.class, false);
if (docComment != null) {
TemplateImpl template = new TemplateImpl("", "");
ConstantNode node = new ConstantNode(document.getText(TextRange.create(paramListMarker)));
ConstantNode node = new ConstantNode(document.getText(paramListMarker.getTextRange()));
template.addVariable("PARAMETERS", node, node, true);
template.addTextSegment(document.getText(TextRange.create(paramListMarker.getEndOffset(), tail)));
template.addEndVariable();
......
......@@ -42,7 +42,7 @@ public class MethodOrClassSelectioner extends BasicSelectioner {
result.add(range);
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
range = TextRange.create(firstChild.getTextRange());
range = firstChild.getTextRange();
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
firstChild = children[i++];
......
......@@ -23,7 +23,7 @@ internal class RangeExpression(
it.isGreedyToRight = true
}
val text: String get() = document.getText(TextRange.create(marker))
val text: String get() = document.getText(marker.textRange)
override fun calculateResult(context: ExpressionContext): Result = TextResult(text)
......
......@@ -839,7 +839,7 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
modelEx.addMarkupModelListener(getTestRootDisposable(), new MarkupModelListener() {
@Override
public void beforeRemoved(@NotNull RangeHighlighterEx highlighter) {
if (TextRange.create(highlighter).substring(highlighter.getDocument().getText()).equals("TTTTTTTTTTTTTTT")) {
if (highlighter.getTextRange().substring(highlighter.getDocument().getText()).equals("TTTTTTTTTTTTTTT")) {
throw new RuntimeException("Must not remove type parameter highlighter");
}
}
......
......@@ -130,7 +130,7 @@ public class OpenFileDescriptor implements FileEditorNavigatable, Comparable<Ope
TextRange range = getRangeToUnfoldOnNavigation(editor);
editor.getFoldingModel().runBatchFoldingOperation(() -> {
for (FoldRegion region : allRegions) {
if (!region.isExpanded() && range.intersects(TextRange.create(region))) {
if (!region.isExpanded() && range.intersects(region)) {
region.setExpanded(true);
}
}
......
......@@ -122,7 +122,7 @@ public final class FileStatusMap implements Disposable {
return document.createRangeMarker(scope);
}
if (old == WHOLE_FILE_DIRTY_MARKER) return old;
TextRange oldRange = TextRange.create(old);
TextRange oldRange = old.getTextRange();
TextRange union = scope.union(oldRange);
if (old.isValid() && union.equals(oldRange)) {
return old;
......@@ -243,7 +243,7 @@ public final class FileStatusMap implements Disposable {
if (marker == WHOLE_FILE_DIRTY_MARKER) {
return file == null ? null : file.getTextRange();
}
return marker.isValid() ? TextRange.create(marker) : new TextRange(0, document.getTextLength());
return marker.isValid() ? marker.getTextRange() : new TextRange(0, document.getTextLength());
}
private static void assertRegisteredPass(int passId, @NotNull FileStatus status) {
......
......@@ -894,7 +894,7 @@ public class HighlightInfo implements Segment {
throw new RuntimeException("info not applied yet");
}
if (!highlighter.isValid()) return "";
return highlighter.getDocument().getText(TextRange.create(highlighter));
return highlighter.getDocument().getText(highlighter.getTextRange());
}
/**
......
......@@ -250,7 +250,7 @@ public class CodeFormatterFacade {
for (FormatTextRange range : textRanges) {
RangeMarker marker = markers[i];
if (marker != null) {
range.setTextRange(TextRange.create(marker));
range.setTextRange(marker.getTextRange());
marker.dispose();
}
i++;
......
......@@ -326,7 +326,7 @@ public final class DocRenderItem {
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject()));
PsiFile file = psiDocumentManager.getPsiFile(editor.getDocument());
if (file != null) {
return findInlineDocumentation(file, TextRange.create(highlighter));
return findInlineDocumentation(file, highlighter.getTextRange());
}
}
return null;
......
......@@ -89,7 +89,7 @@ public class CopyPasteIndentProcessor extends CopyPastePostProcessor<IndentTrans
final boolean useTabs =
CodeStyle.getSettings(psiFile).useTabCharacter(psiFile.getFileType());
CharFilter NOT_INDENT_FILTER = ch -> useTabs ? ch != '\t' : ch != ' ';
String pastedText = document.getText(TextRange.create(bounds));
String pastedText = document.getText(bounds.getTextRange());
int startLine = document.getLineNumber(bounds.getStartOffset());
int endLine = document.getLineNumber(bounds.getEndOffset());
......
......@@ -279,7 +279,7 @@ public class PasteHandler extends EditorActionHandler implements EditorTextInser
caretModel.moveToOffset(bounds.getEndOffset());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
editor.putUserData(EditorEx.LAST_PASTED_REGION, TextRange.create(bounds));
editor.putUserData(EditorEx.LAST_PASTED_REGION, bounds.getTextRange());
}
// Don't dispose the 'bounds' RangeMarker because the processors
// from 'extraData' may use it later, for instance, in an invokeLater() block.
......
......@@ -67,7 +67,7 @@ final class DocumentFoldingInfo implements CodeFoldingState {
if (Strings.areSameInstance(signature, UpdateFoldRegionsOperation.NO_SIGNATURE)) continue;
Boolean storedCollapseByDefault = region.getUserData(UpdateFoldRegionsOperation.COLLAPSED_BY_DEFAULT);
boolean collapseByDefault = storedCollapseByDefault != null && storedCollapseByDefault &&
!FoldingUtil.caretInsideRange(editor, TextRange.create(region));
!FoldingUtil.caretInsideRange(editor, region.getTextRange());
if (collapseByDefault == expanded || signature == null) {
if (signature != null) {
myInfos.add(new Info(signature, expanded));
......
......@@ -295,7 +295,7 @@ public final class FoldingUpdate {
private static boolean addNonConflictingRegion(DocumentEx document, TextRange range, List<? super RangeMarker> hardRefToRangeMarkers) {
int start = range.getStartOffset();
int end = range.getEndOffset();
if (!document.processRangeMarkersOverlappingWith(start, end, rm -> !areConflicting(range, TextRange.create(rm)))) {
if (!document.processRangeMarkersOverlappingWith(start, end, rm -> !areConflicting(range, rm.getTextRange()))) {
return false;
}
RangeMarker marker = document.createRangeMarker(start, end);
......
......@@ -234,7 +234,7 @@ final class UpdateFoldRegionsOperation implements Runnable {
}
if (shouldRemove) {
for (FoldRegion r : regionsToProcess) {
rangeToExpandStatusMap.putIfAbsent(TextRange.create(r), r.isExpanded());
rangeToExpandStatusMap.putIfAbsent(r.getTextRange(), r.isExpanded());
}
toRemove.addAll(regionsToProcess);
}
......
......@@ -325,7 +325,7 @@ public class HighlightUsagesHandler extends HighlightHandlerBase {
RangeHighlighter[] highlighters = ((HighlightManagerImpl)HighlightManager.getInstance(editor.getProject())).getHighlighters(editor);
int caretOffset = editor.getCaretModel().getOffset();
for (RangeHighlighter highlighter : highlighters) {
if (TextRange.create(highlighter).grown(1).contains(caretOffset)) {
if (highlighter.getTextRange().grown(1).contains(caretOffset)) {
return true;
}
}
......@@ -347,7 +347,7 @@ public class HighlightUsagesHandler extends HighlightHandlerBase {
int j = 0;
while (i < highlighters.length && j < rangesToHighlight.size()) {
RangeHighlighter highlighter = highlighters[i];
TextRange highlighterRange = TextRange.create(highlighter);
TextRange highlighterRange = highlighter.getTextRange();
TextRange refRange = rangesToHighlight.get(j);
if (refRange.equals(highlighterRange) &&
highlighter.getLayer() == HighlighterLayer.SELECTION - 1 &&
......
......@@ -17,7 +17,6 @@ import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
......@@ -178,7 +177,7 @@ final class ConsoleTokenUtil {
break;
}
marker.putUserData(USER_INPUT_SENT, true);
textToSend.insert(0, marker.getDocument().getText(TextRange.create(marker)));
textToSend.insert(0, marker.getDocument().getText(marker.getTextRange()));
}
}
return textToSend;
......
......@@ -6,7 +6,6 @@ import com.intellij.find.FindModel;
import com.intellij.find.FindResult;
import com.intellij.find.FindUtil;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
......@@ -47,7 +46,7 @@ public class SelectionManager {
myRegionsToRestore.clear();
for (FoldRegion region : allRegions) {
if (!region.isValid()) continue;
if (cursor.intersects(TextRange.create(region))) {
if (cursor.intersects(region)) {
if (!region.isExpanded()) {
region.setExpanded(true);
myRegionsToRestore.add(region);
......
......@@ -43,7 +43,7 @@ abstract class SplitJoinIntention : PsiElementBaseIntentionAction(), LowPriority
}
documentManager.commitDocument(document)
if (marker.isValid) {
splitJoinContext.reformatRange(containingFile, TextRange.create(marker), operation())
splitJoinContext.reformatRange(containingFile, marker.textRange, operation())
}
}
......
......@@ -508,7 +508,7 @@ public class SoftWrapModelImpl extends InlayModel.SimpleAdapter
// We delay processing of changed fold regions till the invocation of onFoldProcessingEnd(), as
// FoldingModel can return inconsistent data before that moment.
myDeferredFoldRegions.add(TextRange.create(region)); // copy because region can become invalid later
myDeferredFoldRegions.add(region.getTextRange()); // copy because region can become invalid later
}
@Override
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment