source: default/v2/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/quickfix/EbnfQuickfixProvider.xtend @ 66

Last change on this file since 66 was 66, checked in by hkaulbersch, 10 years ago

Changed Validation:
Validation now works between different files
-unused rule
-referenced only ones
-passthrough

File size: 6.4 KB
Line 
1/*
2* generated by Xtext
3*/
4package de.ugoe.cs.swe.bnftools.ui.quickfix
5
6import org.eclipse.xtext.ui.editor.quickfix.Fix
7import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
8import org.eclipse.xtext.validation.Issue
9import de.ugoe.cs.swe.bnftools.validation.EbnfValidator
10import org.eclipse.xtext.ui.editor.model.IXtextDocument
11import org.eclipse.emf.ecore.EObject
12import de.ugoe.cs.swe.bnftools.ebnf.Rule
13import de.ugoe.cs.swe.bnftools.ebnf.EbnfFactory
14import org.eclipse.xtext.nodemodel.util.NodeModelUtils
15import org.eclipse.xtext.nodemodel.INode
16import de.ugoe.cs.swe.bnftools.validation.EbnfAnalysisUtils
17import java.util.List
18import de.ugoe.cs.swe.bnftools.ebnf.RuleReference
19import org.eclipse.xtext.nodemodel.ICompositeNode
20import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf
21import java.util.ArrayList
22import org.eclipse.xtext.EcoreUtil2
23import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry
24import org.eclipse.emf.ecore.util.EcoreUtil
25import org.eclipse.emf.common.util.EList
26import org.eclipse.xtext.resource.IResourceDescriptions
27import com.google.inject.Inject
28import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList
29import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition
30import de.ugoe.cs.swe.bnftools.ebnf.util.EbnfAdapterFactory
31
32/**
33 * Custom quickfixes.
34 *
35 * see http://www.eclipse.org/Xtext/documentation.html#quickfixes
36 */
37class EbnfQuickfixProvider extends org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider {
38
39        @Inject
40        IResourceDescriptions resourceDescriptions;
41
42        //*****************************************************************************************
43        //works but only in one file
44                @Fix(EbnfValidator.ruleReferencedOneDescription)
45                def void fixInlineRuleReferencedOnce(Issue issue, IssueResolutionAcceptor acceptor) {
46                        acceptor.accept(
47                                issue,
48                                "Inline the rule",
49                                "Delete the rule and replace its single reference by the rule's right side",
50                                "upcase.png"
51                        ) [ element, context |
52                                var Rule rule = element as Rule;
53                                var IXtextDocument xtextDocument = context.getXtextDocument();
54                                var ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
55                                var String nodeText = node.text;
56                                var int textLength = nodeText.length - 2;
57                                var int offset = node.textRegion.offset;
58                                //rename references
59                                var ICompositeNode dList = NodeModelUtils.findActualNodeFor(rule.definitionList);
60                                var String refText = "(" + dList.text + ")";
61                                var int refLength = rule.name.length;
62                                var List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
63                                //*********************************
64                                //is the reference in the rule itself?
65                                var boolean foundRule = false;
66                                var EObject r = references.get(0) as EObject;
67                                var Rule containingRule = null;
68                                while (!foundRule) {
69                                        r = r.eContainer;
70                                        if (r instanceof Rule) {
71                                                containingRule = r as Rule;
72                                                foundRule = true;
73                                        }
74                                }
75                                if (!rule.equals(containingRule)) {
76       
77                                                                        for (ruleRef : references) {
78                                                                                var ICompositeNode refNode = NodeModelUtils.findActualNodeFor(ruleRef);
79                                                                                var int refOffset = refNode.textRegion.offset;
80                                       
81                                                                                xtextDocument.replace(refOffset, refLength, refText);
82                                                                                if (refOffset < offset) {
83                                                                                        offset += refText.length - refLength;
84                                                                                }
85                                                                        }
86                                                                       
87                                // delete rule
88                                }
89                               
90                                xtextDocument.replace(offset, textLength, "");
91                        ]
92       
93                }
94       
95//      // this is not working by unknown reasons
96//      @Fix(EbnfValidator.ruleReferencedOneDescription)
97//      def void fixInlineRuleReferencedOnce(Issue issue, IssueResolutionAcceptor acceptor) {
98//              acceptor.accept(
99//                      issue,
100//                      "Inline the rule",
101//                      "Delete the rule and replace its single reference by the rule's right side",
102//                      "upcase.png"
103//              ) [ element, context |
104//                      var Rule rule = element as Rule;
105//                      var DefinitionList dlist = rule.definitionList;
106//                      var List<Rule> rules=EbnfAnalysisUtils.getAllRules(rule.eContainer().eContainer() as EtsiBnf);
107//                      var SingleDefinition sDef = rules.get(1).definitionList.singleDefinition.get(1);
108//                      dlist.singleDefinition.add(sDef);
109//                      rule.name="bla";
110//              ]
111//
112//      }
113
114        //*****************************************************************************************
115        //              @Fix(EbnfValidator.passthroughRuleDescription)
116        //      def void fixRemovePassthroughRule(Issue issue, IssueResolutionAcceptor acceptor) {
117        //              acceptor.accept(
118        //                      issue,
119        //                      "Remove passthrough rule",
120        //                      "Delete the passthrough rule and replace its references with its right side",
121        //                      "upcase.png",
122        //                      [ element, context |
123        //                              var Rule rule = element as Rule;
124        //                              var IXtextDocument xtextDocument = context.getXtextDocument();
125        //                              var ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
126        //                              var String nodeText = node.text;
127        //                              var int textLength = nodeText.length - 2;
128        //                              var int offset = node.textRegion.offset;
129        //                              var ICompositeNode dList = NodeModelUtils.findActualNodeFor(rule.definitionList);
130        //                              var String refText = "(" + dList.text + ")";
131        //                              var int refLength = rule.name.length;
132        //                              var List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
133        //                              //sort references by offset
134        //                              var int i = 0;
135        //                              var List<RuleReference> referenceHleper = new ArrayList<RuleReference>();
136        //                              while(i<references.length){
137        //                                     
138        //                                      i++;
139        //                              }
140        //                              for (ruleRef : references) {
141        //                                      var ICompositeNode refNode = NodeModelUtils.findActualNodeFor(ruleRef);
142        //                                      var int refOffset = refNode.textRegion.offset;
143        //
144        //                                      xtextDocument.replace(refOffset, refLength, refText);
145        //                                      if (refOffset < offset) {
146        //                                              offset += refText.length - refLength;
147        //                                      }
148        //                              }
149        //                      ]
150        //              );
151        //      }
152       
153        //*****************************************************************************************
154        //works
155        @Fix(EbnfValidator.unusedRuleDescription)
156        def void fixUnusedRule(Issue issue, IssueResolutionAcceptor acceptor) {
157
158                acceptor.accept(issue, "Remove unused rule", "Delete the unused rule", "upcase.png",
159                        [ element, context |
160                                var Rule rule = element as Rule;
161                                var IXtextDocument xtextDocument = context.getXtextDocument();
162                                var ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
163                                var int offset = node.textRegion.offset;
164                                var String nodeText = node.text;
165                                var int textLength = nodeText.length - 2;
166                                xtextDocument.replace(offset, textLength, "");
167                        ])
168        }
169}
Note: See TracBrowser for help on using the repository browser.