Skip to content

Commit

Permalink
Fix for #1391: add alias to import labels
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 20, 2022
1 parent a354532 commit bc02d01
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* 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
* https://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,
Expand Down Expand Up @@ -35,54 +35,60 @@ class GroovyCompilationUnitStructureRequestor extends CompilationUnitStructureRe
protected GrapesContainer grapesContainer;
protected GrapesContainerInfo grapesContainerInfo;

@SuppressWarnings("rawtypes")
protected GroovyCompilationUnitStructureRequestor(ICompilationUnit unit, CompilationUnitElementInfo unitInfo, Map newElements) {
super(unit, unitInfo, newElements);
protected GroovyCompilationUnitStructureRequestor(ICompilationUnit unit, CompilationUnitElementInfo unitInfo, @SuppressWarnings("rawtypes") Map elements) {
super(unit, unitInfo, elements);
}

@Override @SuppressWarnings({"rawtypes", "unchecked"})
@Override
protected IAnnotation acceptAnnotation(Annotation annotation, AnnotatableInfo parentInfo, JavaElement parentHandle) {
IAnnotation result = super.acceptAnnotation(annotation, parentInfo, parentHandle);

// check for groovy grapes
if (result.getElementName().endsWith("Grab")) {
String group = null, module = null, version = null;
for (MemberValuePair mvp : annotation.memberValuePairs()) {
String key = String.valueOf(mvp.name);
if (key.equals("group")) {
switch (String.valueOf(mvp.name)) {
case "group":
group = mvp.value.toString();
group = group.substring(1, group.length() - 1);
} else if (key.equals("module")) {
break;
case "module":
module = mvp.value.toString();
module = module.substring(1, module.length() - 1);
} else if (key.equals("version")) {
break;
case "version":
version = mvp.value.toString();
version = version.substring(1, version.length() - 1);
break;
}
}
if (group != null && module != null && version != null) {
if (grapesContainer == null) {
grapesContainer = new GrapesContainer(unit);
grapesContainerInfo = new GrapesContainerInfo();
children.put(grapesContainerInfo, new ArrayList());
((List) children.get(unitInfo)).add(grapesContainer);
addToChildren(grapesContainerInfo, grapesContainer);
newElements.put(grapesContainer, grapesContainerInfo);
}
((List) children.get(grapesContainerInfo)).add(
new GrabDeclaration(grapesContainer, annotation.sourceStart, annotation.sourceEnd, group, module, version));
addToChildren(grapesContainerInfo, new GrabDeclaration(grapesContainer, annotation.sourceStart, annotation.sourceEnd, group, module, version));
}
}

return result;
}

private void addToChildren(Object parentInfo, JavaElement handle) {
@SuppressWarnings("unchecked")
List<JavaElement> list = (List<JavaElement>) children.computeIfAbsent(parentInfo, x -> new ArrayList<>());
list.add(handle);
}

@Override
public void exitCompilationUnit(int unitDeclarationEnd) {
public void exitCompilationUnit(int unitDeclarationEnd) {
super.exitCompilationUnit(unitDeclarationEnd);
if (grapesContainerInfo != null) {
@SuppressWarnings("unchecked")
List<IJavaElement> grapes = (List<IJavaElement>) children.get(grapesContainerInfo);
grapesContainerInfo.children = grapes.toArray(new IJavaElement[grapes.size()]);
List<JavaElement> elements = (List<JavaElement>) children.get(grapesContainerInfo);
grapesContainerInfo.children = elements.toArray(new IJavaElement[elements.size()]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,12 @@ public void visitImports(final ModuleNode node) {
} else {
importName = imp.getClassName().replace('$', '.') + "." + imp.getFieldName();
}
if (imp.getAliasExpr() != null) importName += " as " + imp.getAlias(); // renamed
}

enclosingElement = unit.getImport(importName);
if (!enclosingElement.exists()) {
// GRECLIPSE-1363, GRECLIPSE-1371, et al. -- handle imports like "import static Boolean.TRUE"
if (imp.isStatic()) {
if (imp.isStar()) {
importName = imp.getType().getNameWithoutPackage().replace('$', '.') + ".*";
} else {
importName = imp.getType().getNameWithoutPackage().replace('$', '.') + "." + imp.getFieldName();
}
enclosingElement = unit.getImport(importName);
}
if (!enclosingElement.exists()) {
enclosingElement = oldEnclosingElement;
}
enclosingElement = unit.getImportContainer();
}

VariableScope scope = scopes.getLast();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* 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
* https://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,
Expand Down Expand Up @@ -71,10 +71,8 @@ public TypeReferenceSearchRequestor(TypeReferencePattern pattern, SearchRequesto

protected final char[] extractArray(TypeReferencePattern pattern, String fieldName) {
char[] arr = ReflectionUtils.getPrivateField(TypeReferencePattern.class, fieldName, pattern);
if (!isCaseSensitive) {
arr = CharOperation.toLowerCase(arr);
}
return arr;
if (!isCaseSensitive) arr = CharOperation.toLowerCase(arr);
return arr != null && arr.length > 0 ? arr : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.codehaus.groovy.eclipse.editor.highlighting;

import static org.codehaus.groovy.runtime.DefaultGroovyMethods.last;
import static org.eclipse.jdt.core.IJavaElement.IMPORT_DECLARATION;

import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -50,13 +51,11 @@
import org.codehaus.groovy.eclipse.editor.highlighting.HighlightedTypedPosition.HighlightKind;
import org.codehaus.groovy.transform.trait.Traits;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.core.util.GroovyUtils;
import org.eclipse.jdt.groovy.search.TypeLookupResult;
import org.eclipse.jdt.groovy.search.VariableScope;
import org.eclipse.jdt.internal.core.ImportDeclaration;
import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jdt.internal.core.util.Util;
import org.eclipse.jface.text.Position;
Expand Down Expand Up @@ -140,7 +139,7 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
Iterable<ASTNode> words = node.getNodeMetaData("special.keyword");
words.forEach(word -> typedPositions.add(new HighlightedTypedPosition(word.getStart(), word.getLength(), HighlightKind.KEYWORD)));
}
if (!(enclosingElement instanceof IImportDeclaration || ClassHelper.isPrimitiveType((ClassNode) node) || ((ClassNode) node).isScriptBody())) {
if (!(enclosingElement.getElementType() == IMPORT_DECLARATION || ClassHelper.isPrimitiveType((ClassNode) node) || ((ClassNode) node).isScriptBody())) {
pos = handleClassReference((ClassNode) node, result.type);
}

Expand Down Expand Up @@ -192,8 +191,7 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
if (((MethodNode) result.declaration).isSynthetic() && !((MethodNode) result.declaration).getName().equals(node.getText())) {
pos = handleFieldOrProperty((Expression) node, result.declaration);
} else {
boolean isStaticImport = enclosingElement instanceof ImportDeclaration;
pos = handleMethodReference((Expression) node, result, isStaticImport);
pos = handleMethodReference((Expression) node, result, enclosingElement.getElementType() == IMPORT_DECLARATION);
}
} else if (result.declaration instanceof VariableExpression) {
pos = new HighlightedTypedPosition(node.getStart(), node.getLength(), HighlightKind.VARIABLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
*
Expand Down Expand Up @@ -580,12 +581,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2020 IBM Corporation and others.
*
Expand Down Expand Up @@ -583,12 +584,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2008, 2020 IBM Corporation and others.
*
Expand Down Expand Up @@ -585,12 +586,18 @@ protected void notifySourceElementRequestor(
this.requestor.acceptPackage(importReference);
} else {
final boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
// GROOVY add
char[][] tokens = importReference.tokens; final int length = tokens.length;
if (!onDemand && !CharOperation.equals(tokens[length - 1], importReference.getSimpleName())) { tokens = tokens.clone();
tokens[length - 1] = CharOperation.concat(tokens[length - 1], ' ', new char[] {'a','s'}, ' ', importReference.getSimpleName());
}
// GROOVY end
this.requestor.acceptImport(
importReference.declarationSourceStart,
importReference.declarationSourceEnd,
importReference.sourceStart,
onDemand ? importReference.trailingStarPosition : importReference.sourceEnd,
importReference.tokens,
tokens,
onDemand,
importReference.modifiers);
}
Expand Down
Loading

0 comments on commit bc02d01

Please sign in to comment.