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