Java Code Examples for org.netbeans.api.lexer.Token#text()

The following examples show how to use org.netbeans.api.lexer.Token#text() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompletionContextFinder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static CompletionContext getCompletionContextInComment(TokenSequence<PHPTokenId> tokenSeq, final int caretOffset, ParserResult info) {
    Token<PHPTokenId> token = tokenSeq.token();
    CharSequence text = token.text();

    if (text == null || text.length() == 0) {
        return CompletionContext.NONE;
    }

    int offset = caretOffset - tokenSeq.offset() - 1;
    char charAt = 0;

    if (offset > -1) {
        charAt = text.charAt(offset--);
        while (-1 < offset && !Character.isWhitespace(charAt) && charAt != '$') {
            charAt = text.charAt(offset);
            offset--;
        }
    }

    if (offset < text.length() && charAt == '$') {
        return CompletionContext.STRING;
    }
    return CompletionContext.TYPE_NAME;
}
 
Example 2
Source File: SyntaxHighlighting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static String tokenText(Token<?> token) {
    CharSequence text = token.text();
    StringBuilder sb = new StringBuilder(text.length());
    
    for(int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (Character.isISOControl(ch)) {
            switch (ch) {
            case '\n' : sb.append("\\n"); break; //NOI18N
            case '\t' : sb.append("\\t"); break; //NOI18N
            case '\r' : sb.append("\\r"); break; //NOI18N
            default : sb.append("\\").append(Integer.toOctalString(ch)); break; //NOI18N
            }
        } else {
            sb.append(ch);
        }
    }
    
    return sb.toString();
}
 
Example 3
Source File: AttrImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Node getFirstChildLocked(TokenSequence ts) {
    while (ts.moveNext()) {
        Token<XMLTokenId> t = ts.token();
        if (t.id() == XMLTokenId.VALUE) {
            // fuzziness to relax minor tokenization changes
            CharSequence image = t.text();
            if (image.length() == 1) {
                char test = image.charAt(0);
                if (test == '"' || test == '\'') {
                    if (ts.moveNext()) {
                        t = ts.token();
                    } else {
                        return null;
                    }
                }
            }
            
            if (t.id() == XMLTokenId.VALUE) {
                return new TextImpl(syntax, t, ts.offset(), ts.offset() + t.length(), this);
            } else {
                return null;
            }
        }
    }
    return null;
}
 
Example 4
Source File: OJETUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static String getPrefix(OJETContext ojContext, Document document, int offset) {
    TokenHierarchy th = TokenHierarchy.get(document);
    String empty = "";
    switch (ojContext) {
        case DATA_BINDING:
            TokenSequence<KODataBindTokenId> ts = LexerUtils.getTokenSequence(th, offset, KODataBindTokenId.language(), false);
            if (ts != null) {
                int diff = ts.move(offset);
                if (diff == 0 && ts.movePrevious() || ts.moveNext()) {
                    //we are on a token of ko-data-bind token sequence
                    Token<KODataBindTokenId> etoken = ts.token();
                    if (etoken.id() == KODataBindTokenId.KEY) {
                        //ke|
                        CharSequence prefix = diff == 0 ? etoken.text() : etoken.text().subSequence(0, diff);
                        return prefix.toString();
                    }
                }
                break;
            } 
    }
    return empty;
}
 
Example 5
Source File: JavadocBracesMatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean isTag(Token<? extends TokenId> tag) {
    CharSequence s = tag.text();
    int l = s.length();
    
    boolean b = tag.id() == JavadocTokenId.HTML_TAG &&
        l >= 3 &&
        s.charAt(0) == '<' && //NOI18N
        s.charAt(l - 1) == '>'; //NOI18N
    
    if (b) {
        if (s.charAt(1) == '/') { //NOI18N
            b = l >= 4 && Character.isLetterOrDigit(s.charAt(2));
        } else {
            b = Character.isLetterOrDigit(s.charAt(1));
        }
    }
    
    return b;
}
 
Example 6
Source File: CPCssEditorModule.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends Set<OffsetRange>> NodeVisitor<T> getInstantRenamerVisitor(EditorFeatureContext context, T result) {
    TokenSequence<CssTokenId> tokenSequence = context.getTokenSequence();
    int diff = tokenSequence.move(context.getCaretOffset());
    if (diff > 0 && tokenSequence.moveNext() || diff == 0 && tokenSequence.movePrevious()) {
        Token<CssTokenId> token = tokenSequence.token();
        final CharSequence elementName = token.text();
        return new NodeVisitor<T>(result) {
            @Override
            public boolean visit(Node node) {
                switch (node.type()) {
                    case cp_mixin_name:
                    case cp_variable:
                        if (LexerUtils.equals(elementName, node.image(), false, false)) {
                        OffsetRange range = new OffsetRange(node.from(), node.to());
                        getResult().add(range);
                        break;
                    }
                }
                return false;
            }
        };

    }
    return null;
}
 
Example 7
Source File: AssignComments.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int numberOfNL(Token<JavaTokenId> t, int offset) {
    int count = 0;
    CharSequence charSequence = t.text();
    for (int i = 0; i < charSequence.length(); i++) {
        char a = charSequence.charAt(i);
        if ('\n' == a) {
            lastWhiteNewline = offset + i;
            count++;
        }
    }
    if (offset == -1) {
        lastWhiteNewline = -1;
    }
    return count;
}
 
Example 8
Source File: LatteParser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "# {0} - macro name",
    "ERR_UnopenendMacro=Unopenend macro: {0}",
    "# {0} - macro name",
    "ERR_UnclosedMacro=Unclosed macro: {0}"
})
private void processTokenSequence(TokenSequence<?> tokenSequence) {
    while (tokenSequence.moveNext()) {
        Token<LatteMarkupTokenId> token = (Token<LatteMarkupTokenId>) tokenSequence.token();
        LatteMarkupTokenId tokenId = token.id();
        CharSequence tokenText = token.text();
        if (tokenId == LatteMarkupTokenId.T_MACRO_START && mustBeEnded(tokenText)) {
            macros.push(new Macro(token, tokenSequence.offset()));
        } else if (tokenId == LatteMarkupTokenId.T_MACRO_END && mustBeStarted(tokenText)) {
            Macro lastMacro = macros.peek();
            if (lastMacro == null) {
                if (!CLOSING_SIGN.equals(tokenText)) {
                    parserResult.addError(Bundle.ERR_UnopenendMacro(tokenText), tokenSequence.offset(), token.length());
                }
            } else {
                macros.pop();
                if (!lastMacro.endsWith(tokenText)) {
                    parserResult.addError(Bundle.ERR_UnclosedMacro(lastMacro.getName()), lastMacro.getOffset(), lastMacro.getLength());
                    lastMacro = macros.peek();
                    if (lastMacro != null && lastMacro.endsWith(tokenText)) {
                        macros.pop();
                    }
                }
            }
        }
    }
}
 
Example 9
Source File: JavadocCompletionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks special case of empty javadoc <code>/**|&#42;/</code>. 
 * @param token javadoc token
 * @param offset offset <B>INSIDE</B> jvadoc token
 * @return <code>true</code> in case of empty javadoc and the proper position
 */
private static boolean isEmptyJavadoc(Token<JavaTokenId> token, int offset) {
    if (token != null && token.id() == JavaTokenId.JAVADOC_COMMENT) {
        CharSequence text = token.text();
        // check special case /**|*/
        return offset == 3 && "/***/".contentEquals(text); //NOI18N
    }
    return false;
}
 
Example 10
Source File: DeclarationFinderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void logRecursion(TokenSequence<PHPTokenId> ts) {
    CharSequence tokenText = null;
    if (ts != null) {
        Token<PHPTokenId> token = ts.token();
        if (token != null) {
            tokenText = token.text();
        } else {
            tokenText = "Possibly between tokens"; //NOI18N
        }
    }
    LOGGER.log(Level.WARNING, "Stack overflow detection - limit: {0}, token: {1}", new Object[]{RECURSION_LIMIT, tokenText});
}
 
Example 11
Source File: XMLTextUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Handle fuzziness of attribute end detection. Advances the passed TokenSequence
 * to the token <b>after</b> attribute value end delimiter. The delimiter (quote, doublequote)
 * is passed as a parameter. The method returns the token after the attribute value if the delimiter is
 * found and positions the TokenSequence to the returned token. If there's no delimiter,
 * the method returns {@code null} and the TokenSequence position/state is not defined.
 * 
 * @return Token after attribute value or null.
 */
public static Token<XMLTokenId> skipAttributeValue(TokenSequence ts, char delim) {
    boolean ok = true;
    for (; ok; ok = ts.moveNext()) {
        Token<XMLTokenId> next = ts.token();
        CharSequence cs = next.text();
        if (cs.charAt(cs.length() - 1) == delim) {
            ts.moveNext();
            return ts.token();
        }
    }
    return null;
}
 
Example 12
Source File: JavadocCompletionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isWhiteSpace(Token<JavadocTokenId> token) {
    if (token == null || token.id() != JavadocTokenId.OTHER_TEXT) {
        return false;
    }

    CharSequence text = token.text();
    boolean result = !JAVADOC_WHITE_SPACE.matcher(text).find();
    return result;
}
 
Example 13
Source File: JavadocCompletionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean isInsideIndent(Token<JavadocTokenId> token, int offset) {
    int indent = -1;
    if (token.id() == JavadocTokenId.OTHER_TEXT) {
        CharSequence text = token.text();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == '\n') {
                if (i <= offset) {
                    // new line; reset status
                    indent = -1;
                    if (i < offset) {
                        continue;
                    }
                }
                // stop, line inspection is ready
                break;
            } else if (i == 0) {
                // token must start with \n otherwise it is not indentation
                break;
            }

            if (c == '*' && indent < 0) {
                indent = i;
                if (offset <= i) {
                    // stop, offset is inside indentation
                    break;
                }
            }
        }
    }
    return indent >= offset;
}
 
Example 14
Source File: XmlLexerParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void parseTag(Token<XMLTokenId> t) throws SAXException {
    String tagMark = t.text().toString();
    
    selfClosed = false;
    
    // shorten the tagName so it does not contain the <
    tagMark = tagMark.substring(1);
    if (tagMark.endsWith(SELF_CLOSING_TAG_CLOSE)) {
        selfClosed = true;
        tagMark = tagMark.substring(0, tagMark.length() - 2);
    }
    consume();
    qName = tagMark;

    startLevel();
    
    boolean inError = false;

    out:
    while ((t = nextToken()) != null) {
        XMLTokenId id = t.id();
        
        switch (id) {
            case WS:
                consume();
                break;
            case ARGUMENT:
                inError = !parseAttribute(t);
                break;
                
            case TAG: {
                // expect end tag
                CharSequence cs = t.text();
                if (cs.charAt(0) == TAG_START_CHAR) {
                    // some error - bail out
                    inError = true;
                    errorStartTagInTagName();
                    // report tag start as usual
                    break out;
                }
                if (cs.charAt(cs.length() - 1) != TAG_END_CHAR) {
                    // does not begin with tag start, does not end with tag end - how is it
                    // possible it is a tag ?
                    throw new IllegalStateException("Invalid tag text: " + cs); // NOI18N
                } else if (cs.charAt(0) == CLOSE_TAG_CHAR) {
                    selfClosed = true;
                }
                consume();
                break out;
            }
                
            case ERROR:
                markUnexpectedToken();
                consume();
                // try to recover
                inError = true;
                break;
                
            default:
                break out;
        }
    }
    
    boolean close = selfClosed;
    
    if (inError) {
        close |= determineClosedTag();
    }
    
    handleStartElement(close);
}
 
Example 15
Source File: JavaReference.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void insideMember(TokenSequence<JavadocTokenId> jdts) {
    Token<JavadocTokenId> token;
    if (!jdts.moveNext() || JavadocTokenId.IDENT != (token = jdts.token()).id()) {
        return;
    }
    // member identifier
    member = token.text ();
    end = jdts.offset() + token.length();
    
    // params part (int, String)
    if (!jdts.moveNext ()) return;
    token = jdts.token ();
    if (JavadocTokenId.OTHER_TEXT != token.id ()) return;
    CharSequence cs = token.text ();
    if (cs.length () == 0 ||
        cs.charAt (0) != '(') {
        // no params
        return;
    }

    StringBuilder params = new StringBuilder();
    while (jdts.offset() < tagEndPosition) {
        int len = tagEndPosition - jdts.offset();
        cs = len > 0
                ? token.text()
                : token.text().subSequence(0, len);
        if (token.id () == JavadocTokenId.IDENT) {
            JavaReference parameter = JavaReference.resolve (
                jdts,
                jdts.offset(),
                jdts.offset() + len
            );
            if (parameters == null)
                parameters = new ArrayList<JavaReference> ();
            parameters.add (parameter);
            if (parameter.fqn != null) {
                params.append(parameter.fqn);
            } else {
                params.append(cs);
            }
        } else {
            params.append(cs);
        }
        if (params.indexOf (")") > 0)
            break;
        if (!jdts.moveNext()) {
            break;
        }
        token = jdts.token();
    }
    paramsText = parseParamString(params);
}
 
Example 16
Source File: SimplifiedJspServlet.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected void renderProcess() throws BadLocationException{
    //check servlet API on classpath
    if (!isServletAPIOnClasspath()){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayServletAPIMissingWarning();
            }
        });
        processingSuccessful = false;
        return;
    }

    processIncludes(true, null);

    //XXX The InputAttribute from the document are not copied to the following TokenHierarchy,
    //the JspLexer behaviour may seem to be inaccurate in some cases!
    TokenHierarchy<CharSequence> tokenHierarchy = TokenHierarchy.create(charSequence, JspTokenId.language());

    TokenSequence<JspTokenId> tokenSequence = tokenHierarchy.tokenSequence(JspTokenId.language()); //get top level token sequence
    if (!tokenSequence.moveNext()) {
        return; //no tokens in token sequence
    }

    /**
     * process java code blocks one by one
     * note: We count on the fact the scripting language in JSP is Java
     */
    do {
        Token<JspTokenId> token = tokenSequence.token();
        String tokenText = token.text() == null ? "" : CharSequenceUtilities.toString(token.text()).trim(); //NOI18N
        if (token.id() == JspTokenId.SCRIPTLET) {
            int blockStart = token.offset(tokenHierarchy);
            // workaround for #172594
            int blockLength = Math.min(token.length(), snapshot.getText().length() - blockStart);

            JavaCodeType blockType = (JavaCodeType) token.getProperty(JspTokenId.SCRIPTLET_TOKEN_TYPE_PROPERTY);
            List<Embedding> buff = blockType == JavaCodeType.DECLARATION ? declarations : scriptlets;

            if (blockType == JavaCodeType.EXPRESSION) {
                // the "" + (...) construction is used to preserve compatibility with pre-autoboxing java
                // see issue #116598
                buff.add(snapshot.create(String.format("\n\t\tObject expr%1$d = \"\" + (", expressionIndex++), "text/x-java")); //NOI18N
                buff.add(snapshot.create(blockStart, blockLength, "text/x-java"));
                buff.add(snapshot.create(");", "text/x-java")); //NOI18N
            } else {
                boolean unfinishedScriptlet = false;
                if (isUnfinishedScriptletInQueue(tokenSequence)) {
                    // see issue #213963 - we are trying to cut rest of the tag after the caret position
                    int caretOffset = GsfUtilities.getLastKnownCaretOffset(snapshot, null);
                    if (caretOffset - blockStart > 0) {
                        blockLength = Math.min(blockLength, caretOffset - blockStart);
                        unfinishedScriptlet = true;
                    }
                }
                buff.add(snapshot.create(blockStart, blockLength, "text/x-java"));
                //https://netbeans.org/bugzilla/show_bug.cgi?id=231452
                if(unfinishedScriptlet) {
                    buff.add(snapshot.create(" ; ", "text/x-java"));
                }
            }
        } else if (token.id() == JspTokenId.TAG && "include".equals(tokenText)) {
            processIncludes(false, getIncludedPath(tokenSequence));
        }
    } while (tokenSequence.moveNext());

    processJavaInTagValues(tokenSequence); //repositions the tokenSequence


    String extendsClass = null; //NOI18N
    PageInfo pageInfo = getPageInfo();

    if (pageInfo != null) {
        extendsClass = pageInfo.getExtends();
    }

    if (extendsClass == null ||
            // workaround for issue #116314
            "org.apache.jasper.runtime.HttpJspBase".equals(extendsClass)){ //NOI18N
        extendsClass = "HttpServlet"; //NOI18N
    }

    header.add(snapshot.create("\nclass SimplifiedJSPServlet extends ", "text/x-java")); //NOI18N

    if (pageExtends != null){
        header.add(pageExtends);
    } else {
        header.add(snapshot.create(extendsClass, "text/x-java")); //NOI18N
    }

    header.add(snapshot.create(" {\n\tprivate static final long serialVersionUID = 1L;\n", "text/x-java")); //NOI18N

    implicitImports.add(snapshot.create(createImplicitImportStatements(localImportsFound), "text/x-java"));
    beanDeclarations.add(snapshot.create("\n" + createBeanVarDeclarations(localBeansFound), "text/x-java"));
}
 
Example 17
Source File: ExtDocParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static ExtDocComment parseCommentBlock(TokenSequence ts, OffsetRange range) {
    TokenSequence ets = getEmbeddedExtDocTS(ts);

    List<ExtDocElement> sDocElements = new ArrayList<ExtDocElement>();
    StringBuilder sb = new StringBuilder();

    Token<? extends JsDocumentationTokenId> currentToken;
    boolean afterDescriptionEntry = false;

    ExtDocElementType lastType = null;
    int lastOffset = ts.offset();

    while (ets.moveNext()) {
        currentToken = ets.token();
        if (!isCommentImportantToken(currentToken)) {
            continue;
        }

        if (currentToken.id() == JsDocumentationTokenId.KEYWORD || currentToken.id() == JsDocumentationTokenId.COMMENT_END) {
            if (sb.toString().trim().isEmpty()) {
                // simple tag
                if (lastType != null) {
                    sDocElements.add(ExtDocElementUtils.createElementForType(lastType, "", -1));
                }
            } else {
                // store first description in the comment if any
                if (!afterDescriptionEntry) {
                    sDocElements.add(ExtDocDescriptionElement.create(ExtDocElementType.DESCRIPTION, sb.toString().trim()));
                } else {
                    sDocElements.add(ExtDocElementUtils.createElementForType(lastType, sb.toString().trim(), lastOffset));
                }
                sb = new StringBuilder();
            }

            while (ets.moveNext() && ets.token().id() == JsDocumentationTokenId.WHITESPACE) {
                continue;
            }

            lastOffset = ets.offset();
            if (currentToken.id() != JsDocumentationTokenId.COMMENT_END) {
                ets.movePrevious();
            }
            afterDescriptionEntry = true;
            CharSequence text = currentToken.text();
            lastType = ExtDocElementType.fromString(new StringBuilder(text.length()).append(text).toString());
        } else {
            // store all text which appears before next keyword or comment end
            sb.append(currentToken.text());
        }
    }

    return new ExtDocComment(range, sDocElements);
}
 
Example 18
Source File: ExtJsCodeCompletion.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompletionProposal> complete(CodeCompletionContext ccContext, CompletionContext jsCompletionContext, String prefix) {
    if (jsCompletionContext != CompletionContext.OBJECT_PROPERTY_NAME) {
        return Collections.<CompletionProposal>emptyList();
    }
    // find the object that can be configured
    TokenHierarchy<?> th = ccContext.getParserResult().getSnapshot().getTokenHierarchy();
    if (th == null) {
        return Collections.<CompletionProposal>emptyList();
    }
    int carretOffset  = ccContext.getCaretOffset();
    int eOffset = ccContext.getParserResult().getSnapshot().getEmbeddedOffset(carretOffset);
    TokenSequence<? extends JsTokenId> ts = LexUtilities.getJsTokenSequence(th, eOffset);
    if (ts == null) {
        return Collections.<CompletionProposal>emptyList();
    }
    
    ts.move(eOffset);
    
    if (!ts.moveNext() && !ts.movePrevious()){
        return Collections.<CompletionProposal>emptyList();
    }
    
    Token<? extends JsTokenId> token = null;
    JsTokenId tokenId;
    //find the begining of the object literal
    int balance = 1;
    while (ts.movePrevious() && balance > 0) {
        token = ts.token();
        tokenId = token.id();
        if (tokenId == JsTokenId.BRACKET_RIGHT_CURLY) {
            balance++;
        } else if (tokenId == JsTokenId.BRACKET_LEFT_CURLY) {
            balance--;
        }
    }
    if (token == null || balance != 0) {
        return Collections.<CompletionProposal>emptyList();
    }
    
    // now we should be at the beginning of the object literal. 
    token = LexUtilities.findPreviousToken(ts, Arrays.asList(JsTokenId.IDENTIFIER));
    tokenId = token.id();
    StringBuilder sb = new StringBuilder(token.text());
    while ((tokenId == JsTokenId.IDENTIFIER || tokenId == JsTokenId.OPERATOR_DOT) && ts.movePrevious()) {
        token = ts.token(); tokenId = token.id();
        if (tokenId == JsTokenId.OPERATOR_DOT) {
            sb.insert(0, '.'); // NOI18N
        } else if (tokenId == JsTokenId.IDENTIFIER) {
            sb.insert(0, token.text());
        }
    }
    
    String fqn = sb.toString();
    Map<String, Collection<ExtJsDataItem>> data = getData();
    Collection<ExtJsDataItem> items = data.get(fqn);
    int anchorOffset = eOffset - ccContext.getPrefix().length();
    if (items != null) {
        List<CompletionProposal> result = new ArrayList<CompletionProposal>();
        for (ExtJsDataItem item : items) {
            if (item.getName().startsWith(prefix)) {
                result.add(ExtJsCompletionItem.createExtJsItem(item, anchorOffset));
            }
        }
        return result;
    }
    return Collections.<CompletionProposal>emptyList();
}
 
Example 19
Source File: XmlLexerParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "# {0} - attribute name",
    "ERR_DuplicateAttribute=Duplicate attribute: {0}"
})
private boolean parseAttribute(Token<XMLTokenId> t) {
    String argName = t.text().toString();
    boolean ignore;
    
    if (attrs.containsKey(argName)) {
        addError(ERR_DuplicateAttribute, ERR_DuplicateAttribute(argName));
        ignore = true;
    } else {
        attrOffsets.put(argName, currentAttrOffsets = new int[] { seq.offset(), -1, -1, -1 });
        attrNames.add(argName);
        attrs.put(argName, null);
        ignore = false;
    }

    consume();
    skipWhitespace();
    t = nextToken();
    if (t == null || t.id() != XMLTokenId.OPERATOR) {
        markUnexpectedAttrToken();
        return false;
    }
    consume();
    skipWhitespace();
    t = nextToken();
    if (t == null ||t.id() != XMLTokenId.VALUE) {
        markUnexpectedAttrToken();
        return false;
    }
    consume();
    
    CharSequence s = t.text();
    StringBuilder sb = null;
    
    int valStart = seq.offset();
    int valEnd = seq.offset() + t.length();
    char quote = s.charAt(0);
    int end;
    
    t = nextToken();
    while (t != null && (t.id() == XMLTokenId.VALUE || t.id() == XMLTokenId.CHARACTER)) {
        valEnd = seq.offset() + t.length();
        if (sb == null) {
            sb = new StringBuilder();
            sb.append(s.toString());
        }
        sb.append(t.text());
        consume();
        t = nextToken();
    }
    end = valEnd;
    if (sb != null) {
        s = sb;
    }
    if (quote == '\'' || quote == '"') { // NOI18N
        if (s.charAt(s.length() - 1) == quote) {
            valStart++;
            // also handle opening quote with no content (yet)
            if (s.length() > 1) {
                valEnd--;
                s = s.subSequence(1, s.length() - 1);
            } else {
                s = ""; // NOI18N
            }
        } else if (t == null || t.id() == XMLTokenId.ERROR) {
            // strip at least 1st quote in case of an error
            s = s.subSequence(1, s.length());
            valStart++;
        }
    } 
    if (!ignore) {
        attrs.put(argName, s.toString());
        int[] offsets = attrOffsets.get(argName);
        offsets[OFFSET_END] = end;
        offsets[OFFSET_VALUE_START] = valStart;
        offsets[OFFSET_VALUE_END] = valEnd;
    }
    return true;
}
 
Example 20
Source File: JsonOccurrencesFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@CheckForNull
private static Map<OffsetRange, ColoringAttributes> calculateOccurences(
        @NonNull final ParserResult result,
        final int caretPosition,
        boolean includeQuotes,
        @NonNull final AtomicBoolean cancelled) {
    if (cancelled.getAndSet(false)) {
        return null;
    }
    TokenHierarchy<?> th = result.getSnapshot().getTokenHierarchy();
    if (th == null) {
        return null;
    }
    TokenSequence<JsTokenId> ts = th.tokenSequence(JsTokenId.jsonLanguage());
    if (ts == null) {
        return null;
    }
    int offset = result.getSnapshot().getEmbeddedOffset(caretPosition);
    int delta = ts.move(offset);
    if (!ts.moveNext() && !ts.movePrevious()){
        return null;
    }
    final Model model = Model.getModel(result, false);
    if (model == null) {
        return null;
    }
    Token<? extends JsTokenId> token = ts.token();
    JsTokenId tokenId = token.id();
    if (tokenId != JsTokenId.STRING && delta == 0 && ts.movePrevious()) {
        token = ts.token();
        tokenId = token.id();
    }
    ts.movePrevious();
    final Token<? extends JsTokenId> prevToken = LexUtilities.findPreviousNonWsNonComment(ts);
    final JsTokenId prevTokenId = prevToken.id();
    Set<OffsetRange> ranges = new HashSet<>();
    if (tokenId == JsTokenId.STRING && (prevTokenId == JsTokenId.BRACKET_LEFT_CURLY || prevTokenId == JsTokenId.OPERATOR_COMMA)) {
        CharSequence text = token.text();
        findRanges(model.getGlobalObject(), text.subSequence(1, text.length() - 1).toString(), includeQuotes, ranges);
    }
    final Map<OffsetRange, ColoringAttributes> res = new HashMap<>();
    if (cancelled.getAndSet(false)) {
        return null;
    }
    for (OffsetRange offsetRange : ranges) {
        res.put(ModelUtils.documentOffsetRange(result, offsetRange.getStart(), offsetRange.getEnd()), ColoringAttributes.MARK_OCCURRENCES);
    }
    return res;
}