{"record":{"id":"61de79dc0e527c03","repo":"oraios/serena","slug":"index","errorCode":null,"errorMessage":"{index=}","messagePattern":"\\{index=\\}","errorType":"validation","errorClass":"InvalidTextLocationError","httpStatus":null,"severity":"error","filePath":"src/solidlsp/ls_utils.py","lineNumber":211,"sourceCode":"        # step over full lines; once the line containing the index has been processed, compute\n        # the position from the difference to the respective line's start index\n        text_stepper = TextStepper(text)\n        while text_stepper.step_line():\n            if text_stepper.idx > index:\n                if text_stepper.is_newline:\n                    # position was stepped over as part of the previous line\n                    if index > text_stepper.prev_line_end_idx:\n                        # edge case: the index points into a multi-character newline sequence (\"\\r\\n\");\n                        # map this to the beginning of the following line\n                        return text_stepper.line, 0\n                    return text_stepper.line - 1, index - text_stepper.prev_line_start_idx\n                else:\n                    # position was stepped over as part of the current line (which was not followed by a newline)\n                    return text_stepper.line, index - text_stepper.line_start_idx\n\n        # handle the case where the end of the text was reached without stepping past the index\n        if index > text_stepper.idx:\n            raise InvalidTextLocationError(f\"{index=}\")\n        return text_stepper.line, text_stepper.col\n\n    @classmethod\n    def get_line_from_index(cls, text: str, index: int) -> int:\n        \"\"\"\n        :param text: the text in which the index is to be located\n        :param index: the 0-based index in the text\n        :return: the 0-based line number corresponding to the index in the text\n        \"\"\"\n        return cls.get_line_col_from_index(text, index)[0]\n\n    @staticmethod\n    def get_index_from_line_col(text: str, line: int, col: int) -> int:\n        \"\"\"\n        :param text: the text in which the coordinates are to be located\n        :param line: the 0-based line number\n        :param col: the 0-based column number\n        :return: the corresponding 0-based index in the text","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/solidlsp/ls_utils.py#L193-L229","documentation":"InvalidTextLocationError with the offending index raised by FileUtils.get_line_col_from_index (src/solidlsp/ls_utils.py:211) when the given 0-based character index is past the end of the text — stepping never reached the index.","triggerScenarios":"Calling get_line_col_from_index(text, index), search_text, or find_text_coordinates with an index greater than len(text), often from stale index data captured before the document shrank.","commonSituations":"Caching character offsets across file edits; regex/search offsets applied to a different text version than the one searched; off-by-one using len(text) inclusive.","solutions":["Assert index <= len(text) before converting the index to line/col.","Recompute the index against the current text rather than reusing cached offsets.","Guard search results so indices come from the same text object being queried."],"exampleFix":"// before\nline, col = FileUtils.get_line_col_from_index(text, stale_index)\n// after\nif stale_index <= len(text):\n    line, col = FileUtils.get_line_col_from_index(text, stale_index)","handlingStrategy":"validation","validationCode":"def index_in_range(text: str, index: int) -> bool:\n    return 0 <= index <= len(text)","typeGuard":"def safe_index(text: str, index: int) -> int | None:\n    return index if 0 <= index <= len(text) else None","tryCatchPattern":"try:\n    line, col = FileUtils.get_line_col_from_index(text, idx)\nexcept InvalidTextLocationError:\n    line, col = FileUtils.get_line_col_from_index(text, len(text))","preventionTips":["Validate indices against len(text) of the same text object being queried","Never mix offsets from one document version with another","Derive indices from searches executed on the current content"],"tags":["text-location","bounds","index"],"backgroundTag":"text-location-out-of-range","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}