From 2a665f4d79c97602b5c77f1684091408e6280846 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Sun, 7 Jan 2024 23:56:25 -0800 Subject: [PATCH] wip --- client/vscode/src/ui/editor/codeLens.test.ts | 8 +++- client/vscode/src/ui/editor/codeLens.ts | 41 +++++++++++++++++--- lib/ui-common/src/index.ts | 2 +- lib/ui-common/src/ui.test.ts | 33 +++++++++------- lib/ui-common/src/ui.ts | 4 +- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/client/vscode/src/ui/editor/codeLens.test.ts b/client/vscode/src/ui/editor/codeLens.test.ts index 6a64ef32..11c16468 100644 --- a/client/vscode/src/ui/editor/codeLens.test.ts +++ b/client/vscode/src/ui/editor/codeLens.test.ts @@ -161,7 +161,13 @@ describe('createCodeLensProvider', () => { }) }) expectObservable(provider.observeCodeLenses(doc)).toBe('a', { - a: [{ isResolved: true, range: createRange(1, 0, 1, 0), command: { command: 'noop', title: 'A' } }], + a: [ + { + isResolved: true, + range: createRange(1, 0, 1, 0), + command: { command: 'opencodegraph._showGroup', title: 'G' }, + }, + ], } satisfies Record) }) }) diff --git a/client/vscode/src/ui/editor/codeLens.ts b/client/vscode/src/ui/editor/codeLens.ts index e71f8ab9..f9dd519e 100644 --- a/client/vscode/src/ui/editor/codeLens.ts +++ b/client/vscode/src/ui/editor/codeLens.ts @@ -1,5 +1,8 @@ -import { prepareAnnotationsForPresentation } from '@opencodegraph/ui-common' -import { type AnnotationWithAdjustedRange } from '@opencodegraph/ui-common/src/ui' +import { + groupAnnotations, + prepareAnnotationsForPresentation, + type AnnotationWithAdjustedRange, +} from '@opencodegraph/ui-common' import { firstValueFrom, map, type Observable } from 'rxjs' import * as vscode from 'vscode' import { makeRange, type Controller } from '../../controller' @@ -14,6 +17,9 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP const showHover = createShowHoverCommand() disposables.push(showHover) + const showGroup = createShowGroupCommand() + disposables.push(showGroup) + const changeCodeLenses = new vscode.EventEmitter() disposables.push(changeCodeLenses) @@ -27,9 +33,10 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP if (anns === null) { return [] } - return prepareAnnotationsForPresentation(anns, makeRange).map(ann => - createCodeLens(doc, ann, showHover) + const { groups, ungrouped } = groupAnnotations( + prepareAnnotationsForPresentation(anns, makeRange) ) + return x.map(ann => createCodeLens(doc, ann, showHover, showGroup)) }) ) }, @@ -48,7 +55,8 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP function createCodeLens( doc: vscode.TextDocument, ann: AnnotationWithAdjustedRange, - showHover: ReturnType + showHover: ReturnType, + showGroup: ReturnType ): CodeLens { // If the presentationHint `show-at-top-of-file` is used, show the code lens at the top of the // file, but make it trigger the hover at its actual location. @@ -72,7 +80,7 @@ function createShowHoverCommand(): { createCommandArgs: (uri: vscode.Uri, pos: vscode.Position) => Pick } & vscode.Disposable { const COMMAND_ID = 'opencodegraph._showHover' - const disposable = vscode.commands.registerCommand(COMMAND_ID, (uri: vscode.Uri, pos: vscode.Position) => { + const disposable = vscode.commands.registerCommand(COMMAND_ID, (uri: vscode.Uri, pos: vscode.Position): void => { const editor = vscode.window.activeTextEditor if (!editor || editor.document.uri.toString() !== uri.toString()) { return @@ -94,6 +102,27 @@ function createShowHoverCommand(): { } } +function createShowGroupCommand(): { + createCommandArgs: (uri: vscode.Uri, pos: vscode.Position) => Pick +} & vscode.Disposable { + const COMMAND_ID = 'opencodegraph._showGroup' + const disposable = vscode.commands.registerCommand( + COMMAND_ID, + (annotations: AnnotationWithAdjustedRange[]): void => {} + ) + return { + createCommandArgs(uri, pos) { + return { + command: COMMAND_ID, + arguments: [uri, pos], + } + }, + dispose() { + disposable.dispose() + }, + } +} + function openWebBrowserCommandArgs(url: string): Pick { return { command: 'vscode.open', diff --git a/lib/ui-common/src/index.ts b/lib/ui-common/src/index.ts index 8adf7e96..c350f0ee 100644 --- a/lib/ui-common/src/index.ts +++ b/lib/ui-common/src/index.ts @@ -1 +1 @@ -export { groupAnnotations, prepareAnnotationsForPresentation } from './ui' +export { groupAnnotations, prepareAnnotationsForPresentation, type AnnotationWithAdjustedRange } from './ui' diff --git a/lib/ui-common/src/ui.test.ts b/lib/ui-common/src/ui.test.ts index 0224976a..f01bce7e 100644 --- a/lib/ui-common/src/ui.test.ts +++ b/lib/ui-common/src/ui.test.ts @@ -26,6 +26,10 @@ describe('prepareAnnotationsForPresentation', () => { start: { line: 0, character: 0 }, end: { line: 0, character: 0 }, }, + originalRange: { + start: { line: 3, character: 4 }, + end: { line: 5, character: 6 }, + }, }, ]) }) @@ -50,20 +54,23 @@ describe('groupAnnotations', () => { }, ]) ).toEqual>({ - groups: { - Docs: [ - { - title: '📘 Docs: Page 1', - url: 'https://example.com/1', - ui: { detail: 'Detail 1', group: 'Docs' }, - }, - { - title: '📘 Docs: Page 2', - url: 'https://example.com/2', - ui: { group: 'Docs' }, - }, + groups: [ + [ + 'Docs', + [ + { + title: '📘 Docs: Page 1', + url: 'https://example.com/1', + ui: { detail: 'Detail 1', group: 'Docs' }, + }, + { + title: '📘 Docs: Page 2', + url: 'https://example.com/2', + ui: { group: 'Docs' }, + }, + ], ], - }, + ], ungrouped: [ { title: '📟 http_request_queue (metric)', diff --git a/lib/ui-common/src/ui.ts b/lib/ui-common/src/ui.ts index 8de9d119..fc2cbe6e 100644 --- a/lib/ui-common/src/ui.ts +++ b/lib/ui-common/src/ui.ts @@ -38,7 +38,7 @@ const ZERO_RANGE: Range = { start: { line: 0, character: 0 }, end: { line: 0, ch * Group annotations that have the same `ui.group` value. */ export function groupAnnotations(annotations: Annotation[]): { - groups: { [group: string]: Annotation[] } + groups: [string, Annotation[]][] ungrouped: Annotation[] } { const groups: { [group: string]: Annotation[] } = {} @@ -68,5 +68,5 @@ export function groupAnnotations(annotations: Annotation[]): { } const ungrouped = annotations.filter(ann => !ann.ui?.group || !groups[ann.ui.group]) - return { groups, ungrouped } + return { groups: Object.entries(groups).toSorted(([a], [b]) => a.localeCompare(b)), ungrouped } }