Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate imports from content assist or quickfixes #1014

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.fordiac.ide.model.commands.delete.DeleteAttributeCommand;
import org.eclipse.fordiac.ide.model.data.InternalDataType;
import org.eclipse.fordiac.ide.model.datatype.helper.InternalAttributeDeclarations;
import org.eclipse.fordiac.ide.model.helpers.ImportHelper;
import org.eclipse.fordiac.ide.model.libraryElement.Attribute;
import org.eclipse.fordiac.ide.model.libraryElement.AttributeDeclaration;
import org.eclipse.fordiac.ide.model.libraryElement.ConfigurableObject;
Expand Down Expand Up @@ -244,7 +245,8 @@ protected void configureContentProposalAdapter(final ContentProposalAdapter cont

protected void proposalAccepted(final IContentProposal proposal) {
if (proposal instanceof final ImportContentProposal importProposal
&& EcoreUtil.getRootContainer(getType()) instanceof final LibraryElement libraryElement) {
&& EcoreUtil.getRootContainer(getType()) instanceof final LibraryElement libraryElement
&& !ImportHelper.matchesImports(importProposal.getImportedNamespace(), libraryElement)) {
executeCommand(new AddNewImportCommand(libraryElement, importProposal.getImportedNamespace()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.fordiac.ide.model.data.DirectlyDerivedType;
import org.eclipse.fordiac.ide.model.data.StructuredType;
import org.eclipse.fordiac.ide.model.datatype.helper.IecTypes.GenericTypes;
import org.eclipse.fordiac.ide.model.datatype.helper.InternalAttributeDeclarations;
import org.eclipse.fordiac.ide.model.datatype.helper.TypeDeclarationParser;
import org.eclipse.fordiac.ide.model.eval.Evaluator;
import org.eclipse.fordiac.ide.model.eval.EvaluatorCache;
Expand Down Expand Up @@ -357,8 +358,8 @@ public static Value evaluateValue(final DataType dataType, final String initialV
}

public static Set<String> getDependencies(final VarDeclaration varDeclaration) {
if (varDeclaration.isArray()
&& !TypeDeclarationParser.isSimpleTypeDeclaration(varDeclaration.getArraySize().getValue())) {
if (!isSimpleInitialValue(varDeclaration) || (varDeclaration.isArray()
&& !TypeDeclarationParser.isSimpleTypeDeclaration(varDeclaration.getArraySize().getValue()))) {
final Evaluator evaluator = EvaluatorFactory.createEvaluator(varDeclaration, VarDeclaration.class, null,
Collections.emptySet(), null);
if (evaluator instanceof final VariableEvaluator variableEvaluator) {
Expand All @@ -370,7 +371,10 @@ public static Set<String> getDependencies(final VarDeclaration varDeclaration) {
}

public static Set<String> getDependencies(final Attribute attribute) {
if (hasValue(attribute)) {
if (InternalAttributeDeclarations.isInternalAttribute(attribute)) {
return Set.of();
}
if (!isSimpleAttributeValue(attribute)) {
final Evaluator evaluator = EvaluatorFactory.createEvaluator(attribute, VarDeclaration.class, null,
Collections.emptySet(), null);
if (evaluator instanceof final VariableEvaluator variableEvaluator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,19 @@ private static String mergeImportedTypes(final String first, final String second
return null; // skip colliding names
}

public static boolean matchesImports(final String name, final LibraryElement libraryElement) {
return getImports(libraryElement).stream().anyMatch(imp -> matchesImport(name, imp));
}

public static boolean matchesImports(final String name, final List<Import> imports) {
return imports.stream().anyMatch(imp -> matchesImport(name, imp));
}

private static boolean matchesImport(final String name, final Import imp) {
final String importedNamespace = imp.getImportedNamespace();
return importedNamespace.equals(name)
|| (importedNamespace.endsWith(WILDCARD_IMPORT_SUFFIX) && PackageNameHelper
.extractPackageName(importedNamespace).equals(PackageNameHelper.extractPackageName(name)));
return importedNamespace.equalsIgnoreCase(name) || (importedNamespace.endsWith(WILDCARD_IMPORT_SUFFIX)
&& PackageNameHelper.extractPackageName(importedNamespace)
.equalsIgnoreCase(PackageNameHelper.extractPackageName(name)));
}

public static boolean isImplicitImport(final String imported, final String packageName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.fordiac.ide.structuredtextalgorithm.ui.contentassist;

import org.eclipse.fordiac.ide.model.commands.create.AddNewImportCommand;
import org.eclipse.fordiac.ide.model.helpers.ImportHelper;
import org.eclipse.fordiac.ide.structuredtextalgorithm.ui.editor.STAlgorithmEditorUtils;
import org.eclipse.fordiac.ide.structuredtextcore.resource.LibraryElementXtextResource;
import org.eclipse.fordiac.ide.structuredtextcore.ui.contentassist.STCoreImportReplacementTextApplier;
Expand All @@ -32,7 +33,8 @@ public STAlgorithmImportReplacementTextApplier(final XtextResource resource, fin
@Override
protected void applyImport(final IDocument document, final ConfigurableCompletionProposal proposal)
throws BadLocationException {
if (getResource() instanceof final LibraryElementXtextResource libraryElementResource) {
if (getResource() instanceof final LibraryElementXtextResource libraryElementResource
&& !ImportHelper.matchesImports(getImportedNamespace(), libraryElementResource.getLibraryElement())) {
STAlgorithmEditorUtils.executeCommand(getResource().getURI(),
new AddNewImportCommand(libraryElementResource.getLibraryElement(), getImportedNamespace()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.fordiac.ide.model.commands.create.CreateInternalVariableCommand;
import org.eclipse.fordiac.ide.model.commands.create.CreateVarInOutCommand;
import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.helpers.ImportHelper;
import org.eclipse.fordiac.ide.model.libraryElement.BaseFBType;
import org.eclipse.fordiac.ide.model.libraryElement.ICallable;
import org.eclipse.fordiac.ide.model.libraryElement.INamedElement;
Expand Down Expand Up @@ -151,7 +152,8 @@ protected static STVarTempDeclarationBlock getOrCreateSTVarTempDeclarationBlock(
protected void createImportProposal(final Issue issue, final String label, final String importedNamespace,
final IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, label, label, null, (element, context) -> {
if (element.eResource() instanceof final STAlgorithmResource resource) {
if (element.eResource() instanceof final STAlgorithmResource resource
&& !ImportHelper.matchesImports(importedNamespace, resource.getLibraryElement())) {
STAlgorithmEditorUtils.executeCommand(resource.getURI(),
new AddNewImportCommand(resource.getLibraryElement(), importedNamespace));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ void testGetDependenciesForVarDeclaration() {
@Test
void testGetDependenciesForAttribute() {
assertEquals(Set.of("TestStructAttribute"), VariableOperations.getDependencies(attribute1));
assertEquals(Set.of("TestStructAttribute", "TestStructAttribute::TestStructAttribute"),
VariableOperations.getDependencies(attribute2));
assertEquals(Set.of("TestStructAttribute"), VariableOperations.getDependencies(attribute2));
assertEquals(Set.of("TestDerivedAttribute"), VariableOperations.getDependencies(attribute3));
assertEquals(Set.of("TestDerivedAttribute"), VariableOperations.getDependencies(attribute4));
}
Expand Down
Loading