|
2 | 2 |
|
3 | 3 | The Locate API provides a unified way to specify a position or range in the codebase. It is used as a base for many other requests like [`SymbolRequest`](symbol.md), [`ReferenceRequest`](reference.md), and [`CallHierarchyRequest`](call_hierarchy.md). |
4 | 4 |
|
5 | | -## LocateRequest |
| 5 | +## Locate |
| 6 | + |
| 7 | +The `Locate` model uses a two-stage approach: **scope** (optional) → **find** (optional, but at least one required). |
| 8 | + |
| 9 | +### Resolution Rules |
| 10 | + |
| 11 | +1. **SymbolScope without find**: Returns the symbol declaration position (for references, rename) |
| 12 | +2. **With find containing marker**: Returns the marker position |
| 13 | +3. **With find only**: Returns the start of matched text |
| 14 | +4. **No scope + find**: Searches the entire file |
| 15 | + |
| 16 | +### Locate Fields |
| 17 | + |
| 18 | +| Field | Type | Default | Description | |
| 19 | +| :---------- | :-------------------------------------- | :--------- | :--------------------------------------------------------------- | |
| 20 | +| `file_path` | `string` | Required | Path to search in. | |
| 21 | +| `scope` | `LineScope` \| `SymbolScope` \| `null` | `null` | Optional: narrow search to symbol body or line range. | |
| 22 | +| `find` | `string` \| `null` | `null` | Text pattern with marker for exact position. | |
| 23 | +| `marker` | `string` | `"<HERE>"` | Position marker in find pattern. Change if source contains `"<HERE>"`. | |
| 24 | + |
| 25 | +### LineScope |
| 26 | + |
| 27 | +| Field | Type | Description | |
| 28 | +| :----- | :-------------------------------- | :----------------------------------- | |
| 29 | +| `line` | `number` \| `[number, number]` | Line number or (start, end) range (1-based). | |
| 30 | + |
| 31 | +### SymbolScope |
6 | 32 |
|
7 | | -One of `LocateText` or `LocateSymbol`. |
| 33 | +| Field | Type | Description | |
| 34 | +| :------------ | :--------- | :------------------------------------------------------- | |
| 35 | +| `symbol_path` | `string[]` | Symbol hierarchy, e.g., `["MyClass", "my_method"]`. | |
8 | 36 |
|
9 | | -### LocateText |
| 37 | +## LocateRange |
10 | 38 |
|
11 | | -Finds a position based on a text search. |
| 39 | +For selecting a range of text instead of a point. |
12 | 40 |
|
13 | | -| Field | Type | Default | Description | |
14 | | -| :---------- | :--------------------------- | :-------- | :-------------------------------------------- | |
15 | | -| `file_path` | `string` | Required | Path to search in. | |
16 | | -| `line` | `number \| [number, number]` | `null` | Specific line number or range `[start, end]`. | |
17 | | -| `find` | `string` | Required | String snippet to find. | |
18 | | -| `find_end` | `"start" \| "end"` | `"end"` | Match the start or end of the snippet. | |
| 41 | +| Field | Type | Default | Description | |
| 42 | +| :---------- | :-------------------------------------- | :--------- | :------------------------------------------------ | |
| 43 | +| `file_path` | `string` | Required | Path to search in. | |
| 44 | +| `scope` | `LineScope` \| `SymbolScope` \| `null` | `null` | Scope defines the range; if symbol, uses symbol's full range. | |
| 45 | +| `find` | `string` \| `null` | `null` | Text to match; matched text becomes the range. | |
19 | 46 |
|
20 | | -### LocateSymbol |
| 47 | +## LocateRequest |
| 48 | + |
| 49 | +| Field | Type | Description | |
| 50 | +| :------- | :-------- | :---------------------------- | |
| 51 | +| `locate` | `Locate` | The location to find. | |
21 | 52 |
|
22 | | -Finds a position based on a symbol path. |
| 53 | +## LocateRangeRequest |
23 | 54 |
|
24 | | -| Field | Type | Default | Description | |
25 | | -| :------------ | :--------- | :------- | :-------------------------------------------- | |
26 | | -| `file_path` | `string` | Required | Path to search in. | |
27 | | -| `symbol_path` | `string[]` | Required | Hierarchy list (e.g., `["Class", "Method"]`). | |
| 55 | +| Field | Type | Description | |
| 56 | +| :------- | :----------- | :---------------------------- | |
| 57 | +| `locate` | `LocateRange`| The range to locate. | |
28 | 58 |
|
29 | 59 | ## LocateResponse |
30 | 60 |
|
31 | | -| Field | Type | Description | |
32 | | -| :---------- | :--------- | :---------------------------------------------- | |
33 | | -| `file_path` | `string` | Resolved file path. | |
34 | | -| `position` | `Position` | The coordinates (line, character) of the match. | |
| 61 | +| Field | Type | Description | |
| 62 | +| :---------- | :--------- | :-------------------------------------------------------- | |
| 63 | +| `file_path` | `string` | Resolved file path. | |
| 64 | +| `position` | `Position` | The coordinates (line, character) of the match. | |
35 | 65 |
|
36 | 66 | ## Common Types |
37 | 67 |
|
38 | 68 | ### Position |
39 | 69 |
|
40 | 70 | | Field | Type | Description | |
41 | 71 | | :---------- | :------- | :-------------------------- | |
42 | | -| `line` | `number` | 0-indexed line number. | |
43 | | -| `character` | `number` | 0-indexed character offset. | |
| 72 | +| `line` | `number` | 1-based line number. | |
| 73 | +| `character` | `number` | 1-based character offset. | |
44 | 74 |
|
45 | 75 | ### Range |
46 | 76 |
|
47 | | -| Field | Type | Description | |
48 | | -| :------ | :--------- | :-------------------------- | |
49 | | -| `start` | `Position` | Start position (inclusive). | |
50 | | -| `end` | `Position` | End position (exclusive). | |
| 77 | +| Field | Type | Description | |
| 78 | +| :------ | :--------- | :---------------------------- | |
| 79 | +| `start` | `Position` | Start position (inclusive). | |
| 80 | +| `end` | `Position` | End position (exclusive). | |
51 | 81 |
|
52 | 82 | ## Example Usage |
53 | 83 |
|
54 | | -### Request (LocateText) |
| 84 | +### Scenario 1: Finding a symbol declaration |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "locate": { |
| 89 | + "file_path": "foo.py", |
| 90 | + "scope": { |
| 91 | + "symbol_path": ["MyClass"] |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Scenario 2: Completion trigger point (with marker) |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "locate": { |
| 102 | + "file_path": "foo.py", |
| 103 | + "find": "self.<HERE>" |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Scenario 3: Using custom marker (when source contains "<HERE>") |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "locate": { |
| 113 | + "file_path": "foo.py", |
| 114 | + "find": "x = <|>value", |
| 115 | + "marker": "<|>" |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### Scenario 4: Finding a location within a specific symbol |
55 | 121 |
|
56 | 122 | ```json |
57 | 123 | { |
58 | 124 | "locate": { |
59 | | - "file_path": "src/main.py", |
60 | | - "line": [10, 50], |
61 | | - "find": "def my_func", |
62 | | - "find_end": "start" |
| 125 | + "file_path": "foo.py", |
| 126 | + "scope": { |
| 127 | + "symbol_path": ["process"] |
| 128 | + }, |
| 129 | + "find": "return <HERE>result" |
63 | 130 | } |
64 | 131 | } |
65 | 132 | ``` |
66 | 133 |
|
67 | | -### Markdown Rendered for LLM |
| 134 | +#### Markdown Rendered for LLM |
68 | 135 |
|
69 | 136 | ```markdown |
70 | | -Located `src/main.py` at 12:5 |
| 137 | +Located `foo.py` at 42:10 |
71 | 138 | ``` |
0 commit comments