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); } }
private static String[] getSuggestionsByValue(final String stringValue) { List<String> result = new ArrayList<String>(); StringBuffer currentWord = new StringBuffer(); boolean prevIsUpperCase = false; for (int i = 0; i < stringValue.length(); i++) { final char c = stringValue.charAt(i); if (Character.isUpperCase(c)) { if (currentWord.length() > 0 && !prevIsUpperCase) { result.add(currentWord.toString()); currentWord = new StringBuffer(); } currentWord.append(c); } else if (Character.isLowerCase(c)) { currentWord.append(Character.toUpperCase(c)); } else if (Character.isJavaIdentifierPart(c) && c != '_') { if (Character.isJavaIdentifierStart(c) || currentWord.length() > 0 || !result.isEmpty()) { currentWord.append(c); } } else { if (currentWord.length() > 0) { result.add(currentWord.toString()); currentWord = new StringBuffer(); } } prevIsUpperCase = Character.isUpperCase(c); } if (currentWord.length() > 0) { result.add(currentWord.toString()); } return ArrayUtil.toStringArray(result); }
public static RangeMarker insertTemporary( final int endOffset, final Document document, final String temporary) { final CharSequence chars = document.getCharsSequence(); final int length = chars.length(); final RangeMarker toDelete; if (endOffset < length && Character.isJavaIdentifierPart(chars.charAt(endOffset))) { document.insertString(endOffset, temporary); toDelete = document.createRangeMarker(endOffset, endOffset + 1); } else if (endOffset >= length) { toDelete = document.createRangeMarker(length, length); } else { toDelete = document.createRangeMarker(endOffset, endOffset); } toDelete.setGreedyToLeft(true); toDelete.setGreedyToRight(true); return toDelete; }
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))); }