private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result) { while (myCurrentChild != null) { if (StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT) { Block commentBlock = createJavaBlock( myCurrentChild, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy() ); result.add(commentBlock); myCurrentIndent = Indent.getNoneIndent(); } else if (!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild)) { break; } myCurrentOffset += myCurrentChild.getTextLength(); myCurrentChild = myCurrentChild.getTreeNext(); } }
private static void parseSeeTagValue(@NotNull final PsiBuilder builder) { final IElementType tokenType = getTokenType(builder); if (tokenType == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, builder.mark()); } else if (tokenType == JavaDocTokenType.DOC_TAG_VALUE_TOKEN) { final PsiBuilder.Marker refStart = builder.mark(); builder.remapCurrentToken(JavaDocElementType.DOC_REFERENCE_HOLDER); builder.advanceLexer(); if (getTokenType(builder) == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, refStart); } else { refStart.drop(); } } else { final PsiBuilder.Marker tagValue = builder.mark(); builder.advanceLexer(); tagValue.done(JavaDocElementType.DOC_TAG_VALUE_ELEMENT); } }
public JavaHighlightingLexer(LanguageLevel languageLevel) { super(JavaParserDefinition.createLexer(languageLevel)); registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); registerSelfStoppingLayer(new StringLiteralLexer('\'', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.CHARACTER_LITERAL}, IElementType.EMPTY_ARRAY); LayeredLexer docLexer = new LayeredLexer(JavaParserDefinition.createDocLexer(languageLevel)); HtmlHighlightingLexer lexer = new HtmlHighlightingLexer(); lexer.setHasNoEmbeddments(true); docLexer.registerLayer(lexer, JavaDocTokenType.DOC_COMMENT_DATA); registerSelfStoppingLayer(docLexer, new IElementType[]{JavaDocElementType.DOC_COMMENT}, IElementType.EMPTY_ARRAY); }
private static void parseTag(@NotNull final PsiBuilder builder) { final String tagName = builder.getTokenText(); final PsiBuilder.Marker tag = builder.mark(); builder.advanceLexer(); while(true) { final IElementType tokenType = getTokenType(builder); if(tokenType == null || tokenType == JavaDocTokenType.DOC_TAG_NAME || tokenType == JavaDocTokenType.DOC_COMMENT_END) { break; } parseDataItem(builder, tagName, false); } tag.done(JavaDocElementType.DOC_TAG); }
private static void parseSeeTagValue(@NotNull final PsiBuilder builder, boolean allowBareFieldReference) { final IElementType tokenType = getTokenType(builder); if(tokenType == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, builder.mark()); } else if(tokenType == JavaDocTokenType.DOC_TAG_VALUE_TOKEN) { final PsiBuilder.Marker refStart = builder.mark(); builder.remapCurrentToken(JavaDocElementType.DOC_REFERENCE_HOLDER); builder.advanceLexer(); if(getTokenType(builder) == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, refStart); } else if(allowBareFieldReference) { refStart.rollbackTo(); builder.remapCurrentToken(JavaDocTokenType.DOC_TAG_VALUE_TOKEN); parseMethodRef(builder, builder.mark()); } else { refStart.drop(); } } else { final PsiBuilder.Marker tagValue = builder.mark(); builder.advanceLexer(); tagValue.done(JavaDocElementType.DOC_TAG_VALUE_ELEMENT); } }
private static boolean checkByTypes(IElementType parentType, IElementType nodeType) { if(ElementType.IMPORT_STATEMENT_BASE_BIT_SET.contains(parentType)) { return true; } if(nodeType == JavaElementType.RECEIVER_PARAMETER) { return true; } if(nodeType == JavaElementType.PARAMETER && parentType != JavaElementType.PARAMETER_LIST) { return true; } if(nodeType == JavaElementType.PARAMETER_LIST && parentType == JavaElementType.LAMBDA_EXPRESSION) { return true; } if(nodeType == JavaDocElementType.DOC_COMMENT) { return true; } return false; }
@Nullable public static ASTNode findDocComment(@NotNull CompositeElement element) { TreeElement node = element.getFirstChildNode(); while(node != null && (isWhitespaceOrComment(node) && !(node.getPsi() instanceof PsiDocComment))) { node = node.getTreeNext(); } if(node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) { return node; } else { return null; } }
private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result) { while(myCurrentChild != null) { if(StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT) { Block commentBlock = createJavaBlock(myCurrentChild, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()); result.add(commentBlock); myCurrentIndent = Indent.getNoneIndent(); } else if(!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild)) { break; } myCurrentOffset += myCurrentChild.getTextLength(); myCurrentChild = myCurrentChild.getTreeNext(); } }
private static boolean isPartOfCodeBlock(final ASTNode child) { if(child == null) { return false; } if(child.getElementType() == JavaElementType.BLOCK_STATEMENT) { return true; } if(child.getElementType() == JavaElementType.CODE_BLOCK) { return true; } if(FormatterUtil.containsWhiteSpacesOnly(child)) { return isPartOfCodeBlock(child.getTreeNext()); } if(child.getElementType() == JavaTokenType.END_OF_LINE_COMMENT) { return isPartOfCodeBlock(child.getTreeNext()); } return child.getElementType() == JavaDocElementType.DOC_COMMENT; }
@Override @NotNull public ChildAttributes getChildAttributes(final int newChildIndex) { if(isAfter(newChildIndex, new IElementType[]{ JavaDocElementType.DOC_COMMENT, JavaElementType.MODIFIER_LIST })) { return new ChildAttributes(Indent.getNoneIndent(), null); } else { if(getSubBlocks().size() == newChildIndex) { return new ChildAttributes(Indent.getNoneIndent(), null); } else { return new ChildAttributes(getCodeBlockInternalIndent(myChildrenIndent), null); } } }
private static boolean shouldKeepSpace(final PsiElement parent) { ASTNode node = parent.getNode(); if(node == null) { return true; } final IElementType type = node.getElementType(); if(type == JavaDocElementType.DOC_TAG_VALUE_ELEMENT) { return PsiTreeUtil.getParentOfType(parent, PsiDocMethodOrFieldRef.class) != null; } return type == JavaDocElementType.DOC_COMMENT || type == JavaDocElementType.DOC_TAG || type == JavaDocElementType.DOC_INLINE_TAG; }
@Override public void visitField(PsiField field) { if(myChild1.getElementType() == JavaDocElementType.DOC_COMMENT) { myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_DECLARATIONS); return; } if(myRole1 == ChildRole.INITIALIZER_EQ || myRole2 == ChildRole.INITIALIZER_EQ) { createSpaceInCode(mySettings.SPACE_AROUND_ASSIGNMENT_OPERATORS); } else if(myRole1 == ChildRole.TYPE || myRole2 == ChildRole.TYPE) { createSpaceInCode(true); } else if(myChild2.getElementType() == JavaTokenType.SEMICOLON) { createSpaceProperty(false, false, 0); } else if(myRole1 == ChildRole.MODIFIER_LIST) { createSpaceProperty(true, false, 0); } }
@Override public void visitElement(PsiElement element) { if(myRole1 == ChildRole.MODIFIER_LIST) { processModifierList(); } else if(myRole1 == ChildRole.OPERATION_SIGN) { createSpaceInCode(mySettings.SPACE_AROUND_UNARY_OPERATOR); } else if((myType1 == JavaDocTokenType.DOC_TAG_VALUE_TOKEN || myType1 == JavaDocElementType.DOC_TAG_VALUE_ELEMENT) && (myType2 == JavaDocTokenType.DOC_TAG_VALUE_TOKEN || myType2 == JavaDocElementType.DOC_TAG_VALUE_ELEMENT)) { createSpaceInCode(true); } else if(myRole1 == ChildRole.COMMA) { createSpaceInCode(mySettings.SPACE_AFTER_COMMA); } else if(myRole2 == ChildRole.COMMA) { createSpaceInCode(mySettings.SPACE_BEFORE_COMMA); } }
public JavaHighlightingLexer(LanguageLevel languageLevel) { super(new JavaLexer(languageLevel)); registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); registerSelfStoppingLayer(new StringLiteralLexer('\'', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.CHARACTER_LITERAL}, IElementType.EMPTY_ARRAY); LayeredLexer docLexer = new LayeredLexer(new JavaDocLexer(languageLevel)); HtmlHighlightingLexer lexer = new HtmlHighlightingLexer(); lexer.setHasNoEmbeddments(true); docLexer.registerLayer(lexer, JavaDocTokenType.DOC_COMMENT_DATA); registerSelfStoppingLayer(docLexer, new IElementType[]{JavaDocElementType.DOC_COMMENT}, IElementType.EMPTY_ARRAY); }
private static void parseTag(@NotNull final PsiBuilder builder) { final String tagName = builder.getTokenText(); final PsiBuilder.Marker tag = builder.mark(); builder.advanceLexer(); while (true) { final IElementType tokenType = getTokenType(builder); if (tokenType == null || tokenType == JavaDocTokenType.DOC_TAG_NAME || tokenType == JavaDocTokenType.DOC_COMMENT_END) break; parseDataItem(builder, tagName, false); } tag.done(JavaDocElementType.DOC_TAG); }
private static void parseSeeTagValue(@NotNull final PsiBuilder builder, boolean allowBareFieldReference) { final IElementType tokenType = getTokenType(builder); if (tokenType == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, builder.mark()); } else if (tokenType == JavaDocTokenType.DOC_TAG_VALUE_TOKEN) { final PsiBuilder.Marker refStart = builder.mark(); builder.remapCurrentToken(JavaDocElementType.DOC_REFERENCE_HOLDER); builder.advanceLexer(); if (getTokenType(builder) == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) { parseMethodRef(builder, refStart); } else if (allowBareFieldReference) { refStart.rollbackTo(); builder.remapCurrentToken(JavaDocTokenType.DOC_TAG_VALUE_TOKEN); parseMethodRef(builder, builder.mark()); } else { refStart.drop(); } } else { final PsiBuilder.Marker tagValue = builder.mark(); builder.advanceLexer(); tagValue.done(JavaDocElementType.DOC_TAG_VALUE_ELEMENT); } }
private static void parseSimpleTagValue(@NotNull final PsiBuilder builder, final boolean parameter) { final PsiBuilder.Marker tagValue = builder.mark(); while (TAG_VALUES_SET.contains(getTokenType(builder))) { builder.advanceLexer(); } tagValue.done(parameter ? JavaDocElementType.DOC_PARAMETER_REF : JavaDocElementType.DOC_TAG_VALUE_ELEMENT); }
@Override public int getEdgePosition(final List<IElementType> tokens, final boolean atStreamEdge, final TokenTextGetter getter) { if (tokens.size() == 0) return 0; // 1. bind doc comment for (int idx = tokens.size() - 1; idx >= 0; idx--) { if (tokens.get(idx) == JavaDocElementType.DOC_COMMENT) return idx; } // 2. bind plain comments int result = tokens.size(); for (int idx = tokens.size() - 1; idx >= 0; idx--) { final IElementType tokenType = tokens.get(idx); if (ElementType.JAVA_WHITESPACE_BIT_SET.contains(tokenType)) { if (StringUtil.getLineBreakCount(getter.get(idx)) > 1) break; } else if (ElementType.JAVA_PLAIN_COMMENT_BIT_SET.contains(tokenType)) { if (atStreamEdge || (idx == 0 && myAfterEmptyImport) || (idx > 0 && ElementType.JAVA_WHITESPACE_BIT_SET.contains(tokens.get(idx - 1)) && StringUtil.containsLineBreak(getter.get(idx - 1)))) { result = idx; } } else break; } return result; }
@Override public PsiFieldStub createStub(final LighterAST tree, final LighterASTNode node, final StubElement parentStub) { final TypeInfo typeInfo = TypeInfo.create(tree, node, parentStub); boolean isDeprecatedByComment = false; boolean hasDeprecatedAnnotation = false; boolean hasDocComment = false; String name = null; String initializer = null; boolean expectingInit = false; for (final LighterASTNode child : tree.getChildren(node)) { final IElementType type = child.getTokenType(); if (type == JavaDocElementType.DOC_COMMENT) { hasDocComment = true; isDeprecatedByComment = RecordUtil.isDeprecatedByDocComment(tree, child); } else if (type == JavaElementType.MODIFIER_LIST) { hasDeprecatedAnnotation = RecordUtil.isDeprecatedByAnnotation(tree, child); } else if (type == JavaTokenType.IDENTIFIER) { name = RecordUtil.intern(tree.getCharTable(), child); } else if (type == JavaTokenType.EQ) { expectingInit = true; } else if (expectingInit && !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(type) && type != JavaTokenType.SEMICOLON) { initializer = encodeInitializer(tree, child); break; } } final boolean isEnumConst = node.getTokenType() == JavaElementType.ENUM_CONSTANT; final byte flags = PsiFieldStubImpl.packFlags(isEnumConst, isDeprecatedByComment, hasDeprecatedAnnotation, hasDocComment); return new PsiFieldStubImpl(parentStub, name, typeInfo, initializer, flags); }
@Override public PsiDocComment getContainingComment() { ASTNode scope = getTreeParent(); while (scope.getElementType() != JavaDocElementType.DOC_COMMENT) { scope = scope.getTreeParent(); } return (PsiDocComment)SourceTreeToPsiMap.treeElementToPsi(scope); }
public JavaHighlightingLexer(@NotNull LanguageLevel languageLevel) { super(JavaParserDefinition.createLexer(languageLevel)); registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); registerSelfStoppingLayer(new StringLiteralLexer('\'', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.CHARACTER_LITERAL}, IElementType.EMPTY_ARRAY); LayeredLexer docLexer = new LayeredLexer(JavaParserDefinition.createDocLexer(languageLevel)); HtmlHighlightingLexer htmlLexer = new HtmlHighlightingLexer(null); htmlLexer.setHasNoEmbeddments(true); docLexer.registerLayer(htmlLexer, JavaDocTokenType.DOC_COMMENT_DATA); registerSelfStoppingLayer(docLexer, new IElementType[]{JavaDocElementType.DOC_COMMENT}, IElementType.EMPTY_ARRAY); }
private void generatePackageJavaDoc(final StringBuilder buffer, final PsiPackage psiPackage, boolean generatePrologueAndEpilogue) { for (PsiDirectory directory : psiPackage.getDirectories(new EverythingGlobalScope(myProject))) { final PsiFile packageInfoFile = directory.findFile(PsiPackage.PACKAGE_INFO_FILE); if (packageInfoFile != null) { final ASTNode node = packageInfoFile.getNode(); if (node != null) { final ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT); if (docCommentNode != null) { final PsiDocComment docComment = (PsiDocComment)docCommentNode.getPsi(); if (generatePrologueAndEpilogue) generatePrologue(buffer); generateCommonSection(buffer, docComment); if (generatePrologueAndEpilogue) generateEpilogue(buffer); break; } } } PsiFile packageHtmlFile = directory.findFile("package.html"); if (packageHtmlFile != null) { generatePackageHtmlJavaDoc(buffer, packageHtmlFile, generatePrologueAndEpilogue); break; } } }
private static boolean isPartOfCodeBlock(final ASTNode child) { if (child == null) return false; if (child.getElementType() == JavaElementType.BLOCK_STATEMENT) return true; if (child.getElementType() == JavaElementType.CODE_BLOCK) return true; if (FormatterUtil.containsWhiteSpacesOnly(child)) return isPartOfCodeBlock(child.getTreeNext()); if (child.getElementType() == JavaTokenType.END_OF_LINE_COMMENT) return isPartOfCodeBlock(child.getTreeNext()); return child.getElementType() == JavaDocElementType.DOC_COMMENT; }
@Override @NotNull public ChildAttributes getChildAttributes(final int newChildIndex) { if (isAfter(newChildIndex, new IElementType[]{JavaDocElementType.DOC_COMMENT, JavaElementType.MODIFIER_LIST})) { return new ChildAttributes(Indent.getNoneIndent(), null); } else { if (getSubBlocks().size() == newChildIndex) { return new ChildAttributes(Indent.getNoneIndent(), null); } else { return new ChildAttributes(getCodeBlockInternalIndent(myChildrenIndent), null); } } }
private static boolean lexemsEqual(final PsiClass classToBind, final PsiClass newClass) { Lexer oldTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); Lexer newTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); String oldBuffer = classToBind.getText(); String newBuffer = newClass.getText(); oldTextLexer.start(oldBuffer); newTextLexer.start(newBuffer); while(true) { IElementType oldLexem = oldTextLexer.getTokenType(); IElementType newLexem = newTextLexer.getTokenType(); if (oldLexem == null || newLexem == null) { // must terminate at the same time return oldLexem == null && newLexem == null; } if (oldLexem != newLexem) { return false; } if (oldLexem != TokenType.WHITE_SPACE && oldLexem != JavaDocElementType.DOC_COMMENT) { int oldStart = oldTextLexer.getTokenStart(); int newStart = newTextLexer.getTokenStart(); int oldLength = oldTextLexer.getTokenEnd() - oldTextLexer.getTokenStart(); int newLength = newTextLexer.getTokenEnd() - newTextLexer.getTokenStart(); if (oldLength != newLength) { return false; } for(int i=0; i<oldLength; i++) { if (oldBuffer.charAt(oldStart+i) != newBuffer.charAt(newStart+i)) { return false; } } } oldTextLexer.advance(); newTextLexer.advance(); } }
private void generatePackageJavaDoc(final StringBuilder buffer, final PsiPackage psiPackage, boolean generatePrologueAndEpilogue) { for(PsiDirectory directory: psiPackage.getDirectories()) { final PsiFile packageInfoFile = directory.findFile(PsiPackage.PACKAGE_INFO_FILE); if (packageInfoFile != null) { final ASTNode node = packageInfoFile.getNode(); if (node != null) { final ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT); if (docCommentNode != null) { final PsiDocComment docComment = (PsiDocComment)docCommentNode.getPsi(); if (generatePrologueAndEpilogue) generatePrologue(buffer); generateCommonSection(buffer, docComment); if (generatePrologueAndEpilogue) generateEpilogue(buffer); break; } } } PsiFile packageHtmlFile = directory.findFile("package.html"); if (packageHtmlFile != null) { generatePackageHtmlJavaDoc(buffer, packageHtmlFile, generatePrologueAndEpilogue); break; } } }
@Override public PsiFieldStub createStub(final LighterAST tree, final LighterASTNode node, final StubElement parentStub) { final TypeInfo typeInfo = TypeInfo.create(tree, node, parentStub); boolean isDeprecatedByComment = false; boolean hasDeprecatedAnnotation = false; String name = null; String initializer = null; boolean expectingInit = false; for (final LighterASTNode child : tree.getChildren(node)) { final IElementType type = child.getTokenType(); if (type == JavaDocElementType.DOC_COMMENT) { isDeprecatedByComment = RecordUtil.isDeprecatedByDocComment(tree, child); } else if (type == JavaElementType.MODIFIER_LIST) { hasDeprecatedAnnotation = RecordUtil.isDeprecatedByAnnotation(tree, child); } else if (type == JavaTokenType.IDENTIFIER) { name = RecordUtil.intern(tree.getCharTable(), child); } else if (type == JavaTokenType.EQ) { expectingInit = true; } else if (expectingInit && !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(type) && type != JavaTokenType.SEMICOLON) { initializer = encodeInitializer(tree, child); break; } } final boolean isEnumConst = node.getTokenType() == JavaElementType.ENUM_CONSTANT; final byte flags = PsiFieldStubImpl.packFlags(isEnumConst, isDeprecatedByComment, hasDeprecatedAnnotation); return new PsiFieldStubImpl(parentStub, name, typeInfo, initializer, flags); }
@Nullable public static ASTNode findDocComment(@NotNull CompositeElement element) { TreeElement node = element.getFirstChildNode(); while (node != null && (isWhitespaceOrComment(node) && !(node.getPsi() instanceof PsiDocComment))) { node = node.getTreeNext(); } if (node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) { return node; } else { return null; } }
private static void parseSimpleTagValue(@NotNull final PsiBuilder builder, final boolean parameter) { final PsiBuilder.Marker tagValue = builder.mark(); while(TAG_VALUES_SET.contains(getTokenType(builder))) { builder.advanceLexer(); } tagValue.done(parameter ? JavaDocElementType.DOC_PARAMETER_REF : JavaDocElementType.DOC_TAG_VALUE_ELEMENT); }
@Override public PsiDocComment getContainingComment() { ASTNode scope = getTreeParent(); while(scope.getElementType() != JavaDocElementType.DOC_COMMENT) { scope = scope.getTreeParent(); } return (PsiDocComment) SourceTreeToPsiMap.treeElementToPsi(scope); }
/** * Finds doc comment immediately preceding package statement */ @Nullable private static ASTNode findRelevantCommentNode(@NotNull ASTNode fileNode) { ASTNode node = fileNode.findChildByType(JavaElementType.PACKAGE_STATEMENT); if(node == null) { node = fileNode.getLastChildNode(); } while(node != null && node.getElementType() != JavaDocElementType.DOC_COMMENT) { node = node.getTreePrev(); } return node; }
@Override @NotNull public ChildAttributes getChildAttributes(final int newChildIndex) { if(myUseChildAttributes) { return new ChildAttributes(myChildIndent, myChildAlignment); } if(isAfter(newChildIndex, new IElementType[]{JavaDocElementType.DOC_COMMENT})) { return new ChildAttributes(Indent.getNoneIndent(), myChildAlignment); } return super.getChildAttributes(newChildIndex); }
@Override public void visitDocTag(PsiDocTag tag) { if(myType1 == JavaDocTokenType.DOC_TAG_NAME && myType2 == JavaDocElementType.DOC_TAG_VALUE_ELEMENT) { myResult = Spacing.createSpacing(1, 1, 0, false, 0); } }