1 package org.simantics.scl.ui.editor.completion;
3 import java.io.StringReader;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.contentassist.ICompletionProposal;
11 import org.simantics.scl.compiler.common.names.Name;
12 import org.simantics.scl.compiler.compilation.EnvironmentOfModule;
13 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
14 import org.simantics.scl.compiler.environment.AmbiguousNameException;
15 import org.simantics.scl.compiler.environment.Environment;
16 import org.simantics.scl.compiler.environment.Environments;
17 import org.simantics.scl.compiler.errors.Failable;
18 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
19 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
20 import org.simantics.scl.compiler.module.ImportDeclaration;
21 import org.simantics.scl.compiler.module.InvalidModulePathException;
22 import org.simantics.scl.compiler.module.Module;
23 import org.simantics.scl.compiler.module.ModuleUtils;
24 import org.simantics.scl.compiler.module.repository.ImportFailureException;
25 import org.simantics.scl.compiler.types.TCon;
26 import org.simantics.scl.osgi.SCLOsgi;
28 public class SCLTextEditorEnvironment {
30 private String moduleName;
31 private SCLCompletionProposal[] proposalCache = new SCLCompletionProposal[0];
32 private List<SCLCompletionProposal> moduleProposalCache = new ArrayList<>(0);
33 private Environment env;
35 private List<ImportDeclaration> cachedImports = new ArrayList<>();
36 private boolean cacheUpdated = false;
38 public SCLTextEditorEnvironment(String moduleName) {
39 this.moduleName = moduleName;
42 public void updateModuleName(String moduleName) {
43 this.moduleName = moduleName;
46 public void updateEnvironment(IDocument document) {
47 String contents = document.get();
48 String[] lines = contents.split("\\R+");
49 List<ImportDeclaration> imports = new ArrayList<>();
50 imports.add(new ImportDeclaration("StandardLibrary", ""));
51 for (String line : lines) {
53 if (line.startsWith("import") || line.startsWith("include")) {
54 SCLParserImpl parser = new SCLParserImpl(new StringReader(line));
56 ImportDeclaration importDecl = (ImportDeclaration)parser.parseImport();
57 imports.add(importDecl);
58 } catch (SCLSyntaxErrorException e) {
59 // Import cannot be handled, ignore
64 imports = processRelativeImports(imports);
65 if (!imports.equals(cachedImports)) {
66 cachedImports = imports;
68 env = SCLOsgi.MODULE_REPOSITORY.createEnvironment(cachedImports.toArray(new ImportDeclaration[cachedImports.size()]), null);
69 Failable<Module> module = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName);
70 if(module.didSucceed())
71 env = new EnvironmentOfModule(env, module.getResult());
72 } catch (ImportFailureException e) {
73 //e.printStackTrace();
78 public ICompletionProposal[] getCompletionProposals(String prefix, int offset) {
79 int p = prefix.lastIndexOf('.');
80 String lastPart = p==-1 ? prefix : prefix.substring(p+1);
82 List<SCLCompletionProposal> proposals = new ArrayList<>();
83 for(SCLValue value : Environments.findValuesForPrefix(env, prefix)) {
84 Name name = value.getName();
85 if((name.module.equals(moduleName) || !value.isPrivate()) && !(name.name.contains("$") && Character.isLetter(name.name.charAt(0))))
86 proposals.add(new SCLCompletionProposal(value, offset - lastPart.length(), lastPart));
88 for(TCon type : Environments.findTypesForPrefix(env, prefix)) {
89 proposals.add(new SCLCompletionProposal(type.name, type.module, SCLCompletionType.TYPE, offset - lastPart.length(), lastPart));
92 if(!prefix.contains(".")) {
93 for (ImportDeclaration decl : cachedImports) {
94 if (decl.localName != null && !decl.localName.isEmpty() && decl.localName.toLowerCase().startsWith(prefix.toLowerCase())) {
95 proposals.add(new SCLCompletionProposal(decl.localName, decl.moduleName, SCLCompletionType.CONST, offset - prefix.length(), prefix));
99 Collections.sort(proposals, COMPARATOR);
100 moduleProposalCache = proposals;
101 proposalCache = moduleProposalCache.toArray(new SCLCompletionProposal[moduleProposalCache.size()]);
102 return proposalCache;
105 private ArrayList<ImportDeclaration> processRelativeImports(List<ImportDeclaration> relativeImports) {
106 ArrayList<ImportDeclaration> absoluteImports = new ArrayList<ImportDeclaration>(relativeImports.size());
107 for(ImportDeclaration relativeImport : relativeImports) {
109 String absoluteModuleName = ModuleUtils.resolveAbsolutePath(moduleName, relativeImport.moduleName);
110 absoluteImports.add(new ImportDeclaration(
111 absoluteModuleName, relativeImport.localName,
112 relativeImport.reexport, relativeImport.spec));
113 } catch (InvalidModulePathException e) {
117 return absoluteImports;
120 private static final Comparator<SCLCompletionProposal> COMPARATOR = new Comparator<SCLCompletionProposal>() {
123 public int compare(SCLCompletionProposal prop1, SCLCompletionProposal prop2) {
124 if (prop1.isPrivate() && !prop2.isPrivate())
126 else if (!prop1.isPrivate() && prop2.isPrivate())
128 return prop1.getName().compareTo(prop2.getName());
132 public SCLValue getValue(String text) {
134 return Environments.getValue(env, text);
135 } catch (AmbiguousNameException e) {
136 // TODO could also return one of the conflicting alternatives
141 public String getHoverInfo(String text) {
142 SCLValue value = getValue(text);
144 return value.getDocumentation();