Ejemplo n.º 1
0
 void checkQtObject(Token<CppTokenId> token) {
   if (!isQtObject) {
     isQtObject =
         token.id() == IDENTIFIER
             && CharSequenceUtilities.equals(token.text(), "Q_OBJECT"); // NOI18N
   }
 }
Ejemplo n.º 2
0
  @Override
  public List<Embedding> getEmbeddings() {
    // Initializes template counters.
    TemplateResolver.init();
    // TODO: neprochazet celou sekvenci (ale par radku pred a po caret)
    // jestli je to vubec mozny...
    TokenSequence<LatteTopTokenId> sequence =
        LexUtils.getTopSequence(getSnapshot().getText().toString());

    sequence.moveStart();

    LatteResolver latteResolver = new LatteResolver(this);
    HtmlPhpResolver htmlPhpResolver = new HtmlPhpResolver(this);

    while (sequence.moveNext()) {
      Token t = sequence.token();
      if (t.id() == LatteTopTokenId.LATTE) {
        latteResolver.solve(t, sequence); // deals with all latte macros
        SyntaxUtils.findArrayForHint(getSnapshot().getSource().getDocument(false), sequence);
      } else {
        htmlPhpResolver.solve(t, sequence);
      }
    }

    return super.getEmbeddings();
  }
Ejemplo n.º 3
0
 @Override
 protected boolean isStableFormattingStartToken(
     Token<JspTokenId> token, JoinedTokenSequence<JspTokenId> ts) {
   return token.id() == JspTokenId.SYMBOL2
       && (token.text().toString().equals("<%")
           || token.text().toString().equals("<%=")
           || token.text().toString().equals("<%!"));
 }
Ejemplo n.º 4
0
 @Override
 protected boolean isPreservedLine(
     Token<JspTokenId> token, IndenterContextData<JspTokenId> context) {
   String text = token.text().toString().trim();
   if (token.id() == JspTokenId.COMMENT) {
     if (!text.startsWith("<%--") && !text.startsWith("--%>")) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 5
0
  @NbBundle.Messages({
    "# {0} - PI target",
    "ERR_invalidProcessingInstruction=Invalid processing instruction: {0}. Expected 'import', 'include' or 'language'",
    "ERR_missingProcessingInstruction=Missing processing intruction."
  })
  private void handleErrorInstruction(String target, String data) {
    int start = contentLocator.getElementOffset();
    int offset = -1;
    int piOffset = -1;

    TokenSequence<XMLTokenId> seq = contentLocator.getTokenSequence();

    // lex up to the invalid target:
    seq.move(start);
    boolean found = false;
    while (!found && seq.moveNext()) {
      Token<XMLTokenId> t = seq.token();
      switch (t.id()) {
        case PI_START:
          piOffset = offset;
          if (target == null) {
            found = true;
          }
        case WS:
          break;

        default:
        case PI_TARGET:
          offset = seq.offset();
          found = true;
          break;
      }
    }
    ErrorMark mark;

    if (target != null) {
      mark =
          new ErrorMark(
              offset,
              seq.token().length(),
              "invalid-processing-instruction",
              ERR_invalidProcessingInstruction(target),
              target);
    } else {
      mark =
          new ErrorMark(
              piOffset,
              seq.token().length(),
              "missing-processing-instruction",
              ERR_missingProcessingInstruction());
    }
    addError(mark);
  }
  @SuppressWarnings("empty-statement")
  private DeclarationLocation findUrl(PythonParserResult info, Document doc, int lexOffset) {
    TokenSequence<?> ts = PythonLexerUtils.getPythonSequence((BaseDocument) doc, lexOffset);

    if (ts == null) {
      return DeclarationLocation.NONE;
    }

    ts.move(lexOffset);

    if (!ts.moveNext() && !ts.movePrevious()) {
      return DeclarationLocation.NONE;
    }

    Token<?> token = ts.token();

    TokenSequence<?> embedded = ts.embedded();

    if (embedded != null) {
      ts = embedded;

      embedded.move(lexOffset);

      if (!embedded.moveNext() && !embedded.movePrevious()) {
        return DeclarationLocation.NONE;
      }

      token = embedded.token();
    }

    // Is this a comment? If so, possibly do rdoc-method reference jump
    if ((token != null) && (token.id() == PythonStringTokenId.URL)) {
      // TODO - use findLinkedMethod
      String method = token.text().toString();
      if (method.startsWith("www.")) { // NOI18N
        method = "http://" + method; // NOI18N
      }

      // A URL such as http://netbeans.org - try to open it in a browser!
      try {
        URL url = new URL(method);

        return new DeclarationLocation(url);
      } catch (MalformedURLException mue) {
        // URL is from user source... don't complain with exception dialogs etc.
        ;
      }
    }

    return DeclarationLocation.NONE;
  }
  private void validateChangePoints(Collection<Token> changePoints, int... origs) {
    Set<Pair> awaited = new HashSet<Pair>();

    for (int cntr = 0; cntr < origs.length; cntr += 2) {
      awaited.add(new Pair(origs[cntr], origs[cntr + 1]));
    }

    Set<Pair> got = new HashSet<Pair>();

    for (Token<JavaTokenId> h : changePoints) {
      got.add(new Pair(h.offset(null), h.offset(null) + h.length()));
    }

    assertEquals(awaited, got);
  }
Ejemplo n.º 8
0
  @Override
  protected int getFormatStableStart(
      JoinedTokenSequence<TplTopTokenId> ts,
      int startOffset,
      int endOffset,
      AbstractIndenter.OffsetRanges rangesToIgnore) {
    // start from the end offset to properly calculate braces balance and
    // find correfct formatting start (consider case of a rule defined
    // within a media rule):
    ts.move(endOffset, false);

    if (!ts.moveNext() && !ts.movePrevious()) {
      return LexUtilities.getTokenSequenceStartOffset(ts);
    }

    int balance = 0;
    // Look backwards to find a suitable context - beginning of a rule
    do {
      Token<TplTopTokenId> token = ts.token();
      TokenId id = token.id();

      if (id == TplTopTokenId.T_SMARTY && ts.offset() < startOffset && balance == 0) {
        int[] index = ts.index();
        ts.moveNext();
        Token tk = LexUtilities.findNext(ts, Arrays.asList(TplTopTokenId.T_SMARTY));
        ts.moveIndex(index);
        ts.moveNext();
        if (tk != null && tk.id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
          if (ts.movePrevious()) {
            tk = LexUtilities.findPrevious(ts, Arrays.asList(TplTopTokenId.T_SMARTY));
            if (tk != null) {
              ts.moveNext();
              tk = LexUtilities.findNext(ts, Arrays.asList(TplTopTokenId.T_SMARTY));
            }
          }
          return ts.offset();
        }
      } else if (id == TplTopTokenId.T_SMARTY_CLOSE_DELIMITER) {
        balance++;
      } else if (id == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
        balance--;
      } else if (id == TplTopTokenId.T_SMARTY && ts.offset() < startOffset && balance == 0) {
        return ts.offset();
      }
    } while (ts.movePrevious());

    return LexUtilities.getTokenSequenceStartOffset(ts);
  }
 public boolean verifyState(Document doc, int offset) {
   TokenHierarchy hi = TokenHierarchy.get(doc);
   TokenSequence<HTMLTokenId> ts = hi.tokenSequence(HTMLTokenId.language());
   if (ts != null) {
     ts.move(offset);
     ts.moveNext();
     Token<HTMLTokenId> tok = ts.token();
     int newOffset = ts.offset();
     String matcherText = tok.text().toString();
     Matcher m = MY_SPECIAL_PATTERN.matcher(matcherText);
     if (m.matches()) {
       target = m.group(1);
       int idx = matcherText.indexOf(target);
       targetStart = newOffset + idx;
       targetEnd = targetStart + target.length();
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 10
0
 private String getFunctionalTplTokenId(Token<TplTopTokenId> token) {
   TokenHierarchy<CharSequence> th = TokenHierarchy.create(token.text(), TplTokenId.language());
   TokenSequence<TplTokenId> sequence = th.tokenSequence(TplTokenId.language());
   while (sequence.moveNext()) {
     if (sequence.token().id() == TplTokenId.WHITESPACE) {
       continue;
     } else {
       return sequence.token().toString();
     }
   }
   return "";
 }
Ejemplo n.º 11
0
 private static CharSequence dumpTokens(TokenSequence<?> seq) {
   seq.moveStart();
   StringBuilder builder = new StringBuilder();
   Token<?> token = null;
   while (seq.moveNext()) {
     if (token != null) {
       builder.append('\n');
     }
     token = seq.token();
     builder.append(token.id());
     PartType part = token.partType();
     if (part != PartType.COMPLETE) {
       builder.append(' ');
       builder.append(token.partType());
     }
     builder.append(' ');
     builder.append('\'');
     builder.append(token.text());
     builder.append('\'');
   }
   return builder;
 }
  private OffsetRange getReferenceSpan(
      TokenSequence<?> ts, TokenHierarchy<Document> th, int lexOffset) {
    Token<?> token = ts.token();
    TokenId id = token.id();

    //        if (id == PythonTokenId.IDENTIFIER) {
    //            if (token.length() == 1 && id == PythonTokenId.IDENTIFIER &&
    // token.text().toString().equals(",")) {
    //                return OffsetRange.NONE;
    //            }
    //        }

    // TODO: Tokens.SUPER, Tokens.THIS, Tokens.SELF ...
    if (id == PythonTokenId.IDENTIFIER) {
      return new OffsetRange(ts.offset(), ts.offset() + token.length());
    }

    // Look for embedded RDoc comments:
    TokenSequence<?> embedded = ts.embedded();

    if (embedded != null) {
      ts = embedded;
      embedded.move(lexOffset);

      if (embedded.moveNext()) {
        Token<?> embeddedToken = embedded.token();

        if (embeddedToken.id() == PythonStringTokenId.URL) {
          return new OffsetRange(embedded.offset(), embedded.offset() + embeddedToken.length());
        }
        // Recurse into the range - perhaps there is Ruby code (identifiers

        // etc.) to follow there
        OffsetRange range = getReferenceSpan(embedded, th, lexOffset);

        if (range != OffsetRange.NONE) {
          return range;
        }
      }
    }

    return OffsetRange.NONE;
  }
Ejemplo n.º 13
0
 @Override
 protected boolean isEndTagSymbol(Token<JspTokenId> token) {
   return token.id() == JspTokenId.SYMBOL && token.text().toString().equals(">");
 }
Ejemplo n.º 14
0
 @Override
 protected boolean isStartTagSymbol(Token<JspTokenId> token) {
   return token.id() == JspTokenId.SYMBOL
       && (token.text().toString().equals("<") || token.text().toString().equals("<%@"));
 }
Ejemplo n.º 15
0
 @Override
 protected boolean isStartTagClosingSymbol(Token<JspTokenId> token) {
   return token.id() == JspTokenId.SYMBOL && token.text().toString().equals("</");
 }
Ejemplo n.º 16
0
 @Override
 protected boolean isOpenTagNameToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.TAG;
 }
Ejemplo n.º 17
0
 @Override
 protected boolean isCloseTagNameToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.ENDTAG;
 }
Ejemplo n.º 18
0
  @Override
  protected List<IndentCommand> getLineIndent(
      IndenterContextData<TplTopTokenId> context, List<IndentCommand> preliminaryNextLineIndent)
      throws BadLocationException {
    Stack<TplStackItem> blockStack = getStack();
    List<IndentCommand> iis = new ArrayList<IndentCommand>();
    getIndentFromState(iis, true, context.getLineStartOffset());

    JoinedTokenSequence<TplTopTokenId> ts = context.getJoinedTokenSequences();
    ts.move(context.getLineStartOffset());

    boolean isSmartyBodyCommand = false;
    boolean isSmartyElseCommand = false;
    boolean afterDelimiter = false;
    int embeddingLevel = 0;
    String lastTplCommand = "";
    // iterate over tokens on the line and push to stack any changes
    while (!context.isBlankLine()
        && ts.moveNext()
        && ((ts.isCurrentTokenSequenceVirtual() && ts.offset() < context.getLineEndOffset())
            || ts.offset() <= context.getLineEndOffset())) {
      Token<TplTopTokenId> token = ts.token();
      if (token == null) {
        continue;
      } else if (ts.embedded() != null) {
        // indent for smarty command of the zero embedding level
        if (embeddingLevel == 1 && afterDelimiter) {
          if (token.id() == TplTopTokenId.T_SMARTY && context.isIndentThisLine()) {
            String tplToken = getFunctionalTplTokenId(token);
            isSmartyBodyCommand = isBodyCommand(tplToken, context);
            if (isSmartyBodyCommand) {
              lastTplCommand = tplToken;
              isSmartyElseCommand = isElseCommand(tplToken);
            }
          } else {
            isSmartyBodyCommand = false;
            isSmartyElseCommand = false;
          }
        }
        continue;
      }

      if (token.id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
        afterDelimiter = true;
        embeddingLevel++;
        TplStackItem state = new TplStackItem(StackItemState.IN_RULE);
        blockStack.push(state);
      } else if (token.id() == TplTopTokenId.T_SMARTY_CLOSE_DELIMITER) {
        afterDelimiter = false;
        if (isInState(blockStack, StackItemState.IN_RULE)) {
          // check that IN_RULE is the last state
          TplStackItem item = blockStack.pop();
          embeddingLevel--;
          if (embeddingLevel == 0) {
            assert item.state == StackItemState.IN_RULE;
            if (isSmartyBodyCommand) {
              if (!blockStack.isEmpty()
                  && isInRelatedCommand(lastTplCommand, blockStack.peek().getCommand())) {
                if (isSmartyElseCommand) {
                  String command = blockStack.pop().command;
                  blockStack.push(new TplStackItem(StackItemState.IN_BODY, command));
                } else {
                  blockStack.pop();
                }
                iis.add(new IndentCommand(IndentCommand.Type.RETURN, preservedLineIndentation));
              } else {
                blockStack.push(new TplStackItem(StackItemState.IN_BODY, lastTplCommand));
              }
            }
          }
        }
      } else if (isCommentToken(token)) {
        int start = context.getLineStartOffset();
        if (start < ts.offset()) {
          start = ts.offset();
        }
        int commentEndOffset = ts.offset() + ts.token().text().toString().trim().length() - 1;
        int end = context.getLineEndOffset();
        if (end > commentEndOffset) {
          end = commentEndOffset;
        }
        if (start > end) {
          // do nothing
        } else if (start == ts.offset()) {
          if (end < commentEndOffset) {
            // if comment ends on next line put formatter to IN_COMMENT state
            int lineStart = Utilities.getRowStart(getDocument(), ts.offset());
            preservedLineIndentation = start - lineStart;
          }
        } else if (end == commentEndOffset) {
          String text = getDocument().getText(start, end - start + 1).trim();
          if (!text.startsWith("*/")) {
            // if line does not start with '*/' then treat it as unformattable
            IndentCommand ic =
                new IndentCommand(
                    IndentCommand.Type.PRESERVE_INDENTATION, context.getLineStartOffset());
            ic.setFixedIndentSize(preservedLineIndentation);
            iis.add(ic);
          }
          preservedLineIndentation = -1;
        } else {
          IndentCommand ic =
              new IndentCommand(
                  IndentCommand.Type.PRESERVE_INDENTATION, context.getLineStartOffset());
          ic.setFixedIndentSize(preservedLineIndentation);
          iis.add(ic);
        }
      }
    }
    if (context.isBlankLine() && iis.isEmpty()) {
      IndentCommand ic =
          new IndentCommand(IndentCommand.Type.PRESERVE_INDENTATION, context.getLineStartOffset());
      ic.setFixedIndentSize(preservedLineIndentation);
      iis.add(ic);
    }

    if (iis.isEmpty()) {
      iis.add(new IndentCommand(IndentCommand.Type.NO_CHANGE, context.getLineStartOffset()));
    }

    if (context.getNextLineStartOffset() != -1) {
      getIndentFromState(preliminaryNextLineIndent, false, context.getNextLineStartOffset());
      if (preliminaryNextLineIndent.isEmpty()) {
        preliminaryNextLineIndent.add(
            new IndentCommand(IndentCommand.Type.NO_CHANGE, context.getNextLineStartOffset()));
      }
    }

    return iis;
  }
Ejemplo n.º 19
0
 @Override
 protected boolean isWhiteSpaceToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.WHITESPACE
       || (token.id() == JspTokenId.TEXT && token.text().toString().trim().length() == 0);
 }
  private DeclarationLocation findImport(PythonParserResult info, int lexOffset, BaseDocument doc) {
    TokenSequence<? extends PythonTokenId> ts =
        PythonLexerUtils.getPositionedSequence(doc, lexOffset);
    if (ts == null) {
      return DeclarationLocation.NONE;
    }
    if (ts.offset() == lexOffset) {
      // We're looking at the offset to the RIGHT of the caret
      // and here I care about what's on the left
      if (!ts.movePrevious()) {
        return DeclarationLocation.NONE;
      }
    }

    Token<? extends PythonTokenId> token = ts.token();
    if (token == null) {
      return DeclarationLocation.NONE;
    }

    TokenId id = token.id();

    String moduleName = null;
    while (true) {
      if (id == PythonTokenId.IDENTIFIER || id.primaryCategory().equals(PythonLexer.KEYWORD_CAT)) {
        // Possibly inside the import string
        String tokenText = token.text().toString();
        if (moduleName == null) {
          moduleName = tokenText;
        } else {
          moduleName = tokenText + "." + moduleName;
        }
      } else if (id != PythonTokenId.DOT) {
        break;
      }
      if (!ts.movePrevious()) {
        return DeclarationLocation.NONE;
      }
      token = ts.token();
      id = token.id();
    }

    if (id != PythonTokenId.ERROR
        && id != PythonTokenId.NEWLINE
        && id != PythonTokenId.WHITESPACE) {
      return DeclarationLocation.NONE;
    }

    if (!ts.movePrevious()) {
      return DeclarationLocation.NONE;
    }
    token = ts.token();
    id = token.id();
    if (id != PythonTokenId.IMPORT) {
      return DeclarationLocation.NONE;
    }
    if (moduleName == null) {
      return DeclarationLocation.NONE;
    }

    if (id == PythonTokenId.IMPORT || id == PythonTokenId.FROM) {
      if (id == PythonTokenId.IMPORT
          && ts.movePrevious()
          && ts.token().id() == PythonTokenId.WHITESPACE
          && ts.movePrevious()) {
        // See if this was "from foo import bar" such that we really should
        // be listing symbols inside the foo library
        token = ts.token();
        id = token.id();
        String library = null;
        while (true) {
          if (id == PythonTokenId.IDENTIFIER
              || id.primaryCategory().equals(PythonLexer.KEYWORD_CAT)) {
            // Possibly inside the import string
            String tokenText = token.text().toString();
            if (library == null) {
              library = tokenText;
            } else {
              library = tokenText + "." + library;
            }
          } else if (id != PythonTokenId.DOT) {
            break;
          }
          if (!ts.movePrevious()) {
            return DeclarationLocation.NONE;
          }
          token = ts.token();
          id = token.id();
        }
        if (library != null) {
          if (id == PythonTokenId.WHITESPACE
              && ts.movePrevious()
              && ts.token().id() == PythonTokenId.FROM) {
            return findImport(info, library, moduleName);
          }
        }
      }

      return findImport(info, moduleName, null);
    }

    return DeclarationLocation.NONE;
  }
Ejemplo n.º 21
0
 @Override
 protected boolean isEndTagClosingSymbol(Token<JspTokenId> token) {
   return token.id() == JspTokenId.SYMBOL
       && (token.text().toString().equals("/>") || token.text().toString().equals("%>"));
 }
Ejemplo n.º 22
0
        @SuppressWarnings("unchecked")
        @Override
        protected LanguageEmbedding embedding(
            Token<HTMLTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
          if (LOG) {
            LOGGER.log(
                Level.FINE,
                String.format(
                    "HTMLTokenId$Language<HTMLTokenId>.embedding(...) called on removed token with %s token id. LanguagePath: %s",
                    token.id(), languagePath),
                new IllegalStateException()); // NOI18N
          }

          String mimeType = null;
          switch (token.id()) {
              // BEGIN TOR MODIFICATIONS
            case VALUE_JAVASCRIPT:
              mimeType = JAVASCRIPT_MIMETYPE;
              if (mimeType != null) {
                Language lang = Language.find(mimeType);
                if (lang == null) {
                  return null; // no language found
                } else {
                  // TODO:
                  // XXX Don't handle JavaScript for non-quoted attributes
                  // (Or use separate state so I can do 0,0 as offsets there

                  // Marek: AFAIK value of the onSomething methods is always javascript
                  // so having the attribute unqouted doesn't make much sense -  the html spec
                  // allows only a-zA-Z characters in unqouted values so I belive
                  // it is not possible to write reasonable js code - it ususally
                  // contains some whitespaces, brackets, quotations etc.

                  PartType ptype = token.partType();
                  int startSkipLength =
                      ptype == PartType.COMPLETE || ptype == PartType.START ? 1 : 0;
                  int endSkipLength = ptype == PartType.COMPLETE || ptype == PartType.END ? 1 : 0;
                  // do not join css code sections in attribute value between each other,
                  // only token parts inside one value
                  return LanguageEmbedding.create(
                      lang,
                      startSkipLength,
                      endSkipLength,
                      (ptype == PartType.END || ptype == PartType.COMPLETE) ? false : true);
                }
              }
              break;
              // END TOR MODIFICATIONS
            case VALUE_CSS:
              mimeType = STYLE_MIMETYPE;
              if (mimeType != null) {
                Language lang = Language.find(mimeType);
                if (lang == null) {
                  return null; // no language found
                } else {
                  PartType ptype = token.partType();
                  if (token.isRemoved()) {
                    // strange, but sometimes the embedding is requested on removed token which has
                    // null image
                    return null;
                  }
                  char firstChar = token.text().charAt(0);
                  boolean quoted = firstChar == '\'' || firstChar == '"';
                  int startSkipLength =
                      quoted && (ptype == PartType.COMPLETE || ptype == PartType.START) ? 1 : 0;
                  int endSkipLength =
                      quoted && (ptype == PartType.COMPLETE || ptype == PartType.END) ? 1 : 0;

                  // do not join css code sections in attribute value between each other,
                  // only token parts inside one value
                  return LanguageEmbedding.create(
                      lang,
                      startSkipLength,
                      endSkipLength,
                      (ptype == PartType.END || ptype == PartType.COMPLETE) ? false : true);
                }
              }
              break;
            case SCRIPT:
              mimeType = JAVASCRIPT_MIMETYPE;
              break;
            case STYLE:
              mimeType = STYLE_MIMETYPE;
              break;
          }
          if (mimeType != null) {
            Language lang = Language.find(mimeType);
            if (lang == null) {
              return null; // no language found
            } else {
              return LanguageEmbedding.create(lang, 0, 0, true);
            }
          }
          return null;
        }
Ejemplo n.º 23
0
 @Override
 protected boolean isBlockCommentToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.COMMENT;
 }
Ejemplo n.º 24
0
 @Override
 protected boolean isTagArgumentToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.ATTRIBUTE;
 }
Ejemplo n.º 25
0
 @Override
 protected boolean isForeignLanguageEndToken(
     Token<JspTokenId> token, JoinedTokenSequence<JspTokenId> ts) {
   return token.id() == JspTokenId.SYMBOL2 && token.text().toString().equals("%>");
 }
Ejemplo n.º 26
0
 boolean isSlots(Token<CppTokenId> token) {
   return token.id() == IDENTIFIER
       && CharSequenceUtilities.equals(token.text(), "slots"); // NOI18N
 }
Ejemplo n.º 27
0
 @Override
 protected boolean isTagContentToken(Token<JspTokenId> token) {
   return token.id() == JspTokenId.TEXT;
 }
Ejemplo n.º 28
0
 private boolean isCommentToken(Token<TplTopTokenId> token) {
   return token.id() == TplTopTokenId.T_COMMENT;
 }