Example usage for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameCOMMENT_LINE

List of usage examples for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameCOMMENT_LINE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameCOMMENT_LINE.

Prototype

int TokenNameCOMMENT_LINE

To view the source code for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameCOMMENT_LINE.

Click Source Link

Usage

From source file:ca.uvic.cs.tagsea.extraction.TagExtractor.java

License:Open Source License

/**
 * Gets the comment regions associated with this document, restricted to the given offset and length, if no comment regions 
 * are found an empty array is returned/*from   w ww .  java2 s  .  c o m*/
 * @param document
 * @param offset
 * @param length
 * @param returnSingleLine Wither to collect single line commments
 * @return Array of tag regions
 */
public static IRegion[] getCommentRegions(IDocument document, int offset, int length) {
    try {
        StringBuffer content = new StringBuffer(document.get(offset, length));
        IRegion range = new Region(offset, length);
        List<IRegion> commentRegions = new ArrayList<IRegion>();
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        scanner.setSource(content.toString().toCharArray());

        int shift = range.getOffset();

        while (true) {
            int terminal = scanner.getNextToken();

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameEOF)
                break;
        }

        IRegion[] result = new IRegion[commentRegions.size()];
        commentRegions.toArray(result);
        return result;

    } catch (BadLocationException e) {
        TagSEAPlugin.log("", e);
    } catch (InvalidInputException e) {
        TagSEAPlugin.log("", e);
    }

    return new IRegion[0];
}

From source file:com.cb.eclipse.folding.java.calculation.CommentHelper.java

License:Open Source License

public void handle(int token, int start, int end, IJavaElement owner) throws JavaModelException {
    switch (token) {

    case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
        handleCommentBlock(start, end);//  ww  w .  j av a  2  s  .  c  o  m
        break;
    case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
        handleJavadoc(start, end);
        break;
    default:
        if (ITerminalSymbols.TokenNameCOMMENT_LINE == token
                && !isUserDefinedSentinel(token, start, end, owner)) {
            handleCommentLine(start, end);
        } else {
            handleNonCommentToken(token);
        }
        break;
    }
}

From source file:com.cb.eclipse.folding.java.calculation.CommentPrefixStrategy.java

License:Open Source License

private boolean representsComment(int token) {
    return (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK)
            || (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC)
            || (token == ITerminalSymbols.TokenNameCOMMENT_LINE);
}

From source file:com.cb.eclipse.folding.java.calculation.UserDefinedStrategy.java

License:Open Source License

public void handle(int nextToken, int start, int end, IJavaElement owner) throws JavaModelException {
    if (settings.isDefaultEnabled()) {
        if (nextToken == ITerminalSymbols.TokenNameCOMMENT_LINE) {
            if (helper.isOpeningSentinel(start, end, owner)) {
                trackers.push(new RegionTracker(null, start));
            } else if (helper.isClosingSentinel(start, end, owner)) {
                int matchedStart;
                if (!trackers.isEmpty()) {
                    RegionTracker tracker = (RegionTracker) trackers.pop();
                    matchedStart = tracker.start;
                    boolean doCollapse = settings.isDefaultCollapsed();
                    EnhancedPosition newPos = new EnhancedPosition(matchedStart, end - matchedStart,
                            new JavaPositionMetadata(true, true, doCollapse, true, null));
                    super.addRegion(newPos);
                }/*from w  ww. j  av a  2 s.c om*/

            }
        }
    }
    if (keepProcessingChild) {
        super.handle(nextToken, start, end, owner);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.Member.java

License:Open Source License

public ISourceRange getJavadocRange() throws JavaModelException {
    ISourceRange range = getSourceRange();
    if (range == null)
        return null;
    IBuffer buf = null;//from w w  w .j  av  a2s  .  c om
    if (isBinary()) {
        buf = getClassFile().getBuffer();
    } else {
        ICompilationUnit compilationUnit = getCompilationUnit();
        if (!compilationUnit.isConsistent()) {
            return null;
        }
        buf = compilationUnit.getBuffer();
    }
    final int start = range.getOffset();
    final int length = range.getLength();
    if (length > 0 && buf.getChar(start) == '/') {
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        try {
            scanner.setSource(buf.getText(start, length).toCharArray());
            int docOffset = -1;
            int docEnd = -1;

            int terminal = scanner.getNextToken();
            loop: while (true) {
                switch (terminal) {
                case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
                    docOffset = scanner.getCurrentTokenStartPosition();
                    docEnd = scanner.getCurrentTokenEndPosition() + 1;
                    terminal = scanner.getNextToken();
                    break;
                case ITerminalSymbols.TokenNameCOMMENT_LINE:
                case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
                    terminal = scanner.getNextToken();
                    continue loop;
                default:
                    break loop;
                }
            }
            if (docOffset != -1) {
                return new SourceRange(docOffset + start, docEnd - docOffset);
            }
        } catch (InvalidInputException ex) {
            // try if there is inherited Javadoc
        } catch (IndexOutOfBoundsException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001
        }
    }
    return null;
}

From source file:com.github.elucash.lambda4jdt.FoldingStructureProvider.java

License:Open Source License

/**
 * Computes the projection ranges for a given <code>ISourceReference</code>. More than one range
 * or none at all may be returned. If there are no foldable regions, an empty array is returned.
 * <p>//from  w  w w .j a  v a 2  s.c  o  m
 * The last region in the returned array (if not empty) describes the region for the java element
 * that implements the source reference. Any preceding regions describe javadoc comments of that
 * java element.
 * </p>
 * @param reference a java element that is a source reference
 * @param ctx the folding context
 * @return the regions to be folded
 */
protected final IRegion[] computeProjectionRanges(ISourceReference reference,
        FoldingStructureComputationContext ctx) {
    try {
        ISourceRange range = reference.getSourceRange();
        if (!SourceRange.isAvailable(range))
            return new IRegion[0];

        String contents = reference.getSource();
        if (contents == null)
            return new IRegion[0];

        List<IRegion> regions = new ArrayList<IRegion>();
        if (!ctx.hasFirstType() && reference instanceof IType) {
            ctx.setFirstType((IType) reference);
            IRegion headerComment = computeHeaderComment(ctx);
            if (headerComment != null) {
                regions.add(headerComment);
                ctx.setHasHeaderComment();
            }
        }

        final int shift = range.getOffset();
        IScanner scanner = ctx.getScanner();
        scanner.resetTo(shift, shift + range.getLength());

        int start = shift;
        while (true) {

            int token = scanner.getNextToken();
            start = scanner.getCurrentTokenStartPosition();

            switch (token) {
            case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
            case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
                int end = scanner.getCurrentTokenEndPosition() + 1;
                regions.add(new Region(start, end - start));
                continue;
            }
            case ITerminalSymbols.TokenNameCOMMENT_LINE:
                continue;
            }

            break;
        }

        regions.add(new Region(start, shift + range.getLength() - start));

        return regions.toArray(new IRegion[regions.size()]);
    } catch (JavaModelException e) {
    } catch (InvalidInputException e) {
    }

    return new IRegion[0];
}

From source file:com.github.elucash.lambda4jdt.FoldingStructureProvider.java

License:Open Source License

private IRegion computeHeaderComment(FoldingStructureComputationContext ctx) throws JavaModelException {
    // search at most up to the first type
    ISourceRange range = ctx.getFirstType().getSourceRange();
    if (range == null)
        return null;
    int start = 0;
    int end = range.getOffset();

    /* code adapted from CommentFormattingStrategy:
     * scan the header content up to the first type. Once a comment is
     * found, accumulate any additional comments up to the stop condition.
     * The stop condition is reaching a package declaration, import container,
     * or the end of the input.//from  www.  j  ava  2 s.  co m
     */
    IScanner scanner = ctx.getScanner();
    scanner.resetTo(start, end);

    int headerStart = -1;
    int headerEnd = -1;
    try {
        boolean foundComment = false;
        int terminal = scanner.getNextToken();
        while (terminal != ITerminalSymbols.TokenNameEOF && !(terminal == ITerminalSymbols.TokenNameclass
                || terminal == ITerminalSymbols.TokenNameinterface || terminal == ITerminalSymbols.TokenNameenum
                || (foundComment && (terminal == ITerminalSymbols.TokenNameimport
                        || terminal == ITerminalSymbols.TokenNamepackage)))) {

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                if (!foundComment)
                    headerStart = scanner.getCurrentTokenStartPosition();
                headerEnd = scanner.getCurrentTokenEndPosition();
                foundComment = true;
            }
            terminal = scanner.getNextToken();
        }

    } catch (InvalidInputException ex) {
        return null;
    }

    if (headerEnd != -1) {
        return new Region(headerStart, headerEnd - headerStart);
    }
    return null;
}

From source file:com.ibm.research.tagging.java.extractor.WaypointDefinitionExtractor.java

License:Open Source License

/**
 * Gets the comment regions associated with this document, restricted to the given offset and length, if no comment regions 
 * are found an empty array is returned/*from  ww  w.j a v a2 s. c  o m*/
 * @param document
 * @param offset
 * @param length
 * @param returnSingleLine Wither to collect single line commments
 * @return Array of tag regions
 */
public static IRegion[] getCommentRegions(IDocument document, int offset, int length) {
    try {
        StringBuffer content = new StringBuffer(document.get(offset, length));
        IRegion range = new Region(offset, length);
        List<IRegion> commentRegions = new ArrayList<IRegion>();
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        scanner.setSource(content.toString().toCharArray());

        int shift = range.getOffset();

        while (true) {
            int terminal = scanner.getNextToken();

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameEOF)
                break;
        }

        IRegion[] result = new IRegion[commentRegions.size()];
        commentRegions.toArray(result);
        return result;

    } catch (BadLocationException e) {
        e.printStackTrace();
    } catch (InvalidInputException e) {
        e.printStackTrace();
    }

    return new IRegion[0];
}

From source file:net.sourceforge.tagsea.java.documents.internal.NewWaypointDefinitionExtractor.java

License:Open Source License

/**
 * Gets the comment regions associated with this document, restricted to the given offset and length, if no comment regions 
 * are found an empty array is returned/*  ww w  .  j  a  v  a 2 s.co  m*/
 * @param document
 * @param offset
 * @param length
 * @param returnSingleLine Wither to collect single line commments
 * @return Array of tag regions
 */
public static IRegion[] getCommentRegions(IDocument document, int offset, int length) {
    try {
        StringBuffer content = new StringBuffer(document.get(offset, length));
        IRegion range = new Region(offset, length);
        List<IRegion> commentRegions = new ArrayList<IRegion>();
        IScanner scanner = ToolFactory.createScanner(true, false, false, false);
        scanner.setSource(content.toString().toCharArray());

        int shift = range.getOffset();

        while (true) {
            int terminal = scanner.getNextToken();

            if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
                    || terminal == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameCOMMENT_LINE) {
                int commentOffset = shift + scanner.getCurrentTokenStartPosition();
                int commentEnd = shift + scanner.getCurrentTokenEndPosition() + 1;
                commentRegions.add(new Region(commentOffset, commentEnd - commentOffset));
            } else if (terminal == ITerminalSymbols.TokenNameEOF)
                break;
        }

        IRegion[] result = new IRegion[commentRegions.size()];
        commentRegions.toArray(result);
        return result;

    } catch (BadLocationException e) {
        //e.printStackTrace();
    } catch (InvalidInputException e) {
        //e.printStackTrace();
    }

    return new IRegion[0];
}

From source file:org.eclipse.mylyn.java.ui.editor.AutoFoldingStructureProvider.java

License:Open Source License

/**
 * Computes the projection ranges for a given <code>IJavaElement</code>.
 * More than one range may be returned if the element has a leading comment
 * which gets folded separately. If there are no foldable regions,
 * <code>null</code> is returned.
 *
 * @param element the java element that can be folded
 * @return the regions to be folded, or <code>null</code> if there are
 *         none//from w w  w .j a v  a2  s.c o m
 */
private IRegion[] computeProjectionRanges(IJavaElement element) {

    try {
        if (element instanceof ISourceReference) {
            ISourceReference reference = (ISourceReference) element;
            ISourceRange range = reference.getSourceRange();

            String contents = reference.getSource();
            if (contents == null)
                return null;

            List regions = new ArrayList();
            if (fFirstType == null && element instanceof IType) {
                fFirstType = (IType) element;
                IRegion headerComment = computeHeaderComment(fFirstType);
                if (headerComment != null) {
                    regions.add(headerComment);
                    fHasHeaderComment = true;
                }
            }

            IScanner scanner = ToolFactory.createScanner(true, false, false, false);
            scanner.setSource(contents.toCharArray());
            final int shift = range.getOffset();
            int start = shift;
            while (true) {

                int token = scanner.getNextToken();
                start = shift + scanner.getCurrentTokenStartPosition();

                switch (token) {
                case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
                case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
                    int end = shift + scanner.getCurrentTokenEndPosition() + 1;
                    regions.add(new Region(start, end - start));
                }
                case ITerminalSymbols.TokenNameCOMMENT_LINE:
                    continue;
                }

                break;
            }

            regions.add(new Region(start, shift + range.getLength() - start));

            if (regions.size() > 0) {
                IRegion[] result = new IRegion[regions.size()];
                regions.toArray(result);
                return result;
            }
        }
    } catch (JavaModelException e) {
        MylarStatusHandler.log(e, "");
    } catch (InvalidInputException e) {
        MylarStatusHandler.log(e, "");
    }

    return null;
}