24
24
package org .opensolaris .opengrok .analysis ;
25
25
26
26
import java .io .BufferedReader ;
27
+ import java .io .File ;
27
28
import java .io .IOException ;
28
29
import java .io .InputStream ;
29
30
import java .io .InputStreamReader ;
38
39
import org .opensolaris .opengrok .configuration .RuntimeEnvironment ;
39
40
import org .opensolaris .opengrok .logger .LoggerFactory ;
40
41
import org .opensolaris .opengrok .util .IOUtils ;
42
+ import org .opensolaris .opengrok .util .SourceSplitter ;
41
43
42
44
/**
43
45
* Provides Ctags by having a running subprocess of ctags.
44
46
*
45
47
* @author Chandan
46
48
*/
47
- public class Ctags {
49
+ public class Ctags implements Resettable {
48
50
49
51
private static final Logger LOGGER = LoggerFactory .getLogger (Ctags .class );
50
52
@@ -57,6 +59,7 @@ public class Ctags {
57
59
//default: setCtags(System.getProperty("org.opensolaris.opengrok.analysis.Ctags", "ctags"));
58
60
private String binary ;
59
61
private String CTagsExtraOptionsFile = null ;
62
+ private int tabSize ;
60
63
61
64
private boolean junit_testing = false ;
62
65
@@ -78,11 +81,33 @@ public void setBinary(String binary) {
78
81
this .binary = binary ;
79
82
}
80
83
84
+ public int getTabSize () {
85
+ return tabSize ;
86
+ }
87
+
88
+ public void setTabSize (int tabSize ) {
89
+ this .tabSize = tabSize ;
90
+ }
91
+
81
92
public void setCTagsExtraOptionsFile (String CTagsExtraOptionsFile ) {
82
93
this .CTagsExtraOptionsFile = CTagsExtraOptionsFile ;
83
94
}
84
95
96
+ /**
97
+ * Resets the instance for use for another file but without closing any
98
+ * running ctags instance.
99
+ */
100
+ @ Override
101
+ public void reset () {
102
+ setTabSize (0 );
103
+ }
104
+
105
+ /**
106
+ * {@link #reset()}, and close any running ctags instance.
107
+ * @throws IOException not really thrown -- but logged
108
+ */
85
109
public void close () throws IOException {
110
+ reset ();
86
111
IOUtils .close (ctagsIn );
87
112
if (ctags != null ) {
88
113
closing = true ;
@@ -353,9 +378,11 @@ public Definitions doCtags(String file) throws IOException,
353
378
}
354
379
355
380
CtagsReader rdr = new CtagsReader ();
381
+ rdr .setSplitterSupplier (() -> { return trySplitSource (file ); });
382
+ rdr .setTabSize (tabSize );
356
383
Definitions ret ;
357
384
try {
358
- ctagsIn .write (file );
385
+ ctagsIn .write (file + " \n " );
359
386
if (Thread .interrupted ()) throw new InterruptedException ("write()" );
360
387
ctagsIn .flush ();
361
388
if (Thread .interrupted ()) throw new InterruptedException ("flush()" );
@@ -415,6 +442,7 @@ public void destroy() {
415
442
};
416
443
417
444
CtagsReader rdr = new CtagsReader ();
445
+ rdr .setTabSize (tabSize );
418
446
try {
419
447
readTags (rdr );
420
448
} catch (InterruptedException ex ) {
@@ -468,4 +496,22 @@ private void readTags(CtagsReader reader) throws InterruptedException {
468
496
}
469
497
LOGGER .severe ("CTag reader cycle was interrupted!" );
470
498
}
499
+
500
+ /**
501
+ * Attempts to create a {@link SourceSplitter} instance with content from
502
+ * the specified file.
503
+ * @return a defined instance or {@code null} on failure (without exception)
504
+ */
505
+ private static SourceSplitter trySplitSource (String filename ) {
506
+ SourceSplitter splitter = new SourceSplitter ();
507
+ try {
508
+ StreamSource src = StreamSource .fromFile (new File (filename ));
509
+ splitter .reset (src );
510
+ } catch (NullPointerException |IOException ex ) {
511
+ LOGGER .log (Level .WARNING , "Failed to re-read {0}" , filename );
512
+ return null ;
513
+ }
514
+ LOGGER .log (Level .FINEST , "Re-read {0}" , filename );
515
+ return splitter ;
516
+ }
471
517
}
0 commit comments