Skip to content

Commit 687fef8

Browse files
authored
Merge pull request #148 from sourcegraph/nsc/symbol-annotation-format
2 parents debdbb2 + f0c3556 commit 687fef8

File tree

85 files changed

+1157
-751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1157
-751
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ _site/
2929
metals.sbt
3030
metals/project/
3131

32+
.bsp
33+
3234
.vscode/
3335

3436
local.*

.jvmopts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-Xss2m
22
-Xms1G
33
-Xmx4G
4-
-Dfile.encoding=UTF-8
5-
-Dsbt.server.autostart=false
4+
-Dfile.encoding=UTF-8

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ lazy val unit = project
248248
.in(file("tests/unit"))
249249
.settings(
250250
testSettings,
251+
//javaOptions ++= Seq( "-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"),
251252
buildInfoKeys :=
252253
Seq[BuildInfoKey](
253254
version,

lsif-java/src/main/scala/com/sourcegraph/lsif_java/SemanticdbPrinters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ object SemanticdbPrinters {
8888
if (sig.isEmpty)
8989
" " + info.getDisplayName
9090
else
91-
" " + sig
91+
" " + sig.replace('\n', ' ')
9292
case _ =>
9393
""
9494
}

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/SnapshotLsifCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ object SnapshotLsifCommand {
138138
// we cheese it a bit here, as this is less work than trying to reconstruct
139139
// a Signature from the pretty-printed Signature, with accompanying logic
140140
// to fallback to display_name in SemanticdbPrinters.scala
141-
.setDisplayName(hover).setSymbol(symbol).build()
141+
.setDisplayName(hover.replace('\n', ' ')).setSymbol(symbol).build()
142142
doc.addSymbols(symInfo)
143143
}
144144
}

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/SignatureFormatter.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.sourcegraph.semanticdb_javac.Semanticdb.*;
55

66
import java.util.ArrayList;
7+
import java.util.Arrays;
78
import java.util.List;
89
import java.util.Objects;
910
import java.util.concurrent.atomic.AtomicInteger;
@@ -51,6 +52,8 @@ private void formatClassSignature(ClassSignature classSignature) {
5152

5253
boolean isEnum = has(Property.ENUM);
5354

55+
printKeywordln(formatAnnotations());
56+
5457
printKeyword(formatAccess());
5558
if (!isEnum && !isAnnotation) printKeyword(formatModifiers());
5659

@@ -143,6 +146,7 @@ private void formatClassSignature(ClassSignature classSignature) {
143146
}
144147

145148
private void formatMethodSignature(MethodSignature methodSignature) {
149+
printKeywordln(formatAnnotations());
146150
printKeyword(formatAccess());
147151
printKeyword(formatModifiers());
148152

@@ -177,6 +181,7 @@ private void formatMethodSignature(MethodSignature methodSignature) {
177181
}
178182

179183
private void formatValueSignature(ValueSignature valueSignature) {
184+
printKeywordln(formatAnnotations());
180185
if (isEnumConstant()) {
181186
String ownerSym = SymbolDescriptor.parseFromSymbol(symbolInformation.getSymbol()).owner;
182187
SymbolInformation ownerInfo = symtab.symbols.get(ownerSym);
@@ -234,6 +239,101 @@ private String formatTypeArguments(List<Type> typeArguments) {
234239
return typeArguments.stream().map(this::formatType).collect(Collectors.joining(", ", "<", ">"));
235240
}
236241

242+
private String formatAnnotations() {
243+
return formatAnnotations(symbolInformation);
244+
}
245+
246+
private String formatAnnotations(SymbolInformation symInfo) {
247+
return symInfo.getAnnotationsList().stream()
248+
.map(this::formatAnnotation)
249+
.collect(Collectors.joining("\n"));
250+
}
251+
252+
private String formatAnnotation(AnnotationTree annotation) {
253+
StringBuilder b = new StringBuilder();
254+
b.append('@');
255+
b.append(formatType(annotation.getTpe()));
256+
if (annotation.getParametersCount() > 0) {
257+
b.append('(');
258+
AssignTree firstParam = annotation.getParameters(0).getAssignTree();
259+
// if only 1 parameter, and its LHS is named 'value', we can omit the 'value = '
260+
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.7.3
261+
if (annotation.getParametersCount() == 1
262+
&& SymbolDescriptor.parseFromSymbol(firstParam.getLhs().getIdTree().getSymbol())
263+
.descriptor
264+
.name
265+
.equals("value")) {
266+
b.append(formatTree(firstParam.getRhs()));
267+
} else {
268+
String parameter =
269+
annotation.getParametersList().stream()
270+
.map(
271+
p ->
272+
SymbolDescriptor.parseFromSymbol(
273+
p.getAssignTree().getLhs().getIdTree().getSymbol())
274+
.descriptor
275+
.name
276+
+ " = "
277+
+ formatTree(p.getAssignTree().getRhs()))
278+
.collect(Collectors.joining(", "));
279+
b.append(parameter);
280+
}
281+
b.append(')');
282+
}
283+
return b.toString();
284+
}
285+
286+
private String formatTree(Tree tree) {
287+
if (tree.hasIdTree()) {
288+
return SymbolDescriptor.parseFromSymbol(tree.getIdTree().getSymbol()).descriptor.name;
289+
} else if (tree.hasLiteralTree()) {
290+
return formatConstant(tree.getLiteralTree().getConstant());
291+
} else if (tree.hasSelectTree()) {
292+
return formatTree(tree.getSelectTree().getQualifier())
293+
+ "."
294+
+ SymbolDescriptor.parseFromSymbol(tree.getSelectTree().getId().getSymbol())
295+
.descriptor
296+
.name;
297+
} else if (tree.hasAnnotationTree()) {
298+
return formatAnnotation(tree.getAnnotationTree());
299+
} else if (tree.hasApplyTree()) {
300+
if (tree.getApplyTree().getFunction().hasIdTree()
301+
&& tree.getApplyTree().getFunction().getIdTree().getSymbol().equals(ARRAY_SYMBOL)) {
302+
return tree.getApplyTree().getArgumentsList().stream()
303+
.map(this::formatTree)
304+
.collect(Collectors.joining(", ", "{", "}"));
305+
} else {
306+
throw new IllegalArgumentException(
307+
"unexpected apply tree function " + tree.getApplyTree().getFunction());
308+
}
309+
}
310+
311+
throw new IllegalArgumentException("tree was of unexpected type " + tree);
312+
}
313+
314+
private String formatConstant(Constant constant) {
315+
if (constant.hasBooleanConstant()) {
316+
return Boolean.toString(constant.getBooleanConstant().getValue());
317+
} else if (constant.hasByteConstant()) {
318+
return Integer.toString(constant.getByteConstant().getValue());
319+
} else if (constant.hasShortConstant()) {
320+
return Integer.toString(constant.getShortConstant().getValue());
321+
} else if (constant.hasCharConstant()) {
322+
return String.valueOf((char) constant.getCharConstant().getValue());
323+
} else if (constant.hasIntConstant()) {
324+
return Integer.toString(constant.getIntConstant().getValue());
325+
} else if (constant.hasLongConstant()) {
326+
return Long.toString(constant.getLongConstant().getValue());
327+
} else if (constant.hasFloatConstant()) {
328+
return Float.toString(constant.getFloatConstant().getValue()) + 'f';
329+
} else if (constant.hasDoubleConstant()) {
330+
return Double.toString(constant.getDoubleConstant().getValue());
331+
} else if (constant.hasStringConstant()) {
332+
return '"' + constant.getStringConstant().getValue() + '"';
333+
}
334+
throw new IllegalArgumentException("constant was not of known type " + constant);
335+
}
336+
237337
private String formatType(Type type) {
238338
StringBuilder b = new StringBuilder();
239339
if (type.hasTypeRef()) {
@@ -307,6 +407,11 @@ private void printKeyword(String keyword) {
307407
s.append(keyword).append(' ');
308408
}
309409

410+
private void printKeywordln(String keyword) {
411+
if (keyword.isEmpty()) return;
412+
s.append(keyword).append('\n');
413+
}
414+
310415
private boolean isEnumConstant(SymbolInformation symInfo) {
311416
if (!(has(Property.ENUM, symInfo)
312417
&& has(Property.FINAL, symInfo)

semanticdb-java/src/main/protobuf/semanticdb.proto

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ message SymbolInformation {
103103
Kind kind = 3;
104104
int32 properties = 4;
105105
string display_name = 5;
106+
// -- OUT OF SPEC -- //
107+
repeated AnnotationTree annotations = 13;
108+
// -- OUT OF SPEC -- //
106109
Signature signature = 17;
107110
Access access = 18;
108111
repeated string overridden_symbols = 19;
@@ -178,4 +181,102 @@ message ExistentialType {
178181
reserved 2;
179182
Type tpe = 1;
180183
Scope declarations = 3;
184+
}
185+
186+
message Tree {
187+
oneof sealed_value {
188+
ApplyTree apply_tree = 1;
189+
IdTree id_tree = 3;
190+
LiteralTree literal_tree = 4;
191+
OriginalTree original_tree = 6;
192+
SelectTree select_tree = 7;
193+
// -- OUT OF SPEC -- //
194+
AnnotationTree annotation_tree = 9;
195+
AssignTree assign_tree = 10;
196+
// -- OUT OF SPEC -- //
197+
}
198+
}
199+
200+
message ApplyTree {
201+
Tree function = 1;
202+
repeated Tree arguments = 2;
203+
}
204+
205+
message IdTree {
206+
string symbol = 1;
207+
}
208+
209+
message OriginalTree {
210+
Range range = 1;
211+
}
212+
213+
message LiteralTree {
214+
Constant constant = 1;
215+
}
216+
217+
message SelectTree {
218+
Tree qualifier = 1;
219+
IdTree id = 2;
220+
}
221+
222+
// -- OUT OF SPEC -- //
223+
message AnnotationTree {
224+
Type tpe = 1;
225+
repeated Tree parameters = 2;
226+
}
227+
228+
message AssignTree {
229+
Tree lhs = 1;
230+
Tree rhs = 2;
231+
}
232+
// -- OUT OF SPEC -- //
233+
234+
message Constant {
235+
oneof sealed_value {
236+
BooleanConstant boolean_constant = 2;
237+
ByteConstant byte_constant = 3;
238+
ShortConstant short_constant = 4;
239+
CharConstant char_constant = 5;
240+
IntConstant int_constant = 6;
241+
LongConstant long_constant = 7;
242+
FloatConstant float_constant = 8;
243+
DoubleConstant double_constant = 9;
244+
StringConstant string_constant = 10;
245+
}
246+
}
247+
248+
message BooleanConstant {
249+
bool value = 1;
250+
}
251+
252+
message ByteConstant {
253+
int32 value = 1;
254+
}
255+
256+
message ShortConstant {
257+
int32 value = 1;
258+
}
259+
260+
message CharConstant {
261+
int32 value = 1;
262+
}
263+
264+
message IntConstant {
265+
int32 value = 1;
266+
}
267+
268+
message LongConstant {
269+
int64 value = 1;
270+
}
271+
272+
message FloatConstant {
273+
float value = 1;
274+
}
275+
276+
message DoubleConstant {
277+
double value = 1;
278+
}
279+
280+
message StringConstant {
281+
string value = 1;
181282
}

0 commit comments

Comments
 (0)