示例#1
0
 private static void assertTokens(TokenSequence<SQLTokenId> seq, SQLTokenId... ids) {
   if (ids == null) {
     ids = new SQLTokenId[0];
   }
   assertEquals("Wrong token count.", ids.length, seq.tokenCount());
   seq.moveNext();
   for (SQLTokenId id : ids) {
     assertEquals("Wrong token ID at index " + seq.index(), id, seq.token().id());
     seq.moveNext();
   }
 }
  @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;
  }
  public OffsetRange getReferenceSpan(Document doc, int lexOffset) {
    TokenHierarchy<Document> th = TokenHierarchy.get(doc);

    // BaseDocument doc = (BaseDocument)document;

    TokenSequence<? extends PythonTokenId> ts = PythonLexerUtils.getPythonSequence(th, lexOffset);

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

    ts.move(lexOffset);

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

    // Determine whether the caret position is right between two tokens
    boolean isBetween = (lexOffset == ts.offset());

    OffsetRange range = getReferenceSpan(ts, th, lexOffset);

    if ((range == OffsetRange.NONE) && isBetween) {
      // The caret is between two tokens, and the token on the right
      // wasn't linkable. Try on the left instead.
      if (ts.movePrevious()) {
        range = getReferenceSpan(ts, th, lexOffset);
      }
    }

    return range;
  }
 private Token<PHPTokenId> getPhpToken(int offset) {
   TokenSequence<PHPTokenId> phpts = ts.embeddedJoined(PHPTokenId.languageInPHP());
   assertNotNull(phpts);
   phpts.move(offset);
   assertTrue(phpts.moveNext());
   return phpts.token();
 }
 private Token<TplTokenId> getTplToken(int offset) {
   TokenSequence<TplTokenId> htmlts = ts.embeddedJoined(TplTokenId.language());
   assertNotNull(htmlts);
   htmlts.move(offset);
   assertTrue(htmlts.moveNext());
   return htmlts.token();
 }
  @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();
  }
示例#7
0
  public void testEscapeSingleQuote() throws Exception {
    TokenSequence<SQLTokenId> seq = getTokenSequence("'Frank\\'s Book'");
    assertTrue(seq.moveNext());
    assertEquals(SQLTokenId.STRING, seq.token().id());
    assertEquals("'Frank\\'s Book'", seq.token().text().toString());

    seq = getTokenSequence("'Frank\\s Book'");
    assertTrue(seq.moveNext());
    assertEquals(SQLTokenId.STRING, seq.token().id());
    assertEquals("'Frank\\s Book'", seq.token().text().toString());

    seq = getTokenSequence("'Frank\\");
    assertTokens(seq, SQLTokenId.INCOMPLETE_STRING);

    seq = getTokenSequence("'Frank\\'");
    assertTokens(seq, SQLTokenId.INCOMPLETE_STRING);
  }
示例#8
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 "";
 }
  @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);
  }
  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;
  }
 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;
 }
示例#12
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;
 }
示例#13
0
  public void run(RubyRuleContext context, List<Hint> result) {
    Node node = context.node;
    ParserResult info = context.parserResult;

    IfNode ifNode = (IfNode) node;
    if (ifNode.getCondition() == null) {
      // Can happen for this code:
      //   if ()
      //   end
      // (typically while editing)
      return;
    }
    Node body = ifNode.getThenBody();
    Node elseNode = ifNode.getElseBody();

    if (body != null && elseNode != null) {
      // Can't convert if-then-else conditionals
      return;
    }

    if (body == null && elseNode == null) {
      // Can't convert empty conditions
      return;
    }

    // Can't convert if !x/elseif blocks
    if (ifNode.getElseBody() != null && ifNode.getElseBody().getNodeType() == NodeType.IFNODE) {
      return;
    }

    int start = ifNode.getPosition().getStartOffset();
    if (!RubyHints.isNullOrInvisible(body)
        && (
        // Can't convert blocks with multiple statements
        body.getNodeType() == NodeType.BLOCKNODE
            ||
            // Already a statement modifier?
            body.getPosition().getStartOffset() <= start)) {
      return;
    } else if (!RubyHints.isNullOrInvisible(elseNode)
        && (elseNode.getNodeType() == NodeType.BLOCKNODE
            || elseNode.getPosition().getStartOffset() <= start)) {
      return;
    }

    BaseDocument doc = context.doc;
    try {
      int keywordOffset = ConvertIfToUnless.findKeywordOffset(context, ifNode);
      if (keywordOffset == -1 || keywordOffset > doc.getLength() - 1) {
        return;
      }

      char k = doc.getText(keywordOffset, 1).charAt(0);
      if (!(k == 'i' || k == 'u')) {
        return; // Probably ternary operator, ?:
      }
    } catch (BadLocationException ble) {
      Exceptions.printStackTrace(ble);
    }

    // If statement that is not already a statement modifier
    OffsetRange range = AstUtilities.getRange(node);

    if (RubyUtils.isRhtmlDocument(doc) || RubyUtils.isYamlDocument(doc)) {
      // Make sure that we're in a single contiguous Ruby section; if not, this won't work
      range = LexUtilities.getLexerOffsets(info, range);
      if (range == OffsetRange.NONE) {
        return;
      }

      try {
        doc.readLock();
        TokenHierarchy th = TokenHierarchy.get(doc);
        TokenSequence ts = th.tokenSequence();
        ts.move(range.getStart());
        if (!ts.moveNext() && !ts.movePrevious()) {
          return;
        }

        if (ts.offset() + ts.token().length() < range.getEnd()) {
          return;
        }
      } finally {
        doc.readUnlock();
      }
    }

    ConvertToModifier fix = new ConvertToModifier(context, ifNode);

    if (fix.getEditList() == null) {
      return;
    }

    List<HintFix> fixes = Collections.<HintFix>singletonList(fix);

    String displayName = NbBundle.getMessage(ConvertConditionals.class, "ConvertConditionals");
    Hint desc = new Hint(this, displayName, RubyUtils.getFileObject(info), range, fixes, 500);
    result.add(desc);
  }
 private Token<TplTopTokenId> getTplTopToken(int offset) {
   ts.move(offset);
   assertTrue(ts.moveNext());
   return ts.token();
 }