Skip to content

Commit 1ea70ed

Browse files
idodeclaretarzanek
authored andcommitted
DEFS stores true offsets from enhanced CtagsReader
1 parent 39bf187 commit 1ea70ed

24 files changed

+1870
-81
lines changed

src/org/opensolaris/opengrok/analysis/Ctags.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.opensolaris.opengrok.analysis;
2525

2626
import java.io.BufferedReader;
27+
import java.io.File;
2728
import java.io.IOException;
2829
import java.io.InputStream;
2930
import java.io.InputStreamReader;
@@ -38,13 +39,14 @@
3839
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3940
import org.opensolaris.opengrok.logger.LoggerFactory;
4041
import org.opensolaris.opengrok.util.IOUtils;
42+
import org.opensolaris.opengrok.util.SourceSplitter;
4143

4244
/**
4345
* Provides Ctags by having a running subprocess of ctags.
4446
*
4547
* @author Chandan
4648
*/
47-
public class Ctags {
49+
public class Ctags implements Resettable {
4850

4951
private static final Logger LOGGER = LoggerFactory.getLogger(Ctags.class);
5052

@@ -57,6 +59,7 @@ public class Ctags {
5759
//default: setCtags(System.getProperty("org.opensolaris.opengrok.analysis.Ctags", "ctags"));
5860
private String binary;
5961
private String CTagsExtraOptionsFile = null;
62+
private int tabSize;
6063

6164
private boolean junit_testing = false;
6265

@@ -78,11 +81,33 @@ public void setBinary(String binary) {
7881
this.binary = binary;
7982
}
8083

84+
public int getTabSize() {
85+
return tabSize;
86+
}
87+
88+
public void setTabSize(int tabSize) {
89+
this.tabSize = tabSize;
90+
}
91+
8192
public void setCTagsExtraOptionsFile(String CTagsExtraOptionsFile) {
8293
this.CTagsExtraOptionsFile = CTagsExtraOptionsFile;
8394
}
8495

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+
*/
85109
public void close() throws IOException {
110+
reset();
86111
IOUtils.close(ctagsIn);
87112
if (ctags != null) {
88113
closing = true;
@@ -353,9 +378,11 @@ public Definitions doCtags(String file) throws IOException,
353378
}
354379

355380
CtagsReader rdr = new CtagsReader();
381+
rdr.setSplitterSupplier(() -> { return trySplitSource(file); });
382+
rdr.setTabSize(tabSize);
356383
Definitions ret;
357384
try {
358-
ctagsIn.write(file);
385+
ctagsIn.write(file + "\n");
359386
if (Thread.interrupted()) throw new InterruptedException("write()");
360387
ctagsIn.flush();
361388
if (Thread.interrupted()) throw new InterruptedException("flush()");
@@ -415,6 +442,7 @@ public void destroy() {
415442
};
416443

417444
CtagsReader rdr = new CtagsReader();
445+
rdr.setTabSize(tabSize);
418446
try {
419447
readTags(rdr);
420448
} catch (InterruptedException ex) {
@@ -468,4 +496,22 @@ private void readTags(CtagsReader reader) throws InterruptedException {
468496
}
469497
LOGGER.severe("CTag reader cycle was interrupted!");
470498
}
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+
}
471517
}

0 commit comments

Comments
 (0)