use of com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl in project intellij-community by JetBrains.
the class GenerateMembersUtil method positionCaret.
/**
* @see GenerationInfo#positionCaret(Editor, boolean)
*/
public static void positionCaret(@NotNull Editor editor, @NotNull PsiElement firstMember, boolean toEditMethodBody) {
LOG.assertTrue(firstMember.isValid());
Project project = firstMember.getProject();
if (toEditMethodBody) {
PsiMethod method = (PsiMethod) firstMember;
PsiCodeBlock body = method.getBody();
if (body != null) {
PsiElement firstBodyElement = body.getFirstBodyElement();
PsiElement l = firstBodyElement;
while (l instanceof PsiWhiteSpace) l = l.getNextSibling();
if (l == null)
l = body;
PsiElement lastBodyElement = body.getLastBodyElement();
PsiElement r = lastBodyElement;
while (r instanceof PsiWhiteSpace) r = r.getPrevSibling();
if (r == null)
r = body;
int start = l.getTextRange().getStartOffset();
int end = r.getTextRange().getEndOffset();
boolean adjustLineIndent = false;
// body is whitespace
if (start > end && firstBodyElement == lastBodyElement && firstBodyElement instanceof PsiWhiteSpaceImpl) {
CharSequence chars = ((PsiWhiteSpaceImpl) firstBodyElement).getChars();
if (chars.length() > 1 && chars.charAt(0) == '\n' && chars.charAt(1) == '\n') {
start = end = firstBodyElement.getTextRange().getStartOffset() + 1;
adjustLineIndent = true;
}
}
editor.getCaretModel().moveToOffset(Math.min(start, end));
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
if (start < end) {
//Not an empty body
editor.getSelectionModel().setSelection(start, end);
} else if (adjustLineIndent) {
Document document = editor.getDocument();
RangeMarker marker = document.createRangeMarker(start, start);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
if (marker.isValid()) {
CodeStyleManager.getInstance(project).adjustLineIndent(document, marker.getStartOffset());
}
}
return;
}
}
int offset;
if (firstMember instanceof PsiMethod) {
PsiMethod method = (PsiMethod) firstMember;
PsiCodeBlock body = method.getBody();
if (body == null) {
offset = method.getTextRange().getStartOffset();
} else {
PsiJavaToken lBrace = body.getLBrace();
assert lBrace != null : firstMember.getText();
offset = lBrace.getTextRange().getEndOffset();
}
} else {
offset = firstMember.getTextRange().getStartOffset();
}
editor.getCaretModel().moveToOffset(offset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
use of com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl in project intellij-community by JetBrains.
the class ASTFactory method whitespace.
@NotNull
public static LeafElement whitespace(final CharSequence text) {
final PsiWhiteSpaceImpl w = new PsiWhiteSpaceImpl(WHITESPACES.intern(text));
CodeEditUtil.setNodeGenerated(w, true);
return w;
}
use of com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl in project intellij-community by JetBrains.
the class TraverserBasedASTNode method getPsiImpl.
public PsiElement getPsiImpl() {
IElementType type = getElementType();
ParserDefinition pd = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage());
if (pd != null && pd.getWhitespaceTokens().contains(type) || pd == null && type == TokenType.WHITE_SPACE) {
return new PsiWhiteSpaceImpl(getChars());
} else if (pd != null && pd.getCommentTokens().contains(type)) {
return new ASTWrapperPsiComment(this);
} else if (pd == null || myNode instanceof LighterASTTokenNode) {
return new ASTWrapperPsiElement(this);
} else {
return pd.createElement(this);
}
}
Aggregations