Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.kotlin.internal;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jetbrains.kotlin.KtFakeSourceElement;
import org.jetbrains.kotlin.KtRealPsiSourceElement;
import org.jetbrains.kotlin.KtSourceElement;
import org.jetbrains.kotlin.com.intellij.openapi.util.TextRange;
import org.jetbrains.kotlin.com.intellij.psi.PsiElement;
import org.jetbrains.kotlin.fir.FirElement;
import org.jetbrains.kotlin.fir.backend.FirMetadataSource;
import org.jetbrains.kotlin.fir.declarations.FirDeclaration;
import org.jetbrains.kotlin.fir.declarations.FirFile;
import org.jetbrains.kotlin.fir.declarations.FirProperty;
import org.jetbrains.kotlin.fir.expressions.*;
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference;
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag;
import org.jetbrains.kotlin.fir.types.*;
import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitor;
import org.jetbrains.kotlin.ir.IrElement;
import org.jetbrains.kotlin.ir.declarations.IrFile;
import org.jetbrains.kotlin.ir.declarations.IrMetadataSourceOwner;
import org.jetbrains.kotlin.ir.declarations.MetadataSource;
import org.jetbrains.kotlin.ir.expressions.IrConst;
import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.tree.*;
import org.openrewrite.kotlin.KotlinIrTypeMapping;
import org.openrewrite.kotlin.tree.K;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.StreamSupport.stream;
@SuppressWarnings({"unused", "DuplicatedCode"})
public class PsiTreePrinter {
private static final String TAB = " ";
private static final String ELEMENT_PREFIX = "\\---";
private static final char BRANCH_CONTINUE_CHAR = '|';
private static final char BRANCH_END_CHAR = '\\';
private static final int CONTENT_MAX_LENGTH = 200;
private static final String CONTINUE_PREFIX = "----";
private static final String UNVISITED_PREFIX = "#";
private static final KotlinIrTypeMapping irTypeMapping = new KotlinIrTypeMapping(new JavaTypeCache());
// Set to true to print types and verify, otherwise just verify the parse to print idempotent.
private final static boolean printTypes = true;
private final List outputLines;
protected PsiTreePrinter() {
outputLines = new ArrayList<>();
}
public static String print(PsiElement psiElement) {
return printPsiTree(psiElement);
}
public static String print(Parser.Input input) {
return printIndexedSourceCode(input.getSource(new InMemoryExecutionContext()).readFully());
}
public static String print(@Nullable IrFile file) {
return printIrFile(file);
}
public static String print(Tree tree) {
return printJTree(tree);
}
public static String print(@Nullable FirElement firElement) {
if (firElement == null) {
return "null";
}
return printFirTree(firElement);
}
public static String printPsiTreeSkeleton(PsiElement psiElement) {
PsiTreePrinter treePrinter = new PsiTreePrinter();
StringBuilder sb = new StringBuilder();
sb.append("------------").append("\n");
sb.append("PSI Tree Skeleton").append("\n");
Set covered = new HashSet<>();
collectCovered(psiElement, covered);
treePrinter.printSkeletonNode(psiElement, 1);
sb.append(java.lang.String.join("\n", treePrinter.outputLines));
return sb.toString();
}
public static String printPsiTree(PsiElement psiElement) {
PsiTreePrinter treePrinter = new PsiTreePrinter();
StringBuilder sb = new StringBuilder();
treePrinter.printNode(psiElement, 1);
sb.append(java.lang.String.join("\n", treePrinter.outputLines));
return sb.toString();
}
@AllArgsConstructor
@Data
public static class TreePrinterContext {
List lines;
int depth;
}
public static String printFirFile(FirFile file) {
StringBuilder sb = new StringBuilder();
List lines = new ArrayList<>();
sb.append("------------").append("\n");
sb.append("FirFile:").append("\n\n");
TreePrinterContext context = new TreePrinterContext(lines, 1);
new FirDefaultVisitor() {
@Override
public Void visitElement(FirElement firElement, TreePrinterContext ctx) {
StringBuilder line = new StringBuilder();
line.append(leftPadding(ctx.getDepth()))
.append(printFirElement(firElement));
connectToLatestSibling(ctx.getDepth(), ctx.getLines());
ctx.getLines().add(line);
ctx.setDepth(ctx.getDepth() + 1);
firElement.acceptChildren(this, ctx);
if (firElement instanceof FirResolvedTypeRef) {
// not sure why this isn't taken care of by `FirResolvedTypeRefImpl#acceptChildren()`
FirTypeRef firTypeRef = ((FirResolvedTypeRef) firElement).getDelegatedTypeRef();
if (firTypeRef != null) {
firTypeRef.accept(this, ctx);
}
}
ctx.setDepth(ctx.getDepth() - 1);
return null;
}
}.visitFile(file, context);
sb.append(java.lang.String.join("\n", lines));
return sb.toString();
}
public static String printFirTree(FirElement firElement) {
StringBuilder sb = new StringBuilder();
List lines = new ArrayList<>();
TreePrinterContext context = new TreePrinterContext(lines, 1);
new FirDefaultVisitor() {
@Override
public Void visitElement(FirElement fir, TreePrinterContext ctx) {
StringBuilder line = new StringBuilder();
line.append(leftPadding(ctx.getDepth()))
.append(printFirElement(fir));
connectToLatestSibling(ctx.getDepth(), ctx.getLines());
ctx.getLines().add(line);
ctx.setDepth(ctx.getDepth() + 1);
fir.acceptChildren(this, ctx);
if (fir instanceof FirResolvedTypeRef) {
// not sure why this isn't taken care of by `FirResolvedTypeRefImpl#acceptChildren()`
FirTypeRef firTypeRef = ((FirResolvedTypeRef) fir).getDelegatedTypeRef();
if (firTypeRef != null) {
firTypeRef.accept(this, ctx);
}
}
ctx.setDepth(ctx.getDepth() - 1);
return null;
}
}.visitElement(firElement, context);
sb.append(java.lang.String.join("\n", lines));
return sb.toString();
}
public static class IrPrinter {
public void printElement(IrElement element, PsiTreePrinter.TreePrinterContext ctx) {
StringBuilder line = new StringBuilder();
line.append(leftPadding(ctx.getDepth()))
.append(printIrElement(element));
connectToLatestSibling(ctx.getDepth(), ctx.getLines());
ctx.getLines().add(line);
}
}
public static String printIrFile(@Nullable IrFile file) {
if (file == null) {
return "";
}
StringBuilder sb = new StringBuilder();
List lines = new ArrayList<>();
sb.append("------------").append("\n");
sb.append("IrFile:").append("\n\n");
TreePrinterContext context = new TreePrinterContext(lines, 1);
new IrTreePrinterVisitor(new IrPrinter()).visitFile(file, context);
sb.append(java.lang.String.join("\n", lines));
return sb.toString();
}
/**
* print J tree with all types
*/
static class TreeVisitingPrinter extends TreeVisitor {
private List