Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,101 @@ 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(AnnotationTree annotation) {
StringBuilder b = new StringBuilder();
b.append('@');
b.append(formatType(annotation.getTpe()));
if (annotation.getParametersCount() > 0) {
b.append('(');
AssignTree firstParam = annotation.getParameters(0).getAssignTree();
// if only 1 parameter, and its LHS is named 'value', we can omit the 'value = '
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.7.3
if (annotation.getParametersCount() == 1
Comment thread
Strum355 marked this conversation as resolved.
&& SymbolDescriptor.parseFromSymbol(firstParam.getLhs().getIdTree().getSymbol())
.descriptor
.name
.equals("value")) {
b.append(formatTree(firstParam.getRhs()));
} else {
String parameter =
annotation.getParametersList().stream()
.map(
p ->
SymbolDescriptor.parseFromSymbol(
p.getAssignTree().getLhs().getIdTree().getSymbol())
.descriptor
.name
+ " = "
+ formatTree(p.getAssignTree().getRhs()))
.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());
} 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) {
Comment thread
Strum355 marked this conversation as resolved.
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 +407,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,6 +103,9 @@ message SymbolInformation {
Kind kind = 3;
int32 properties = 4;
string display_name = 5;
// -- OUT OF SPEC -- //
repeated AnnotationTree annotations = 13;
// -- OUT OF SPEC -- //
Signature signature = 17;
Access access = 18;
repeated string overridden_symbols = 19;
Expand Down Expand Up @@ -178,4 +181,102 @@ 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 = 9;
AssignTree assign_tree = 10;
// -- 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;
}

// -- OUT OF SPEC -- //
message AnnotationTree {
Type tpe = 1;
repeated Tree parameters = 2;
}

message AssignTree {
Tree lhs = 1;
Tree rhs = 2;
}
// -- OUT OF SPEC -- //

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