mirror of
https://github.com/voideditor/void
synced 2026-05-24 09:58:23 +00:00
BEFORE DELETE RESIZE
This commit is contained in:
parent
4ac509f0c8
commit
1437f592d2
2 changed files with 69 additions and 68 deletions
|
|
@ -1,18 +1,20 @@
|
||||||
|
|
||||||
import { Range } from 'vscode';
|
import { Range } from 'vscode';
|
||||||
import { Diff } from './registerInlineDiffs';
|
|
||||||
|
|
||||||
import { diffLines } from './react/out/util/diffLines.js'
|
import { diffLines } from './react/out/util/diffLines.js'
|
||||||
|
|
||||||
type Fields =
|
export type BaseDiff = {
|
||||||
| 'type'
|
type: 'edit' | 'insertion' | 'deletion';
|
||||||
| 'originalCode'
|
originalCode: string;
|
||||||
| 'originalStartLine' | 'originalEndLine'
|
|
||||||
| 'startLine' | 'endLine'
|
|
||||||
| 'startCol' | 'endCol'
|
|
||||||
|
|
||||||
|
startLine: number; // 1-indexed
|
||||||
|
endLine: number;
|
||||||
|
originalStartLine: number;
|
||||||
|
originalEndLine: number;
|
||||||
|
|
||||||
|
startCol: number; // 1-indexed
|
||||||
|
endCol: number;
|
||||||
|
}
|
||||||
|
|
||||||
export type BaseDiff = Pick<Diff, Fields>
|
|
||||||
|
|
||||||
// Andrew diff algo:
|
// Andrew diff algo:
|
||||||
export type SuggestedEdit = {
|
export type SuggestedEdit = {
|
||||||
|
|
|
||||||
|
|
@ -63,11 +63,11 @@ export type Diff = {
|
||||||
|
|
||||||
startLine: number; // 1-indexed
|
startLine: number; // 1-indexed
|
||||||
endLine: number;
|
endLine: number;
|
||||||
originalStartLine: number;
|
// originalStartLine: number;
|
||||||
originalEndLine: number;
|
// originalEndLine: number;
|
||||||
|
|
||||||
startCol: number; // 1-indexed
|
// startCol: number; // 1-indexed
|
||||||
endCol: number;
|
// endCol: number;
|
||||||
|
|
||||||
_disposeDiffZone: (() => void) | null;
|
_disposeDiffZone: (() => void) | null;
|
||||||
|
|
||||||
|
|
@ -80,8 +80,8 @@ export type Diff = {
|
||||||
// _ means anything we don't include if we clone it
|
// _ means anything we don't include if we clone it
|
||||||
type DiffArea = {
|
type DiffArea = {
|
||||||
diffareaid: number,
|
diffareaid: number,
|
||||||
originalStartLine: number,
|
// originalStartLine: number,
|
||||||
originalEndLine: number,
|
// originalEndLine: number,
|
||||||
originalCode: string,
|
originalCode: string,
|
||||||
startLine: number,
|
startLine: number,
|
||||||
endLine: number,
|
endLine: number,
|
||||||
|
|
@ -198,7 +198,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
// this._register(
|
// this._register(
|
||||||
// model.onDidChangeContent(e => {
|
// model.onDidChangeContent(e => {
|
||||||
// const changes = e.changes.map(c => ({ startLine: c.range.startLineNumber, endLine: c.range.endLineNumber, text: c.text, }))
|
// const changes = e.changes.map(c => ({ startLine: c.range.startLineNumber, endLine: c.range.endLineNumber, text: c.text, }))
|
||||||
// this._resizeOnTextChange(model.id, changes, 'currentFile')
|
// this._realignDiffAreas(model.id, changes, 'currentFile')
|
||||||
// this._refreshAllDiffs(model)
|
// this._refreshAllDiffs(model)
|
||||||
// })
|
// })
|
||||||
// )
|
// )
|
||||||
|
|
@ -210,57 +210,55 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
// )
|
// )
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // changes the start/line locations based on the changes that were recently made. does not change any of the diffs in the diff areas
|
|
||||||
// // changes tells us how many lines were inserted/deleted so we can grow/shrink the diffAreas accordingly
|
|
||||||
// private _resizeOnTextChange(modelid: string, changes: { text: string, startLine: number, endLine: number }[], changesTo: 'originalFile' | 'currentFile') {
|
|
||||||
|
|
||||||
// // resize all diffareas on page (adjust their start/end based on the change)
|
|
||||||
|
|
||||||
// let endLine: 'originalEndLine' | 'endLine'
|
|
||||||
// let startLine: 'originalStartLine' | 'startLine'
|
|
||||||
|
|
||||||
// if (changesTo === 'originalFile') {
|
// changes the start/line locations of all DiffAreas on the page (adjust their start/end based on the change) based on the change that was recently made
|
||||||
// endLine = 'originalEndLine' as const
|
private _realignDiffAreas(model: ITextModel, change: { startLine: number; endLine: number; text: string }, changesTo: 'originalFile' | 'currentFile') {
|
||||||
// startLine = 'originalStartLine' as const
|
|
||||||
// } else {
|
|
||||||
// endLine = 'endLine' as const
|
|
||||||
// startLine = 'startLine' as const
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // here, `change.range` is the range of the original file that gets replaced with `change.text`
|
let endLine: 'originalEndLine' | 'endLine'
|
||||||
// for (const change of changes) {
|
let startLine: 'originalStartLine' | 'startLine'
|
||||||
|
|
||||||
// // compute net number of newlines lines that were added/removed
|
if (changesTo === 'currentFile') {
|
||||||
// const numNewLines = (change.text.match(/\n/g) || []).length
|
endLine = 'endLine' as const
|
||||||
// const numLineDeletions = change.endLine - change.startLine
|
startLine = 'startLine' as const
|
||||||
// const deltaNewlines = numNewLines - numLineDeletions
|
} else {
|
||||||
|
endLine = 'originalEndLine' as const
|
||||||
|
startLine = 'originalStartLine' as const
|
||||||
|
}
|
||||||
|
|
||||||
// // compute overlap with each diffArea and shrink/elongate each diffArea accordingly
|
// `change.range` is the range of the original file that gets replaced with `change.text`
|
||||||
// for (const diffareaid of this.diffAreasOfModelId[modelid] || []) {
|
|
||||||
// const diffArea = this.diffAreaOfId[diffareaid]
|
|
||||||
|
|
||||||
// // if the change is fully within the diffArea, elongate it by the delta amount of newlines
|
// compute net number of newlines lines that were added/removed
|
||||||
// if (change.startLine >= diffArea[startLine] && change.endLine <= diffArea[endLine]) {
|
const numNewLines = (change.text.match(/\n/g) || []).length
|
||||||
// diffArea[endLine] += deltaNewlines
|
const numLineDeletions = change.endLine - change.startLine
|
||||||
// }
|
const deltaNewlines = numNewLines - numLineDeletions
|
||||||
// // check if the `diffArea` was fully deleted and remove it if so
|
|
||||||
// if (diffArea[startLine] > diffArea[endLine]) {
|
|
||||||
// this.diffAreasOfModelId[modelid].delete(diffareaid)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // if a diffArea is below the last character of the change, shift the diffArea up/down by the delta amount of newlines
|
// compute overlap with each diffArea and shrink/elongate each diffArea accordingly
|
||||||
// if (diffArea[startLine] > change.endLine) {
|
for (const diffareaid of this.diffAreasOfModelId[model.id] || []) {
|
||||||
// diffArea[startLine] += deltaNewlines
|
const diffArea = this.diffAreaOfId[diffareaid]
|
||||||
// diffArea[endLine] += deltaNewlines
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // TODO handle other cases where eg. the change overlaps many diffAreas
|
// if the change is fully within the diffArea, elongate it by the delta amount of newlines
|
||||||
// }
|
if (change.startLine >= diffArea[startLine] && change.endLine <= diffArea[endLine]) {
|
||||||
// // TODO merge any diffAreas if they overlap with each other as a result from the shift
|
diffArea[endLine] += deltaNewlines
|
||||||
|
}
|
||||||
|
// check if the `diffArea` was fully deleted and remove it if so
|
||||||
|
if (diffArea[startLine] > diffArea[endLine]) {
|
||||||
|
this.diffAreasOfModelId[model.id].delete(diffareaid)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// }
|
// if a diffArea is below the last character of the change, shift the diffArea up/down by the delta amount of newlines
|
||||||
// }
|
if (diffArea[startLine] > change.endLine) {
|
||||||
|
diffArea[startLine] += deltaNewlines
|
||||||
|
diffArea[endLine] += deltaNewlines
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO handle other cases where eg. the change overlaps many diffAreas
|
||||||
|
// TODO merge any diffAreas if they overlap with each other as a result from the shift
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -507,17 +505,14 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private _writeToModel(model: ITextModel, text: string, range: IRange) {
|
private _writeToModel(model: ITextModel, text: string, range: IRange) {
|
||||||
if (!model.isDisposed())
|
if (!model.isDisposed())
|
||||||
model.applyEdits([{ range, text }]) // applies edits without adding them to undo/redo stack
|
model.applyEdits([{ range, text }]) // applies edits without adding them to undo/redo stack
|
||||||
|
|
||||||
|
// realign diffAreas
|
||||||
|
this._realignDiffAreas(model, ,)
|
||||||
|
|
||||||
|
|
||||||
// model.pushEditOperations(null, [{ range, text }], () => null) // applies edits in the group
|
// model.pushEditOperations(null, [{ range, text }], () => null) // applies edits in the group
|
||||||
|
|
||||||
// this._bulkEditService.apply([new ResourceTextEdit(model.uri, {
|
// this._bulkEditService.apply([new ResourceTextEdit(model.uri, {
|
||||||
|
|
@ -528,6 +523,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// @throttle(100)
|
// @throttle(100)
|
||||||
private _onGetNewDiffAreaText(diffArea: DiffArea, newCodeSoFar: string) {
|
private _onGetNewDiffAreaText(diffArea: DiffArea, newCodeSoFar: string) {
|
||||||
|
|
||||||
|
|
@ -582,8 +578,8 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// lines are 1-indexed
|
// lines are 1-indexed
|
||||||
const newFileTop = newCodeSoFar.split('\n').slice(0, (newFileEndLine - 1)).join('\n')
|
|
||||||
const oldFileBottom = diffArea.originalCode.split('\n').slice((oldFileStartLine - 1), Infinity).join('\n')
|
const oldFileBottom = diffArea.originalCode.split('\n').slice((oldFileStartLine - 1), Infinity).join('\n')
|
||||||
|
const newFileTop = newCodeSoFar.split('\n').slice(0, (newFileEndLine - 1)).join('\n')
|
||||||
|
|
||||||
let newCode = `${newFileTop}\n${oldFileBottom}`
|
let newCode = `${newFileTop}\n${oldFileBottom}`
|
||||||
|
|
||||||
|
|
@ -659,8 +655,8 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService {
|
||||||
// in ctrl+L the start and end lines are the full document
|
// in ctrl+L the start and end lines are the full document
|
||||||
const diffArea: DiffArea = {
|
const diffArea: DiffArea = {
|
||||||
diffareaid: diffareaid,
|
diffareaid: diffareaid,
|
||||||
originalStartLine: beginLine,
|
// originalStartLine: beginLine,
|
||||||
originalEndLine: endLine,
|
// originalEndLine: endLine,
|
||||||
originalCode: originalCode,
|
originalCode: originalCode,
|
||||||
startLine: beginLine,
|
startLine: beginLine,
|
||||||
endLine: endLine, // starts out the same as the current file
|
endLine: endLine, // starts out the same as the current file
|
||||||
|
|
@ -714,6 +710,9 @@ Please finish writing the new file by applying the diff to the original file. Re
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// on done, update original
|
||||||
|
diffArea.originalCode =
|
||||||
|
|
||||||
// onFinishEdit()
|
// onFinishEdit()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue