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;
public void updateEnvironment(IDocument document) {
String contents = document.get();
- String[] lines = contents.split("\\R+");
+ // Strip triple quoted strings from the contents since they may contain lines that look exactly like normal imports / includes
+ contents = contents.replaceAll("(?s)\"\"\".*?\"\"\"", "\"\"");
+ // Strip multiline comments since they may contain lines that look exactly like normal imports / includes
+ contents = contents.replaceAll("(?s)/\\*.*?\\*/", "");
+
+ String[] lines = contents.split("\\R+"); //$NON-NLS-1$
List<ImportDeclaration> imports = new ArrayList<>();
- imports.add(new ImportDeclaration("StandardLibrary", ""));
+ imports.add(new ImportDeclaration("StandardLibrary", "")); //$NON-NLS-1$ //$NON-NLS-2$
for (String line : lines) {
line = line.trim();
- if (line.startsWith("import") || line.startsWith("include")) {
+ if (line.startsWith("import ") || line.startsWith("include ")) { //$NON-NLS-1$ //$NON-NLS-2$
SCLParserImpl parser = new SCLParserImpl(new StringReader(line));
try {
ImportDeclaration importDecl = (ImportDeclaration)parser.parseImport();
List<SCLCompletionProposal> proposals = new ArrayList<>();
for(SCLValue value : Environments.findValuesForPrefix(env, prefix)) {
Name name = value.getName();
- if((name.module.equals(moduleName) || !value.isPrivate()) && !(name.name.contains("$") && Character.isLetter(name.name.charAt(0))))
+ if((name.module.equals(moduleName) || !value.isPrivate()) && !(name.name.contains("$") && Character.isLetter(name.name.charAt(0)))) //$NON-NLS-1$
proposals.add(new SCLCompletionProposal(value, offset - lastPart.length(), lastPart));
}
for(TCon type : Environments.findTypesForPrefix(env, prefix)) {
proposals.add(new SCLCompletionProposal(type.name, type.module, SCLCompletionType.TYPE, offset - lastPart.length(), lastPart));
}
- if(!prefix.contains(".")) {
+ if(!prefix.contains(".")) { //$NON-NLS-1$
for (ImportDeclaration decl : cachedImports) {
if (decl.localName != null && !decl.localName.isEmpty() && decl.localName.toLowerCase().startsWith(prefix.toLowerCase())) {
proposals.add(new SCLCompletionProposal(decl.localName, decl.moduleName, SCLCompletionType.CONST, offset - prefix.length(), prefix));
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(
+ relativeImport.location,
+ 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