private static void autoImport(final PsiFile file, int offset, final Editor editor) {
    final CharSequence text = editor.getDocument().getCharsSequence();
    while (offset > 0 && Character.isJavaIdentifierPart(text.charAt(offset))) offset--;
    if (offset <= 0) return;

    while (offset > 0 && Character.isWhitespace(text.charAt(offset))) offset--;
    if (offset <= 0 || text.charAt(offset) != '.') return;

    offset--;

    while (offset > 0 && Character.isWhitespace(text.charAt(offset))) offset--;
    if (offset <= 0) return;

    PsiJavaCodeReferenceElement element =
        extractReference(
            PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiExpression.class, false));
    if (element == null) return;

    while (true) {
      final PsiJavaCodeReferenceElement qualifier = extractReference(element.getQualifier());
      if (qualifier == null) break;

      element = qualifier;
    }
    if (!(element.getParent() instanceof PsiMethodCallExpression)
        && element.multiResolve(true).length == 0) {
      new ImportClassFix(element).doFix(editor, false, false);
    }
  }
Esempio n. 2
0
  private static void autoImport(
      @NotNull final PsiFile file, int offset, @NotNull final Editor editor) {
    final CharSequence text = editor.getDocument().getCharsSequence();
    while (offset > 0 && Character.isJavaIdentifierPart(text.charAt(offset))) offset--;
    if (offset <= 0) return;

    while (offset > 0 && Character.isWhitespace(text.charAt(offset))) offset--;
    if (offset <= 0 || text.charAt(offset) != '.') return;

    offset--;

    while (offset > 0 && Character.isWhitespace(text.charAt(offset))) offset--;
    if (offset <= 0) return;

    autoImportReference(
        file,
        editor,
        extractReference(
            PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiExpression.class, false)));
  }
    @Nullable
    private static Object getLookupItem(@Nullable final XmlEntityDecl decl) {
      if (decl == null) {
        return null;
      }

      final String name = decl.getName();
      if (name == null) {
        return null;
      }

      final XmlAttributeValue value = decl.getValueElement();
      final ASTNode node = value.getNode();
      if (node != null) {
        final ASTNode[] nodes = node.getChildren(TokenSet.create(XmlTokenType.XML_CHAR_ENTITY_REF));
        if (nodes.length == 1) {
          final String valueText = nodes[0].getText();
          final int i = valueText.indexOf('#');
          if (i > 0) {
            String s = valueText.substring(i + 1);
            if (s.endsWith(";")) {
              s = s.substring(0, s.length() - 1);
            }

            try {
              final int unicodeChar = Integer.valueOf(s).intValue();
              return LookupValueFactory.createLookupValueWithHint(
                  name, null, new String(Character.toChars(unicodeChar)));
            } catch (NumberFormatException e) {
              return null;
            }
          }
        }
      }

      return null;
    }