@@ -5,12 +5,8 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8- "os"
9- "path/filepath"
10- "strings"
118
129 basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0"
13- codev0 "github.com/codefly-dev/core/generated/go/codefly/services/code/v0"
1410 runners "github.com/codefly-dev/core/runners/base"
1511)
1612
@@ -176,133 +172,3 @@ print(json.dumps({'calls': calls, 'implements': implements, 'module': workdir}))
176172 result .Module = srcDir
177173 return result
178174}
179-
180- // handleFindReferences finds all usages of a symbol via grep + AST.
181- func (c * Code ) handleFindReferences (ctx context.Context , req * codev0.CodeRequest ) (* codev0.CodeResponse , error ) {
182- c .EnsureInit ()
183- srcDir := c .SourceDir ()
184-
185- refs := req .GetFindReferences ()
186- if refs == nil {
187- return & codev0.CodeResponse {
188- Result : & codev0.CodeResponse_FindReferences {
189- FindReferences : & codev0.FindReferencesResponse {},
190- },
191- }, nil
192- }
193-
194- symbolName , err := symbolAtPosition (srcDir , refs .File , refs .Line , refs .Column )
195- if err != nil || symbolName == "" {
196- return & codev0.CodeResponse {
197- Result : & codev0.CodeResponse_FindReferences {
198- FindReferences : & codev0.FindReferencesResponse {},
199- },
200- }, nil
201- }
202-
203- locations := c .grepForSymbol (ctx , srcDir , symbolName )
204-
205- return & codev0.CodeResponse {
206- Result : & codev0.CodeResponse_FindReferences {
207- FindReferences : & codev0.FindReferencesResponse {
208- Locations : locations ,
209- },
210- },
211- }, nil
212- }
213-
214- func symbolAtPosition (srcDir , file string , line , col int32 ) (string , error ) {
215- absPath := file
216- if ! filepath .IsAbs (file ) {
217- absPath = filepath .Join (srcDir , file )
218- }
219- data , err := os .ReadFile (absPath )
220- if err != nil {
221- return "" , err
222- }
223- lines := strings .Split (string (data ), "\n " )
224- if int (line ) < 1 || int (line ) > len (lines ) {
225- return "" , fmt .Errorf ("line %d out of range" , line )
226- }
227- lineText := lines [line - 1 ]
228- if int (col ) < 1 || int (col ) > len (lineText ) {
229- col = 1
230- }
231- start := int (col ) - 1
232- end := start
233- for start > 0 && isIdentChar (lineText [start - 1 ]) {
234- start --
235- }
236- for end < len (lineText ) && isIdentChar (lineText [end ]) {
237- end ++
238- }
239- if start >= end {
240- return "" , nil
241- }
242- return lineText [start :end ], nil
243- }
244-
245- func isIdentChar (c byte ) bool {
246- return (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' ) || (c >= '0' && c <= '9' ) || c == '_'
247- }
248-
249- func (c * Code ) grepForSymbol (ctx context.Context , srcDir , symbol string ) []* codev0.Location {
250- // Route grep through the plugin's active RunnerEnvironment so a
251- // Docker-mode Python agent searches its own mounted tree. Pre-Init
252- // uses ResolveStandaloneEnvironment which honors declared context.
253- env := c .Service .ActiveEnv
254- if env == nil {
255- var rctx * basev0.RuntimeContext
256- if c .Service .Base != nil && c .Service .Base .Runtime != nil {
257- rctx = c .Service .Base .Runtime .RuntimeContext
258- }
259- env = runners .ResolveStandaloneEnvironment (ctx , srcDir , rctx )
260- }
261- proc , procErr := env .NewProcess ("grep" , "-rn" , "--include=*.py" , "-w" , symbol , srcDir )
262- if procErr != nil {
263- return nil
264- }
265- var buf bytes.Buffer
266- proc .WithOutput (& buf )
267- _ = proc .Run (ctx ) // grep returns non-zero on no match; that's fine
268- out := buf .Bytes ()
269-
270- var locations []* codev0.Location
271- for _ , line := range strings .Split (string (out ), "\n " ) {
272- line = strings .TrimSpace (line )
273- if line == "" {
274- continue
275- }
276- parts := strings .SplitN (line , ":" , 3 )
277- if len (parts ) < 2 {
278- continue
279- }
280- filePath := parts [0 ]
281- relPath , _ := filepath .Rel (srcDir , filePath )
282- if relPath == "" {
283- relPath = filePath
284- }
285-
286- lineNum := int32 (0 )
287- fmt .Sscanf (parts [1 ], "%d" , & lineNum )
288-
289- col := int32 (1 )
290- if len (parts ) >= 3 {
291- idx := strings .Index (parts [2 ], symbol )
292- if idx >= 0 {
293- col = int32 (idx + 1 )
294- }
295- }
296-
297- locations = append (locations , & codev0.Location {
298- File : relPath ,
299- Line : lineNum ,
300- Column : col ,
301- })
302-
303- if len (locations ) >= 100 {
304- break
305- }
306- }
307- return locations
308- }
0 commit comments