MCPcopy
hub / github.com/codeaashu/claude-code / getSymbolAtPosition

Function getSymbolAtPosition

src/tools/LSPTool/symbolContext.ts:21–90  ·  view source on GitHub ↗
(
  filePath: string,
  line: number,
  character: number,
)

Source from the content-addressed store, hash-verified

19 * @returns The symbol at that position, or null if extraction fails
20 */
21export function getSymbolAtPosition(
22 filePath: string,
23 line: number,
24 character: number,
25): string | null {
26 try {
27 const fs = getFsImplementation()
28 const absolutePath = expandPath(filePath)
29
30 // Read only the first 64KB instead of the whole file. Most LSP hover/goto
31 // targets are near recent edits; 64KB covers ~1000 lines of typical code.
32 // If the target line is past this window we fall back to null (the UI
33 // already handles that by showing `position: line:char`).
34 // eslint-disable-next-line custom-rules/no-sync-fs -- called from sync React render (renderToolUseMessage)
35 const { buffer, bytesRead } = fs.readSync(absolutePath, {
36 length: MAX_READ_BYTES,
37 })
38 const content = buffer.toString('utf-8', 0, bytesRead)
39 const lines = content.split('\n')
40
41 if (line < 0 || line >= lines.length) {
42 return null
43 }
44 // If we filled the full buffer the file continues past our window,
45 // so the last split element may be truncated mid-line.
46 if (bytesRead === MAX_READ_BYTES && line === lines.length - 1) {
47 return null
48 }
49
50 const lineContent = lines[line]
51 if (!lineContent || character < 0 || character >= lineContent.length) {
52 return null
53 }
54
55 // Extract the word/symbol at the character position
56 // Pattern matches:
57 // - Standard identifiers: alphanumeric + underscore + dollar
58 // - Rust lifetimes: 'a, 'static
59 // - Rust macros: macro_name!
60 // - Operators and special symbols: +, -, *, etc.
61 // This is more inclusive to handle various programming languages
62 const symbolPattern = /[\w$'!]+|[+\-*/%&|^~<>=]+/g
63 let match: RegExpExecArray | null
64
65 while ((match = symbolPattern.exec(lineContent)) !== null) {
66 const start = match.index
67 const end = start + match[0].length
68
69 // Check if the character position falls within this match
70 if (character >= start && character < end) {
71 const symbol = match[0]
72 // Limit length to 30 characters to avoid overly long symbols
73 return truncate(symbol, 30)
74 }
75 }
76
77 return null
78 } catch (error) {

Callers 1

renderToolUseMessageFunction · 0.85

Calls 5

getFsImplementationFunction · 0.85
expandPathFunction · 0.85
logForDebuggingFunction · 0.85
toStringMethod · 0.65
truncateFunction · 0.50

Tested by

no test coverage detected