Skip to content

Commit

Permalink
update inline diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewpareles committed Nov 1, 2024
1 parent b34119c commit d7c00ad
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 25 deletions.
5 changes: 3 additions & 2 deletions extensions/void/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
"--extensionDevelopmentPath=${workspaceFolder}",
"--enable-proposed-api=void.void",
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}
}
5 changes: 5 additions & 0 deletions extensions/void/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const convertTSXtoJS = async ({ from, to }) => {
to: 'dist/webviews/ctrlk/index.js',
})

await convertTSXtoJS({
from: 'src/webviews/diffline/index.tsx',
to: 'dist/webviews/diffline/index.js',
})

// convert tailwind to css
await convertTailwindToCSS({
from: 'src/webviews/styles.css',
Expand Down
3 changes: 3 additions & 0 deletions extensions/void/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"categories": [
"Other"
],
"enabledApiProposals": [
"editorInsets"
],
"activationEvents": [],
"main": "./out/extension/extension.js",
"contributes": {
Expand Down
25 changes: 21 additions & 4 deletions extensions/void/src/extension/DiffProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { findDiffs } from './findDiffs';
import { throttle } from 'lodash';
import { DiffArea, BaseDiff, Diff } from '../common/shared_types';
import { readFileContentOfUri } from './extensionLib/readFileContentOfUri';
import { updateWebviewHTML } from './extensionLib/updateWebviewHTML';


const THROTTLE_TIME = 100
Expand Down Expand Up @@ -31,6 +32,8 @@ export class DiffProvider implements vscode.CodeLensProvider {
private _diffareaidPool = 0
private _diffidPool = 0

private _extensionUri: vscode.Uri

// used internally by vscode
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); // signals a UI refresh on .fire() events
public readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
Expand All @@ -42,7 +45,8 @@ export class DiffProvider implements vscode.CodeLensProvider {
}

// declared by us, registered with vscode.languages.registerCodeLensProvider()
constructor() {
constructor(context: vscode.ExtensionContext) {
this._extensionUri = context.extensionUri

console.log('Creating DisplayChangesProvider')

Expand Down Expand Up @@ -210,6 +214,21 @@ export class DiffProvider implements vscode.CodeLensProvider {
)
);

// update red highlighting
// this._diffsOfDocument[docUriStr]
// .filter(diff => diff.originalCode !== '')
// .forEach(diff => {
// const text = originalFile.split('\n').slice(diff.originalRange.start.line, diff.originalRange.start.line + 1).join('\n')
// const height = text.split('\n').length
// const line = diff.range.start.line - 1

// const inset = vscode.window.createWebviewTextEditorInset(editor, line, height);
// updateWebviewHTML(inset.webview, this._extensionUri, { jsOutLocation: 'dist/webviews/diffline/index.js', cssOutLocation: 'dist/webviews/styles.css' },
// { text }
// )


// })

// for each diffArea, highlight its sweepIndex in dark gray
editor.setDecorations(
Expand All @@ -234,8 +253,6 @@ export class DiffProvider implements vscode.CodeLensProvider {
)
)

// TODO update red highlighting
// this._diffsOfDocument[docUriStr].map(diff => diff.deletedCode)

// update code lenses
this._onDidChangeCodeLenses.fire()
Expand Down Expand Up @@ -392,7 +409,7 @@ export class DiffProvider implements vscode.CodeLensProvider {

// figure out where to highlight based on where the AI is in the stream right now, use the last diff in findDiffs to figure that out
const diffs = findDiffs(originalDiffAreaCode, newDiffAreaCode)
const lastDiff = diffs[diffs.length - 1] ?? null
const lastDiff = diffs?.[diffs.length - 1] ?? null

// these are two different coordinate systems - new and old line number
let newFileEndLine: number // get new[0...newStoppingPoint] with line=newStoppingPoint highlighted
Expand Down
2 changes: 1 addition & 1 deletion extensions/void/src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function activate(context: vscode.ExtensionContext) {
);

// 3. Show an approve/reject codelens above each change
const diffProvider = new DiffProvider();
const diffProvider = new DiffProvider(context);
context.subscriptions.push(vscode.languages.registerCodeLensProvider('*', diffProvider));

// 4. Add approve/reject commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const updateWebviewHTML = (webview: vscode.Webview, extensionUri: vscode.
<link href="${stylesUri}" rel="stylesheet">
</head>
<body>
<div id="root" ${props ? `data-void-props="${encodeURIComponent(JSON.stringify(props))}"` : ''}></div>
<div id="root"${props ? ` data-void-props="${encodeURIComponent(JSON.stringify(props))}"` : ''}></div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
Expand Down
27 changes: 19 additions & 8 deletions extensions/void/src/webviews/common/contextForProps.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import React, { ReactNode, createContext, useCallback, useContext, useEffect, useRef, useState, } from "react"

type PropsType = { [s: string]: any } | null
const PropsContext = createContext<any>(undefined as unknown as any)

type PropsValue = { props: PropsType }
// provider for whatever came in data-void-props
export function PropsProvider({ children, rootElement }: { children: ReactNode, rootElement: HTMLElement }) {

const PropsContext = createContext<PropsValue>(undefined as unknown as PropsValue)
const [props, setProps] = useState<object | null>(null)

// update props when rootElement changes
useEffect(() => {
let props = rootElement.getAttribute("data-void-props")
let propsObj: object | null = null
if (props !== null) {
propsObj = JSON.parse(decodeURIComponent(props))
}
setProps(propsObj)
}, [rootElement])

// provider for whatever came in data-void-props
export function PropsProvider({ children, props }: { children: ReactNode, props: PropsType }) {
return (
<PropsContext.Provider value={{ props }}>
<PropsContext.Provider value={props}>
{children}
</PropsContext.Provider>
)
}

export function useVoidProps(): PropsValue {
const context = useContext<PropsValue>(PropsContext)
export function useVoidProps<T extends {}>(): T | null {
// context is the "value" from above
const context: T | null | undefined = useContext<T>(PropsContext)
// only undefined if has no provider
if (context === undefined) {
throw new Error("useVoidProps missing Provider")
}
Expand Down
12 changes: 3 additions & 9 deletions extensions/void/src/webviews/common/mount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,12 @@ export const mount = (children: React.ReactNode) => {

// mount the sidebar on the id="root" element
const rootElement = document.getElementById("root")!
console.log("Void root Element:", rootElement)

let props = rootElement.getAttribute("data-void-props")
let propsObj: object | null = null
if (props !== null) {
propsObj = JSON.parse(decodeURIComponent(props))
}
// console.log("Void root Element:", rootElement)

const content = (<>
<ListenersAndTracking />

<PropsProvider props={propsObj}>
<PropsProvider rootElement={rootElement}>
<ThreadsProvider>
<ConfigProvider>
{children}
Expand All @@ -69,4 +63,4 @@ export const mount = (children: React.ReactNode) => {
const root = ReactDOM.createRoot(rootElement)
root.render(content);

}
}
29 changes: 29 additions & 0 deletions extensions/void/src/webviews/diffline/DiffLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState } from 'react';
import { useOnVSCodeMessage } from '../common/getVscodeApi';
import { useVoidProps } from '../common/contextForProps';


type props = {
text: string
}

export const DiffLine = () => {

const props = useVoidProps<props>()

console.log('props!', props)

if (!props) {
return null
}

// eslint-disable-next-line react/prop-types
const text = props.text

return <>
<div>
{text}
</div>
</>
};

7 changes: 7 additions & 0 deletions extensions/void/src/webviews/diffline/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react"
import { mount } from "../common/mount"
import { DiffLine } from "./DiffLine"

// this is the entry point that mounts diffline
mount(<DiffLine />)

0 comments on commit d7c00ad

Please sign in to comment.