Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Jan 8, 2024
1 parent fc59e01 commit 2a665f4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
8 changes: 7 additions & 1 deletion client/vscode/src/ui/editor/codeLens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, vscode.CodeLens[] | null>)
})
})
Expand Down
41 changes: 35 additions & 6 deletions client/vscode/src/ui/editor/codeLens.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<void>()
disposables.push(changeCodeLenses)

Expand All @@ -27,9 +33,10 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP
if (anns === null) {
return []
}
return prepareAnnotationsForPresentation<vscode.Range>(anns, makeRange).map(ann =>
createCodeLens(doc, ann, showHover)
const { groups, ungrouped } = groupAnnotations(
prepareAnnotationsForPresentation<vscode.Range>(anns, makeRange)
)
return x.map(ann => createCodeLens(doc, ann, showHover, showGroup))
})
)
},
Expand All @@ -48,7 +55,8 @@ export function createCodeLensProvider(controller: Controller): vscode.CodeLensP
function createCodeLens(
doc: vscode.TextDocument,
ann: AnnotationWithAdjustedRange<vscode.Range>,
showHover: ReturnType<typeof createShowHoverCommand>
showHover: ReturnType<typeof createShowHoverCommand>,
showGroup: ReturnType<typeof createShowGroupCommand>
): 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.
Expand All @@ -72,7 +80,7 @@ function createShowHoverCommand(): {
createCommandArgs: (uri: vscode.Uri, pos: vscode.Position) => Pick<vscode.Command, 'command' | 'arguments'>
} & 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
Expand All @@ -94,6 +102,27 @@ function createShowHoverCommand(): {
}
}

function createShowGroupCommand(): {
createCommandArgs: (uri: vscode.Uri, pos: vscode.Position) => Pick<vscode.Command, 'command' | 'arguments'>
} & vscode.Disposable {
const COMMAND_ID = 'opencodegraph._showGroup'
const disposable = vscode.commands.registerCommand(
COMMAND_ID,
(annotations: AnnotationWithAdjustedRange<vscode.Range>[]): void => {}
)
return {
createCommandArgs(uri, pos) {
return {
command: COMMAND_ID,
arguments: [uri, pos],
}
},
dispose() {
disposable.dispose()
},
}
}

function openWebBrowserCommandArgs(url: string): Pick<vscode.Command, 'command' | 'arguments'> {
return {
command: 'vscode.open',
Expand Down
2 changes: 1 addition & 1 deletion lib/ui-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { groupAnnotations, prepareAnnotationsForPresentation } from './ui'
export { groupAnnotations, prepareAnnotationsForPresentation, type AnnotationWithAdjustedRange } from './ui'
33 changes: 20 additions & 13 deletions lib/ui-common/src/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
},
])
})
Expand All @@ -50,20 +54,23 @@ describe('groupAnnotations', () => {
},
])
).toEqual<ReturnType<typeof groupAnnotations>>({
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)',
Expand Down
4 changes: 2 additions & 2 deletions lib/ui-common/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] } = {}
Expand Down Expand Up @@ -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 }
}

0 comments on commit 2a665f4

Please sign in to comment.