[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.common.util.EList;
|
---|
| 7 | import org.eclipse.emf.ecore.EObject;
|
---|
| 8 | import org.eclipse.jface.text.BadLocationException;
|
---|
| 9 | import org.eclipse.jface.text.ITextSelection;
|
---|
| 10 | import org.eclipse.xtext.builder.builderState.impl.ReferenceDescriptionImpl;
|
---|
| 11 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 12 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 13 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 14 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
| 15 | import org.eclipse.xtext.resource.IReferenceDescription;
|
---|
| 16 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
| 17 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
| 18 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 19 | import org.eclipse.xtext.ui.editor.model.IXtextDocument;
|
---|
| 20 |
|
---|
| 21 | import com.google.common.base.Predicate;
|
---|
| 22 | import com.google.common.collect.Iterables;
|
---|
| 23 |
|
---|
| 24 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
| 25 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 26 |
|
---|
| 27 | public class Utils {
|
---|
| 28 |
|
---|
| 29 | static public NodeAdapter findNodeFromSelection(
|
---|
| 30 | final XtextEditor xtextEditor, ITextSelection mySelection) {
|
---|
| 31 | NodeAdapter node = null;
|
---|
| 32 | if ((xtextEditor == null) || (xtextEditor.getDocument() == null))
|
---|
| 33 | return null;
|
---|
| 34 | EtsiBnf root = (EtsiBnf) xtextEditor.getDocument().readOnly(
|
---|
| 35 | new RootEObjectFinder());
|
---|
| 36 | EList<Rule> rules = root.getRule();
|
---|
| 37 | for (int i = 0; i < rules.size(); i++) {
|
---|
| 38 | node = NodeUtil.getNodeAdapter(rules.get(i));
|
---|
| 39 | if ((node.getParserNode().getOffset() <= mySelection.getOffset())
|
---|
| 40 | && (mySelection.getOffset() <= node.getParserNode()
|
---|
| 41 | .getOffset()
|
---|
| 42 | + node.getParserNode().getLength())) {
|
---|
| 43 | return node;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | return null;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | // --------------------------------------------------------------------------------
|
---|
| 50 |
|
---|
| 51 | static public List<CompositeNode> findReferenceDescriptions(
|
---|
| 52 | final IEObjectDescription eObjectDescription, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
| 53 | //TODO: resolved nur innerhalb derselben datei!
|
---|
| 54 | List<CompositeNode> references = new ArrayList<CompositeNode>();
|
---|
| 55 |
|
---|
| 56 | for (IResourceDescription resourceDescription : resourceDescriptions
|
---|
| 57 | .getAllResourceDescriptions()) {
|
---|
| 58 | Iterable<IReferenceDescription> matchingReferenceDescriptors = Iterables
|
---|
| 59 | .filter(resourceDescription.getReferenceDescriptions(),
|
---|
| 60 | new Predicate<IReferenceDescription>() {
|
---|
| 61 | public boolean apply(IReferenceDescription input) {
|
---|
| 62 | return eObjectDescription
|
---|
| 63 | .getEObjectURI()
|
---|
| 64 | .equals(input.getTargetEObjectUri());
|
---|
| 65 | }
|
---|
| 66 | });
|
---|
| 67 | for (IReferenceDescription matchingReferenceDescription : matchingReferenceDescriptors) {
|
---|
| 68 | //TODO: dieser check ignoriert links zu anderen dateien wg. exception
|
---|
| 69 | if ((matchingReferenceDescription instanceof org.eclipse.xtext.resource.impl.DefaultReferenceDescription) || (matchingReferenceDescription instanceof ReferenceDescriptionImpl)){
|
---|
| 70 | CompositeNode node = editor.getDocument().readOnly(
|
---|
| 71 | new URIFragmentResolver(matchingReferenceDescription
|
---|
| 72 | .getSourceEObjectUri().fragment()));
|
---|
| 73 | references.add(node);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | return references;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // --------------------------------------------------------------------------------
|
---|
| 81 |
|
---|
| 82 | static public List<CompositeNode> findReferences(EObject o, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
| 83 | final IEObjectDescription eObjectDescription = editor.getDocument()
|
---|
| 84 | .readOnly(new EObjectResolver(o, resourceDescriptions));
|
---|
| 85 |
|
---|
| 86 | List<CompositeNode> referenceDescriptions = Utils.findReferenceDescriptions(eObjectDescription, resourceDescriptions, editor);
|
---|
| 87 | return referenceDescriptions;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | // --------------------------------------------------------------------------------
|
---|
| 91 |
|
---|
| 92 | static public int stepBackNewlines(IXtextDocument document, int offset) {
|
---|
| 93 | if (offset <= 0)
|
---|
| 94 | return 0;
|
---|
| 95 |
|
---|
| 96 | try {
|
---|
| 97 | int tempOffset = offset;
|
---|
| 98 | String str = document.get(tempOffset-1, 1);
|
---|
| 99 | while (str.equals("\n") || str.equals("\r") || str.equals(" ")) {
|
---|
| 100 | tempOffset -= 1;
|
---|
| 101 | str = document.get(tempOffset-1, 1);
|
---|
| 102 | }
|
---|
| 103 | return offset - tempOffset;
|
---|
| 104 | } catch (BadLocationException e) {
|
---|
| 105 | e.printStackTrace();
|
---|
| 106 | }
|
---|
| 107 | return 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|