mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
decrease the invocation counts of psi.getText in several java places
This commit is contained in:
@@ -32,8 +32,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiVariableEx;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.javadoc.PsiDocTag;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -53,7 +52,7 @@ public class JavaSuppressionUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getInspectionIdSuppressedInAnnotationAttribute(PsiElement element) {
|
||||
private static String getInspectionIdSuppressedInAnnotationAttribute(PsiElement element) {
|
||||
if (element instanceof PsiLiteralExpression) {
|
||||
final Object value = ((PsiLiteralExpression)element).getValue();
|
||||
if (value instanceof String) {
|
||||
@@ -73,7 +72,7 @@ public class JavaSuppressionUtil {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Collection<String> getInspectionIdsSuppressedInAnnotation(final PsiModifierList modifierList) {
|
||||
public static Collection<String> getInspectionIdsSuppressedInAnnotation(@Nullable PsiModifierList modifierList) {
|
||||
if (modifierList == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -86,6 +85,13 @@ public class JavaSuppressionUtil {
|
||||
if (annotation == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return CachedValuesManager.getCachedValue(annotation, () ->
|
||||
CachedValueProvider.Result.create(getInspectionIdsSuppressedInAnnotation(annotation),
|
||||
PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Collection<String> getInspectionIdsSuppressedInAnnotation(PsiAnnotation annotation) {
|
||||
final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
|
||||
if (attributes.length == 0) {
|
||||
return Collections.emptyList();
|
||||
|
||||
+3
-1
@@ -390,7 +390,9 @@ class ContractInferenceInterpreter {
|
||||
|
||||
static int resolveParameter(@Nullable PsiExpression expr, PsiMethod method) {
|
||||
if (expr instanceof PsiReferenceExpression && !((PsiReferenceExpression)expr).isQualified()) {
|
||||
String name = expr.getText();
|
||||
String name = ((PsiReferenceExpression)expr).getReferenceName();
|
||||
if (name == null) return -1;
|
||||
|
||||
PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
if (name.equals(parameters[i].getName())) {
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ public class VariableInIncompleteCodeSearcher extends QueryExecutorBase<PsiRefer
|
||||
|
||||
PsiSearchHelper.SERVICE.getInstance(p.getProject()).processElementsWithWord((element, offsetInElement) -> {
|
||||
for (PsiElement child = element.findElementAt(offsetInElement); child != null; child = child.getParent()) {
|
||||
if (!name.equals(child.getText())) {
|
||||
if (!child.textMatches(name)) {
|
||||
break;
|
||||
}
|
||||
if (child instanceof PsiJavaCodeReferenceElement) {
|
||||
|
||||
+11
-11
@@ -16,7 +16,6 @@
|
||||
package com.intellij.psi.impl.source.tree.java;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilCore;
|
||||
import com.intellij.extapi.psi.StubBasedPsiElementBase;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
@@ -120,6 +119,17 @@ public class PsiLiteralExpressionImpl
|
||||
@Override
|
||||
public Object getValue() {
|
||||
final IElementType type = getLiteralElementType();
|
||||
if (type == JavaTokenType.TRUE_KEYWORD) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (type == JavaTokenType.FALSE_KEYWORD) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if (type == JavaTokenType.STRING_LITERAL) {
|
||||
String innerText = getInnerText();
|
||||
return innerText == null ? null : internedParseStringCharacters(innerText);
|
||||
}
|
||||
|
||||
String text = NUMERIC_LITERALS.contains(type) ? getCanonicalText().toLowerCase(Locale.ENGLISH) : getCanonicalText();
|
||||
final int textLength = text.length();
|
||||
|
||||
@@ -201,16 +211,6 @@ public class PsiLiteralExpressionImpl
|
||||
if (chars.length() != 1) return null;
|
||||
return Character.valueOf(chars.charAt(0));
|
||||
}
|
||||
if (type == JavaTokenType.STRING_LITERAL) {
|
||||
String innerText = getInnerText();
|
||||
return innerText == null ? null : internedParseStringCharacters(innerText);
|
||||
}
|
||||
if (type == JavaTokenType.TRUE_KEYWORD) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (type == JavaTokenType.FALSE_KEYWORD) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
+8
-10
@@ -22,7 +22,6 @@ import com.intellij.codeInspection.lang.InspectionExtensionsFactory;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -31,8 +30,6 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author cdr
|
||||
*/
|
||||
@@ -60,13 +57,8 @@ public class EditInspectionToolsSettingsInSuppressedPlaceIntention implements In
|
||||
for (InspectionExtensionsFactory factory : Extensions.getExtensions(InspectionExtensionsFactory.EP_NAME)) {
|
||||
final String suppressedIds = factory.getSuppressedInspectionIdsIn(element);
|
||||
if (suppressedIds != null) {
|
||||
String text = element.getText();
|
||||
List<String> ids = StringUtil.split(suppressedIds, ",");
|
||||
for (String id : ids) {
|
||||
int i = text.indexOf(id);
|
||||
if (i == -1) continue;
|
||||
int idOffset = element.getTextRange().getStartOffset() + i;
|
||||
if (TextRange.from(idOffset, id.length()).contains(offset)) {
|
||||
for (String id : StringUtil.split(suppressedIds, ",")) {
|
||||
if (isCaretOnSuppressedId(file, offset, id)) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -77,6 +69,12 @@ public class EditInspectionToolsSettingsInSuppressedPlaceIntention implements In
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isCaretOnSuppressedId(PsiFile file, int caretOffset, String suppressedId) {
|
||||
CharSequence fileText = file.getViewProvider().getContents();
|
||||
CharSequence vicinity = fileText.subSequence(caretOffset - suppressedId.length(), caretOffset + suppressedId.length());
|
||||
return StringUtil.indexOf(vicinity, suppressedId) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
myId = getSuppressedId(editor, file);
|
||||
|
||||
Reference in New Issue
Block a user