Skip to content

Commit f534790

Browse files
committed
Show line range in github uri references
1 parent 85aa15d commit f534790

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/vs/workbench/contrib/chat/browser/chatContentParts/chatReferencesContentPart.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { basename } from '../../../../../base/common/path.js';
1515
import { basenameOrAuthority, isEqualAuthority } from '../../../../../base/common/resources.js';
1616
import { ThemeIcon } from '../../../../../base/common/themables.js';
1717
import { URI } from '../../../../../base/common/uri.js';
18+
import { IRange } from '../../../../../editor/common/core/range.js';
1819
import { localize, localize2 } from '../../../../../nls.js';
1920
import { getFlatContextMenuActions } from '../../../../../platform/actions/browser/menuEntryActionViewItem.js';
2021
import { MenuWorkbenchToolBar } from '../../../../../platform/actions/browser/toolbar.js';
@@ -31,7 +32,7 @@ import { IOpenerService } from '../../../../../platform/opener/common/opener.js'
3132
import { IProductService } from '../../../../../platform/product/common/productService.js';
3233
import { IThemeService } from '../../../../../platform/theme/common/themeService.js';
3334
import { fillEditorsDragData } from '../../../../browser/dnd.js';
34-
import { IResourceLabel, ResourceLabels } from '../../../../browser/labels.js';
35+
import { IResourceLabel, IResourceLabelProps, ResourceLabels } from '../../../../browser/labels.js';
3536
import { ColorScheme } from '../../../../browser/web.api.js';
3637
import { ResourceContextKey } from '../../../../common/contextkeys.js';
3738
import { SETTINGS_AUTHORITY } from '../../../../services/preferences/common/preferences.js';
@@ -366,9 +367,7 @@ class CollapsibleListRenderer implements IListRenderer<IChatCollapsibleListItem,
366367
const extraClasses = data.excluded ? ['excluded'] : [];
367368
if (uri.scheme === 'https' && isEqualAuthority(uri.authority, 'github.com') && uri.path.includes('/tree/')) {
368369
// Parse a nicer label for GitHub URIs that point at a particular commit + file
369-
const label = uri.path.split('/').slice(1, 3).join('/');
370-
const description = uri.path.split('/').slice(5).join('/');
371-
templateData.label.setResource({ resource: uri, name: label, description }, { icon: Codicon.github, title: data.title, strikethrough: data.excluded, extraClasses });
370+
templateData.label.setResource(getResourceLabelForGithubUri(uri), { icon: Codicon.github, title: data.title, strikethrough: data.excluded, extraClasses });
372371
} else if (uri.scheme === this.productService.urlProtocol && isEqualAuthority(uri.authority, SETTINGS_AUTHORITY)) {
373372
// a nicer label for settings URIs
374373
const settingId = uri.path.substring(1);
@@ -425,6 +424,49 @@ class CollapsibleListRenderer implements IListRenderer<IChatCollapsibleListItem,
425424
}
426425
}
427426

427+
function getResourceLabelForGithubUri(uri: URI): IResourceLabelProps {
428+
const repoPath = uri.path.split('/').slice(1, 3).join('/');
429+
const filePath = uri.path.split('/').slice(5);
430+
const fileName = filePath.at(-1);
431+
const range = getLineRangeFromGithubUri(uri);
432+
return {
433+
resource: uri,
434+
name: fileName ?? filePath.join('/'),
435+
description: [repoPath, ...filePath.slice(0, -1)].join('/'),
436+
range
437+
};
438+
}
439+
440+
function getLineRangeFromGithubUri(uri: URI): IRange | undefined {
441+
if (!uri.fragment) {
442+
return undefined;
443+
}
444+
445+
// Extract the line range from the fragment
446+
// Github line ranges are 1-based
447+
const match = uri.fragment.match(/\bL(\d+)(?:-L(\d+))?/);
448+
if (!match) {
449+
return undefined;
450+
}
451+
452+
const startLine = parseInt(match[1]);
453+
if (isNaN(startLine)) {
454+
return undefined;
455+
}
456+
457+
const endLine = match[2] ? parseInt(match[2]) : startLine;
458+
if (isNaN(endLine)) {
459+
return undefined;
460+
}
461+
462+
return {
463+
startLineNumber: startLine,
464+
startColumn: 1,
465+
endLineNumber: endLine,
466+
endColumn: 1
467+
};
468+
}
469+
428470
function getResourceForElement(element: IChatCollapsibleListItem): URI | null {
429471
if (element.kind === 'warning') {
430472
return null;

0 commit comments

Comments
 (0)