Skip to content

Commit dc27710

Browse files
yangdanny97meta-codesync[bot]
authored andcommitted
Improve LSP benchmark location validation and column resolution
Summary: _looks_like_valid_location only checked URI parseability and non-negative coordinates, counting non-existent files and out-of-bounds lines as valid. Attribute column resolution used find(token) from line start, which matched the wrong occurrence when a token appeared multiple times. Add file-existence and line-bound checks; search from AST col_offset first, falling back to line start. Reviewed By: grievejia Differential Revision: D97012692 fbshipit-source-id: dc2ff552670a6e9ddbbf16683a898680308c6f67
1 parent f636e9e commit dc27710

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

scripts/benchmark/lsp_benchmark.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,13 @@ def pick_random_identifier_case(
398398
# For Attribute nodes we recorded col_offset of the base; try to locate the attribute token on the line.
399399
col0 = o.col_0b
400400
if o.token and o.token != "":
401-
idx = lines[line0].find(o.token)
401+
# Search forward from AST col_offset first (preferred — avoids
402+
# matching an earlier occurrence of the same token on the line).
403+
idx = lines[line0].find(o.token, o.col_0b)
404+
if idx == -1:
405+
# Fallback: search from start (Attribute col_offset points to
406+
# base, not attr, so the token may precede col_offset).
407+
idx = lines[line0].find(o.token)
402408
if idx != -1:
403409
col0 = idx
404410

@@ -794,17 +800,33 @@ def loc_from(obj: Any) -> Optional[Location]:
794800

795801

796802
def _looks_like_valid_location(loc: Location, repo_root: Path) -> bool:
803+
"""Check whether a definition location looks valid.
804+
805+
Verifies the resolved path exists on disk and that the start line is
806+
within the file's line count.
807+
"""
797808
p = _uri_to_path(loc.uri)
798809
try:
799-
p.resolve()
810+
p = p.resolve()
800811
except Exception:
801812
return False
802813

814+
if not p.exists():
815+
return False
816+
803817
if loc.range.start.line < 0 or loc.range.start.character < 0:
804818
return False
805819
if loc.range.end.line < 0 or loc.range.end.character < 0:
806820
return False
807821

822+
# Verify line is within file bounds
823+
try:
824+
line_count = sum(1 for _ in open(p, encoding="utf-8", errors="replace"))
825+
if loc.range.start.line >= line_count:
826+
return False
827+
except OSError:
828+
return False
829+
808830
return True
809831

810832

0 commit comments

Comments
 (0)