Skip to content

Commit eaa1cac

Browse files
committed
Use more robust logic to determine reference relationship
Previously, we used the symbol itself, which was not reliable because it didn't distinguish between fields an objects. This commit changes the logic to be based on `SymbolInformation.kind` from SemanticDB, that is a more robust and idiomatic solution. Fixes #414.
1 parent cf77817 commit eaa1cac

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/ScipSemanticdb.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.sourcegraph.Scip;
1111

1212
import java.io.IOException;
13-
import java.io.InputStream;
1413
import java.net.URI;
1514
import java.nio.file.*;
1615
import java.util.*;
@@ -131,7 +130,7 @@ private void processTypedDocument(Path path, PackageTable packages) {
131130
Scip.Relationship.newBuilder()
132131
.setSymbol(typedSymbol(overriddenSymbol, overriddenSymbolPkg))
133132
.setIsImplementation(true)
134-
.setIsReference(SemanticdbSymbols.isMethodOrField(info.getSymbol())));
133+
.setIsReference(supportsReferenceRelationship(info)));
135134
}
136135
if (info.hasSignature()) {
137136
String language =
@@ -284,7 +283,7 @@ private Integer processDocumentUnsafe(
284283

285284
// Overrides
286285
if (symbolInformation.getOverriddenSymbolsCount() > 0
287-
&& SemanticdbSymbols.isMethodOrField(symbolInformation.getSymbol())
286+
&& supportsReferenceRelationship(symbolInformation)
288287
&& occ.getRole() == Role.DEFINITION) {
289288
List<Integer> overriddenReferenceResultIds =
290289
new ArrayList<>(symbolInformation.getOverriddenSymbolsCount());
@@ -310,6 +309,17 @@ private Integer processDocumentUnsafe(
310309
return documentId;
311310
}
312311

312+
private static boolean supportsReferenceRelationship(SymbolInformation info) {
313+
switch (info.getKind()) {
314+
case CLASS:
315+
case OBJECT:
316+
case PACKAGE_OBJECT:
317+
return false;
318+
default:
319+
return true;
320+
}
321+
}
322+
313323
private Stream<ScipTextDocument> parseTextDocument(Path semanticdbPath) {
314324
try {
315325
return textDocumentsParseFrom(semanticdbPath).getDocumentsList().stream()

semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbSymbols.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sourcegraph.semanticdb_javac;
22

33
import java.util.Objects;
4-
import java.util.Optional;
54

65
/**
76
* Utilities to construct SemanticDB symbols.
@@ -36,7 +35,7 @@ public static boolean isGlobal(String symbol) {
3635
}
3736

3837
public static boolean isMethodOrField(String symbol) {
39-
return symbol.endsWith(".");
38+
return symbol.endsWith("#");
4039
}
4140

4241
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package minimized
2+
3+
object Issue414 {
4+
trait A {
5+
def b(): Unit
6+
}
7+
val a: A =
8+
new A {
9+
override def b(): Unit = {
10+
print("Hello")
11+
}
12+
}
13+
println(a.b())
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package minimized
2+
// ^^^^^^^^^ definition minimized/
3+
4+
object Issue414 {
5+
// ^^^^^^^^ definition minimized/Issue414. object Issue414
6+
trait A {
7+
// ^ definition minimized/Issue414.A# trait A
8+
def b(): Unit
9+
// ^ definition minimized/Issue414.A#b(). def b(): Unit
10+
// ^^^^ reference scala/Unit#
11+
}
12+
val a: A =
13+
// ^ definition minimized/Issue414.a. val a: A
14+
// ^ reference minimized/Issue414.A#
15+
new A {
16+
// definition local0 final class $anon
17+
// ^ reference minimized/Issue414.A#
18+
// reference java/lang/Object#`<init>`().
19+
override def b(): Unit = {
20+
// ^ definition local1 def b(): Unit
21+
// ^^^^ reference scala/Unit#
22+
print("Hello")
23+
// ^^^^^ reference scala/Predef.print().
24+
}
25+
}
26+
println(a.b())
27+
//^^^^^^^ reference scala/Predef.println(+1).
28+
// ^ reference minimized/Issue414.a.
29+
// ^ reference minimized/Issue414.A#b().
30+
}

0 commit comments

Comments
 (0)