Skip to content

Code vision ​

Rows above each declaration show how often it is used and who wrote it. Pressing the usages opens a peek of the references; pressing the authors opens a card.

tsx
import type { Editor } from '@adecore/editor';
import type { EditorLanguage, GitBlameResult } from '@adecore/editor-react';
import type { FakeLanguageService } from '@adecore/editor-react/testing';
import { splitLines } from '@adecore/merge';
import { useCallback } from 'react';
import { LanguageFrame } from '../shared/editor-frame.tsx';
import { SAMPLE, useDemoLanguage } from '../shared/editor.ts';
import { respondNavigation, respondSymbols } from '../shared/order-service.ts';

function configure(service: FakeLanguageService): void {
    respondSymbols(service);
    respondNavigation(service);
}

const DAY = 24 * 60 * 60 * 1000;

/* What `git blame` would say about the file on disk: two commits, the second of which wrote the last function. */
const BLAME: GitBlameResult = {
    commits: [
        { hash: 'a1b2c3d4e5', shortHash: 'a1b2c3d', author: 'Ada Lovelace', email: 'ada@example.com', at: Date.now() - 30 * DAY, summary: 'Add order totals' },
        { hash: 'f6e5d4c3b2', shortHash: 'f6e5d4c', author: 'Grace Hopper', email: 'grace@example.com', at: Date.now() - 2 * DAY, summary: 'Describe an order' }
    ],
    lines: splitLines(SAMPLE).map((_line, index) => (index >= 10 ? 1 : 0))
};

export default function CodeVisionDemo() {
    const { project, onMount } = useDemoLanguage(configure);

    const mount = useCallback(
        (editor: Editor, language: EditorLanguage | null) => {
            language?.codeVision.configure({ usages: true, authors: true });
            language?.codeVision.setBlame({ kind: 'ready', blame: BLAME, base: SAMPLE });
            return onMount(editor, language);
        },
        [onMount]
    );

    return (
        <LanguageFrame
            project={project}
            onMount={mount}
            toolbar={<span className="px-1 text-xs text-text-muted">Press the authors above a function, or its usages.</span>}
        />
    );
}

Both kinds are off until the app turns them on with CodeVisionSettings:

ts
language.codeVision.configure({ usages: true, authors: true });

Usages come from textDocument/references for the declarations in and near the view, once text and scroll settle. Declarations come from the document's symbols.

Authors come from Git, which the package never runs. The app runs git blame on the file on disk and passes the result with that text as base:

ts
language.codeVision.setBlame({ kind: 'pending' }); // holds the rows' height while Git runs
language.codeVision.setBlame({ kind: 'ready', blame, base: textOnDisk, openCommit: (hash) => showCommit(hash) });

setBlame takes a BlameSource. A GitBlameResult has the commits, each a GitBlameCommit (hash, shortHash, author, email, at in milliseconds, summary), and lines, the commit index of each line of base. omitted: 'untracked' or 'too-large' says why there is no blame. Lines typed since count as uncommitted. setBlame(null) removes the authors.

The card lists the declaration's authors by lines written, the uncommitted lines and the latest commit, with a button to it when openCommit is set. A host with its own card reads the authors slot, an AuthorsView, of language.popups.

Files over 20,000 lines or 3,000 declarations get no rows.

Licenses and third-party notices are listed in each package.