1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
2 |
|
---|
3 | import org.eclipse.core.resources.IFile;
|
---|
4 | import org.eclipse.core.runtime.CoreException;
|
---|
5 | import org.eclipse.core.runtime.IProgressMonitor;
|
---|
6 | import org.eclipse.core.runtime.OperationCanceledException;
|
---|
7 | import org.eclipse.jface.text.BadLocationException;
|
---|
8 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
9 | import org.eclipse.ltk.core.refactoring.CompositeChange;
|
---|
10 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
11 | import org.eclipse.ltk.core.refactoring.TextFileChange;
|
---|
12 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
---|
13 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
|
---|
14 | import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
|
---|
15 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
|
---|
16 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
17 | import org.eclipse.text.edits.ReplaceEdit;
|
---|
18 | import org.eclipse.text.edits.TextEditGroup;
|
---|
19 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
20 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
21 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
22 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
25 |
|
---|
26 | public class FormatterProcessor extends RefactoringProcessor {
|
---|
27 | IFile file;
|
---|
28 | private CompositeNode rootNode;
|
---|
29 | private XtextDocument document;
|
---|
30 |
|
---|
31 | // ----------------------------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | public FormatterProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc) {
|
---|
34 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
35 | this.rootNode = root;
|
---|
36 | this.document = doc;
|
---|
37 | }
|
---|
38 |
|
---|
39 | // ----------------------------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Object[] getElements() {
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // ----------------------------------------------------------------------------------------------------
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String getIdentifier() {
|
---|
50 | return "Formatter Processor";
|
---|
51 | }
|
---|
52 |
|
---|
53 | // ----------------------------------------------------------------------------------------------------
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public String getProcessorName() {
|
---|
57 | return "Formatter Processor";
|
---|
58 | }
|
---|
59 |
|
---|
60 | // ----------------------------------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean isApplicable() throws CoreException {
|
---|
64 | return (rootNode != null);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // ----------------------------------------------------------------------------------------------------
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
---|
71 | throws CoreException, OperationCanceledException {
|
---|
72 |
|
---|
73 | RefactoringStatus status = new RefactoringStatus();
|
---|
74 |
|
---|
75 | if (rootNode == null)
|
---|
76 | status.addFatalError("Root node is null!");
|
---|
77 |
|
---|
78 | return status;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // ----------------------------------------------------------------------------------------------------
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
|
---|
85 | CheckConditionsContext context) throws CoreException,
|
---|
86 | OperationCanceledException {
|
---|
87 |
|
---|
88 | return checkInitialConditions(pm);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // ----------------------------------------------------------------------------------------------------
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Change createChange(IProgressMonitor pm) throws CoreException,
|
---|
95 | OperationCanceledException {
|
---|
96 | CompositeChange compositeChange = new CompositeChange("Custom formatter");
|
---|
97 | pm.beginTask("Custom formatter", 2);
|
---|
98 |
|
---|
99 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
100 | TextFileChange fileChange = new TextFileChange("Custom formatter text file change", file);
|
---|
101 | fileChange.setEdit(multiEdit);
|
---|
102 | fileChange.setTextType("bnf");
|
---|
103 | compositeChange.add(fileChange);
|
---|
104 |
|
---|
105 | FormatterConfig config = new FormatterConfig();
|
---|
106 | EbnfFormatterVisitor visitor = new EbnfFormatterVisitor(config);
|
---|
107 | visitor.accept(rootNode.getElement());
|
---|
108 |
|
---|
109 | ReplaceEdit replaceEdit = new ReplaceEdit(0, document.getLength(), visitor.getBuf().toString());
|
---|
110 | multiEdit.addChild(replaceEdit);
|
---|
111 |
|
---|
112 | return compositeChange;
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | // ----------------------------------------------------------------------------------------------------
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
120 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
121 | return null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | } |
---|