Skip to content

Commit 4d7d167

Browse files
wenytang-msCopilot
andcommitted
Return read_file input from Java symbol lookup
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3ce4077 commit 4d7d167

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
{
8181
"name": "lsp_java_findSymbol",
8282
"toolReferenceName": "javaFindSymbol",
83-
"modelDescription": "Find Java class, interface, method, or field definitions across the workspace by name or partial identifier. Prefer over grep_search, file_search, semantic_search, or search subagents for Java symbol lookup.\n\nResults include file, startLine, endLine, and range. On relevant results, do not repeat with a similar query; use read_file on the returned file/range, or lsp_java_getFileStructure with the returned file when broader file context is needed. The tool retries internally, so on an empty result do not re-search\u2014retry once only if it reports indexing in progress, otherwise use generic search.\n\nDo not use for non-Java files, literals, comments, build/XML files, or conceptual exploration.",
83+
"modelDescription": "Find Java class, interface, method, or field definitions across the workspace by name or partial identifier. Prefer over grep_search, file_search, semantic_search, or search subagents for Java symbol lookup.\n\nResults include file, startLine, endLine, range, and readFileInput ({ filePath, offset, limit }) compatible with read_file. On relevant results, use read_file with readFileInput when source is needed, or lsp_java_getFileStructure with the returned file when broader file context is needed. The tool retries internally, so on an empty result do not re-search\u2014retry once only if it reports indexing in progress, otherwise use generic search.\n\nDo not use for non-Java files, literals, comments, build/XML files, or conceptual exploration.",
8484
"displayName": "Java: Find Symbol",
8585
"userDescription": "Find Java class, method, field, or interface definitions by name.",
8686
"tags": [

resources/instruments/javaLspContext.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ For Java symbol navigation, two compiler-accurate `lsp_java_*` tools are availab
1010

1111
If these tools are not already available in the current tool list, load them with `tool_search` using a query such as `Java LSP symbol navigation lsp_java`.
1212

13-
Use `lsp_java_findSymbol` before `grep_search`, `search_subagent`, `semantic_search`, or `file_search` only when the task is to locate Java symbols by name or partial identifier. If it returns relevant symbols, do not call it again with the same or similar query; next use `read_file` on the returned `file`/`range`, or call `lsp_java_getFileStructure` with the returned `file` when broader file context is needed.
13+
Use `lsp_java_findSymbol` before `grep_search`, `search_subagent`, `semantic_search`, or `file_search` only when the task is to locate Java symbols by name or partial identifier. If it returns relevant symbols and source is needed, call `read_file` with the returned `readFileInput`, or call `lsp_java_getFileStructure` with the returned `file` when broader file context is needed.
1414

1515
Use `lsp_java_getFileStructure` only with a path confirmed by the user or a previous tool result. Prefer `file` from `findSymbol`; do not guess paths. Use generic search for string literals, comments, XML, Gradle/Maven files, non-Java files, or broad conceptual exploration. `findSymbol` already retries internally with a normalized identifier, so do not re-issue the same search on an empty result: if it reports indexing in progress, retry once after a short pause; otherwise fall back to generic search.

resources/skills/java-lsp-tools/SKILL.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Two compiler-accurate tools backed by the Java Language Server (jdtls). They ret
1212
### `lsp_java_findSymbol`
1313
Search for Java symbol definitions (classes, methods, fields) by name across the workspace. Supports partial matching.
1414
- Input: `{ query, limit? }` — limit defaults to 20, max 50
15-
- Output: `{ results: [{ name, kind, container?, file, startLine, endLine, location, range }], total }`; `range` is `L start-end`, and `file` can be passed to `lsp_java_getFileStructure`
15+
- Output: `{ results: [{ name, kind, container?, file, startLine, endLine, readFileInput, location, range }], total }`; `readFileInput` is `{ filePath, offset, limit }` for `read_file`, and `file` can be passed to `lsp_java_getFileStructure`
1616
- **Use instead of** `grep_search`, `file_search`, `semantic_search`, or `search_subagent` when looking for where a Java class/method/field is defined by identifier
17-
- Do not repeat with the same or similar query after relevant results are returned
17+
- When source is needed for a returned symbol, use its `readFileInput` directly
1818

1919
### `lsp_java_getFileStructure`
2020
Get hierarchical outline of a Java file (classes, methods, fields) with line ranges.
@@ -36,7 +36,7 @@ Get hierarchical outline of a Java file (classes, methods, fields) with line ran
3636

3737
**findSymbol → getFileStructure → read_file (specific lines only)**
3838

39-
If `findSymbol` returns relevant symbols, use `read_file` on the returned `file`/`range`, or call `getFileStructure` with the returned `file` when broader file context is needed. Do not call `findSymbol` again with the same or similar identifier unless the returned symbols are irrelevant.
39+
If `findSymbol` returns relevant symbols and source is needed, call `read_file` with the returned `readFileInput`, or call `getFileStructure` with the returned `file` when broader file context is needed.
4040

4141
## Fallback
4242

src/copilot/tools/javaContextTools.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ function getResponseCharCount(data: unknown): number {
4444
return typeof data === "string" ? data.length : JSON.stringify(data, null, 2).length;
4545
}
4646

47+
interface ReadFileInput {
48+
filePath: string;
49+
offset: number;
50+
limit: number;
51+
}
52+
53+
function toInclusiveLineRange(range: vscode.Range): { startLine: number; endLine: number } {
54+
const startLine = range.start.line + 1;
55+
const endLine = Math.max(startLine, range.end.character === 0 && range.end.line > range.start.line
56+
? range.end.line
57+
: range.end.line + 1);
58+
return { startLine, endLine };
59+
}
60+
61+
function toReadFileInput(filePath: string, startLine: number, endLine: number): ReadFileInput {
62+
return {
63+
filePath,
64+
offset: startLine,
65+
limit: endLine - startLine + 1,
66+
};
67+
}
68+
4769
/**
4870
* Normalize a workspace-symbol query for a single fallback retry.
4971
* Strips a fully-qualified package prefix (com.foo.Bar -> Bar), generic parameters
@@ -303,15 +325,15 @@ const findSymbolTool: vscode.LanguageModelTool<FindSymbolInput> = {
303325
totalResults = symbols.length;
304326
const results = symbols.slice(0, limit).map(s => {
305327
const file = vscode.workspace.asRelativePath(s.location.uri);
306-
const startLine = s.location.range.start.line + 1;
307-
const endLine = s.location.range.end.line + 1;
328+
const { startLine, endLine } = toInclusiveLineRange(s.location.range);
308329
return {
309330
name: s.name,
310331
kind: vscode.SymbolKind[s.kind],
311332
container: s.containerName || undefined,
312333
file,
313334
startLine,
314335
endLine,
336+
readFileInput: toReadFileInput(file, startLine, endLine),
315337
location: `${file}:${startLine}`,
316338
range: `L${startLine}-${endLine}`,
317339
};

0 commit comments

Comments
 (0)