fix integration

This commit is contained in:
2026-02-03 20:48:20 -10:00
parent dfff777d04
commit ab01fd0abe
4 changed files with 84 additions and 24 deletions
+5 -1
View File
@@ -3,12 +3,16 @@
.sourcegraph-hover {
max-width: 600px;
max-height: 400px;
overflow: auto;
display: flex;
flex-direction: column;
}
.sourcegraph-hover .sg-content {
padding: 8px;
font-size: 13px;
overflow-y: auto;
flex: 1;
min-height: 0; /* Allow shrinking */
}
.sourcegraph-hover .sg-content pre {
+51 -20
View File
@@ -94,23 +94,25 @@ function hideRefsPanel(): void {
}
}
function createHoverContent(
async function createHoverContent(
contents: string,
config: SourcegraphConfig,
path: string,
line: number,
char: number,
): HTMLElement {
): Promise<HTMLElement> {
const el = document.createElement('div');
el.className = 'sourcegraph-hover';
// Render markdown content as pre-formatted text (simple approach)
// In production, you might want to use a proper markdown renderer
// Content is pre-rendered as HTML by the backend
const contentDiv = document.createElement('div');
contentDiv.className = 'sg-content';
contentDiv.innerHTML = `<pre>${escapeHtml(contents)}</pre>`;
contentDiv.className = 'sg-content markup';
contentDiv.innerHTML = contents;
el.appendChild(contentDiv);
// Pre-fetch definitions to know if button should be enabled
const definitions = await fetchDefinition(config, path, line, char);
// Action buttons
const actionsDiv = document.createElement('div');
actionsDiv.className = 'sg-actions';
@@ -118,15 +120,19 @@ function createHoverContent(
const goToDefBtn = document.createElement('button');
goToDefBtn.className = 'ui mini basic button';
goToDefBtn.textContent = 'Go to definition';
goToDefBtn.addEventListener('click', async () => {
hideActiveTippy();
const locations = await fetchDefinition(config, path, line, char);
if (locations.length === 1) {
navigateToLocation(locations[0]);
} else if (locations.length > 1) {
showLocationsPanel('Definitions', locations);
}
});
if (definitions.length === 0) {
goToDefBtn.disabled = true;
goToDefBtn.classList.add('disabled');
} else {
goToDefBtn.addEventListener('click', () => {
hideActiveTippy();
if (definitions.length === 1) {
navigateToLocation(definitions[0]);
} else {
showLocationsPanel('Definitions', definitions);
}
});
}
actionsDiv.appendChild(goToDefBtn);
const findRefsBtn = document.createElement('button');
@@ -151,6 +157,17 @@ function escapeHtml(text: string): string {
return div.innerHTML;
}
function normalizeRepoName(repo: string): string {
// Sourcegraph returns repo names like "git.example.com/owner/repo"
// We need just "owner/repo" for Gitea URLs
const parts = repo.split('/');
if (parts.length >= 3) {
// Has domain prefix - return last two parts (owner/repo)
return parts.slice(-2).join('/');
}
return repo;
}
function navigateToLocation(loc: Location): void {
// Build URL to the location
// If it's in the same repo, navigate to file
@@ -159,14 +176,17 @@ function navigateToLocation(loc: Location): void {
const repoMatch = currentPath.match(/^\/([^/]+\/[^/]+)/);
const currentRepo = repoMatch ? repoMatch[1] : '';
// Normalize repo name (strip domain prefix if present)
const targetRepo = normalizeRepoName(loc.repo || '');
let url: string;
if (loc.repo === currentRepo || !loc.repo) {
if (targetRepo === currentRepo || !targetRepo) {
// Same repo - construct relative URL
const basePath = currentPath.replace(/\/src\/.*$/, '');
url = `${basePath}/src/branch/main/${loc.path}#L${loc.line + 1}`;
} else {
// Different repo
url = `/${loc.repo}/src/branch/main/${loc.path}#L${loc.line + 1}`;
url = `/${targetRepo}/src/branch/main/${loc.path}#L${loc.line + 1}`;
}
window.location.href = url;
@@ -298,7 +318,7 @@ export function initSourcegraph(): void {
hideActiveTippy();
// Create and show new tippy
const content = createHoverContent(hover.contents, config, pos.path, pos.line, pos.char);
const content = await createHoverContent(hover.contents, config, pos.path, pos.line, pos.char);
activeTippy = createTippy(target, {
content,
theme: 'default',
@@ -324,11 +344,22 @@ export function initSourcegraph(): void {
}
});
// Close refs panel when clicking outside
// Close tippy and refs panel when clicking outside
document.addEventListener('click', (e) => {
if (refsPanel && !refsPanel.contains(e.target as Node)) {
const target = e.target as Node;
// Close refs panel if clicking outside it
if (refsPanel && !refsPanel.contains(target)) {
hideRefsPanel();
}
// Close tippy if clicking outside it
if (activeTippy) {
const tippyBox = document.querySelector('.tippy-box');
if (tippyBox && !tippyBox.contains(target)) {
hideActiveTippy();
}
}
});
});
}