import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
import org.simantics.scl.compiler.module.ConcreteModule;
import org.simantics.scl.compiler.module.ImportDeclaration;
+import org.simantics.scl.compiler.module.InvalidModulePathException;
+import org.simantics.scl.compiler.module.ModuleUtils;
import org.simantics.scl.compiler.module.repository.ImportFailure;
import org.simantics.scl.compiler.module.repository.ImportFailureException;
import org.simantics.scl.compiler.types.TCon;
ArrayList<ImportDeclaration> absoluteImports = new ArrayList<ImportDeclaration>(relativeImports.size());
for(ImportDeclaration relativeImport : relativeImports) {
if(relativeImport.moduleName.startsWith(".")) {
- String absoluteModuleName = convertRelativeModulePath(relativeImport.location, relativeImport.moduleName);
- if(absoluteModuleName != null) {
+ try {
+ String absoluteModuleName = ModuleUtils.resolveAbsolutePath(moduleName, relativeImport.moduleName);
ImportDeclaration absoluteImport = new ImportDeclaration(
absoluteModuleName, relativeImport.localName,
relativeImport.reexport, relativeImport.spec);
absoluteImport.location = relativeImport.location;
absoluteImports.add(absoluteImport);
- }
+ } catch (InvalidModulePathException e) {
+ errorLog.log(relativeImport.location, e.getMessage());
+ }
}
else
absoluteImports.add(relativeImport);
return absoluteImports;
}
- private String convertRelativeModulePath(long location, String relativeModuleName) {
- String originalRelativeModuleName = relativeModuleName;
- int p = moduleName.lastIndexOf('/');
- String parentPackage = p < 0 ? "" : moduleName.substring(0, p);
- while(relativeModuleName.startsWith(".")) {
- if(relativeModuleName.startsWith("./")) {
- relativeModuleName = relativeModuleName.substring(2);
- }
- else if(relativeModuleName.startsWith("../")) {
- relativeModuleName = relativeModuleName.substring(3);
- if(parentPackage.isEmpty()) {
- errorLog.log(location, "Couldn't resolve the relative module name " + originalRelativeModuleName + " when the current module name is " + moduleName + ".");
- return null;
- }
- p = parentPackage.lastIndexOf('/');
- parentPackage = p < 0 ? "" : parentPackage.substring(0, p);
- }
- else {
- errorLog.log(location, "Couldn't resolve the relative module name " + originalRelativeModuleName + ". It has an invalid syntax.");
- return null;
- }
- }
- return parentPackage + "/" + relativeModuleName;
- }
-
public void addTypesToEnvironment(
ArrayList<DDataAst> dataTypesAst,
ArrayList<DTypeAst> typeAliasesAst,
--- /dev/null
+package org.simantics.scl.compiler.module;
+
+public class InvalidModulePathException extends Exception {
+
+ private static final long serialVersionUID = 4981982587509793105L;
+
+ public InvalidModulePathException(String message) {
+ super(message);
+ }
+
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.scl.compiler.module;
+
+public class ModuleUtils {
+ public static String resolveAbsolutePath(String moduleName, String relativeModuleName) throws InvalidModulePathException {
+ if (relativeModuleName.startsWith(".")) {
+ String originalRelativeModuleName = relativeModuleName;
+ int p = moduleName.lastIndexOf('/');
+ String parentPackage = p < 0 ? "" : moduleName.substring(0, p);
+ while(relativeModuleName.startsWith(".")) {
+ if(relativeModuleName.startsWith("./")) {
+ relativeModuleName = relativeModuleName.substring(2);
+ }
+ else if(relativeModuleName.startsWith("../")) {
+ relativeModuleName = relativeModuleName.substring(3);
+ if(parentPackage.isEmpty()) {
+ throw new InvalidModulePathException("Couldn't resolve the relative module name " + originalRelativeModuleName + " when the current module name is " + moduleName + ".");
+ }
+ p = parentPackage.lastIndexOf('/');
+ parentPackage = p < 0 ? "" : parentPackage.substring(0, p);
+ }
+ else {
+ throw new InvalidModulePathException("Couldn't resolve the relative module name " + originalRelativeModuleName + ". It has an invalid syntax.");
+ }
+ }
+ return parentPackage + "/" + relativeModuleName;
+ } else {
+ return relativeModuleName;
+ }
+ }
+}
import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
import org.simantics.scl.compiler.module.ImportDeclaration;
+import org.simantics.scl.compiler.module.InvalidModulePathException;
import org.simantics.scl.compiler.module.Module;
+import org.simantics.scl.compiler.module.ModuleUtils;
import org.simantics.scl.compiler.module.repository.ImportFailureException;
import org.simantics.scl.compiler.types.TCon;
import org.simantics.scl.osgi.SCLOsgi;
private ArrayList<ImportDeclaration> processRelativeImports(List<ImportDeclaration> relativeImports) {
ArrayList<ImportDeclaration> absoluteImports = new ArrayList<ImportDeclaration>(relativeImports.size());
for(ImportDeclaration relativeImport : relativeImports) {
- if(relativeImport.moduleName.startsWith(".")) {
- String absoluteModuleName = convertRelativeModulePath(relativeImport.moduleName);
- if(absoluteModuleName != null)
- absoluteImports.add(new ImportDeclaration(
- absoluteModuleName, relativeImport.localName,
- relativeImport.reexport, relativeImport.spec));
- }
- else
- absoluteImports.add(relativeImport);
+ try {
+ String absoluteModuleName = ModuleUtils.resolveAbsolutePath(moduleName, relativeImport.moduleName);
+ absoluteImports.add(new ImportDeclaration(
+ absoluteModuleName, relativeImport.localName,
+ relativeImport.reexport, relativeImport.spec));
+ } catch (InvalidModulePathException e) {
+ // Nothing to do
+ }
}
return absoluteImports;
}
- private String convertRelativeModulePath(String relativeModuleName) {
- String originalRelativeModuleName = relativeModuleName;
- int p = moduleName.lastIndexOf('/');
- String parentPackage = p < 0 ? "" : moduleName.substring(0, p);
- while(relativeModuleName.startsWith(".")) {
- if(relativeModuleName.startsWith("./")) {
- relativeModuleName = relativeModuleName.substring(2);
- }
- else if(relativeModuleName.startsWith("../")) {
- relativeModuleName = relativeModuleName.substring(3);
- if(parentPackage.isEmpty()) {
- System.err.println("Couldn't resolve the relative module name " + originalRelativeModuleName + " when the current module name is " + moduleName + ".");
- return null;
- }
- p = parentPackage.lastIndexOf('/');
- parentPackage = p < 0 ? "" : parentPackage.substring(0, p);
- }
- else {
- System.err.println("Couldn't resolve the relative module name " + originalRelativeModuleName + ". It has an invalid syntax.");
- return null;
- }
- }
- return parentPackage + "/" + relativeModuleName;
- }
-
private static final Comparator<SCLCompletionProposal> COMPARATOR = new Comparator<SCLCompletionProposal>() {
@Override
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.module.InvalidModulePathException;
+import org.simantics.scl.compiler.module.ModuleUtils;
+import org.simantics.scl.compiler.source.ModuleSource;
+import org.simantics.scl.osgi.SCLOsgi;
import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
public class OpenDeclaration extends AbstractHandler {
if(lineAtCaret.startsWith("import ") || lineAtCaret.startsWith("include ")) {
int p1 = lineAtCaret.indexOf('"', 6);
int p2 = lineAtCaret.indexOf('"', p1+1);
- String moduleName = lineAtCaret.substring(p1+1, p2);
- OpenSCLModule.openModule(moduleName);
+ SCLModuleEditorInput input = (SCLModuleEditorInput)moduleEditor.getEditorInput();
+ try {
+ String moduleName = ModuleUtils.resolveAbsolutePath(input.getModuleName(), lineAtCaret.substring(p1+1, p2));
+ ModuleSource source = SCLOsgi.SOURCE_REPOSITORY.getModuleSource(moduleName, null);
+ if (source != null) {
+ OpenSCLModule.openModule(moduleName);
+ }
+ } catch (InvalidModulePathException e) {
+ // Nothing to do
+ }
}
else {
// Try to find an identifier at caret