Skip to content

Commit d60f0e6

Browse files
🐛 creating an enum for the locations to make the code understandable (#1021)
fixes #977 @shawn-hurley is this what you had in mind? I looked at the link you had in the issue. I also changed the `symbolKindToString` fn with same approach. Let me know if this makes sense. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Expanded symbol recognition with many additional symbol kinds for richer code insights. * Finer-grained location categories to improve tracking of where code elements appear. * **Refactor** * Replaced numeric codes with type-safe enumerations for symbol kinds and locations, making behavior more consistent and maintainable. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Savitha Raghunathan <[email protected]>
1 parent 8225fad commit d60f0e6

File tree

2 files changed

+105
-50
lines changed

2 files changed

+105
-50
lines changed

external-providers/java-external-provider/pkg/java_external_provider/provider.go

Lines changed: 96 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,46 @@ const (
4848
baseDepKey = "baseDep"
4949
)
5050

51+
// LocationType represents different types of rule locations for filtering
52+
type LocationType int
53+
54+
const (
55+
LocationTypeDefault LocationType = iota // 0 - Type (default)
56+
LocationInheritance // 1 - inheritance
57+
LocationMethodCall // 2 - method_call
58+
LocationConstructorCall // 3 - constructor_call
59+
LocationAnnotation // 4 - annotation
60+
LocationImplementsType // 5 - implements_type
61+
LocationEnum // 6 - enum (not implemented)
62+
LocationReturnType // 7 - return_type
63+
LocationImport // 8 - import
64+
LocationVariableDeclaration // 9 - variable_declaration
65+
LocationTypeKeyword // 10 - type
66+
LocationPackage // 11 - package
67+
LocationField // 12 - field
68+
LocationMethod // 13 - method
69+
LocationClass // 14 - class
70+
)
71+
5172
// Rule Location to location that the bundle understands
5273
var locationToCode = map[string]int{
5374
//Type is the default.
54-
"": 0,
55-
"inheritance": 1,
56-
"method_call": 2,
57-
"constructor_call": 3,
58-
"annotation": 4,
59-
"implements_type": 5,
75+
"": int(LocationTypeDefault),
76+
"inheritance": int(LocationInheritance),
77+
"method_call": int(LocationMethodCall),
78+
"constructor_call": int(LocationConstructorCall),
79+
"annotation": int(LocationAnnotation),
80+
"implements_type": int(LocationImplementsType),
6081
// Not Implemented
61-
"enum": 6,
62-
"return_type": 7,
63-
"import": 8,
64-
"variable_declaration": 9,
65-
"type": 10,
66-
"package": 11,
67-
"field": 12,
68-
"method": 13,
69-
"class": 14,
82+
"enum": int(LocationEnum),
83+
"return_type": int(LocationReturnType),
84+
"import": int(LocationImport),
85+
"variable_declaration": int(LocationVariableDeclaration),
86+
"type": int(LocationTypeKeyword),
87+
"package": int(LocationPackage),
88+
"field": int(LocationField),
89+
"method": int(LocationMethod),
90+
"class": int(LocationClass),
7091
}
7192

7293
type javaProvider struct {
@@ -157,59 +178,92 @@ func (p *javaProvider) Capabilities() []provider.Capability {
157178
return caps
158179
}
159180

181+
// SymbolKind represents LSP symbol kinds
182+
type SymbolKind int
183+
184+
const (
185+
_ SymbolKind = iota // Skip 0
186+
SymbolKindFile
187+
SymbolKindModule
188+
SymbolKindNamespace
189+
SymbolKindPackage
190+
SymbolKindClass
191+
SymbolKindMethod
192+
SymbolKindProperty
193+
SymbolKindField
194+
SymbolKindConstructor
195+
SymbolKindEnum
196+
SymbolKindInterface
197+
SymbolKindFunction
198+
SymbolKindVariable
199+
SymbolKindConstant
200+
SymbolKindString
201+
SymbolKindNumber
202+
SymbolKindBoolean
203+
SymbolKindArray
204+
SymbolKindObject
205+
SymbolKindKey
206+
SymbolKindNull
207+
SymbolKindEnumMember
208+
SymbolKindStruct
209+
SymbolKindEvent
210+
SymbolKindOperator
211+
SymbolKindTypeParameter
212+
)
213+
160214
func symbolKindToString(symbolKind protocol.SymbolKind) string {
161-
switch symbolKind {
162-
case 1:
215+
switch SymbolKind(symbolKind) {
216+
case SymbolKindFile:
163217
return "File"
164-
case 2:
218+
case SymbolKindModule:
165219
return "Module"
166-
case 3:
220+
case SymbolKindNamespace:
167221
return "Namespace"
168-
case 4:
222+
case SymbolKindPackage:
169223
return "Package"
170-
case 5:
224+
case SymbolKindClass:
171225
return "Class"
172-
case 6:
226+
case SymbolKindMethod:
173227
return "Method"
174-
case 7:
228+
case SymbolKindProperty:
175229
return "Property"
176-
case 8:
230+
case SymbolKindField:
177231
return "Field"
178-
case 9:
232+
case SymbolKindConstructor:
179233
return "Constructor"
180-
case 10:
234+
case SymbolKindEnum:
181235
return "Enum"
182-
case 11:
236+
case SymbolKindInterface:
183237
return "Interface"
184-
case 12:
238+
case SymbolKindFunction:
185239
return "Function"
186-
case 13:
240+
case SymbolKindVariable:
187241
return "Variable"
188-
case 14:
242+
case SymbolKindConstant:
189243
return "Constant"
190-
case 15:
244+
case SymbolKindString:
191245
return "String"
192-
case 16:
246+
case SymbolKindNumber:
193247
return "Number"
194-
case 17:
248+
case SymbolKindBoolean:
195249
return "Boolean"
196-
case 18:
250+
case SymbolKindArray:
197251
return "Array"
198-
case 19:
252+
case SymbolKindObject:
199253
return "Object"
200-
case 20:
254+
case SymbolKindKey:
201255
return "Key"
202-
case 21:
256+
case SymbolKindNull:
203257
return "Null"
204-
case 22:
258+
case SymbolKindEnumMember:
205259
return "EnumMember"
206-
case 23:
260+
case SymbolKindStruct:
207261
return "Struct"
208-
case 24:
262+
case SymbolKindEvent:
209263
return "Event"
210-
case 25:
264+
case SymbolKindOperator:
211265
return "Operator"
212-
case 26:
266+
case SymbolKindTypeParameter:
213267
return "TypeParameter"
214268
}
215269
return ""

external-providers/java-external-provider/pkg/java_external_provider/service_client.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ func (p *javaServiceClient) Evaluate(ctx context.Context, cap string, conditionI
7575
p.log.Info("Symbols retrieved", "symbols", len(symbols), "cap", cap, "conditionInfo", cond)
7676

7777
incidents := []provider.IncidentContext{}
78-
switch locationToCode[strings.ToLower(cond.Referenced.Location)] {
79-
case 0, 3, 4, 6, 10, 11, 12, 13, 14:
80-
// Filter handle for type, find all the referneces to this type.
78+
locationCode := LocationType(locationToCode[strings.ToLower(cond.Referenced.Location)])
79+
switch locationCode {
80+
case LocationTypeDefault, LocationConstructorCall, LocationAnnotation, LocationEnum, LocationTypeKeyword, LocationPackage, LocationField, LocationMethod, LocationClass:
81+
// Filter handle for type, find all the references to this type.
8182
incidents, err = p.filterDefault(symbols)
82-
case 1, 5:
83+
case LocationInheritance, LocationImplementsType:
8384
incidents, err = p.filterTypesInheritance(symbols)
84-
case 2:
85+
case LocationMethodCall:
8586
incidents, err = p.filterMethodSymbols(symbols)
86-
case 7:
87+
case LocationReturnType:
8788
incidents, err = p.filterMethodSymbols(symbols)
88-
case 8:
89+
case LocationImport:
8990
incidents, err = p.filterModulesImports(symbols)
90-
case 9:
91+
case LocationVariableDeclaration:
9192
incidents, err = p.filterVariableDeclaration(symbols)
9293
default:
9394

0 commit comments

Comments
 (0)