[5] | 1 | package de.ugoe.cs.swe.bnftools.utils;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
| 6 | import org.eclipse.emf.ecore.EObject;
|
---|
| 7 | import org.eclipse.jface.text.BadLocationException;
|
---|
| 8 | import org.eclipse.jface.text.ITextSelection;
|
---|
| 9 | import org.eclipse.xtext.builder.builderState.impl.ReferenceDescriptionImpl;
|
---|
| 10 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 11 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 12 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 13 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
| 14 | import org.eclipse.xtext.resource.IReferenceDescription;
|
---|
| 15 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
| 16 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
| 17 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 18 | import org.eclipse.xtext.ui.editor.model.IXtextDocument;
|
---|
| 19 |
|
---|
| 20 | import com.google.common.base.Predicate;
|
---|
| 21 | import com.google.common.collect.Iterables;
|
---|
| 22 |
|
---|
[17] | 23 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
| 24 | import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
|
---|
[5] | 25 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
[17] | 26 | import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
|
---|
[5] | 27 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 28 |
|
---|
| 29 | public class Utils {
|
---|
| 30 |
|
---|
| 31 | static public NodeAdapter findNodeFromSelection(
|
---|
| 32 | final XtextEditor xtextEditor, ITextSelection mySelection) {
|
---|
| 33 | if ((xtextEditor == null) || (xtextEditor.getDocument() == null))
|
---|
| 34 | return null;
|
---|
| 35 | EtsiBnf root = (EtsiBnf) xtextEditor.getDocument().readOnly(
|
---|
| 36 | new RootEObjectFinder());
|
---|
[17] | 37 |
|
---|
| 38 | NodeAdapter resultNode = null;
|
---|
| 39 | if (root.getBnfEntry() != null) {
|
---|
| 40 | System.out.println("bnf grammar!");
|
---|
| 41 | for (int i=0; i < root.getBnfEntry().size(); i++) {
|
---|
| 42 | BnfEntry entry = root.getBnfEntry().get(i);
|
---|
| 43 | if (entry.getRule() != null) {
|
---|
| 44 | resultNode = findNodeInRule(entry.getRule(), mySelection);
|
---|
| 45 | if (resultNode != null)
|
---|
| 46 | return resultNode;
|
---|
| 47 | }
|
---|
[5] | 48 | }
|
---|
[17] | 49 | } else if (root.getDeltaEntry() != null) {
|
---|
| 50 | for (int i=0; i < root.getDeltaEntry().size(); i++) {
|
---|
| 51 | DeltaEntry entry = root.getDeltaEntry().get(i);
|
---|
| 52 | if (entry.getRule() != null) {
|
---|
| 53 | resultNode = findNodeInRule(entry.getRule(), mySelection);
|
---|
| 54 | if (resultNode != null)
|
---|
| 55 | return resultNode;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | } else if (root.getMergeEntry() != null) {
|
---|
| 59 | for (int i=0; i < root.getMergeEntry().size(); i++) {
|
---|
| 60 | MergeEntry entry = root.getMergeEntry().get(i);
|
---|
| 61 | if (entry.getMergeRule() != null) {
|
---|
| 62 | // TODO: doesn't work here...
|
---|
| 63 | // resultNode = findNodeInRule(entry.getgetRule(), mySelection);
|
---|
| 64 | // if (resultNode != null)
|
---|
| 65 | // return resultNode;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
[5] | 68 | }
|
---|
[17] | 69 |
|
---|
| 70 | return resultNode;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // --------------------------------------------------------------------------------
|
---|
| 74 |
|
---|
| 75 | static private NodeAdapter findNodeInRule(Rule rule, ITextSelection mySelection) {
|
---|
| 76 | NodeAdapter node = NodeUtil.getNodeAdapter(rule);
|
---|
| 77 | if ((node.getParserNode().getOffset() <= mySelection.getOffset())
|
---|
| 78 | && (mySelection.getOffset() <= node.getParserNode()
|
---|
| 79 | .getOffset()
|
---|
| 80 | + node.getParserNode().getLength())) {
|
---|
| 81 | return node;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[5] | 84 | return null;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | // --------------------------------------------------------------------------------
|
---|
| 88 |
|
---|
| 89 | static public List<CompositeNode> findReferenceDescriptions(
|
---|
| 90 | final IEObjectDescription eObjectDescription, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
| 91 | //TODO: resolved nur innerhalb derselben datei!
|
---|
| 92 | List<CompositeNode> references = new ArrayList<CompositeNode>();
|
---|
| 93 |
|
---|
| 94 | for (IResourceDescription resourceDescription : resourceDescriptions
|
---|
| 95 | .getAllResourceDescriptions()) {
|
---|
| 96 | Iterable<IReferenceDescription> matchingReferenceDescriptors = Iterables
|
---|
| 97 | .filter(resourceDescription.getReferenceDescriptions(),
|
---|
| 98 | new Predicate<IReferenceDescription>() {
|
---|
| 99 | public boolean apply(IReferenceDescription input) {
|
---|
| 100 | return eObjectDescription
|
---|
| 101 | .getEObjectURI()
|
---|
| 102 | .equals(input.getTargetEObjectUri());
|
---|
| 103 | }
|
---|
| 104 | });
|
---|
| 105 | for (IReferenceDescription matchingReferenceDescription : matchingReferenceDescriptors) {
|
---|
| 106 | //TODO: dieser check ignoriert links zu anderen dateien wg. exception
|
---|
| 107 | if ((matchingReferenceDescription instanceof org.eclipse.xtext.resource.impl.DefaultReferenceDescription) || (matchingReferenceDescription instanceof ReferenceDescriptionImpl)){
|
---|
| 108 | CompositeNode node = editor.getDocument().readOnly(
|
---|
| 109 | new URIFragmentResolver(matchingReferenceDescription
|
---|
| 110 | .getSourceEObjectUri().fragment()));
|
---|
| 111 | references.add(node);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return references;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // --------------------------------------------------------------------------------
|
---|
| 119 |
|
---|
| 120 | static public List<CompositeNode> findReferences(EObject o, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
| 121 | final IEObjectDescription eObjectDescription = editor.getDocument()
|
---|
| 122 | .readOnly(new EObjectResolver(o, resourceDescriptions));
|
---|
| 123 |
|
---|
| 124 | List<CompositeNode> referenceDescriptions = Utils.findReferenceDescriptions(eObjectDescription, resourceDescriptions, editor);
|
---|
| 125 | return referenceDescriptions;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | // --------------------------------------------------------------------------------
|
---|
| 129 |
|
---|
| 130 | static public int stepBackNewlines(IXtextDocument document, int offset) {
|
---|
| 131 | if (offset <= 0)
|
---|
| 132 | return 0;
|
---|
| 133 |
|
---|
| 134 | try {
|
---|
| 135 | int tempOffset = offset;
|
---|
| 136 | String str = document.get(tempOffset-1, 1);
|
---|
| 137 | while (str.equals("\n") || str.equals("\r") || str.equals(" ")) {
|
---|
| 138 | tempOffset -= 1;
|
---|
| 139 | str = document.get(tempOffset-1, 1);
|
---|
| 140 | }
|
---|
| 141 | return offset - tempOffset;
|
---|
| 142 | } catch (BadLocationException e) {
|
---|
| 143 | e.printStackTrace();
|
---|
| 144 | }
|
---|
| 145 | return 0;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | }
|
---|