Skip to content

Commit fa6d886

Browse files
authored
Merge pull request #3053 from idodeclare/feature/redirect_1_result
Feature/redirect 1 result
2 parents 182f294 + 384ec09 commit fa6d886

26 files changed

+818
-430
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/CtagsReader.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017-2018, 2020, Chris Fraire <[email protected]>.
2323
*/
2424

2525
package org.opengrok.indexer.analysis;
@@ -395,9 +395,9 @@ private CpatIndex bestIndexOfTag(int lineno, String whole, String str) {
395395

396396
int woff = strictIndexOf(whole, str);
397397
if (woff < 0) {
398-
/**
398+
/*
399399
* When a splitter is available, search the entire line.
400-
* (N.b. use 0-offset vs ctags's 1-offset.)
400+
* (N.b. use 0-based indexing vs ctags's 1-based.)
401401
*/
402402
String cut = trySplitterCut(lineno - 1, 1);
403403
if (cut == null || !cut.startsWith(whole)) {
@@ -512,9 +512,9 @@ private CpatIndex bestIndexOfArg(int lineno, String whole, String arg) {
512512
return new CpatIndex(lineno, s, e);
513513
}
514514

515-
/**
515+
/*
516516
* When a splitter is available, search the next several lines.
517-
* (N.b. use 0-offset vs ctags's 1-offset.)
517+
* (N.b. use 0-based indexing vs ctags's 1-based.)
518518
*/
519519
String cut = trySplitterCut(lineno - 1, MAX_CUT_LINES);
520520
if (cut == null || !cut.startsWith(whole)) {
@@ -640,22 +640,22 @@ private PatResult strictMatch(String whole, String substr, Pattern pat) {
640640
}
641641

642642
/**
643-
* Finds the line with the longest content from {@code midx}.
643+
* Finds the line with the longest content from {@code cut}.
644644
* <p>
645645
* The {@link Definitions} tag model is based on a match within a line.
646646
* "signature" fields, however, can be condensed from multiple lines; and a
647647
* fuzzy match can therefore span multiple lines.
648648
*/
649649
private CpatIndex bestLineOfMatch(int lineno, PatResult pr, String cut) {
650-
// (N.b. use 0-offset vs ctags's 1-offset.)
651-
int lpos = splitter.getPosition(lineno - 1);
652-
int mpos = lpos + pr.start;
653-
int moff = splitter.findLineOffset(mpos);
654-
int zpos = lpos + pr.end - 1;
655-
int zoff = splitter.findLineOffset(zpos);
650+
// (N.b. use 0-based indexing vs ctags's 1-based.)
651+
int lineOff = splitter.getOffset(lineno - 1);
652+
int mOff = lineOff + pr.start;
653+
int mIndex = splitter.findLineIndex(mOff);
654+
int zOff = lineOff + pr.end - 1;
655+
int zIndex = splitter.findLineIndex(zOff);
656656

657657
int t = tabSize;
658-
int resoff = moff;
658+
int resIndex = mIndex;
659659
int contentLength = 0;
660660
/**
661661
* Initialize the following just to silence warnings but with values
@@ -664,31 +664,31 @@ private CpatIndex bestLineOfMatch(int lineno, PatResult pr, String cut) {
664664
String whole = "";
665665
int s = 0;
666666
int e = 1;
667-
/**
668-
* Iterate to determine the length of the portion of `midx' that
669-
* is contained within each line.
667+
/*
668+
* Iterate to determine the length of the portion of cut that is
669+
* contained within each line.
670670
*/
671-
for (int ioff = moff; ioff <= zoff; ++ioff) {
672-
String iwhole = splitter.getLine(ioff);
673-
int ioffpos = splitter.getPosition(ioff);
674-
int iendpos = ioffpos + iwhole.length();
675-
int i_s = pr.start + lpos < ioffpos ? ioffpos : pr.start + lpos;
676-
int i_e = pr.end + lpos > iendpos ? iendpos : pr.end + lpos;
677-
if (i_e - i_s > contentLength) {
678-
contentLength = i_e - i_s;
679-
resoff = ioff;
671+
for (int lIndex = mIndex; lIndex <= zIndex; ++lIndex) {
672+
String iwhole = splitter.getLine(lIndex);
673+
int lOff = splitter.getOffset(lIndex);
674+
int lOffZ = lOff + iwhole.length();
675+
int offStart = Math.max(pr.start + lineOff, lOff);
676+
int offEnd = Math.min(pr.end + lineOff, lOffZ);
677+
if (offEnd - offStart > contentLength) {
678+
contentLength = offEnd - offStart;
679+
resIndex = lIndex;
680680
whole = iwhole;
681681
// (The following are not yet adjusted for tabs.)
682-
s = i_s - ioffpos;
683-
e = i_e - ioffpos;
682+
s = offStart - lOff;
683+
e = offEnd - lOff;
684684
}
685685
}
686686

687687
if (s >= 0 && s < whole.length() && e >= 0 && e <= whole.length()) {
688688
s = ExpandTabsReader.translate(whole, s, t);
689689
e = ExpandTabsReader.translate(whole, e, t);
690-
// (N.b. use ctags's 1-offset.)
691-
return new CpatIndex(resoff + 1, s, e);
690+
// (N.b. use ctags's 1-based indexing.)
691+
return new CpatIndex(resIndex + 1, s, e);
692692
}
693693

694694
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/plain/DefinitionsTokenStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.analysis.plain;
@@ -118,7 +118,7 @@ private void createTokens(Definitions defs, LineBreaker brk) {
118118

119119
if (lineno >= 0 && lineno < brk.count() && tag.symbol != null &&
120120
tag.text != null) {
121-
int lineoff = brk.getPosition(lineno);
121+
int lineoff = brk.getOffset(lineno);
122122
if (tag.lineStart >= 0) {
123123
PendingToken tok = new PendingToken(tag.symbol, lineoff +
124124
tag.lineStart, lineoff + tag.lineEnd);

opengrok-indexer/src/main/java/org/opengrok/indexer/search/Results.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ private static Reader getXrefReader(
151151
* <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
152152
* <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
153153
* <li>{@link SearchHelper#summarizer} (if sourceContext is not
154-
* {@code null})</li> <li>{@link SearchHelper#compressed} (if sourceContext
155-
* is not {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
154+
* {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
156155
* sourceContext or historyContext is not {@code null})</li> </ul>
157156
*
158157
* @param out write destination
@@ -232,7 +231,7 @@ public static void prettyPrint(Writer out, SearchHelper sh, int start,
232231
AbstractAnalyzer.Genre genre = AbstractAnalyzer.Genre.get(
233232
doc.get(QueryBuilder.T));
234233
if (AbstractAnalyzer.Genre.XREFABLE == genre && sh.summarizer != null) {
235-
String xtags = getTags(xrefDataDir, rpath, sh.compressed);
234+
String xtags = getTags(xrefDataDir, rpath, env.isCompressXref());
236235
// FIXME use Highlighter from lucene contrib here,
237236
// instead of summarizer, we'd also get rid of
238237
// apache lucene in whole source ...

opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/Context.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/*
2121
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright 2011 Jens Elkner.
23-
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2424
*/
2525
package org.opengrok.indexer.search.context;
2626

@@ -56,15 +56,14 @@
5656
*/
5757
public class Context {
5858

59+
static final int MAXFILEREAD = 1024 * 1024;
60+
5961
private static final Logger LOGGER = LoggerFactory.getLogger(Context.class);
6062

6163
private final Query query;
6264
private final QueryBuilder qbuilder;
6365
private final LineMatcher[] m;
64-
static final int MAXFILEREAD = 1024 * 1024;
65-
private char[] buffer;
66-
PlainLineTokenizer tokens;
67-
String queryAsURI;
66+
private final String queryAsURI;
6867

6968
/**
7069
* Map whose keys tell which fields to look for in the source file, and
@@ -96,10 +95,9 @@ public Context(Query query, QueryBuilder qbuilder) {
9695
QueryMatchers qm = new QueryMatchers();
9796
m = qm.getMatchers(query, TOKEN_FIELDS);
9897
if (m != null) {
99-
buildQueryAsURI(qbuilder.getQueries());
100-
//System.err.println("Found Matchers = "+ m.length + " for " + query);
101-
buffer = new char[MAXFILEREAD];
102-
tokens = new PlainLineTokenizer((Reader) null);
98+
queryAsURI = buildQueryAsURI(qbuilder.getQueries());
99+
} else {
100+
queryAsURI = "";
103101
}
104102
}
105103

@@ -212,7 +210,7 @@ public boolean getContext2(RuntimeEnvironment env, IndexSearcher searcher,
212210

213211
try {
214212
List<String> fieldList = qbuilder.getContextFields();
215-
String[] fields = fieldList.toArray(new String[fieldList.size()]);
213+
String[] fields = fieldList.toArray(new String[0]);
216214

217215
String res = uhi.highlightFieldsUnion(fields, query, docId,
218216
linelimit);
@@ -236,10 +234,9 @@ public boolean getContext2(RuntimeEnvironment env, IndexSearcher searcher,
236234
*
237235
* @param subqueries a map containing the query text for each field
238236
*/
239-
private void buildQueryAsURI(Map<String, String> subqueries) {
237+
private String buildQueryAsURI(Map<String, String> subqueries) {
240238
if (subqueries.isEmpty()) {
241-
queryAsURI = "";
242-
return;
239+
return "";
243240
}
244241
StringBuilder sb = new StringBuilder();
245242
for (Map.Entry<String, String> entry : subqueries.entrySet()) {
@@ -249,7 +246,7 @@ private void buildQueryAsURI(Map<String, String> subqueries) {
249246
.append('&');
250247
}
251248
sb.setLength(sb.length() - 1);
252-
queryAsURI = sb.toString();
249+
return sb.toString();
253250
}
254251

255252
private boolean alt = true;
@@ -298,7 +295,8 @@ public boolean getContext(Reader in, Writer out, String urlPrefix,
298295
if (scopes != null) {
299296
Scope scp = scopes.getScope(tag.line);
300297
scope = scp.getName() + "()";
301-
scopeUrl = "<a href=\"" + urlPrefixE + pathE + "#" + Integer.toString(scp.getLineFrom()) + "\">" + scope + "</a>";
298+
scopeUrl = "<a href=\"" + urlPrefixE + pathE + "#" +
299+
scp.getLineFrom() + "\">" + scope + "</a>";
302300
}
303301

304302
/* desc[0] is matched symbol
@@ -367,16 +365,18 @@ public boolean getContext(Reader in, Writer out, String urlPrefix,
367365
if (in == null) {
368366
return anything;
369367
}
370-
int charsRead = 0;
371-
boolean truncated = false;
372368

369+
PlainLineTokenizer tokens = new PlainLineTokenizer(null);
370+
boolean truncated = false;
373371
boolean lim = limit;
374372
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
375373
if (!env.isQuickContextScan()) {
376374
lim = false;
377375
}
378376

379377
if (lim) {
378+
char[] buffer = new char[MAXFILEREAD];
379+
int charsRead;
380380
try {
381381
charsRead = in.read(buffer);
382382
if (charsRead == MAXFILEREAD) {

opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/ContextFormatter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.search.context;
@@ -323,7 +323,7 @@ private void writeScope(int lineOffset, Appendable dest)
323323
throws IOException {
324324
Scopes.Scope scope = null;
325325
if (scopes != null) {
326-
// N.b. use ctags 1-offset vs 0-offset.
326+
// N.b. use ctags 1-based indexing vs 0-based.
327327
scope = scopes.getScope(lineOffset + 1);
328328
}
329329
if (scope != null && scope != scopes.getScope(-1)) {
@@ -340,7 +340,7 @@ private void writeScope(int lineOffset, Appendable dest)
340340
private void writeTag(int lineOffset, Appendable dest, List<String> marks)
341341
throws IOException {
342342
if (defs != null) {
343-
// N.b. use ctags 1-offset vs 0-offset.
343+
// N.b. use ctags 1-based indexing vs 0-based.
344344
List<Tag> linetags = defs.getTags(lineOffset + 1);
345345
if (linetags != null) {
346346
Tag pickedTag = findTagForMark(linetags, marks);

opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/PassageConverter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.search.context;
@@ -75,11 +75,11 @@ public SortedMap<Integer, LineHighlight> convert(Passage[] passages,
7575
continue;
7676
}
7777

78-
int m = splitter.findLineOffset(start);
78+
int m = splitter.findLineIndex(start);
7979
if (m < 0) {
8080
continue;
8181
}
82-
int n = splitter.findLineOffset(end - 1);
82+
int n = splitter.findLineIndex(end - 1);
8383
if (n < 0) {
8484
continue;
8585
}
@@ -97,23 +97,23 @@ public SortedMap<Integer, LineHighlight> convert(Passage[] passages,
9797
// Create LineHighlight entries for passage matches.
9898
for (int i = 0; i < passage.getNumMatches(); ++i) {
9999
int mstart = passage.getMatchStarts()[i];
100-
int mm = splitter.findLineOffset(mstart);
100+
int mm = splitter.findLineIndex(mstart);
101101
int mend = passage.getMatchEnds()[i];
102-
int nn = splitter.findLineOffset(mend - 1);
102+
int nn = splitter.findLineIndex(mend - 1);
103103
if (mstart < mend && mm >= m && mm <= n && nn >= m && nn <= n) {
104104
if (mm == nn) {
105-
int lbeg = splitter.getPosition(mm);
105+
int lbeg = splitter.getOffset(mm);
106106
int lstart = mstart - lbeg;
107107
int lend = mend - lbeg;
108108
LineHighlight lhigh = res.get(mm);
109109
lhigh.addMarkup(PhraseHighlight.create(lstart, lend));
110110
} else {
111-
int lbeg = splitter.getPosition(mm);
111+
int lbeg = splitter.getOffset(mm);
112112
int loff = mstart - lbeg;
113113
LineHighlight lhigh = res.get(mm);
114114
lhigh.addMarkup(PhraseHighlight.createStarter(loff));
115115

116-
lbeg = splitter.getPosition(nn);
116+
lbeg = splitter.getOffset(nn);
117117
loff = mend - lbeg;
118118
lhigh = res.get(nn);
119119
lhigh.addMarkup(PhraseHighlight.createEnder(loff));

0 commit comments

Comments
 (0)