Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Belle2019-03-10 06:42:26 -0400
committerJan Belle2019-03-10 06:42:26 -0400
commit314575730c85372b358e8ac58b89a76d67887955 (patch)
treeed3d96b9755d3e988e6c6344b98a7824cc553273
parent3f0fe727f66f0e8e9c3a02bd2aa79c1253a8e472 (diff)
downloadorg.eclipse.etrice-314575730c85372b358e8ac58b89a76d67887955.tar.gz
org.eclipse.etrice-314575730c85372b358e8ac58b89a76d67887955.tar.xz
org.eclipse.etrice-314575730c85372b358e8ac58b89a76d67887955.zip
[core] Implement validation and quickfix for room model name
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/ModelPathResourceSetInitializer.java6
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/WorkspaceModelPath.java19
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/quickfix/BaseQuickfixProvider.xtend13
-rw-r--r--plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/validation/BaseJavaValidator.java2
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickFixProviderXtend.xtend17
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java8
-rw-r--r--plugins/org.eclipse.etrice.core.room/META-INF/MANIFEST.MF3
-rw-r--r--plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java24
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/FileSystemModelPath.java48
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/IModelPath.java14
10 files changed, 150 insertions, 4 deletions
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/ModelPathResourceSetInitializer.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/ModelPathResourceSetInitializer.java
index 3d7c87124..964d125e3 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/ModelPathResourceSetInitializer.java
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/ModelPathResourceSetInitializer.java
@@ -15,6 +15,7 @@
package org.eclipse.etrice.core.common.ui.modelpath;
+import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.core.resources.IProject;
@@ -53,5 +54,10 @@ public class ModelPathResourceSetInitializer implements IResourceSetInitializer
public Stream<URI> getAllFiles() {
return ModelPathManager.INSTANCE.getModelPath(project).getAllFiles();
}
+
+ @Override
+ public Optional<QualifiedName> getQualifiedName(URI uri) {
+ return ModelPathManager.INSTANCE.getModelPath(project).getQualifiedName(uri);
+ }
}
}
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/WorkspaceModelPath.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/WorkspaceModelPath.java
index 5daf25a4d..c153186a1 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/WorkspaceModelPath.java
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/modelpath/WorkspaceModelPath.java
@@ -15,6 +15,7 @@
package org.eclipse.etrice.core.common.ui.modelpath;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.core.resources.IContainer;
@@ -22,6 +23,7 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.etrice.generator.base.io.IModelPath;
@@ -60,6 +62,19 @@ public class WorkspaceModelPath implements IModelPath {
.flatMap(container -> getAllFiles(container));
}
+ @Override
+ public Optional<QualifiedName> getQualifiedName(URI uri) {
+ if(uri.isPlatform()) {
+ IPath path = new Path(uri.toPlatformString(true));
+ return paths.stream().map(container -> container.getFullPath())
+ .filter(p -> p.isPrefixOf(path))
+ .map(p -> path.makeRelativeTo(p).removeFileExtension())
+ .map(p -> QualifiedName.create(p.segments()))
+ .findFirst();
+ }
+ return Optional.empty();
+ }
+
public List<IContainer> getPaths() {
return paths;
}
@@ -122,8 +137,8 @@ public class WorkspaceModelPath implements IModelPath {
/**
* Creates an uri for the passed file.
*/
- private URI createURI(IFile file) {
- return URI.createPlatformResourceURI(file.getFullPath().toString(), true);
+ private URI createURI(IResource resource) {
+ return URI.createPlatformResourceURI(resource.getFullPath().toString(), true);
}
/**
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/quickfix/BaseQuickfixProvider.xtend b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/quickfix/BaseQuickfixProvider.xtend
index de7b80f7e..29de662ce 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/quickfix/BaseQuickfixProvider.xtend
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/quickfix/BaseQuickfixProvider.xtend
@@ -14,6 +14,11 @@
package org.eclipse.etrice.core.common.ui.quickfix
import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider
+import org.eclipse.xtext.ui.editor.quickfix.Fix
+import org.eclipse.etrice.core.common.validation.BaseJavaValidator
+import org.eclipse.xtext.validation.Issue
+import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
+import org.eclipse.etrice.core.common.base.Import
//import org.eclipse.xtext.ui.editor.quickfix.Fix
//import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
@@ -26,6 +31,14 @@ import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider
*/
class BaseQuickfixProvider extends DefaultQuickfixProvider {
+ @Fix(BaseJavaValidator.DEPRECATED_IMPORT_URI)
+ def removeImportUri(Issue issue, IssueResolutionAcceptor acceptor) {
+ acceptor.accept(issue, 'Remove import uri', 'Remove the import uri', null) [ element, context |
+ val imp = element as Import
+ imp.importURI = null
+ ]
+ }
+
// @Fix(MyDslValidator::INVALID_NAME)
// def capitalizeName(Issue issue, IssueResolutionAcceptor acceptor) {
// acceptor.accept(issue, 'Capitalize name', 'Capitalize the name.', 'upcase.png') [
diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/validation/BaseJavaValidator.java b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/validation/BaseJavaValidator.java
index 2e29cf4a7..c85c9c19a 100644
--- a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/validation/BaseJavaValidator.java
+++ b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/validation/BaseJavaValidator.java
@@ -59,6 +59,7 @@ public class BaseJavaValidator extends org.eclipse.etrice.core.common.validation
public static final String UNDEFINED_ANNOTATION_ATTRIBUTE = "BaseJavaValidator.UndfinedAnnotationAttribute";
public static final String UNDEFINED_ANNOTATION_ATTRIBUTE_VALUE = "BaseJavaValidator.UndfinedAnnotationAttributeValue";
public static final String DUPLICATE_ANNOTATION_ATTRIBUTE = "BaseJavaValidator.DuplicateAnnotationAttribute";
+ public static final String DEPRECATED_IMPORT_URI = "BaseJavaValidator.DeprecatedImportUri";
@Inject ImportUriResolver importUriResolver;
@Inject IGlobalScopeProvider globalScopeProvider;
@@ -197,6 +198,7 @@ public class BaseJavaValidator extends org.eclipse.etrice.core.common.validation
if(imp.getImportURI() == null) {
return;
}
+ warning("import statements using uris are deprecated", BasePackage.Literals.IMPORT__IMPORT_URI, DEPRECATED_IMPORT_URI);
String uriString = importUriResolver.resolve(imp);
if (uriString == null) {
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickFixProviderXtend.xtend b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickFixProviderXtend.xtend
index 024135c1d..f7d4a2226 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickFixProviderXtend.xtend
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickFixProviderXtend.xtend
@@ -31,9 +31,13 @@ import org.eclipse.xtext.validation.Issue
import static org.eclipse.xtext.EcoreUtil2.getContainerOfType
import static org.eclipse.etrice.core.room.RoomPackage.Literals.*
+import org.eclipse.etrice.core.common.scoping.ModelPathGlobalScopeProvider
+import org.eclipse.emf.ecore.resource.Resource
+import org.eclipse.emf.ecore.EReference
class RoomQuickFixProviderXtend extends RoomQuickfixProvider {
+ @Inject ModelPathGlobalScopeProvider modelPathGlobalScopeProvider
@Inject IResourceDescriptions resourceDescriptions
// override xtext linking issues
@@ -56,6 +60,8 @@ class RoomQuickFixProviderXtend extends RoomQuickfixProvider {
val reference = getUnresolvedEReference(issue, target)
if(reference !== null) {
+ // import without uri by element
+ importQuickfixElementWithoutURI(issueString, resource, reference, issue, acceptor)
// import by namespace per default
importQuickfixNamespace(issueString, reference.EReferenceType, issue, acceptor)
// import by element
@@ -65,6 +71,17 @@ class RoomQuickFixProviderXtend extends RoomQuickfixProvider {
}
}
+ protected def void importQuickfixElementWithoutURI(String issueString, Resource resource, EReference reference, Issue issue, IssueResolutionAcceptor acceptor) {
+ modelPathGlobalScopeProvider.getScope(resource, reference, [name.lastSegment.equalsIgnoreCase(issueString)]).allElements.forEach[ eObjDesc |
+ acceptor.accept(issue, '''Import '«eObjDesc.qualifiedName»' ''', '', null) [elem, ctx |
+ val model = getContainerOfType(elem, RoomModel)
+ model.imports += BaseFactory.eINSTANCE.createImport => [
+ importedNamespace = eObjDesc.qualifiedName.toString
+ ]
+ ]
+ ]
+ }
+
protected def void importQuickfixNamespace(String issueString, EClass type, Issue issue, IssueResolutionAcceptor acceptor) {
resourceDescriptions.getExportedObjectsByType(type).filter[name.lastSegment.equalsIgnoreCase(issueString)].forEach [ eObjDesc |
acceptor.accept(issue, '''Import namespace '«eObjDesc.qualifiedName.skipLast(1)»' from '«eObjDesc.EObjectURI.lastSegment»' ''', '', null) [elem, ctx |
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java
index abe07dfe1..49644afbc 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/quickfix/RoomQuickfixProvider.java
@@ -46,6 +46,7 @@ import org.eclipse.xtext.ui.editor.model.edit.ISemanticModification;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolution;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
+import org.eclipse.xtext.ui.editor.quickfix.ReplaceModification;
import org.eclipse.xtext.validation.Issue;
public class RoomQuickfixProvider extends FSMQuickfixProvider {
@@ -89,6 +90,13 @@ public class RoomQuickfixProvider extends FSMQuickfixProvider {
return super.hasResolutionFor(issueCode) || QuickfixExtensionManager.getInstance().hasResolutionFor(issueCode);
}
+ @Fix(RoomJavaValidator.WRONG_MODEL_NAME)
+ public void renameModel(final Issue issue, IssueResolutionAcceptor acceptor) {
+ String replacement = issue.getData()[1];
+ acceptor.accept(issue, "Rename model", "Replace model name with its location on the modelpath \nfollowed by its file name",
+ "correction_change.gif", new ReplaceModification(issue, replacement));
+ }
+
@Fix(RoomJavaValidator.THREAD_MISSING)
public void fixMissingThread(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Add a default thread", issue.getData()[0], "add.gif", new IModification() {
diff --git a/plugins/org.eclipse.etrice.core.room/META-INF/MANIFEST.MF b/plugins/org.eclipse.etrice.core.room/META-INF/MANIFEST.MF
index 9dd44b4f0..cd7acc2d9 100644
--- a/plugins/org.eclipse.etrice.core.room/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.etrice.core.room/META-INF/MANIFEST.MF
@@ -21,7 +21,8 @@ Require-Bundle: org.eclipse.etrice.core.common;bundle-version="2.0.0",
org.antlr.runtime;bundle-version="3.0.0",
org.eclipse.core.resources;bundle-version="3.7.100",
org.eclipse.core.runtime;bundle-version="3.7.0",
- org.eclipse.etrice.core.genmodel.fsm;bundle-version="2.0.0"
+ org.eclipse.etrice.core.genmodel.fsm;bundle-version="2.0.0",
+ org.eclipse.etrice.generator.base
Import-Package: org.apache.log4j,
org.eclipse.etrice.core.room
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
diff --git a/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java b/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java
index e91c80909..1c365ee81 100644
--- a/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java
+++ b/plugins/org.eclipse.etrice.core.room/src/org/eclipse/etrice/core/validation/RoomJavaValidator.java
@@ -28,6 +28,7 @@ import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
+import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.common.base.Annotation;
import org.eclipse.etrice.core.common.base.BasePackage;
@@ -76,6 +77,9 @@ import org.eclipse.etrice.core.room.StandardOperation;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.room.SubSystemClass;
import org.eclipse.etrice.core.room.util.RoomHelpers;
+import org.eclipse.etrice.generator.base.io.IModelPath;
+import org.eclipse.etrice.generator.base.io.IModelPathProvider;
+import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
@@ -100,7 +104,11 @@ public class RoomJavaValidator extends AbstractRoomJavaValidator {
@Inject protected IQualifiedNameProvider fqnProvider;
+ @Inject protected IQualifiedNameConverter nameConverter;
+
@Inject ImportUriResolver importUriResolver;
+
+ @Inject private IModelPathProvider modelPathProvider;
/* message strings */
public static final String OPTIONAL_REFS_HAVE_TO_HAVE_MULTIPLICITY_ANY = "optional refs have to have multiplicity any [*]";
@@ -108,6 +116,7 @@ public class RoomJavaValidator extends AbstractRoomJavaValidator {
public static final String A_REPLICATED_PORT_MUST_HAVE_AT_MOST_ONE_REPLICATED_PEER = "a replicated port must have at most one replicated peer (with arbitrary multiplicity each)";
/* tags for quick fixes */
+ public static final String WRONG_MODEL_NAME = "RoomJavaValidator.WrongModelName";
public static final String THREAD_MISSING = "RoomJavaValidator.ThreadMissing";
public static final String DUPLICATE_ACTOR_INSTANCE_MAPPING = "RoomJavaValidator.DuplicateActorInstanceMapping";
public static final String WRONG_NAMESPACE = "RoomJavaValidator.WrongNamespace";
@@ -123,6 +132,21 @@ public class RoomJavaValidator extends AbstractRoomJavaValidator {
public static final String DEPRECATED_MESSAGE_DATA_NAME = "RoomJavaValidator.DeprecatedMessageDataName";
@Check
+ public void checkRoomModelName(RoomModel roomModel) {
+ QualifiedName modelName = fqnProvider.getFullyQualifiedName(roomModel);
+ if(modelName != null) {
+ Resource resource = roomModel.eResource();
+ IModelPath modelpath = modelPathProvider.get(resource);
+ modelpath.getQualifiedName(resource.getURI()).ifPresent(fileName -> {
+ if(!fileName.equals(modelName)) {
+ warning("model name doesn't match its file name and its location on the modelpath", RoomPackage.Literals.ROOM_MODEL__NAME,
+ WRONG_MODEL_NAME, nameConverter.toString(modelName), nameConverter.toString(fileName));
+ }
+ });
+ }
+ }
+
+ @Check
public void checkRoomImportedNamespace(Import imp) {
QualifiedName importedFQN = ImportHelpers.toFQN(imp);
if(importedFQN == null)
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/FileSystemModelPath.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/FileSystemModelPath.java
index 995027399..b087dc788 100644
--- a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/FileSystemModelPath.java
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/FileSystemModelPath.java
@@ -20,10 +20,13 @@ import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.naming.QualifiedName;
@@ -62,8 +65,20 @@ public class FileSystemModelPath implements IModelPath {
}
@Override
+ public Optional<QualifiedName> getQualifiedName(URI uri) {
+ if(uri.isFile()) {
+ Path path = Paths.get(uri.toFileString());
+ return paths.stream().filter(p -> path.startsWith(p))
+ .map(p -> p.relativize(path))
+ .map(p -> createQualifiedName(p))
+ .findFirst();
+ }
+ return Optional.empty();
+ }
+
+ @Override
public String toString() {
- return packages.toString();
+ return paths.toString();
}
/**
@@ -136,4 +151,35 @@ public class FileSystemModelPath implements IModelPath {
return dir;
}
+ /**
+ * Converts a path to a qualified name.
+ * Removes a possible file extension.
+ *
+ * @param path the path to convert
+ * @return a fully qualified name consisting of the path segments
+ */
+ private QualifiedName createQualifiedName(Path path) {
+ String[] segments = StreamSupport.stream(path.spliterator(), false).map(p -> p.toString()).toArray(String[]::new);
+ if(segments.length > 0) {
+ int lastIndex = segments.length - 1;
+ segments[lastIndex] = trimFileExtension(segments[lastIndex]);
+ }
+ return QualifiedName.create(segments);
+ }
+
+ /**
+ * Searches for the last dot in the string and discards everything that follows it.
+ * If the fileName doesn't contain a dot it is simply returned.
+ *
+ * @param fileName the name of the file to trim the extension
+ * @return the name of the file without a file extension
+ */
+ private String trimFileExtension(String fileName) {
+ int periodIndex = fileName.lastIndexOf('.');
+ if(periodIndex != -1) {
+ return fileName.substring(0, periodIndex);
+ }
+ return fileName;
+ }
+
}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/IModelPath.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/IModelPath.java
index 7ad76417d..ff44f2441 100644
--- a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/IModelPath.java
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/io/IModelPath.java
@@ -15,6 +15,7 @@
package org.eclipse.etrice.generator.base.io;
+import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.emf.common.util.URI;
@@ -39,6 +40,14 @@ public interface IModelPath {
*/
public Stream<URI> getAllFiles();
+ /**
+ * Returns a qualified name for the model objects in the file.
+ *
+ * @param uri an uri to a file
+ * @return the qualified name for the file or nothing if the file is not on the modelpath.
+ */
+ public Optional<QualifiedName> getQualifiedName(URI uri);
+
static class EmptyModelPath implements IModelPath {
@Override
@@ -50,5 +59,10 @@ public interface IModelPath {
public Stream<URI> getAllFiles() {
return Stream.empty();
}
+
+ @Override
+ public Optional<QualifiedName> getQualifiedName(URI uri) {
+ return Optional.empty();
+ }
}
}

Back to the top