protected void doTestVariantsInner(String fileName) throws Throwable { final VirtualFile virtualFile = myFixture.copyFileToProject(fileName); final Scanner in = new Scanner(virtualFile.getInputStream()); final CompletionType type = CompletionType.valueOf(in.next()); final int count = in.nextInt(); final CheckType checkType = CheckType.valueOf(in.next()); final List<String> variants = new ArrayList<>(); while (in.hasNext()) { final String variant = StringUtil.strip(in.next(), CharFilter.NOT_WHITESPACE_FILTER); if (variant.length() > 0) { variants.add(variant); } } myFixture.complete(type, count); checkCompletion(checkType, variants); }
protected void doTestVariantsInner(String fileName) throws Throwable { final VirtualFile virtualFile = myFixture.copyFileToProject(fileName); final Scanner in = new Scanner(virtualFile.getInputStream()); final CompletionType type = CompletionType.valueOf(in.next()); final int count = in.nextInt(); final CheckType checkType = CheckType.valueOf(in.next()); final List<String> variants = new ArrayList<String>(); while (in.hasNext()) { final String variant = StringUtil.strip(in.next(), CharFilter.NOT_WHITESPACE_FILTER); if (variant.length() > 0) { variants.add(variant); } } myFixture.complete(type, count); checkCompletion(checkType, variants); }
protected PsiReference[] createReferences(@NotNull PsiElement element, String s, int offset, final boolean soft) { int contentOffset = StringUtil.findFirst(s, CharFilter.NOT_WHITESPACE_FILTER); if (contentOffset >= 0) { offset += contentOffset; } return new FileReferenceSet(s.trim(), element, offset, this, true) { @Override protected boolean isSoft() { return soft; } }.getAllReferences(); }
private int getStartOffset(@NotNull final PsiElement element, @NotNull final Editor editor) { if (isBunchOfElement(element)) { final PsiElement firstElement = getFirstElement(element); return firstElement != null? firstElement.getTextRange().getStartOffset() : element.getTextRange().getStartOffset(); } final int offset = editor.getCaretModel().getOffset(); final int elementTextOffset = element.getTextOffset(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(offset); while (lineNumber != document.getLineNumber(elementTextOffset)) { final String text = document.getText(TextRange.create(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber))); if (StringUtil.isEmptyOrSpaces(text)) { lineNumber += 1; break; } lineNumber -= 1; } final int lineStartOffset = lineNumber == document.getLineNumber(elementTextOffset) ? elementTextOffset : document.getLineStartOffset(lineNumber); final String lineText = document .getText(TextRange.create(lineStartOffset, document.getLineEndOffset(lineNumber))); int shift = StringUtil.findFirst(lineText, CharFilter.NOT_WHITESPACE_FILTER); return lineStartOffset + shift; }
@NotNull protected String getPrefix(@NotNull final PsiElement element) { final PyStringLiteralExpression stringLiteralExpression = PsiTreeUtil.getParentOfType(element, PyStringLiteralExpression.class); if (stringLiteralExpression != null) { final String text = stringLiteralExpression.getText(); final Pair<String,String> quotes = PythonStringUtil.getQuotes(text); final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyDocStringOwner.class); if (docStringOwner != null && stringLiteralExpression.equals(docStringOwner.getDocStringExpression())) { String indent = getIndent(stringLiteralExpression); if (quotes != null) { final List<String> strings = StringUtil.split(text, "\n"); if (strings.get(0).trim().equals(quotes.getFirst())) { return quotes.getFirst() + indent; } else { final String value = stringLiteralExpression.getStringValue(); final int firstNotSpace = StringUtil.findFirst(value, CharFilter.NOT_WHITESPACE_FILTER); return quotes.getFirst() + value.substring(0, firstNotSpace); } } return "\"" + indent; } else return quotes != null? quotes.getFirst() : "\""; } return element instanceof PsiComment? "# " : ""; }
private static String findDigits(String s) { int pos = StringUtil.findFirst(s, new CharFilter() { public boolean accept(char ch) { return Character.isDigit(ch); } }); if (pos >= 0) { return s.substring(pos); } return s; }
private static String addLeadingSpaces(String text, final CharFilter filter, int indentSize, String indentChar) { final List<String> strings = StringUtil.split(text, "\n", false); if (strings.size() > 1) { int firstLineIndent = StringUtil.findFirst(strings.get(0), filter); int secondLineIndent = StringUtil.findFirst(strings.get(1), filter); final int diff = secondLineIndent - firstLineIndent; if (diff > indentSize) { text = StringUtil.repeat(indentChar, diff - indentSize) + text; } } return text; }
private static int calculateIndentToRemove(@NotNull String text, @NotNull final CharFilter filter) { final List<String> strings = StringUtil.split(text, "\n", false); int minIndent = StringUtil.findFirst(text, filter); for (String s : strings) { final int indent = StringUtil.findFirst(s, filter); if (indent < minIndent && !StringUtil.isEmptyOrSpaces(s)) minIndent = indent; } return minIndent >= 0 ? minIndent : 0; }
protected PsiReference[] createReferences(PsiElement element, String s, int offset, final boolean soft) { int contentOffset = StringUtil.findFirst(s, CharFilter.NOT_WHITESPACE_FILTER); if (contentOffset >= 0) { offset += contentOffset; } return new FileReferenceSet(s.trim(), element, offset, this, true) { @Override protected boolean isSoft() { return soft; } }.getAllReferences(); }
private int getStartOffset(@NotNull final PsiElement element, @NotNull final Editor editor) { if (isBunchOfElement(element)) { final PsiElement firstElement = getFirstElement(element); return firstElement != null? firstElement.getTextRange().getStartOffset() : element.getTextRange().getStartOffset(); } final int offset = editor.getCaretModel().getOffset(); final int elementTextOffset = element.getTextOffset(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(offset); while (lineNumber != document.getLineNumber(elementTextOffset)) { final String text = document.getText(TextRange.create(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber))); if (StringUtil.isEmptyOrSpaces(text)) { lineNumber += 1; break; } lineNumber -= 1; } final int lineStartOffset = document.getLineStartOffset(lineNumber); final String lineText = document .getText(TextRange.create(lineStartOffset, document.getLineEndOffset(lineNumber))); int shift = StringUtil.findFirst(lineText, CharFilter.NOT_WHITESPACE_FILTER); return lineStartOffset + shift; }
private int getStartOffset(@Nonnull final PsiElement element, @Nonnull final Editor editor) { if (isBunchOfElement(element)) { final PsiElement firstElement = getFirstElement(element); return firstElement != null? firstElement.getTextRange().getStartOffset() : element.getTextRange().getStartOffset(); } final int offset = editor.getCaretModel().getOffset(); final int elementTextOffset = element.getTextOffset(); final Document document = editor.getDocument(); int lineNumber = document.getLineNumber(offset); while (lineNumber != document.getLineNumber(elementTextOffset)) { final String text = document.getText(TextRange.create(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber))); if (StringUtil.isEmptyOrSpaces(text)) { lineNumber += 1; break; } lineNumber -= 1; } final int lineStartOffset = lineNumber == document.getLineNumber(elementTextOffset) ? elementTextOffset : document.getLineStartOffset(lineNumber); final String lineText = document .getText(TextRange.create(lineStartOffset, document.getLineEndOffset(lineNumber))); int shift = StringUtil.findFirst(lineText, CharFilter.NOT_WHITESPACE_FILTER); return lineStartOffset + shift; }
@NotNull @Override public String preprocessOnPaste(Project project, PsiFile file, Editor editor, String text, RawText rawText) { if (!CodeInsightSettings.getInstance().INDENT_TO_CARET_ON_PASTE || file.getLanguage() != PythonLanguage.getInstance()) { return text; } final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project); final boolean useTabs = codeStyleSettings.useTabCharacter(PythonFileType.INSTANCE); final int indentSize = codeStyleSettings.getIndentSize(PythonFileType.INSTANCE); CharFilter NOT_INDENT_FILTER = new CharFilter() { public boolean accept(char ch) { return useTabs? ch != '\t' : ch != ' '; } }; final String indentChar = useTabs ? "\t" : " "; final CaretModel caretModel = editor.getCaretModel(); final SelectionModel selectionModel = editor.getSelectionModel(); final Document document = editor.getDocument(); final int caretOffset = selectionModel.getSelectionStart() != selectionModel.getSelectionEnd() ? selectionModel.getSelectionStart() : caretModel.getOffset(); final int lineNumber = document.getLineNumber(caretOffset); final int lineStartOffset = getLineStartSafeOffset(document, lineNumber); final int lineEndOffset = document.getLineEndOffset(lineNumber); final PsiElement element = file.findElementAt(caretOffset); if (PsiTreeUtil.getParentOfType(element, PyStringLiteralExpression.class) != null) return text; text = addLeadingSpaces(text, NOT_INDENT_FILTER, indentSize, indentChar); int firstLineIndent = StringUtil.findFirst(text, NOT_INDENT_FILTER); final String indentText = getIndentText(file, document, caretOffset, lineNumber, firstLineIndent); int toRemove = calculateIndentToRemove(text, NOT_INDENT_FILTER); final String toString = document.getText(TextRange.create(lineStartOffset, lineEndOffset)); if (StringUtil.isEmptyOrSpaces(indentText) && isApplicable(file, text, caretOffset)) { caretModel.moveToOffset(lineStartOffset); if (StringUtil.isEmptyOrSpaces(toString)) { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { document.deleteString(lineStartOffset, lineEndOffset); } }); } editor.getSelectionModel().setSelection(lineStartOffset, selectionModel.getSelectionEnd()); } final List<String> strings = StringUtil.split(text, "\n", false); String newText = ""; if (StringUtil.isEmptyOrSpaces(indentText)) { for (String s : strings) { newText += indentText + StringUtil.trimStart(s, StringUtil.repeat(indentChar, toRemove)); } } else { newText = text; } if (addLinebreak(text, toString, useTabs) && selectionModel.getSelectionStart() == selectionModel.getSelectionEnd()) newText += "\n"; return newText; }