Skip to content

Commit fc9c8a9

Browse files
committed
first step for making Gradle Outline: basic parser out plain tree in
Outline; Outline is done a-la Winterstein Markdown Editor
1 parent e0b53eb commit fc9c8a9

5 files changed

Lines changed: 531 additions & 3 deletions

File tree

‎org.nodeclipse.enide.editors.gradle/META-INF/MANIFEST.MF‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui,
99
org.eclipse.core.resources,
1010
org.eclipse.ui.ide,
1111
org.eclipse.jface.text,
12-
org.eclipse.ui.editors
12+
org.eclipse.ui.editors,
13+
org.eclipse.ui.views
1314
Bundle-ActivationPolicy: lazy
1415
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
1516
Export-Package: org.nodeclipse.enide.editors.gradle

‎org.nodeclipse.enide.editors.gradle/src/org/nodeclipse/enide/editors/gradle/editors/GradleEditor.java‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package org.nodeclipse.enide.editors.gradle.editors;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.eclipse.core.runtime.CoreException;
37
import org.eclipse.jface.preference.IPreferenceStore;
8+
import org.eclipse.jface.text.IDocument;
49
import org.eclipse.jface.text.IDocumentExtension3;
510
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
11+
import org.eclipse.ui.IEditorInput;
612
import org.eclipse.ui.editors.text.TextEditor;
13+
import org.eclipse.ui.texteditor.IDocumentProvider;
714
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
8-
15+
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
916

1017
/**
1118
* copied from NodeclipseNodejsEditor
12-
* TODO base classes, color theme support
19+
* TODO consider base classes
1320
*
1421
* @author Paul Verest
1522
*/
@@ -21,6 +28,12 @@ public class GradleEditor extends TextEditor {
2128
public final static String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor";
2229

2330
private DefaultCharacterPairMatcher matcher;
31+
32+
//+
33+
private GradleOutline fOutlinePage = null;
34+
private boolean pageDirty = true;
35+
private GradlePage page;
36+
IDocument oldDoc = null; //used only in doSetInput(IEditorInput input)
2437

2538
public GradleEditor() {
2639
setSourceViewerConfiguration(new NodeSourceViewerConfiguration());
@@ -48,6 +61,71 @@ protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupp
4861
store.setDefault(EDITOR_MATCHING_BRACKETS, true);
4962
store.setDefault(EDITOR_MATCHING_BRACKETS_COLOR, "128,128,128");
5063
}
64+
65+
//+ block for Outline taken from Winterstein Markdown Editor {
66+
public Object getAdapter(Class required) {
67+
if (IContentOutlinePage.class.equals(required)) {
68+
if (fOutlinePage == null) {
69+
fOutlinePage= new GradleOutline(getDocumentProvider(), this);
70+
if (getEditorInput() != null)
71+
fOutlinePage.setInput(getEditorInput());
72+
}
73+
return fOutlinePage;
74+
}
75+
return super.getAdapter(required);
76+
}
77+
78+
public GradlePage getGradlePage() {
79+
if (pageDirty) updateGradlePage();
80+
return page;
81+
}
82+
83+
private void updateGradlePage() {
84+
String text = getText();
85+
if (text==null) text="";
86+
page = new GradlePage(text); //GradleParser.parse(text); /
87+
pageDirty = false;
88+
}
89+
90+
/**
91+
* @return The text of the editor's document, or null if unavailable.
92+
*/
93+
public String getText() {
94+
IDocument doc = getDocument();
95+
return doc==null? null : doc.get();
96+
}
97+
98+
public IDocument getDocument() {
99+
IEditorInput input = getEditorInput();
100+
IDocumentProvider docProvider = getDocumentProvider();
101+
return docProvider==null? null : docProvider.getDocument(input);
102+
}
103+
104+
public static GradleEditor getEditor(IDocument doc) {
105+
return doc2editor.get(doc);
106+
}
51107

108+
private static final Map<IDocument, GradleEditor> doc2editor = new HashMap<IDocument, GradleEditor>();
109+
110+
@Override
111+
protected void doSetInput(IEditorInput input) throws CoreException {
112+
// Detach from old
113+
if (oldDoc!= null) {
114+
//oldDoc.removeDocumentListener(this);
115+
if (doc2editor.get(oldDoc) == this) doc2editor.remove(oldDoc);
116+
}
117+
// Set
118+
super.doSetInput(input);
119+
// Attach as a listener to new doc
120+
IDocument doc = getDocument();
121+
oldDoc = doc;
122+
if (doc==null) return;
123+
//doc.addDocumentListener(this);
124+
doc2editor.put(doc, this);
125+
// // Initialise code folding
126+
// haveRunFolding = false;
127+
// updateSectionFoldingAnnotations(null);
128+
}
129+
//}
52130
}
53131

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package org.nodeclipse.enide.editors.gradle.editors;
2+
3+
import org.eclipse.jface.text.DocumentEvent;
4+
import org.eclipse.jface.text.IDocument;
5+
import org.eclipse.jface.text.IDocumentListener;
6+
import org.eclipse.jface.viewers.ITreeContentProvider;
7+
import org.eclipse.jface.viewers.TreeViewer;
8+
import org.eclipse.jface.viewers.Viewer;
9+
import org.eclipse.swt.widgets.Composite;
10+
import org.eclipse.swt.widgets.Control;
11+
import org.eclipse.ui.texteditor.IDocumentProvider;
12+
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
13+
14+
15+
16+
public class GradleOutline extends ContentOutlinePage {
17+
18+
private final IDocumentProvider fDocumentProvider;
19+
private final GradleEditor fTextEditor;
20+
private Object fInput = null;
21+
22+
public GradleOutline(IDocumentProvider documentProvider, GradleEditor gradleEditor) {
23+
this.fDocumentProvider=documentProvider;
24+
this.fTextEditor=gradleEditor;
25+
}
26+
27+
/**
28+
* Sets the input of the outline page
29+
*
30+
* @param input
31+
* the input of this outline page
32+
*/
33+
public void setInput(Object input) {
34+
fInput = input;
35+
update();
36+
}
37+
38+
/**
39+
* Updates the outline page.
40+
*/
41+
public void update() {
42+
TreeViewer viewer = getTreeViewer();
43+
44+
if (viewer != null) {
45+
Control control = viewer.getControl();
46+
if (control != null && !control.isDisposed()) {
47+
control.setRedraw(false);
48+
viewer.setInput(fInput);
49+
viewer.expandAll();
50+
control.setRedraw(true);
51+
}
52+
}
53+
}
54+
55+
/*
56+
* (non-Javadoc) Method declared on ContentOutlinePage
57+
*/
58+
@Override
59+
public void createControl(Composite parent) {
60+
super.createControl(parent);
61+
TreeViewer viewer = getTreeViewer();
62+
viewer.setContentProvider(new ContentProvider());
63+
//...
64+
viewer.addSelectionChangedListener(this);
65+
66+
if (fInput != null)
67+
viewer.setInput(fInput);
68+
69+
70+
}
71+
72+
public final class ContentProvider implements ITreeContentProvider, IDocumentListener {
73+
74+
private GradlePage fContent;
75+
// protected List fContent= new ArrayList(10);
76+
private GradleEditor fTextEditor;
77+
78+
private void parse() {
79+
if (fTextEditor==null){
80+
System.out.println("fTextEditor==null");
81+
return;
82+
}
83+
fContent = fTextEditor.getGradlePage();
84+
}
85+
86+
87+
/*
88+
* @see IContentProvider#inputChanged(Viewer, Object, Object)
89+
*/
90+
@Override
91+
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
92+
// Detach from old
93+
if (oldInput != null) {
94+
IDocument document = fDocumentProvider.getDocument(oldInput);
95+
if (document != null) {
96+
document.removeDocumentListener(this);
97+
}
98+
}
99+
fContent = null;
100+
// Attach to new
101+
if (newInput == null)
102+
return;
103+
IDocument document = fDocumentProvider.getDocument(newInput);
104+
if (document == null)
105+
return;
106+
fTextEditor = GradleEditor.getEditor(document);
107+
document.addDocumentListener(this);
108+
parse();
109+
}
110+
111+
@Override
112+
public void documentAboutToBeChanged(DocumentEvent event) {
113+
// nothing
114+
}
115+
116+
@Override
117+
public void documentChanged(DocumentEvent event) {
118+
parse();
119+
update();
120+
}
121+
122+
/*
123+
* @see IContentProvider#dispose
124+
*/
125+
@Override
126+
public void dispose() {
127+
fContent = null;
128+
}
129+
130+
// /*
131+
// * @see IContentProvider#isDeleted(Object)
132+
// */
133+
// @Override
134+
// public boolean isDeleted(Object element) {
135+
// return false;
136+
// }
137+
138+
/*
139+
* @see IStructuredContentProvider#getElements(Object)
140+
*/
141+
@Override
142+
public Object[] getElements(Object inputElement) {
143+
// //TODO
144+
// if (fContent==null){
145+
// System.out.println("fContent==null");
146+
// return null;
147+
// }
148+
// if (fContent.nodes==null){
149+
// System.out.println("fContent.nodes==null");
150+
// return null;
151+
// }
152+
return fContent.nodes.toArray();
153+
}
154+
155+
/*
156+
* @see ITreeContentProvider#hasChildren(Object)
157+
*/
158+
@Override
159+
public boolean hasChildren(Object element) {
160+
if (element == fInput) {
161+
return true;
162+
}
163+
if (element instanceof GradlePage.Node) {
164+
GradlePage.Node node = (GradlePage.Node) element;
165+
//return header.getSubHeaders().size() > 0;
166+
return false;
167+
}
168+
;
169+
return false;
170+
}
171+
172+
/*
173+
* @see ITreeContentProvider#getParent(Object)
174+
*/
175+
@Override
176+
public Object getParent(Object element) {
177+
if (!(element instanceof GradlePage.Node))
178+
return null;
179+
//TODO
180+
return null;
181+
}
182+
183+
/*
184+
* @see ITreeContentProvider#getChildren(Object)
185+
*/
186+
@Override
187+
public Object[] getChildren(Object element) {
188+
if (element == fInput) {
189+
return getElements(null);
190+
}
191+
if (!(element instanceof GradlePage.Node))
192+
return null;
193+
//return ((MarkdownPage.Header) element).getSubHeaders().toArray();
194+
//TODO
195+
return null;
196+
}
197+
198+
199+
200+
}
201+
202+
}

0 commit comments

Comments
 (0)