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 rTrim(String text) { int idx = text.length(); while (idx > 0) { if (!Character.isWhitespace(text.charAt(idx - 1))) break; idx--; } return text.substring(0, idx); }
private static int getNewIndent(final PsiFile psiFile, final int firstWhitespace) { final Document document = psiFile.getViewProvider().getDocument(); final int startOffset = document.getLineStartOffset(document.getLineNumber(firstWhitespace)); int endOffset = startOffset; final CharSequence charsSequence = document.getCharsSequence(); while (Character.isWhitespace(charsSequence.charAt(endOffset++))) ; final String newIndentStr = charsSequence.subSequence(startOffset, endOffset - 1).toString(); return IndentHelperImpl.getIndent( psiFile.getProject(), psiFile.getFileType(), newIndentStr, true); }
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))); }
protected void reparse() { String str = myPathStringNonTrimmed; final List<FileReference> referencesList = new ArrayList<FileReference>(); String separatorString = getSeparatorString(); // separator's length can be more then 1 char int sepLen = separatorString.length(); int currentSlash = -sepLen; // skip white space while (currentSlash + sepLen < str.length() && Character.isWhitespace(str.charAt(currentSlash + sepLen))) { currentSlash++; } if (currentSlash + sepLen + sepLen < str.length() && str.substring(currentSlash + sepLen, currentSlash + sepLen + sepLen) .equals(separatorString)) { currentSlash += sepLen; } int index = 0; if (str.equals(separatorString)) { final FileReference fileReference = createFileReference( new TextRange(myStartInElement, myStartInElement + sepLen), index++, separatorString); referencesList.add(fileReference); } while (true) { final int nextSlash = str.indexOf(separatorString, currentSlash + sepLen); final String subreferenceText = nextSlash > 0 ? str.substring(currentSlash + sepLen, nextSlash) : str.substring(currentSlash + sepLen); final FileReference ref = createFileReference( new TextRange( myStartInElement + currentSlash + sepLen, myStartInElement + (nextSlash > 0 ? nextSlash : str.length())), index++, subreferenceText); referencesList.add(ref); if ((currentSlash = nextSlash) < 0) { break; } } myReferences = referencesList.toArray(new FileReference[referencesList.size()]); }
protected static String findPrefixDefault( final PsiElement insertedElement, final int offset, @NotNull final ElementPattern trimStart) { String substr = insertedElement .getText() .substring(0, offset - insertedElement.getTextRange().getStartOffset()); if (substr.length() == 0 || Character.isWhitespace(substr.charAt(substr.length() - 1))) return ""; substr = substr.trim(); int i = 0; while (substr.length() > i && trimStart.accepts(substr.charAt(i))) i++; return substr.substring(i).trim(); }