fix codespan links

This commit is contained in:
Mathew Pareles 2025-03-16 22:29:17 -07:00
parent 97b44a94e4
commit a3cfd0d023
3 changed files with 25 additions and 9 deletions

View file

@ -27,6 +27,7 @@ import { ChatMessage, CodespanLocationLink, StagingSelectionItem, ToolMessage, T
import { Position } from '../../../../editor/common/core/position.js'; import { Position } from '../../../../editor/common/core/position.js';
import { ITerminalToolService } from './terminalToolService.js'; import { ITerminalToolService } from './terminalToolService.js';
import { IMetricsService } from '../common/metricsService.js'; import { IMetricsService } from '../common/metricsService.js';
import { shorten } from '../../../../base/common/labels.js';
const findLastIndex = <T>(arr: T[], condition: (t: T) => boolean): number => { const findLastIndex = <T>(arr: T[], condition: (t: T) => boolean): number => {
for (let i = arr.length - 1; i >= 0; i--) { for (let i = arr.length - 1; i >= 0; i--) {
@ -910,15 +911,30 @@ class ChatThreadService extends Disposable implements IChatThreadService {
const doesUriMatchTarget = (uri: URI) => uri.path.includes(target) const doesUriMatchTarget = (uri: URI) => uri.path.includes(target)
// check if any prevFiles are the `codespanSearch` // check if any prevFiles are the `codespanSearch`
for (const uri of prevUris) { for (const [idx, uri] of prevUris.entries()) {
if (doesUriMatchTarget(uri)) return { uri } if (doesUriMatchTarget(uri)) {
// shorten it
const prevUriStrs = prevUris.map(uri => uri.toString())
const shortenedUriStrs = shorten(prevUriStrs)
const displayText = shortenedUriStrs[idx]
return { uri, displayText }
}
} }
// else search codebase for file // else search codebase for file
const { uris } = await this._toolsService.callTool['pathname_search']({ queryStr: target, pageNumber: 0 }) const { uris } = await this._toolsService.callTool['pathname_search']({ queryStr: target, pageNumber: 0 })
for (const uri of uris) { for (const [idx, uri] of uris.entries()) {
if (doesUriMatchTarget(uri)) return { uri } if (doesUriMatchTarget(uri)) {
const prevUriStrs = prevUris.map(uri => uri.toString())
const shortenedUriStrs = shorten(prevUriStrs)
const displayText = shortenedUriStrs[idx]
return { uri, displayText }
}
} }
} }
@ -967,7 +983,8 @@ class ChatThreadService extends Disposable implements IChatThreadService {
startColumn: definition.range.startColumn, startColumn: definition.range.startColumn,
endLineNumber: definition.range.endLineNumber, endLineNumber: definition.range.endLineNumber,
endColumn: definition.range.endColumn, endColumn: definition.range.endColumn,
} },
displayText: _codespanStr,
}; };
// const defModelRef = await this._textModelService.createModelReference(definition.uri); // const defModelRef = await this._textModelService.createModelReference(definition.uri);

View file

@ -34,13 +34,11 @@ const Codespan = ({ text, className, onClick }: { text: string, className?: stri
// TODO compute this once for efficiency. we should use `labels.ts/shorten` to display duplicates properly // TODO compute this once for efficiency. we should use `labels.ts/shorten` to display duplicates properly
const filename = useMemo(() => getBasename(text), [text])
return <code return <code
className={`font-mono font-medium rounded-sm bg-void-bg-1 px-1 ${className}`} className={`font-mono font-medium rounded-sm bg-void-bg-1 px-1 ${className}`}
onClick={onClick} onClick={onClick}
> >
{filename} {text}
</code> </code>
} }
@ -101,7 +99,7 @@ const CodespanWithLink = ({ text, rawText, chatMessageLocation }: { text: string
} }
return <Codespan return <Codespan
text={text} text={link?.displayText || text}
onClick={onClick} onClick={onClick}
className={link ? 'underline hover:brightness-90 transition-all duration-200 cursor-pointer' : ''} className={link ? 'underline hover:brightness-90 transition-all duration-200 cursor-pointer' : ''}
/> />

View file

@ -75,6 +75,7 @@ export type StagingSelectionItem = CodeSelection | FileSelection
export type CodespanLocationLink = { export type CodespanLocationLink = {
uri: URI, // we handle serialization for this uri: URI, // we handle serialization for this
displayText: string,
selection?: { // store as JSON so dont have to worry about serialization selection?: { // store as JSON so dont have to worry about serialization
startLineNumber: number startLineNumber: number
startColumn: number, startColumn: number,