Skip to content

Commit

Permalink
add pretty-printing of annotations on symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Strum355 committed Apr 8, 2021
1 parent d033d95 commit f8db39d
Show file tree
Hide file tree
Showing 85 changed files with 1,153 additions and 753 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ _site/
metals.sbt
metals/project/

.bsp

.vscode/

local.*
Expand Down
3 changes: 1 addition & 2 deletions .jvmopts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-Xss2m
-Xms1G
-Xmx4G
-Dfile.encoding=UTF-8
-Dsbt.server.autostart=false
-Dfile.encoding=UTF-8
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ lazy val unit = project
.in(file("tests/unit"))
.settings(
testSettings,
//javaOptions ++= Seq( "-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"),
buildInfoKeys :=
Seq[BuildInfoKey](
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ object SemanticdbPrinters {
if (sig.isEmpty)
" " + info.getDisplayName
else
" " + sig
" " + sig.replace('\n', ' ')
case _ =>
""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ object SnapshotLsifCommand {
// we cheese it a bit here, as this is less work than trying to reconstruct
// a Signature from the pretty-printed Signature, with accompanying logic
// to fallback to display_name in SemanticdbPrinters.scala
.setDisplayName(hover).setSymbol(symbol).build()
.setDisplayName(hover.replace('\n', ' ')).setSymbol(symbol).build()
doc.addSymbols(symInfo)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sourcegraph.semanticdb_javac.Semanticdb.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -51,6 +52,8 @@ private void formatClassSignature(ClassSignature classSignature) {

boolean isEnum = has(Property.ENUM);

printKeywordln(formatAnnotations());

printKeyword(formatAccess());
if (!isEnum && !isAnnotation) printKeyword(formatModifiers());

Expand Down Expand Up @@ -143,6 +146,7 @@ private void formatClassSignature(ClassSignature classSignature) {
}

private void formatMethodSignature(MethodSignature methodSignature) {
printKeywordln(formatAnnotations());
printKeyword(formatAccess());
printKeyword(formatModifiers());

Expand Down Expand Up @@ -177,6 +181,7 @@ private void formatMethodSignature(MethodSignature methodSignature) {
}

private void formatValueSignature(ValueSignature valueSignature) {
printKeywordln(formatAnnotations());
if (isEnumConstant()) {
String ownerSym = SymbolDescriptor.parseFromSymbol(symbolInformation.getSymbol()).owner;
SymbolInformation ownerInfo = symtab.symbols.get(ownerSym);
Expand Down Expand Up @@ -234,6 +239,96 @@ private String formatTypeArguments(List<Type> typeArguments) {
return typeArguments.stream().map(this::formatType).collect(Collectors.joining(", ", "<", ">"));
}

private String formatAnnotations() {
return formatAnnotations(symbolInformation);
}

private String formatAnnotations(SymbolInformation symInfo) {
return symInfo.getAnnotationsList().stream()
.map(this::formatAnnotation)
.collect(Collectors.joining("\n"));
}

private String formatAnnotation(Annotation annotation) {
StringBuilder b = new StringBuilder();
b.append('@');
b.append(formatType(annotation.getTpe()));
if (annotation.getParametersCount() > 0) {
b.append('(');
Annotation.ParameterPair firstParam = annotation.getParameters(0);
if (annotation.getParametersCount() == 1
&& SymbolDescriptor.parseFromSymbol(firstParam.getKey())
.descriptor
.name
.equals("value")) {
b.append(formatTree(firstParam.getValue()));
} else {
String parameter =
annotation.getParametersList().stream()
.map(
p ->
SymbolDescriptor.parseFromSymbol(p.getKey()).descriptor.name
+ " = "
+ formatTree(p.getValue()))
.collect(Collectors.joining(", "));
b.append(parameter);
}
b.append(')');
}
return b.toString();
}

private String formatTree(Tree tree) {
if (tree.hasIdTree()) {
return SymbolDescriptor.parseFromSymbol(tree.getIdTree().getSymbol()).descriptor.name;
} else if (tree.hasLiteralTree()) {
return formatConstant(tree.getLiteralTree().getConstant());
} else if (tree.hasSelectTree()) {
return formatTree(tree.getSelectTree().getQualifier())
+ "."
+ SymbolDescriptor.parseFromSymbol(tree.getSelectTree().getId().getSymbol())
.descriptor
.name;
} else if (tree.hasAnnotationTree()) {
return formatAnnotation(tree.getAnnotationTree().getAnnotation());
} else if (tree.hasApplyTree()) {
if (tree.getApplyTree().getFunction().hasIdTree()
&& tree.getApplyTree().getFunction().getIdTree().getSymbol().equals(ARRAY_SYMBOL)) {
return tree.getApplyTree().getArgumentsList().stream()
.map(this::formatTree)
.collect(Collectors.joining(", ", "{", "}"));
} else {
throw new IllegalArgumentException(
"unexpected apply tree function " + tree.getApplyTree().getFunction());
}
}

throw new IllegalArgumentException("tree was of unexpected type " + tree);
}

private String formatConstant(Constant constant) {
if (constant.hasBooleanConstant()) {
return Boolean.toString(constant.getBooleanConstant().getValue());
} else if (constant.hasByteConstant()) {
return Integer.toString(constant.getByteConstant().getValue());
} else if (constant.hasShortConstant()) {
return Integer.toString(constant.getShortConstant().getValue());
} else if (constant.hasCharConstant()) {
return String.valueOf((char) constant.getCharConstant().getValue());
} else if (constant.hasIntConstant()) {
return Integer.toString(constant.getIntConstant().getValue());
} else if (constant.hasLongConstant()) {
return Long.toString(constant.getLongConstant().getValue());
} else if (constant.hasFloatConstant()) {
return Float.toString(constant.getFloatConstant().getValue()) + 'f';
} else if (constant.hasDoubleConstant()) {
return Double.toString(constant.getDoubleConstant().getValue());
} else if (constant.hasStringConstant()) {
return '"' + constant.getStringConstant().getValue() + '"';
}
throw new IllegalArgumentException("constant was not of known type " + constant);
}

private String formatType(Type type) {
StringBuilder b = new StringBuilder();
if (type.hasTypeRef()) {
Expand Down Expand Up @@ -307,6 +402,11 @@ private void printKeyword(String keyword) {
s.append(keyword).append(' ');
}

private void printKeywordln(String keyword) {
if (keyword.isEmpty()) return;
s.append(keyword).append('\n');
}

private boolean isEnumConstant(SymbolInformation symInfo) {
if (!(has(Property.ENUM, symInfo)
&& has(Property.FINAL, symInfo)
Expand Down
101 changes: 101 additions & 0 deletions semanticdb-java/src/main/protobuf/semanticdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,24 @@ message SymbolInformation {
Kind kind = 3;
int32 properties = 4;
string display_name = 5;
repeated Annotation annotations = 13;
Signature signature = 17;
Access access = 18;
repeated string overridden_symbols = 19;
Documentation documentation = 20;
}

message Annotation {
Type tpe = 1;
// -- OUT OF SPEC -- //
message ParameterPair {
string key = 1;
Tree value = 2;
}
repeated ParameterPair parameters = 2;
// -- OUT OF SPEC -- //
}

message Access {
oneof sealed_value {
PrivateAccess private_access = 1;
Expand Down Expand Up @@ -178,4 +190,93 @@ message ExistentialType {
reserved 2;
Type tpe = 1;
Scope declarations = 3;
}

message Tree {
oneof sealed_value {
ApplyTree apply_tree = 1;
IdTree id_tree = 3;
LiteralTree literal_tree = 4;
OriginalTree original_tree = 6;
SelectTree select_tree = 7;
// -- OUT OF SPEC -- //
AnnotationTree annotation_tree = 8;
// -- OUT OF SPEC -- //
}
}

message ApplyTree {
Tree function = 1;
repeated Tree arguments = 2;
}

message IdTree {
string symbol = 1;
}

message OriginalTree {
Range range = 1;
}

message LiteralTree {
Constant constant = 1;
}

message SelectTree {
Tree qualifier = 1;
IdTree id = 2;
}

message AnnotationTree {
Annotation annotation = 1;
}

message Constant {
oneof sealed_value {
BooleanConstant boolean_constant = 2;
ByteConstant byte_constant = 3;
ShortConstant short_constant = 4;
CharConstant char_constant = 5;
IntConstant int_constant = 6;
LongConstant long_constant = 7;
FloatConstant float_constant = 8;
DoubleConstant double_constant = 9;
StringConstant string_constant = 10;
}
}

message BooleanConstant {
bool value = 1;
}

message ByteConstant {
int32 value = 1;
}

message ShortConstant {
int32 value = 1;
}

message CharConstant {
int32 value = 1;
}

message IntConstant {
int32 value = 1;
}

message LongConstant {
int64 value = 1;
}

message FloatConstant {
float value = 1;
}

message DoubleConstant {
double value = 1;
}

message StringConstant {
string value = 1;
}
Loading

0 comments on commit f8db39d

Please sign in to comment.