|
| 1 | +package de.cognicrypt.codegenerator.ui.contentassist; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 11 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 12 | +import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer; |
| 13 | +import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; |
| 14 | +import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; |
| 15 | +import org.eclipse.jface.text.contentassist.CompletionProposal; |
| 16 | +import org.eclipse.jface.text.contentassist.ICompletionProposal; |
| 17 | + |
| 18 | +import de.cognicrypt.codegenerator.Activator; |
| 19 | +import de.cognicrypt.core.Constants; |
| 20 | + |
| 21 | + |
| 22 | +@SuppressWarnings("restriction") |
| 23 | +public class CustomCompletionProposalComputer extends JavaCompletionProposalComputer { |
| 24 | + |
| 25 | + |
| 26 | + static private List<String> ruleNames = readClassnames(Constants.JCA_LATEST_ECLIPSE_RULES_DIR); |
| 27 | + |
| 28 | + @Override |
| 29 | + public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) { |
| 30 | + List<ICompletionProposal> defaultProposals = super.computeCompletionProposals(context, monitor); |
| 31 | + |
| 32 | + JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context; |
| 33 | + ICompilationUnit cu = javaContext.getCompilationUnit(); |
| 34 | + int offset = context.getInvocationOffset(); |
| 35 | + SelectionFinder finder = new SelectionFinder(cu, offset); |
| 36 | + |
| 37 | + if (finder.getMemberName() == null) { |
| 38 | + return defaultProposals; |
| 39 | + } |
| 40 | + |
| 41 | + List<ICompletionProposal> customProposals = new ArrayList<ICompletionProposal>(); |
| 42 | + for (String name : ruleNames) { |
| 43 | + String s = '"' + name + '"'; |
| 44 | + CompletionProposal proposal = new CompletionProposal(s, offset, 0, s.length()); |
| 45 | + customProposals.add(proposal); |
| 46 | + } |
| 47 | + |
| 48 | + customProposals.addAll(defaultProposals); |
| 49 | + return customProposals; |
| 50 | + } |
| 51 | + |
| 52 | + private static List<String> readClassnames(String rulesFolder) { |
| 53 | + List<String> classnames = new ArrayList<String>(); |
| 54 | + for (File rule : (new File(rulesFolder)).listFiles()) { |
| 55 | + if (rule.isDirectory()) { |
| 56 | + classnames.addAll(readClassnames(rule.getAbsolutePath())); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + String classname = null; |
| 61 | + try { |
| 62 | + classname = readClassnameFromRule(rule); |
| 63 | + } catch (IOException e) { |
| 64 | + Activator.getDefault().logError(e); |
| 65 | + } |
| 66 | + |
| 67 | + if (classname != null) { |
| 68 | + classnames.add(classname); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return classnames; |
| 73 | + } |
| 74 | + |
| 75 | + private static String readClassnameFromRule(File ruleFile) throws IOException { |
| 76 | + final String fileName = ruleFile.getName(); |
| 77 | + if (!fileName.endsWith(Constants.cryslFileEnding)) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + BufferedReader reader = null; |
| 82 | + String classname = null; |
| 83 | + try { |
| 84 | + reader = new BufferedReader(new FileReader(ruleFile)); |
| 85 | + String spec = reader.readLine(); |
| 86 | + classname = spec.split(" ")[1]; |
| 87 | + } finally { |
| 88 | + reader.close(); |
| 89 | + } |
| 90 | + |
| 91 | + return classname; |
| 92 | + } |
| 93 | +} |
0 commit comments