use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartTypeHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
myAfterTypeOrComponentName = false;
myAfterDollarInStringInterpolation = false;
if (fileType != DartFileType.INSTANCE)
return Result.CONTINUE;
if (c == '<' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
myAfterTypeOrComponentName = isAfterTypeOrComponentName(file, editor.getCaretModel().getOffset());
return Result.CONTINUE;
}
if (c == '>' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
if (handleDartGT(editor, DartTokenTypes.LT, DartTokenTypes.GT, INVALID_INSIDE_REFERENCE)) {
return Result.STOP;
}
return Result.CONTINUE;
}
if (c == '{') {
TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isAfterDollarInStringInterpolation(element)) {
myAfterDollarInStringInterpolation = true;
return Result.CONTINUE;
}
PsiElement nextLeaf = element == null ? null : PsiTreeUtil.nextLeaf(element);
if (nextLeaf instanceof PsiWhiteSpace) {
nextLeaf = PsiTreeUtil.nextLeaf(nextLeaf);
}
if (PsiTreeUtil.getParentOfType(element, DartLazyParseableBlock.class, false) != null || nextLeaf != null && nextLeaf.getText().equals("=>")) {
// Use case 1: manually wrapping code with {} in 'if', 'while', etc (if (a) <caret>return;). Closing '}' will be auto-inserted on Enter.
// Use case 2: manual transformation of arrow block to standard (foo()<caret> => 499;)
EditorModificationUtil.insertStringAtCaret(editor, "{");
return Result.STOP;
}
}
return Result.CONTINUE;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartEnterInDocLineCommentHandler method preprocessEnter.
// EnterInLineCommentHandler doesn't work well enough for Dart doc comments
@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffsetRef, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
if (file.getLanguage() != DartLanguage.INSTANCE && !HtmlUtil.isHtmlFile(file))
return Result.Continue;
final int caretOffset = caretOffsetRef.get().intValue();
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
final PsiElement psiAtOffset = file.findElementAt(caretOffset);
final PsiElement probablyDocComment = psiAtOffset instanceof PsiWhiteSpace && psiAtOffset.getText().startsWith("\n") ? psiAtOffset.getPrevSibling() : psiAtOffset == null && caretOffset > 0 && caretOffset == document.getTextLength() ? file.findElementAt(caretOffset - 1) : psiAtOffset;
if (probablyDocComment != null && probablyDocComment.getTextRange().getStartOffset() < caretOffset && probablyDocComment.getNode().getElementType() == DartTokenTypesSets.SINGLE_LINE_DOC_COMMENT) {
final CharSequence text = document.getCharsSequence();
final int offset = CharArrayUtil.shiftForward(text, caretOffset, " \t");
if (StringUtil.startsWith(text, offset, DartDocUtil.SINGLE_LINE_DOC_COMMENT)) {
caretOffsetRef.set(offset);
} else {
final String docText = StringUtil.trimStart(probablyDocComment.getText(), DartDocUtil.SINGLE_LINE_DOC_COMMENT);
final int spacesBeforeText = StringUtil.isEmptyOrSpaces(docText) ? 1 : StringUtil.countChars(docText, ' ', 0, true);
final int spacesToAdd = Math.max(0, spacesBeforeText - StringUtil.countChars(text, ' ', caretOffset, true));
document.insertString(caretOffset, DartDocUtil.SINGLE_LINE_DOC_COMMENT + StringUtil.repeatSymbol(' ', spacesToAdd));
caretAdvance.set(spacesBeforeText);
}
return Result.Default;
}
return Result.Continue;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartDocUtil method getMultilineDocCommentText.
@NotNull
private static String getMultilineDocCommentText(@NotNull final DartDocComment docComment) {
final StringBuilder buf = new StringBuilder();
boolean afterAsterisk = false;
for (PsiElement child = docComment.getFirstChild(); child != null; child = child.getNextSibling()) {
final IElementType elementType = child.getNode().getElementType();
final String text = child.getText();
if (elementType != DartTokenTypesSets.MULTI_LINE_DOC_COMMENT_START && elementType != DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK && elementType != DartTokenTypesSets.MULTI_LINE_COMMENT_END) {
int newLinesCount;
if (child instanceof PsiWhiteSpace && (newLinesCount = StringUtil.countNewLines(text)) > 0) {
buf.append(StringUtil.repeatSymbol('\n', newLinesCount));
} else {
if (afterAsterisk && text.startsWith(" ")) {
buf.append(text.substring(1));
} else {
buf.append(text);
}
}
}
afterAsterisk = elementType == DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK;
}
return buf.toString();
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartRefactoringUtil method findExpressionInRange.
@Nullable
public static DartExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset) {
PsiElement element1 = file.findElementAt(startOffset);
PsiElement element2 = file.findElementAt(endOffset - 1);
if (element1 instanceof PsiWhiteSpace) {
startOffset = element1.getTextRange().getEndOffset();
}
if (element2 instanceof PsiWhiteSpace) {
endOffset = element2.getTextRange().getStartOffset();
}
DartExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, DartExpression.class);
if (expression == null || expression.getTextRange().getEndOffset() != endOffset)
return null;
if (expression instanceof DartReference && expression.getParent() instanceof DartCallExpression)
return null;
return expression;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class HbErrorFilter method hasWhitespacesInHtmlBetweenErrorAndOpenTokens.
private static boolean hasWhitespacesInHtmlBetweenErrorAndOpenTokens(int offset, TemplateLanguageFileViewProvider viewProvider) {
PsiElement at = viewProvider.findElementAt(offset, viewProvider.getTemplateDataLanguage());
if (!(at instanceof PsiWhiteSpace))
return false;
PsiElement elementAt = viewProvider.findElementAt(at.getTextRange().getEndOffset() + 1, viewProvider.getBaseLanguage());
if (elementAt != null && START_TEMPLATE_TOKENS.contains(elementAt.getNode().getElementType()))
return true;
return false;
}
Aggregations