Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Dec 29, 2023
1 parent 0618690 commit 6de7510
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
11 changes: 4 additions & 7 deletions client/browser/src/contentScript/github/codeView.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Annotation, type AnnotationsParams, type Item } from '@opencodegraph/client'
import { type Annotation, type AnnotationsParams } from '@opencodegraph/client'
import { createChipList } from '@opencodegraph/ui-standalone'
import { combineLatest, debounceTime, EMPTY, map, mergeMap, Observable, startWith, tap } from 'rxjs'
import { toLineRangeStrings } from '../../shared/util/toLineRangeStrings'
Expand Down Expand Up @@ -102,19 +102,16 @@ function redraw(annotations: Annotation[]): void {

const lineAnns = byLine.find(a => a.line === line)?.annotations
if (lineAnns !== undefined) {
addChipsToCodeRow(
line,
lineAnns.map(ann => ann.item)
)
addChipsToCodeRow(line, lineAnns)
}
}

function addChipsToCodeRow(line: number, items: Item[]): void {
function addChipsToCodeRow(line: number, annotations: Annotation[]): void {
const lineEl = document.querySelector(`.react-file-line[data-line-number="${line + 1}"]`)
if (lineEl) {
const chipList = createChipList(
styledItemChipListParams({
annotations: items,
annotations,
})
)
lineEl.append(chipList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function redraw(file: DiffViewFileVersionData, annotations: Annotation[]): void

const chipList = createChipList(
styledItemChipListParams({
annotations: lineAnnotations.map(ann => ann.item),
annotations: lineAnnotations,
})
)
lineEl.append(chipList)
Expand Down
5 changes: 3 additions & 2 deletions client/browser/src/contentScript/ocgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export function annotationsByLine(annotations: Annotation[]): { line: number; an
const byLine: { line: number; annotations: Annotation[] }[] = []
for (const ann of annotations) {
let cur = byLine.at(-1)
if (!cur || cur.line !== ann.range.start.line) {
cur = { line: ann.range.start.line, annotations: [] }
const startLine = ann.range?.start.line ?? 0
if (!cur || cur.line !== startLine) {
cur = { line: startLine, annotations: [] }
byLine.push(cur)
}
cur.annotations.push(ann)
Expand Down
12 changes: 6 additions & 6 deletions client/vscode/src/ui/editor/codeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP

function createCodeLens(
doc: vscode.TextDocument,
{ item, range }: Annotation<vscode.Range>,
ann: Annotation<vscode.Range>,
showHover: ReturnType<typeof createShowHoverCommand>
): CodeLens {
const range = ann.range ?? new vscode.Range(0, 0, 0, 0)
return {
range,
command: {
title: item.title,
tooltip: item.detail,
...(item.image
title: ann.title,
...(ann.ui?.detail
? showHover.createCommandArgs(doc.uri, range.start)
: item.url
? openWebBrowserCommandArgs(item.url)
: ann.url
? openWebBrowserCommandArgs(ann.url)
: { command: 'noop' }),
},
isResolved: true,
Expand Down
48 changes: 23 additions & 25 deletions client/vscode/src/ui/editor/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function createHoverProvider(controller: Controller): vscode.HoverProvide
return firstValueFrom(
controller.observeAnnotations(doc).pipe(
map(anns => {
const containedByAnns = anns?.filter(ann => ann.range.contains(pos))
const containedByAnns = anns?.filter(ann => (ann.range ?? ZERO_RANGE).contains(pos))
return containedByAnns && containedByAnns.length > 0 ? createHover(containedByAnns) : null
})
)
Expand All @@ -21,35 +21,33 @@ export function createHoverProvider(controller: Controller): vscode.HoverProvide
}
}

const ZERO_RANGE = new vscode.Range(0, 0, 0, 0)

function createHover(anns: Annotation<vscode.Range>[]): vscode.Hover {
const contents: vscode.Hover['contents'] = []
for (const { item } of anns) {
contents.push(new vscode.MarkdownString(item.title.includes('*') ? item.title : `**${item.title}**`))
if (item.detail) {
contents.push(new vscode.MarkdownString(item.detail))
}
if (item.image) {
// Scale down image dimensions so the image isn't too big.
const MAX_WIDTH = 400
let width = item.image.width ?? MAX_WIDTH
let height = item.image.height
if (width > MAX_WIDTH) {
const origWidth = width
width = MAX_WIDTH
height = height ? Math.round(height * (MAX_WIDTH / origWidth)) : undefined
}
for (const ann of anns) {
const content = new vscode.MarkdownString()
content.supportHtml = true

const m = new vscode.MarkdownString(
`<img src="${encodeURI(item.image.url)}" width="${width}" height="${height ?? 'auto'}" ${
item.image.alt ? `alt="${item.image.alt}"` : ''
} />`
)
m.supportHtml = true
contents.push(m)
// Render title in bold.
content.appendMarkdown('**')
content.appendText(ann.title)
content.appendMarkdown('**')

if (ann.ui?.detail) {
content.appendMarkdown('\n\n')
if (ann.ui.format === 'markdown') {
content.appendMarkdown(ann.ui.detail)
} else {
content.appendText(ann.ui.detail)
}
}
if (item.url) {
contents.push(new vscode.MarkdownString(`[Open in browser...](${item.url})`))
if (ann.url) {
content.appendMarkdown('\n\n')
content.appendMarkdown(`[Open in browser...](${ann.url})`)
}

contents.push(content)
}

return {
Expand Down

0 comments on commit 6de7510

Please sign in to comment.