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: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
pname = "scip-go";
inherit version;
src = ./.;
vendorHash = "sha256-V+A5nMSnAAk+FBoMX1frf5eZUETvLb2BCE81WuEx33c=";
vendorHash = "sha256-AdEvJ4KxxzNaaBiewwv2IRpasQotF4P08aDEF1ownW0=";
subPackages = [ "cmd/scip-go" ];
env.CGO_ENABLED = 0;
checkPhase = "go test ./...";
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.25.0

require (
github.com/alecthomas/kong v1.15.0
github.com/scip-code/scip/bindings/go/scip v0.8.1
github.com/scip-code/scip/bindings/go/scip v0.9.0
golang.org/x/mod v0.37.0
golang.org/x/tools v0.47.0
golang.org/x/tools/go/vcs v0.1.0-deprecated
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/scip-code/scip/bindings/go/scip v0.8.0 h1:WdmhYzDbzgCe8D1PhS61VtFsa+/
github.com/scip-code/scip/bindings/go/scip v0.8.0/go.mod h1:QhuSgP19HyWJIU/bvfBGn/RmkL/BX2IPoZWTNQ9M5wY=
github.com/scip-code/scip/bindings/go/scip v0.8.1 h1:ZkyT9RDgkbd6ffvMbi9nYdwh7+gLoz8Z9ftFMxgUiVs=
github.com/scip-code/scip/bindings/go/scip v0.8.1/go.mod h1:QhuSgP19HyWJIU/bvfBGn/RmkL/BX2IPoZWTNQ9M5wY=
github.com/scip-code/scip/bindings/go/scip v0.9.0 h1:C0LVhTl9Gw+2UC4d7RZdvB0iWUkaOyRA1fQW1CrhsMA=
github.com/scip-code/scip/bindings/go/scip v0.9.0/go.mod h1:QhuSgP19HyWJIU/bvfBGn/RmkL/BX2IPoZWTNQ9M5wY=
github.com/sourcegraph/beaut v0.0.0-20240611013027-627e4c25335a h1:j/CQ27s679M9wRGBRJYyXGrfkYuQA6VMnD7R08mHD9c=
github.com/sourcegraph/beaut v0.0.0-20240611013027-627e4c25335a/go.mod h1:JG1sdvGTKWwe/oH3/3UKQ26vfcHIN//7fwEJhoqaBcM=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand Down
2 changes: 1 addition & 1 deletion internal/index/scip.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func indexVisitPackages(
position := pkg.Fset.Position(f.Name.NamePos)

doc.PackageOccurrence = &scip.Occurrence{
Range: symbols.RangeFromName(position, f.Name.Name, false),
TypedRange: symbols.RangeFromName(position, f.Name.Name, false).AsTypedRange(),
Symbol: pkgSymbol,
SymbolRoles: int32(scip.SymbolRole_Definition),
}
Expand Down
24 changes: 24 additions & 0 deletions internal/index/scip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestSnapshots(t *testing.T) {
filteredDocs = append(filteredDocs, doc)
}
scipIndex.Documents = filteredDocs
assertTypedOccurrenceRanges(t, &scipIndex)

symbolFormatter := scip.SymbolFormatter{
OnError: func(err error) error { return err },
Expand All @@ -105,6 +106,29 @@ func TestSnapshots(t *testing.T) {
)
}

func assertTypedOccurrenceRanges(t *testing.T, scipIndex *scip.Index) {
t.Helper()
for _, doc := range scipIndex.Documents {
for i, occ := range doc.Occurrences {
if occ == nil {
t.Fatalf("%s: nil occurrence at index %d", doc.RelativePath, i)
}
if occ.GetTypedRange() == nil {
t.Fatalf("%s: occurrence %q has no typed range", doc.RelativePath, occ.GetSymbol())
}
if _, ok := occ.SourceRange(); !ok {
t.Fatalf("%s: occurrence %q has malformed typed range", doc.RelativePath, occ.GetSymbol())
}
if len(occ.GetRange()) != 0 {
t.Fatalf("%s: occurrence %q set deprecated range: %v", doc.RelativePath, occ.GetSymbol(), occ.GetRange())
}
if len(occ.GetEnclosingRange()) != 0 {
t.Fatalf("%s: occurrence %q set deprecated enclosing_range: %v", doc.RelativePath, occ.GetSymbol(), occ.GetEnclosingRange())
}
}
}
}

// getTestdataRoot returns the absolute path to the testdata directory of this repository.
func getTestdataRoot(t *testing.T) string {
testdata, err := filepath.Abs("../testdata")
Expand Down
7 changes: 5 additions & 2 deletions internal/symbols/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func FromDescriptors(pkg *packages.Package, descriptors ...*scip.Descriptor) str
})
}

func RangeFromName(position token.Position, name string, adjust bool) []int32 {
func RangeFromName(position token.Position, name string, adjust bool) scip.Range {
var adjustment int32 = 0
if adjust {
adjustment = 1
Expand All @@ -34,7 +34,10 @@ func RangeFromName(position token.Position, name string, adjust bool) []int32 {
column := int32(position.Column - 1)
n := int32(len(name))

return []int32{line, column + adjustment, column + n + adjustment}
return scip.Range{
Start: scip.Position{Line: line, Character: column + adjustment},
End: scip.Position{Line: line, Character: column + n + adjustment},
}
}

func FormatCode(v string) string {
Expand Down
38 changes: 16 additions & 22 deletions internal/visitors/visitor_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,9 @@ func (v *fileVisitor) Visit(n ast.Node) ast.Visitor {
}

if node.Name != nil && node.Name.Name != "." && node.Name.Name != "_" {
rangeFromName := symbols.RangeFromName(
v.pkg.Fset.Position(node.Name.Pos()), node.Name.Name, false)
if rangeFromName != nil {
if sym, ok := v.globalSymbols.GetPkgSymbol(importedPackage); ok {
v.newReference(sym, rangeFromName, false)
}
if sym, ok := v.globalSymbols.GetPkgSymbol(importedPackage); ok {
v.newReference(sym, symbols.RangeFromName(
v.pkg.Fset.Position(node.Name.Pos()), node.Name.Name, false), false)
}
}

Expand Down Expand Up @@ -279,31 +276,27 @@ func (v *fileVisitor) emitImportReference(
position token.Position,
importedPackage *packages.Package,
) {
scipRange := symbols.RangeFromName(position, importedPackage.PkgPath, true)
if scipRange == nil {
slog.Debug(fmt.Sprintf("Missing symbol for package path: %s", importedPackage.ID))
return
}

sym, ok := globalSymbols.GetPkgSymbol(importedPackage)
if !ok {
slog.Debug(fmt.Sprintf("Missing symbol information for package: %s", importedPackage.ID))
return
}

v.newReference(sym, scipRange, false)
v.newReference(sym, symbols.RangeFromName(position, importedPackage.PkgPath, true), false)
}

// newDefinition emits a scip.Occurence ONLY. This will not emit a
// new symbol. You must do that using DeclareNewSymbol[ForPos]
func (v *fileVisitor) newDefinition(
symbol string, rng []int32, enclRng []int32, deprecated bool,
symbol string, rng scip.Range, enclRng *scip.Range, deprecated bool,
) {
occ := &scip.Occurrence{
Range: rng,
Symbol: symbol,
SymbolRoles: int32(scip.SymbolRole_Definition),
EnclosingRange: enclRng,
TypedRange: rng.AsTypedRange(),
Symbol: symbol,
SymbolRoles: int32(scip.SymbolRole_Definition),
}
if enclRng != nil {
occ.TypedEnclosingRange = enclRng.AsTypedEnclosingRange()
}
if deprecated {
occ.Diagnostics = deprecatedDiagnostics()
Expand All @@ -312,10 +305,10 @@ func (v *fileVisitor) newDefinition(
}

func (v *fileVisitor) newReference(
symbol string, rng []int32, deprecated bool,
symbol string, rng scip.Range, deprecated bool,
) {
occ := &scip.Occurrence{
Range: rng,
TypedRange: rng.AsTypedRange(),
Symbol: symbol,
SymbolRoles: int32(scip.SymbolRole_ReadAccess),
}
Expand Down Expand Up @@ -364,13 +357,14 @@ func (v *fileVisitor) ToScipDocument() *scip.Document {
}
}

func (v *fileVisitor) enclosingRange(n *ast.Ident) []int32 {
func (v *fileVisitor) enclosingRange(n *ast.Ident) *scip.Range {
if v.currentFuncDecl == nil || v.currentFuncDecl.Name != n {
return nil
}
startPosition := v.pkg.Fset.Position(v.currentFuncDecl.Pos())
endPosition := v.pkg.Fset.Position(v.currentFuncDecl.End())
return scipRange(startPosition, endPosition, v.pkg.TypesInfo.Defs[n])
rng := scipRange(startPosition, endPosition, v.pkg.TypesInfo.Defs[n])
return &rng
}

func deprecatedDiagnostics() []*scip.Diagnostic {
Expand Down
8 changes: 4 additions & 4 deletions internal/visitors/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func descriptorTerm(name string) *scip.Descriptor {
}
}

func scipRange(start, end token.Position, obj types.Object) []int32 {
func scipRange(start, end token.Position, obj types.Object) scip.Range {
var adjustment int32 = 0
if pkgName, ok := obj.(*types.PkgName); ok && strings.HasPrefix(pkgName.Name(), `"`) {
adjustment = 1
Expand All @@ -105,10 +105,10 @@ func scipRange(start, end token.Position, obj types.Object) []int32 {
startColumn := int32(start.Column - 1)
endLine := int32(end.Line - 1)
endColumn := int32(end.Column - 1)
if startLine != endLine {
return []int32{startLine, startColumn + adjustment, endLine, endColumn - adjustment}
return scip.Range{
Start: scip.Position{Line: startLine, Character: startColumn + adjustment},
End: scip.Position{Line: endLine, Character: endColumn - adjustment},
}
return []int32{startLine, startColumn + adjustment, endColumn - adjustment}
}

func getIdentOfTypeExpr(pkg *packages.Package, ty ast.Expr) []*ast.Ident {
Expand Down
Loading