StringBuilder (@NotNull PsiElement element, @Nullable StringBuilder buffer) { if (element instanceof PsiNamedElement named) { final String name = named.getName(); if (StringUtil.isEmpty(name)) { return null; } PsiElement parent = element.getParent(); if (parent == null) return null; int index = getChildIndex(named, parent, name, PsiNamedElement.class); if (index < 0) return null; StringBuilder bufferToUse = buffer; if (bufferToUse == null) { bufferToUse = new StringBuilder(); } bufferToUse.append(TYPE_MARKER).append(ELEMENT_TOKENS_SEPARATOR).append(escape(name)) .append(ELEMENT_TOKENS_SEPARATOR).append(index); return bufferToUse; } if (element instanceof PsiComment) { PsiElement parent = element.getParent(); boolean nestedComment = false; if (parent instanceof PsiComment && parent.getTextRange().equals(element.getTextRange())) { parent = parent.getParent(); nestedComment = true; } if (parent instanceof PsiNamedElement && (nestedComment || parent.getFirstChild() == element)) { // Consider doc comment element that is the first child of named element to be a doc comment. StringBuilder bufferToUse = buffer; if (bufferToUse == null) { bufferToUse = new StringBuilder(); } bufferToUse.append(TYPE_MARKER).append(ELEMENT_TOKENS_SEPARATOR).append(DOC_COMMENT_MARKER); return bufferToUse; } } PsiElement parent = element.getParent(); if (parent instanceof PsiNamedElement && !(parent instanceof PsiFile)) { if (isBlockElement(element)) { int index = getBlockElementIndex(element); StringBuilder bufferToUse = buffer; if (bufferToUse == null) { bufferToUse = new StringBuilder(); } bufferToUse.append(TYPE_MARKER).append(ELEMENT_TOKENS_SEPARATOR).append(CODE_BLOCK_MARKER); if (index > 0) { bufferToUse.append(ELEMENT_TOKENS_SEPARATOR).append(index); } return bufferToUse; } } return null; }