use of com.intellij.psi.PsiWhiteSpace in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoFoldingBuilder method buildLanguageFoldRegions.
@Override
protected void buildLanguageFoldRegions(@NotNull List<FoldingDescriptor> result, @NotNull PsiElement root, @NotNull Document document, boolean quick) {
if (!(root instanceof GoFile))
return;
GoFile file = (GoFile) root;
if (!file.isContentsLoaded())
return;
GoImportList importList = ((GoFile) root).getImportList();
if (importList != null) {
GoImportDeclaration firstImport = ContainerUtil.getFirstItem(importList.getImportDeclarationList());
if (firstImport != null) {
PsiElement importKeyword = firstImport.getImport();
int offset = importKeyword.getTextRange().getEndOffset();
int startOffset = importKeyword.getNextSibling() instanceof PsiWhiteSpace ? offset + 1 : offset;
int endOffset = importList.getTextRange().getEndOffset();
if (endOffset - startOffset > 3) {
result.add(new NamedFoldingDescriptor(importList, startOffset, endOffset, null, "..."));
}
}
}
for (GoBlock block : PsiTreeUtil.findChildrenOfType(file, GoBlock.class)) {
if (block.getTextRange().getLength() > 1) {
result.add(new NamedFoldingDescriptor(block.getNode(), block.getTextRange(), null, "{...}"));
}
}
for (GoExprSwitchStatement switchStatement : PsiTreeUtil.findChildrenOfType(file, GoExprSwitchStatement.class)) {
fold(switchStatement, switchStatement.getLbrace(), switchStatement.getRbrace(), "{...}", result);
}
for (GoSelectStatement selectStatement : PsiTreeUtil.findChildrenOfType(file, GoSelectStatement.class)) {
fold(selectStatement, selectStatement.getLbrace(), selectStatement.getRbrace(), "{...}", result);
}
for (GoTypeSpec type : file.getTypes()) {
foldTypes(type.getSpecType().getType(), result);
}
for (GoCaseClause caseClause : PsiTreeUtil.findChildrenOfType(file, GoCaseClause.class)) {
PsiElement colon = caseClause.getColon();
if (colon != null && !caseClause.getStatementList().isEmpty()) {
fold(caseClause, colon.getNextSibling(), caseClause, "...", result);
}
}
for (GoCommClause commClause : PsiTreeUtil.findChildrenOfType(file, GoCommClause.class)) {
PsiElement colon = commClause.getColon();
if (colon != null && !commClause.getStatementList().isEmpty()) {
fold(commClause, colon.getNextSibling(), commClause, "...", result);
}
}
for (GoVarDeclaration varDeclaration : PsiTreeUtil.findChildrenOfType(file, GoVarDeclaration.class)) {
if (varDeclaration.getVarSpecList().size() > 1) {
fold(varDeclaration, varDeclaration.getLparen(), varDeclaration.getRparen(), "(...)", result);
}
}
for (GoConstDeclaration constDeclaration : PsiTreeUtil.findChildrenOfType(file, GoConstDeclaration.class)) {
if (constDeclaration.getConstSpecList().size() > 1) {
fold(constDeclaration, constDeclaration.getLparen(), constDeclaration.getRparen(), "(...)", result);
}
}
for (GoTypeDeclaration typeDeclaration : PsiTreeUtil.findChildrenOfType(file, GoTypeDeclaration.class)) {
if (typeDeclaration.getTypeSpecList().size() > 1) {
fold(typeDeclaration, typeDeclaration.getLparen(), typeDeclaration.getRparen(), "(...)", result);
}
}
for (GoCompositeLit compositeLit : PsiTreeUtil.findChildrenOfType(file, GoCompositeLit.class)) {
GoLiteralValue literalValue = compositeLit.getLiteralValue();
if (literalValue != null && literalValue.getElementList().size() > 1) {
fold(literalValue, literalValue.getLbrace(), literalValue.getRbrace(), "{...}", result);
}
}
if (!quick) {
Set<PsiElement> processedComments = ContainerUtil.newHashSet();
PsiTreeUtil.processElements(file, element -> {
ASTNode node = element.getNode();
IElementType type = node.getElementType();
TextRange range = element.getTextRange();
if (type == GoParserDefinition.MULTILINE_COMMENT && range.getLength() > 2) {
result.add(new NamedFoldingDescriptor(node, range, null, "/*...*/"));
}
if (type == GoParserDefinition.LINE_COMMENT) {
addCommentFolds(element, processedComments, result);
}
foldTypes(element, result);
return true;
});
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class HtmlLocalInspectionTool method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlToken(final XmlToken token) {
IElementType tokenType = token.getTokenType();
if (tokenType == XmlTokenType.XML_NAME || tokenType == XmlTokenType.XML_TAG_NAME) {
PsiElement element = token.getPrevSibling();
while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
PsiElement parent = element.getParent();
if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
XmlTag tag = (XmlTag) parent;
checkTag(tag, holder, isOnTheFly);
}
}
}
}
@Override
public void visitXmlAttribute(final XmlAttribute attribute) {
checkAttribute(attribute, holder, isOnTheFly);
}
};
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class RemoveUnnecessaryBackslashQuickFix method removeBackSlash.
public static void removeBackSlash(PsiElement parent) {
if (parent != null) {
Stack<PsiElement> stack = new Stack<>();
if (parent instanceof PyParenthesizedExpression)
stack.push(((PyParenthesizedExpression) parent).getContainedExpression());
else
stack.push(parent);
while (!stack.isEmpty()) {
PsiElement el = stack.pop();
PsiWhiteSpace[] children = PsiTreeUtil.getChildrenOfType(el, PsiWhiteSpace.class);
if (children != null) {
for (PsiWhiteSpace ws : children) {
if (ws.getText().contains("\\")) {
ws.delete();
}
}
}
for (PsiElement psiElement : el.getChildren()) {
stack.push(psiElement);
}
}
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class ReplaceExceptPartQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement exceptPart = descriptor.getPsiElement();
if (exceptPart instanceof PyExceptPart) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PsiElement element = ((PyExceptPart) exceptPart).getExceptClass().getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
assert element != null;
PyTryExceptStatement newElement = elementGenerator.createFromText(LanguageLevel.forElement(exceptPart), PyTryExceptStatement.class, "try: pass except a as b: pass");
ASTNode node = newElement.getExceptParts()[0].getNode().findChildByType(PyTokenTypes.AS_KEYWORD);
assert node != null;
element.replace(node.getPsi());
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class CompatibilityVisitor method visitPyExceptBlock.
@Override
public void visitPyExceptBlock(PyExceptPart node) {
super.visitPyExceptBlock(node);
final PyExpression exceptClass = node.getExceptClass();
if (exceptClass != null) {
PsiElement element = exceptClass.getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
if (element != null && "as".equals(element.getText())) {
registerOnFirstMatchingVersion(level -> level.isOlderThan(LanguageLevel.PYTHON26), "Python versions < 2.6 do not support this syntax.", node);
}
if (element != null && ",".equals(element.getText())) {
registerForAllMatchingVersions(LanguageLevel::isPy3K, " not support this syntax.", node, new ReplaceExceptPartQuickFix());
}
}
}
Aggregations