1010 *
1111 * Contributors:
1212 * Angelo Zerr <[email protected] > - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515 13+ * Dietrich Travkin <[email protected] > - Fix code mining redrawing - Issue 3405 1314 */
1415package org .eclipse .jface .text .examples .codemining ;
1516
3435import org .eclipse .jface .text .source .ISourceViewerExtension5 ;
3536import org .eclipse .jface .text .source .SourceViewer ;
3637import org .eclipse .swt .SWT ;
38+ import org .eclipse .swt .events .SelectionListener ;
3739import org .eclipse .swt .layout .GridLayout ;
40+ import org .eclipse .swt .widgets .Button ;
3841import org .eclipse .swt .widgets .Display ;
3942import org .eclipse .swt .widgets .Shell ;
4043import org .eclipse .swt .widgets .Text ;
4548public class CodeMiningDemo {
4649
4750 private static boolean showWhitespaces = false ;
51+ private static AtomicReference <Boolean > useInLineCodeMinings = new AtomicReference <>(false );;
52+
53+ private static String LINE_HEADER = "Line header" ;
54+ private static String IN_LINE = "In-line" ;
4855
4956 public static void main (String [] args ) throws Exception {
5057
5158 Display display = new Display ();
5259 Shell shell = new Shell (display );
53- shell .setLayout (new GridLayout ());
60+ shell .setLayout (new GridLayout (2 , false ));
5461 shell .setText ("Code Mining demo" );
5562
63+ Button toggleInLineButton = new Button (shell , SWT .PUSH );
64+ toggleInLineButton .setText (LINE_HEADER );
65+ GridDataFactory .fillDefaults ().align (SWT .BEGINNING , SWT .FILL ).grab (false , false ).applyTo (toggleInLineButton );
66+
5667 AtomicReference <String > endOfLineString = new AtomicReference <>("End of line" );
5768 Text endOfLineText = new Text (shell , SWT .NONE );
5869 endOfLineText .setText (endOfLineString .get ());
@@ -70,7 +81,9 @@ public static void main(String[] args) throws Exception {
7081 + "// Name class with a number N to emulate Nms before resolving the references CodeMining\n "
7182 + "// Empty lines show a header annotating they're empty.\n "
7283 + "// The word `echo` is echoed.\n "
73- + "// Lines containing `end` get an annotation at their end\n \n "
84+ + "// Lines containing `end` get an annotation at their end\n "
85+ + "// Press the toggle button in the upper left corner to switch between\n "
86+ + "// showing reference titles in-line and showing them in additional lines.\n \n "
7487 + "class A\n " //
7588 + "new A\n " //
7689 + "new A\n \n " //
@@ -79,29 +92,39 @@ public static void main(String[] args) throws Exception {
7992 + "class 5\n " //
8093 + "new 5\n " //
8194 + "new 5\n " //
82- + "new 5\n " //
95+ + "new 5\n \n " //
96+ + "Text with some references like [REF-X]\n " + "and [REF-Y] in it.\n \n "
8397 + "multiline \n " //
8498 + "multiline \n \n " //
8599 + "suffix \n " ),
86100 new AnnotationModel ());
87- GridDataFactory .fillDefaults ().grab (true , true ).applyTo (sourceViewer .getTextWidget ());
101+ GridDataFactory .fillDefaults ().span ( 2 , 1 ). grab (true , true ).applyTo (sourceViewer .getTextWidget ());
88102 // Add AnnotationPainter (required by CodeMining)
89103 addAnnotationPainter (sourceViewer );
104+
105+ toggleInLineButton .addSelectionListener (SelectionListener .widgetSelectedAdapter (e -> {
106+ useInLineCodeMinings .set (!useInLineCodeMinings .get ());
107+ toggleInLineButton .setText (useInLineCodeMinings .get () ? IN_LINE : LINE_HEADER );
108+ sourceViewer .updateCodeMinings ();
109+ }));
110+
90111 // Initialize codemining providers
91- (( ISourceViewerExtension5 ) sourceViewer ) .setCodeMiningProviders (new ICodeMiningProvider [] {
112+ sourceViewer .setCodeMiningProviders (new ICodeMiningProvider [] {
92113 new ClassReferenceCodeMiningProvider (), //
93114 new ClassImplementationsCodeMiningProvider (), //
94115 new ToEchoWithHeaderAndInlineCodeMiningProvider ("echo" ), //
95116 new MultilineCodeMiningProvider (), //
96117 new EmptyLineCodeMiningProvider (), //
97118 new EchoAtEndOfLineCodeMiningProvider (endOfLineString ), //
98- new LineContentCodeMiningAfterPositionProvider () });
119+ new LineContentCodeMiningAfterPositionProvider (), //
120+ new ReferenceCodeMiningProvider (useInLineCodeMinings ) });
121+
99122 // Execute codemining in a reconciler
100123 MonoReconciler reconciler = new MonoReconciler (new IReconcilingStrategy () {
101124
102125 @ Override
103126 public void setDocument (IDocument document ) {
104- (( ISourceViewerExtension5 ) sourceViewer ) .updateCodeMinings ();
127+ sourceViewer .updateCodeMinings ();
105128 }
106129
107130 @ Override
@@ -111,14 +134,14 @@ public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
111134
112135 @ Override
113136 public void reconcile (IRegion partition ) {
114- (( ISourceViewerExtension5 ) sourceViewer ) .updateCodeMinings ();
137+ sourceViewer .updateCodeMinings ();
115138 }
116139 }, false );
117140 reconciler .install (sourceViewer );
118141
119142 endOfLineText .addModifyListener (event -> {
120143 endOfLineString .set (endOfLineText .getText ());
121- (( ISourceViewerExtension5 ) sourceViewer ) .updateCodeMinings ();
144+ sourceViewer .updateCodeMinings ();
122145 });
123146
124147 shell .open ();
0 commit comments