Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 4b3cfa3

Browse files
chore: Add SymbolUsageKind to GraphQL API (#63371)
Mostly a documentation-related PR, the implementation will be done later. Fixes GRAPH-697 Co-authored-by: Anton Sviridov <[email protected]>
1 parent 2c2ec72 commit 4b3cfa3

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

cmd/frontend/graphqlbackend/codeintel.codenav.graphql

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,149 @@ type Usage {
721721
until the start/end of the file.
722722
"""
723723
surroundingContent(surroundingLines: SurroundingLines = { linesBefore: 0, linesAfter: 0 }): String
724+
725+
"""
726+
Describes the relationship between this usage and the input symbol
727+
(as specified by the source range or directly).
728+
"""
729+
usageKind: SymbolUsageKind!
730+
}
731+
732+
"""
733+
Categorizes a usage based on its relationship to the symbol of interest.
734+
735+
This enum may be expanded in the future.
736+
737+
EXPERIMENTAL: This type may change in a backwards-incompatible way in the future.
738+
"""
739+
enum SymbolUsageKind {
740+
"""
741+
Denotes a usage as being a definition.
742+
743+
interface Animal:
744+
sound()
745+
746+
class Dog implements Animal:
747+
sound() override { ... }
748+
749+
func makeSounds(animal: Animal, dog: Dog):
750+
animal.sound()
751+
# ^---^ (1)
752+
dog.sound()
753+
# ^---^ (2)
754+
755+
Here, usagesForSymbol for (1) will return a Definition usage for Animal.sound().
756+
Similarly, usagesForSymbol for (2) will return a Definition usage for Dog.sound().
757+
758+
In the general case, a symbol may have multiple definitions.
759+
Here are some examples:
760+
761+
1. Python allows for multiple inheritance, so the same field can
762+
be declared in multiple parent classes. In such a situation,
763+
even Precise results may have multiple definitions.
764+
2. A function may have different definitions based on the build
765+
configuration, such as for macOS vs Windows. A precise SCIP indexer
766+
may unify all such definitions into a single index as SCIP
767+
currently (as of June 20 2024) doesn't support tracking build
768+
configuration.
769+
3. Syntactic or search-based results may not be able to find the
770+
exact definition, so they may return a superset of the full set
771+
of definitions.
772+
"""
773+
DEFINITION
774+
775+
"""
776+
Denotes a usage as being a reference. References are unified across
777+
the inheritance hierarchy. For example, consider the following pseudocode:
778+
779+
interface Animal:
780+
sound()
781+
782+
class Dog implements Animal:
783+
sound() override { ... }
784+
785+
func makeSounds(animal: Animal, dog: Dog):
786+
animal.sound()
787+
# ^---^ (1)
788+
dog.sound()
789+
# ^---^ (2)
790+
791+
Here, usagesForSymbol for both (1) and (2) will return Reference usages
792+
for both Animal.sound() and Dog.sound().
793+
- For (1), it makes sense to also return reference usages for Dog.sound()
794+
because 'animal' may actually be a Dog.
795+
- For (2), it makes sense to also return reference usages for Animal.sound()
796+
because 'dog' value may be up-cast to Animal at some point and the
797+
and 'sound()' might be called on it after that.
798+
"""
799+
REFERENCE
800+
801+
"""
802+
Denotes a usage as being an 'implementation', generally of a method, interface
803+
or similar (the exact terminology varies across languages - traits, protocols etc.).
804+
805+
For example, consider the following pseudocode:
806+
807+
interface Animal:
808+
# ^----^ (1)
809+
sound()
810+
# ^---^ (2)
811+
812+
class Dog implements Animal:
813+
sound() override { ... }
814+
815+
Here, usagesForSymbol for (1) will return an Implementation usage for Dog.
816+
Similarly, usagesForSymbol for (2) will return an Implementation usage for Dog.sound().
817+
818+
As of June 20 2024, Implementation usages are only supported by
819+
Precise indexers. Syntactic and search-based usagesForSymbol will mark all
820+
such usages as Reference.
821+
"""
822+
IMPLEMENTATION
823+
824+
"""
825+
Denotes a usage as being a 'super', generally of a method, type or similar.
826+
The exact terminology varies across languages and the syntax under question -
827+
for functions, it might be 'superclass method', 'interface method', 'trait method' etc.
828+
and for types, it might be 'superclass', 'interface', 'trait' etc.
829+
830+
For example, consider the following pseudocode:
831+
832+
interface Animal:
833+
sound()
834+
835+
class Dog implements Animal:
836+
sound() override { ... }
837+
838+
func bark(dog: Dog):
839+
^-^ (1)
840+
dog.sound()
841+
^---^ (2)
842+
843+
Here, usagesForSymbol for (1) will return a Super usage for Animal.
844+
Similarly, usagesForSymbol for (2) will return a Super usage for Animal.sound().
845+
846+
As of June 20 2024, Super usages are only supported by
847+
Precise indexers. Syntactic and search-based usagesForSymbol will mark all
848+
such usages as Reference.
849+
850+
UI note: Strictly speaking, depending on the exact symbol and language under
851+
consideration, 'Super' usages would be better be grouped under a heading like:
852+
853+
- Method specification (for methods satisfying the signature of an interface
854+
method in Go or Java)
855+
- Interface (for types implementing an interface in Go or Java)
856+
- Trait method (for methods satisfying the signature of a trait method in Rust)
857+
- Trait (for types implementing a trait in Rust)
858+
859+
and so on. Due to this large variation across languages, we've chosen
860+
to group all such usages under 'Super' for now.
861+
862+
Historical note: This was previously called 'prototype' in the old API.
863+
However, 'prototype' has a specific meaning in C++ different from our usage,
864+
so we recommend avoiding the term 'prototype' in the UI.
865+
"""
866+
SUPER
724867
}
725868

726869
"""

internal/codeintel/codenav/transport/graphql/root_resolver_usages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (u *usageResolver) SurroundingContent(_ context.Context, args *struct {
4242
panic("implement me")
4343
}
4444

45+
func (u *usageResolver) UsageKind() resolverstubs.SymbolUsageKind {
46+
panic("implement me")
47+
}
48+
4549
type symbolInformationResolver struct {
4650
}
4751

internal/codeintel/resolvers/codenav.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ type UsageResolver interface {
615615
SurroundingContent(_ context.Context, args *struct {
616616
*SurroundingLines `json:"surroundingLines"`
617617
}) (*string, error)
618+
UsageKind() SymbolUsageKind
618619
}
619620

620621
type SymbolInformationResolver interface {
@@ -635,3 +636,16 @@ type SurroundingLines struct {
635636
LinesBefore *int32 `json:"linesBefore"`
636637
LinesAfter *int32 `json:"linesAfter"`
637638
}
639+
640+
// SymbolUsageKind corresponds to the matching type in the GraphQL API.
641+
//
642+
// Make sure this type maintains its marshaling/unmarshaling behavior in
643+
// case the type definition is changed.
644+
type SymbolUsageKind string
645+
646+
const (
647+
UsageKindDefinition SymbolUsageKind = "DEFINITION"
648+
UsageKindReference SymbolUsageKind = "REFERENCE"
649+
UsageKindImplementation SymbolUsageKind = "IMPLEMENTATION"
650+
UsageKindSuper SymbolUsageKind = "SUPER"
651+
)

0 commit comments

Comments
 (0)