mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
Resource declaration conflicts highlighting
This commit is contained in:
@@ -566,15 +566,18 @@ public class HighlightUtil {
|
||||
String name = variable.getName();
|
||||
if (variable instanceof PsiLocalVariable ||
|
||||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection ||
|
||||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement ||
|
||||
variable instanceof PsiResource && ((PsiResource)variable).getResourceElement() instanceof PsiLocalVariable) {
|
||||
PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
|
||||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement) {
|
||||
PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
|
||||
VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {
|
||||
protected boolean check(final PsiVariable var, final ResolveState state) {
|
||||
return (var instanceof PsiLocalVariable || var instanceof PsiParameter) && super.check(var, state);
|
||||
}
|
||||
};
|
||||
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
|
||||
if (scope instanceof PsiResourceList && proc.size() == 0) {
|
||||
scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
|
||||
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
|
||||
}
|
||||
if (proc.size() > 0) {
|
||||
isIncorrect = true;
|
||||
}
|
||||
|
||||
@@ -194,17 +194,16 @@ public class PsiImplUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean processDeclarationsInTryStatement(@NotNull final PsiTryStatement statement,
|
||||
public static boolean processDeclarationsInResourceList(@NotNull final PsiResourceList resourceList,
|
||||
@NotNull final PsiScopeProcessor processor,
|
||||
@NotNull final ResolveState state,
|
||||
final PsiElement lastParent) {
|
||||
final PsiResourceList resourceList = statement.getResourceList();
|
||||
if (resourceList != null && lastParent instanceof PsiCodeBlock && lastParent == statement.getTryBlock()) {
|
||||
final List<PsiResource> resources = resourceList.getResources();
|
||||
for (PsiResource resource : resources) {
|
||||
final PsiElement resourceElement = resource.getResourceElement();
|
||||
if (resourceElement instanceof PsiLocalVariable && !processor.execute(resourceElement, state)) return false;
|
||||
}
|
||||
final List<PsiResource> resources = resourceList.getResources();
|
||||
for (PsiResource resource : resources) {
|
||||
final PsiElement resourceElement = resource.getResourceElement();
|
||||
if (resourceElement instanceof PsiLocalVariable &&
|
||||
!resourceElement.equals(lastParent) &&
|
||||
!processor.execute(resourceElement, state)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -15,12 +15,11 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.source.tree.java;
|
||||
|
||||
import com.intellij.psi.JavaElementVisitor;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.psi.PsiResource;
|
||||
import com.intellij.psi.PsiResourceList;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiImplUtil;
|
||||
import com.intellij.psi.impl.source.tree.CompositePsiElement;
|
||||
import com.intellij.psi.impl.source.tree.JavaElementType;
|
||||
import com.intellij.psi.scope.PsiScopeProcessor;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -47,6 +46,14 @@ public class PsiResourceListImpl extends CompositePsiElement implements PsiResou
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean processDeclarations(@NotNull final PsiScopeProcessor processor,
|
||||
@NotNull final ResolveState state,
|
||||
final PsiElement lastParent,
|
||||
@NotNull final PsiElement place) {
|
||||
return PsiImplUtil.processDeclarationsInResourceList(this, processor, state, lastParent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PsiResourceList:" + getText();
|
||||
|
||||
@@ -164,7 +164,12 @@ public class PsiTryStatementImpl extends CompositePsiElement implements PsiTrySt
|
||||
@NotNull final ResolveState state,
|
||||
final PsiElement lastParent,
|
||||
@NotNull final PsiElement place) {
|
||||
return PsiImplUtil.processDeclarationsInTryStatement(this, processor, state, lastParent);
|
||||
final PsiResourceList resourceList = getResourceList();
|
||||
if (resourceList != null && lastParent instanceof PsiCodeBlock && lastParent == getTryBlock()) {
|
||||
return PsiImplUtil.processDeclarationsInResourceList(resourceList, processor, state, lastParent);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
+11
-3
@@ -34,7 +34,7 @@ class C {
|
||||
try (<error descr="Incompatible types. Found: 'java.lang.String', required: 'java.lang.AutoCloseable'">"resource"</error>) { }
|
||||
}
|
||||
|
||||
void m3() throws Exception {
|
||||
void m3(int p) throws Exception {
|
||||
try (MyResource r = new MyResource()) {
|
||||
r.doSomething();
|
||||
/* todo: < error descr="Cannot assign a value to final variable 'r'">r = null</error >;*/
|
||||
@@ -47,10 +47,18 @@ class C {
|
||||
}
|
||||
<error descr="Cannot resolve symbol 'r'">r</error> = null;
|
||||
|
||||
try (MyResource <error descr="Variable 'r' is already defined in the scope">r</error> = new MyResource(); MyResource <error descr="Variable 'r' is already defined in the scope">r</error> = new MyResource()) { }
|
||||
|
||||
MyResource r = null;
|
||||
|
||||
try (MyResource <error descr="Variable 'r' is already defined in the scope">r</error> = new MyResource()) { }
|
||||
|
||||
try (r = new MyResource()) { }
|
||||
|
||||
try (MyResource <error descr="Variable 'p' is already defined in the scope">p</error> = new MyResource()) { }
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
try (MyResource p = new MyResource()) { }
|
||||
catch (E e) { }
|
||||
}
|
||||
}.run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user