Search in sources :

Example 1 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class JFlexXref method startScope.

protected void startScope() {
    if (scopesEnabled && scope == null) {
        int line = getLineNumber();
        List<Tag> tags = defs.getTags(line);
        if (tags != null) {
            for (Tag tag : tags) {
                if (tag.type.startsWith("function") || tag.type.startsWith("method")) {
                    scope = new Scope(tag.line, tag.line, tag.symbol, tag.namespace, tag.signature);
                    scopeLevel = 0;
                    break;
                }
            }
        }
    }
}
Also used : Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Tag(org.opensolaris.opengrok.analysis.Definitions.Tag)

Example 2 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class Context method getContext.

/**
     * ???.
     * Closes the given <var>in</var> reader on return.
     *
     * @param in File to be matched
     * @param out to write the context
     * @param morePrefix to link to more... page
     * @param path path of the file
     * @param tags format to highlight defs.
     * @param limit should the number of matching lines be limited?
     * @return Did it get any matching context?
     */
public boolean getContext(Reader in, Writer out, String urlPrefix, String morePrefix, String path, Definitions tags, boolean limit, boolean isDefSearch, List<Hit> hits, Scopes scopes) {
    alt = !alt;
    if (m == null) {
        IOUtils.close(in);
        return false;
    }
    boolean anything = false;
    TreeMap<Integer, String[]> matchingTags = null;
    String urlPrefixE = (urlPrefix == null) ? "" : Util.URIEncodePath(urlPrefix);
    String pathE = Util.URIEncodePath(path);
    if (tags != null) {
        matchingTags = new TreeMap<Integer, String[]>();
        try {
            for (Definitions.Tag tag : tags.getTags()) {
                for (int i = 0; i < m.length; i++) {
                    if (m[i].match(tag.symbol) == LineMatcher.MATCHED) {
                        String scope = null;
                        String scopeUrl = null;
                        if (scopes != null) {
                            Scope scp = scopes.getScope(tag.line);
                            scope = scp.getName() + "()";
                            scopeUrl = "<a href=\"" + urlPrefixE + pathE + "#" + Integer.toString(scp.getLineFrom()) + "\">" + scope + "</a>";
                        }
                        /* desc[0] is matched symbol
                             * desc[1] is line number
                             * desc[2] is type
                             * desc[3] is matching line;
                             * desc[4] is scope
                             */
                        String[] desc = { tag.symbol, Integer.toString(tag.line), tag.type, tag.text, scope };
                        if (in == null) {
                            if (out == null) {
                                Hit hit = new Hit(path, Util.htmlize(desc[3]).replace(desc[0], "<b>" + desc[0] + "</b>"), desc[1], false, alt);
                                hits.add(hit);
                                anything = true;
                            } else {
                                out.write("<a class=\"s\" href=\"");
                                out.write(urlPrefixE);
                                out.write(pathE);
                                out.write("#");
                                out.write(desc[1]);
                                out.write("\"><span class=\"l\">");
                                out.write(desc[1]);
                                out.write("</span> ");
                                out.write(Util.htmlize(desc[3]).replace(desc[0], "<b>" + desc[0] + "</b>"));
                                out.write("</a> ");
                                if (desc[4] != null) {
                                    out.write("<span class=\"scope\"><a href\"");
                                    out.write(scopeUrl);
                                    out.write("\">in ");
                                    out.write(desc[4]);
                                    out.write("</a></span> ");
                                }
                                out.write("<i>");
                                out.write(desc[2]);
                                out.write("</i><br/>");
                                anything = true;
                            }
                        } else {
                            matchingTags.put(tag.line, desc);
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            if (hits != null) {
                // @todo verify why we ignore all exceptions?
                LOGGER.log(Level.WARNING, "Could not get context for " + path, e);
            }
        }
    }
    /**
         * Just to get the matching tag send a null in
         */
    if (in == null) {
        return anything;
    }
    int charsRead = 0;
    boolean truncated = false;
    boolean lim = limit;
    if (!RuntimeEnvironment.getInstance().isQuickContextScan()) {
        lim = false;
    }
    if (lim) {
        try {
            charsRead = in.read(buffer);
            if (charsRead == MAXFILEREAD) {
                // we probably only read parts of the file, so set the
                // truncated flag to enable the [all...] link that
                // requests all matches
                truncated = true;
                // characters back)
                for (int i = charsRead - 1; i > charsRead - 100; i--) {
                    if (buffer[i] == '\n') {
                        charsRead = i;
                        break;
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "An error occured while reading data", e);
            return anything;
        }
        if (charsRead == 0) {
            return anything;
        }
        tokens.reInit(buffer, charsRead, out, urlPrefixE + pathE + "#", matchingTags, scopes);
    } else {
        tokens.reInit(in, out, urlPrefixE + pathE + "#", matchingTags, scopes);
    }
    if (hits != null) {
        tokens.setAlt(alt);
        tokens.setHitList(hits);
        tokens.setFilename(path);
    }
    try {
        String token;
        int matchState = LineMatcher.NOT_MATCHED;
        int matchedLines = 0;
        while ((token = tokens.yylex()) != null && (!lim || matchedLines < 10)) {
            for (int i = 0; i < m.length; i++) {
                matchState = m[i].match(token);
                if (matchState == LineMatcher.MATCHED) {
                    if (!isDefSearch) {
                        tokens.printContext();
                    } else if (tokens.tags.containsKey(tokens.markedLine)) {
                        tokens.printContext();
                    }
                    matchedLines++;
                    //out.write("<br> <i>Matched " + token + " maxlines = " + matchedLines + "</i><br>");
                    break;
                } else if (matchState == LineMatcher.WAIT) {
                    tokens.holdOn();
                } else {
                    tokens.neverMind();
                }
            }
        }
        anything = matchedLines > 0;
        tokens.dumpRest();
        if (lim && (truncated || matchedLines == 10) && out != null) {
            out.write("<a href=\"" + Util.URIEncodePath(morePrefix) + pathE + "?" + queryAsURI + "\">[all...]</a>");
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Could not get context for " + path, e);
    } finally {
        IOUtils.close(in);
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Failed to flush stream: ", e);
            }
        }
    }
    return anything;
}
Also used : Hit(org.opensolaris.opengrok.search.Hit) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Definitions(org.opensolaris.opengrok.analysis.Definitions) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class JavaAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     *
     * @throws java.lang.Exception
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/java/Sample.java";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("java testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    // foo, bar, main
    assertEquals(5, scopes.size());
    for (int i = 0; i < 74; ++i) {
        if (i >= 29 && i <= 31) {
            assertEquals("Sample", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i >= 33 && i <= 41) {
            assertEquals("Method", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i == 43) {
            assertEquals("AbstractMethod", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else if (i >= 47 && i <= 56) {
            assertEquals("InnerMethod", scopes.getScope(i).getName());
            assertEquals("Sample.InnerClass", scopes.getScope(i).getNamespace());
        } else if (i >= 60 && i <= 72) {
            assertEquals("main", scopes.getScope(i).getName());
            assertEquals("Sample", scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 4 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class CxxAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/c/sample.cxx";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("cxx testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    System.out.println(path);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    assertEquals(9, scopes.size());
    for (int i = 0; i < 50; ++i) {
        if (i >= 11 && i <= 15) {
            assertEquals("SomeClass", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 17 && i <= 20) {
            assertEquals("~SomeClass", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 22 && i <= 25) {
            assertEquals("MemberFunc", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 27 && i <= 29) {
            assertEquals("operator ++", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 32 && i <= 34) {
            assertEquals("TemplateMember", scopes.getScope(i).getName());
            assertEquals("SomeClass", scopes.getScope(i).getNamespace());
        } else if (i >= 44 && i <= 46) {
            assertEquals("SomeFunc", scopes.getScope(i).getName());
            assertEquals("ns1::NamespacedClass", scopes.getScope(i).getNamespace());
        } else if (i >= 51 && i <= 54) {
            assertEquals("foo", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 59 && i <= 73) {
            assertEquals("bar", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 76 && i <= 87) {
            assertEquals("main", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 5 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class CSharpAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CSharpAnalyzerFactory.
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/csharp/Sample.cs";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("csharp testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    //TODO 5
    assertEquals(4, scopes.size());
    for (int i = 0; i < 41; ++i) {
        if (i >= 10 && i <= 10) {
            assertEquals("M1", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        } else if (i >= 12 && i <= 14) {
            assertEquals("M2", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        } else if (i >= 19 && i <= 25) {
            assertEquals("M3", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass", scopes.getScope(i).getNamespace());
        //TODO add support for generic classes                
        //            } else if (i >= 28 && i <= 30) { 
        //                assertEquals("M4", scopes.getScope(i).name);
        //                assertEquals("MyNamespace.TopClass", scopes.getScope(i).namespace);
        } else if (i >= 34 && i <= 36) {
            assertEquals("M5", scopes.getScope(i).getName());
            assertEquals("MyNamespace.TopClass.InnerClass", scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Aggregations

Scope (org.opensolaris.opengrok.analysis.Scopes.Scope)7 Test (org.junit.Test)5 File (java.io.File)4 StringWriter (java.io.StringWriter)4 Document (org.apache.lucene.document.Document)4 Field (org.apache.lucene.document.Field)4 IndexableField (org.apache.lucene.index.IndexableField)4 Scopes (org.opensolaris.opengrok.analysis.Scopes)4 IOException (java.io.IOException)1 Definitions (org.opensolaris.opengrok.analysis.Definitions)1 Tag (org.opensolaris.opengrok.analysis.Definitions.Tag)1 Hit (org.opensolaris.opengrok.search.Hit)1