getFunctionAtPosition finds the function name and parameter index at a position
(content string, pos Position)
| 1361 | |
| 1362 | // getFunctionAtPosition finds the function name and parameter index at a position |
| 1363 | func (h *Handler) getFunctionAtPosition(content string, pos Position) (string, int) { |
| 1364 | lines := strings.Split(content, "\n") |
| 1365 | if pos.Line >= len(lines) { |
| 1366 | return "", 0 |
| 1367 | } |
| 1368 | |
| 1369 | line := lines[pos.Line] |
| 1370 | if pos.Character > len(line) { |
| 1371 | return "", 0 |
| 1372 | } |
| 1373 | |
| 1374 | // Look backwards for opening parenthesis to find function name |
| 1375 | parenCount := 0 |
| 1376 | paramIndex := 0 |
| 1377 | funcEnd := -1 |
| 1378 | |
| 1379 | for i := pos.Character - 1; i >= 0; i-- { |
| 1380 | ch := line[i] |
| 1381 | if ch == ')' { |
| 1382 | parenCount++ |
| 1383 | } else if ch == '(' { |
| 1384 | if parenCount == 0 { |
| 1385 | funcEnd = i |
| 1386 | break |
| 1387 | } |
| 1388 | parenCount-- |
| 1389 | } else if ch == ',' && parenCount == 0 { |
| 1390 | paramIndex++ |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | if funcEnd < 0 { |
| 1395 | return "", 0 |
| 1396 | } |
| 1397 | |
| 1398 | // Extract function name (word before the parenthesis) |
| 1399 | funcStart := funcEnd - 1 |
| 1400 | for funcStart >= 0 && (isAlphanumeric(line[funcStart]) || line[funcStart] == '_') { |
| 1401 | funcStart-- |
| 1402 | } |
| 1403 | funcStart++ |
| 1404 | |
| 1405 | if funcStart >= funcEnd { |
| 1406 | return "", 0 |
| 1407 | } |
| 1408 | |
| 1409 | return line[funcStart:funcEnd], paramIndex |
| 1410 | } |
| 1411 | |
| 1412 | // isAlphanumeric checks if a byte is alphanumeric |
| 1413 | func isAlphanumeric(c byte) bool { |