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

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

Fixed the Quickfix for referencedOnlyOnce

File size: 4.7 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
22
23/**
24 * Custom quickfixes.
25 *
26 * see http://www.eclipse.org/Xtext/documentation.html#quickfixes
27 */
28class EbnfQuickfixProvider extends org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider {
29        //works
30        @Fix(EbnfValidator.ruleReferencedOneDescription)
31        def void fixInlineRuleReferencedOnce(Issue issue, IssueResolutionAcceptor acceptor) {
32                acceptor.accept(
33                        issue,
34                        "Inline the rule",
35                        "Delete the rule and replace its single reference by the rule's right side",
36                        "upcase.png"
37                ) [ element, context |
38                        var Rule rule = element as Rule;
39                        var IXtextDocument xtextDocument = context.getXtextDocument();
40                        var ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
41                        var String nodeText = node.text;
42                        var int textLength = nodeText.length - 2;
43                        var int offset = node.textRegion.offset;
44                        //rename references
45                        var ICompositeNode dList = NodeModelUtils.findActualNodeFor(rule.definitionList);
46                        var String refText = "(" + dList.text + ")";
47                        var int refLength = rule.name.length;
48                        var List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
49                        //is the reference in the rule itself?
50                        var boolean foundRule = false;
51                        var EObject r = references.get(0) as EObject;
52                        var Rule containingRule=null;
53                        while (!foundRule) {
54                                r = r.eContainer;
55                                if (r instanceof Rule) {
56                                        containingRule = r as Rule;
57                                        foundRule = true;
58                                }
59                        }
60                        if (!rule.equals(containingRule)) {
61                                for (ruleRef : references) {
62                                        var ICompositeNode refNode = NodeModelUtils.findActualNodeFor(ruleRef);
63                                        var int refOffset = refNode.textRegion.offset;
64
65                                        xtextDocument.replace(refOffset, refLength, refText);
66                                        if (refOffset < offset) {
67                                                offset += refText.length - refLength;
68                                        }
69                                }
70                        }
71               
72
73                // delete rule
74                xtextDocument.replace(offset, textLength, "");
75                ]
76
77        }
78
79        //works
80//              @Fix(EbnfValidator.passthroughRuleDescription)
81//      def void fixRemovePassthroughRule(Issue issue, IssueResolutionAcceptor acceptor) {
82//              acceptor.accept(
83//                      issue,
84//                      "Remove passthrough rule",
85//                      "Delete the passthrough rule and replace its references with its right side",
86//                      "upcase.png",
87//                      [ element, context |
88//                              var Rule rule = element as Rule;
89//                              var IXtextDocument xtextDocument = context.getXtextDocument();
90//                              var ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
91//                              var String nodeText = node.text;
92//                              var int textLength = nodeText.length - 2;
93//                              var int offset = node.textRegion.offset;
94//                              var ICompositeNode dList = NodeModelUtils.findActualNodeFor(rule.definitionList);
95//                              var String refText = "(" + dList.text + ")";
96//                              var int refLength = rule.name.length;
97//                              var List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
98//                              //sort references by offset
99//                              var int i = 0;
100//                              var List<RuleReference> referenceHleper = new ArrayList<RuleReference>();
101//                              while(i<references.length){
102//                                     
103//                                      i++;
104//                              }
105//                              for (ruleRef : references) {
106//                                      var ICompositeNode refNode = NodeModelUtils.findActualNodeFor(ruleRef);
107//                                      var int refOffset = refNode.textRegion.offset;
108//
109//                                      xtextDocument.replace(refOffset, refLength, refText);
110//                                      if (refOffset < offset) {
111//                                              offset += refText.length - refLength;
112//                                      }
113//                              }
114//                      ]
115//              );
116//      }
117
118                @Fix(EbnfValidator.unusedRuleDescription)
119        def void fixUnusedRule(Issue issue, IssueResolutionAcceptor acceptor) {
120
121                acceptor.accept(issue, "Remove unused rule", "Delete the unused rule", "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 int offset = node.textRegion.offset;
127                                var String nodeText = node.text;
128                                var int textLength = nodeText.length - 2;
129                                xtextDocument.replace(offset, textLength, "");
130                        ])
131        }
132}
133               
Note: See TracBrowser for help on using the repository browser.