Skip to content

Commit

Permalink
identifier resolver fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dancioca committed Nov 6, 2023
1 parent d944378 commit 2517ecf
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import java.util.Collection;

import static com.dci.intellij.dbn.common.dispose.Failsafe.guarded;

@Getter
public class CodeCompletionLookupConsumer implements CancellableConsumer<Object> {
private final CodeCompletionContext context;
Expand All @@ -29,67 +27,65 @@ public class CodeCompletionLookupConsumer implements CancellableConsumer<Object>

@Override
public void accept(Object object) {
guarded(() -> {
if (object instanceof Object[]) {
consumeArray((Object[]) object);

} else if (object instanceof Collection) {
consumeCollection((Collection) object);
if (object instanceof Object[]) {
consumeArray((Object[]) object);

} else {
checkCancelled();
LookupItemBuilder lookupItemBuilder = null;
DBLanguage language = context.getLanguage();
if (object instanceof DBObject) {
DBObject dbObject = (DBObject) object;
lookupItemBuilder = dbObject.getLookupItemBuilder(language);
} else if (object instanceof DBObjectPsiElement) {
DBObjectPsiElement objectPsiElement = (DBObjectPsiElement) object;
lookupItemBuilder = objectPsiElement.ensureObject().getLookupItemBuilder(language);
} else if (object instanceof Collection) {
consumeCollection((Collection) object);

} else if (object instanceof TokenElementType) {
TokenElementType tokenElementType = (TokenElementType) object;
String text = tokenElementType.getText();
if (Strings.isNotEmpty(text)) {
lookupItemBuilder = tokenElementType.getLookupItemBuilder(language);
} else {
CodeCompletionFilterSettings filterSettings = context.getCodeCompletionFilterSettings();
TokenTypeCategory tokenTypeCategory = tokenElementType.getTokenTypeCategory();
if (tokenTypeCategory == TokenTypeCategory.OBJECT) {
TokenType tokenType = tokenElementType.getTokenType();
DBObjectType objectType = tokenType.getObjectType();
if (objectType != null) {
if (filterSettings.acceptsRootObject(objectType)) {
lookupItemBuilder = new BasicLookupItemBuilder(tokenType.getValue(), objectType.getName(), objectType.getIcon());
}
} else {
checkCancelled();
LookupItemBuilder lookupItemBuilder = null;
DBLanguage language = context.getLanguage();
if (object instanceof DBObject) {
DBObject dbObject = (DBObject) object;
lookupItemBuilder = dbObject.getLookupItemBuilder(language);
} else if (object instanceof DBObjectPsiElement) {
DBObjectPsiElement objectPsiElement = (DBObjectPsiElement) object;
lookupItemBuilder = objectPsiElement.ensureObject().getLookupItemBuilder(language);

} else if (object instanceof TokenElementType) {
TokenElementType tokenElementType = (TokenElementType) object;
String text = tokenElementType.getText();
if (Strings.isNotEmpty(text)) {
lookupItemBuilder = tokenElementType.getLookupItemBuilder(language);
} else {
CodeCompletionFilterSettings filterSettings = context.getCodeCompletionFilterSettings();
TokenTypeCategory tokenTypeCategory = tokenElementType.getTokenTypeCategory();
if (tokenTypeCategory == TokenTypeCategory.OBJECT) {
TokenType tokenType = tokenElementType.getTokenType();
DBObjectType objectType = tokenType.getObjectType();
if (objectType != null) {
if (filterSettings.acceptsRootObject(objectType)) {
lookupItemBuilder = new BasicLookupItemBuilder(tokenType.getValue(), objectType.getName(), objectType.getIcon());
}
} else if (filterSettings.acceptReservedWord(tokenTypeCategory)) {
lookupItemBuilder = tokenElementType.getLookupItemBuilder(language);
}
} else if (filterSettings.acceptReservedWord(tokenTypeCategory)) {
lookupItemBuilder = tokenElementType.getLookupItemBuilder(language);
}
} else if (object instanceof IdentifierPsiElement) {
IdentifierPsiElement identifierPsiElement = (IdentifierPsiElement) object;
if (identifierPsiElement.isValid()) {
CharSequence chars = identifierPsiElement.getChars();
IdentifierType identifierType = identifierPsiElement.getIdentifierType();
if (identifierType == IdentifierType.VARIABLE) {
lookupItemBuilder = new VariableLookupItemBuilder(chars, true);
} else if (identifierType == IdentifierType.ALIAS) {
lookupItemBuilder = new AliasLookupItemBuilder(chars, true);
} else if (identifierType == IdentifierType.OBJECT && identifierPsiElement.isDefinition()) {
lookupItemBuilder = new IdentifierLookupItemBuilder(identifierPsiElement);
}
} else if (object instanceof IdentifierPsiElement) {
IdentifierPsiElement identifierPsiElement = (IdentifierPsiElement) object;
if (identifierPsiElement.isValid()) {
CharSequence chars = identifierPsiElement.getChars();
IdentifierType identifierType = identifierPsiElement.getIdentifierType();
if (identifierType == IdentifierType.VARIABLE) {
lookupItemBuilder = new VariableLookupItemBuilder(chars, true);
} else if (identifierType == IdentifierType.ALIAS) {
lookupItemBuilder = new AliasLookupItemBuilder(chars, true);
} else if (identifierType == IdentifierType.OBJECT && identifierPsiElement.isDefinition()) {
lookupItemBuilder = new IdentifierLookupItemBuilder(identifierPsiElement);

}
}
} else if (object instanceof String) {
lookupItemBuilder = new AliasLookupItemBuilder((CharSequence) object, true);
}
} else if (object instanceof String) {
lookupItemBuilder = new AliasLookupItemBuilder((CharSequence) object, true);
}

if (lookupItemBuilder != null) {
lookupItemBuilder.createLookupItem(object, this);
}
if (lookupItemBuilder != null) {
lookupItemBuilder.createLookupItem(object, this);
}
});
}
}

private void consumeArray(Object[] array) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@

import com.dci.intellij.dbn.common.util.Commons;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class SetCollector<T> implements Consumer<T> {
private Set<T> elements;

protected SetCollector() {}
SetCollector() {}


public static <T> SetCollector<T> create() {
public static <T> SetCollector<T> basic() {
return new SetCollector<>();
}

public static <T> SetCollector<T> concurrent() {
return new SetCollector<>() {
@Override
protected Set<T> createSet() {
return Collections.newSetFromMap(new ConcurrentHashMap<>());
}
};
}

public static <T> SetCollector<T> linked() {
return new SetCollector<>() {
@Override
protected Set<T> createSet() {
return new LinkedHashSet<>();
}
};
}

public static <T> SetCollector<T> sorted(Comparator<T> comparator) {
return new SetCollector<>() {
@Override
protected Set<T> createSet() {
return new TreeSet<>(comparator);
}
};
}

@Override
public void accept(T element) {
if (elements == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
public interface WeakRefCache<K, V> {
V get(K key);

V ensure(K key);

V get(K key, Function<K, V> loader);

V compute(K key, BiFunction<K, V, V> loader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import static com.dci.intellij.dbn.common.dispose.Failsafe.nd;

abstract class WeakRefCacheBase<K, V> implements WeakRefCache<K, V> {
private final Map<K, V> cache = createCache();

Expand All @@ -18,6 +20,11 @@ public V get(K key) {
return cache.get(key);
}

@Override
public V ensure(K key) {
return nd(get(key));
}

@Override
@SneakyThrows
public V get(K key, Function<K, V> loader) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dci/intellij/dbn/common/util/Lists.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static <T> List<T> filter(@NotNull List<T> list, @Nullable Filter<T> filt
}

@NotNull
public static <S, T> List<T> convert(@NotNull List<S> list, Function<S, T> mapper) {
public static <S, T> List<T> convert(@NotNull Collection<S> list, Function<S, T> mapper) {
List<T> result = new ArrayList<>();
for (S s : list) {
T value = mapper.apply(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ public String getText() {
return Read.call(this, e -> e.getSuperText());
}

public CharSequence getChars() {
return getNode().getChars();
}

private String getSuperText() {
return super.getText();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Set;
import java.util.function.Consumer;

import static com.dci.intellij.dbn.common.dispose.Failsafe.guarded;
import static com.dci.intellij.dbn.common.thread.ThreadMonitor.isDispatchThread;
import static com.dci.intellij.dbn.common.util.Commons.nvl;
import static com.dci.intellij.dbn.diagnostics.Diagnostics.conditionallyLog;
Expand Down Expand Up @@ -394,16 +393,14 @@ private void resolveWithScopeParentLookup(DBObjectType objectType, IdentifierEle
if (!elementType.isLocalReference() && activeConnection != null && !activeConnection.isVirtual()) {
String objectName = refText.toString();
Set<DBObject> parentObjects = identifyPotentialParentObjects(objectType, null, this, this);
if (parentObjects != null && parentObjects.size() > 0) {
for (DBObject parentObject : parentObjects) {
DBObject childObject = parentObject.getChildObject(objectType, objectName, false);

if (childObject == null && objectType.isOverloadable()) {
childObject = parentObject.getChildObject(objectType, objectName, (short) 1, false);
}
for (DBObject parentObject : parentObjects) {
DBObject childObject = parentObject.getChildObject(objectType, objectName, false);

if (updateReference(null, elementType, childObject)) return;
if (childObject == null && objectType.isOverloadable()) {
childObject = parentObject.getChildObject(objectType, objectName, (short) 1, false);
}

if (updateReference(null, elementType, childObject)) return;
}

DBObjectBundle objectBundle = activeConnection.getObjectBundle();
Expand Down Expand Up @@ -502,11 +499,6 @@ public PsiElement getNavigationElement() {
@Override
@Nullable
public PsiElement resolve() {
return guarded(null, this, e -> e.doResolve());
}

@Nullable
private PsiElement doResolve() {
if (isResolving()) return ref.getReference();

// alias definitions do not have references.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,24 @@ public Object[] getVariants() {
}

public static Set<DBObject> identifyPotentialParentObjects(DBObjectType objectType, @Nullable ObjectTypeFilter filter, @NotNull BasePsiElement sourceScope, LeafPsiElement lookupIssuer) {
SetCollector<DBObject> parentObjects = SetCollector.create();
SetCollector<DBObject> parentObjects = SetCollector.linked();
Set<DBObjectType> parentTypes = objectType.getGenericParents();
if (parentTypes.size() > 0) {
if (!parentTypes.isEmpty()) {
Consumer<BasePsiElement> parentObjectPsiElements = parentObjectPsiElement -> {
if (parentObjectPsiElement.containsPsiElement(sourceScope)) return;

DBObject parentObject = parentObjectPsiElement.getUnderlyingObject();
collectObject(parentObjects, parentObject);
};
for (DBObjectType parentObjectType : parentTypes) {
PsiLookupAdapter lookupAdapter = new ObjectLookupAdapter(lookupIssuer, parentObjectType, null);
lookupAdapter.setAssertResolved(true);

if (!objectType.isSchemaObject() && parentObjectType.isSchemaObject())
lookupAdapter.collectInScope(sourceScope, parentObjectPsiElements); else
lookupAdapter.collectInParentScopeOf(sourceScope, parentObjectPsiElements);
}

if (objectType.isSchemaObject()) {
ConnectionHandler connection = sourceScope.getConnection();

Expand All @@ -121,21 +136,6 @@ public static Set<DBObject> identifyPotentialParentObjects(DBObjectType objectTy
}
}
}

Consumer<BasePsiElement> parentObjectPsiElements = parentObjectPsiElement -> {
if (!parentObjectPsiElement.containsPsiElement(sourceScope)) {
DBObject parentObject = parentObjectPsiElement.getUnderlyingObject();
collectObject(parentObjects, parentObject);
}
};
for (DBObjectType parentObjectType : parentTypes) {
PsiLookupAdapter lookupAdapter = new ObjectLookupAdapter(lookupIssuer, parentObjectType, null);
lookupAdapter.setAssertResolved(true);

if (!objectType.isSchemaObject() && parentObjectType.isSchemaObject())
lookupAdapter.collectInScope(sourceScope, parentObjectPsiElements); else
lookupAdapter.collectInParentScopeOf(sourceScope, parentObjectPsiElements);
}
}

DBObject fileObject = sourceScope.getFile().getUnderlyingObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.dci.intellij.dbn.vfs.file.DBSourceCodeVirtualFile;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
Expand All @@ -21,7 +22,7 @@ public NamedPsiElement(ASTNode astNode, NamedElementType elementType) {

@Nullable
public String createSubjectList() {
SetCollector<IdentifierPsiElement> subjects = SetCollector.create();
SetCollector<IdentifierPsiElement> subjects = SetCollector.linked();
collectSubjectPsiElements(subjects);
return subjects.isNotEmpty() ? Naming.createNamesList(subjects.elements(), 3) : null;
}
Expand Down Expand Up @@ -100,8 +101,7 @@ public Icon getIcon(boolean open) {
@Nullable
@Override
public BasePsiElement findPsiElement(PsiLookupAdapter lookupAdapter, int scopeCrossCount) {
// TODO small performance impact (removing this freezes the UI though)
//ProgressIndicatorProvider.checkCanceled();
ProgressManager.checkCanceled();
return super.findPsiElement(lookupAdapter, scopeCrossCount);
}

Expand Down
Loading

0 comments on commit 2517ecf

Please sign in to comment.