DiffInformation API - cleanup + renames (#234248)

* Refactoring + renaming things

* Renamed TextEditorDiffKind to TextEditorChangeKind
This commit is contained in:
Ladislau Szomoru 2024-11-20 10:16:04 +01:00 коммит произвёл GitHub
Родитель c83b443da0
Коммит 975e05cd1c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
10 изменённых файлов: 70 добавлений и 62 удалений

Просмотреть файл

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ConfigurationChangeEvent, DecorationOptions, l10n, Position, Range, TextDocument, TextEditor, TextEditorDecorationType, TextEditorDiff, TextEditorDiffKind, ThemeColor, Uri, window, workspace } from 'vscode';
import { ConfigurationChangeEvent, DecorationOptions, l10n, Position, Range, TextDocument, TextEditor, TextEditorChange, TextEditorDecorationType, TextEditorChangeKind, ThemeColor, Uri, window, workspace } from 'vscode';
import { Model } from './model';
import { dispose, fromNow, IDisposable } from './util';
import { Repository } from './repository';
@ -12,10 +12,10 @@ import { BlameInformation } from './git';
const notCommittedYetId = '0000000000000000000000000000000000000000';
function isLineChanged(lineNumber: number, changes: readonly TextEditorDiff[]): boolean {
function isLineChanged(lineNumber: number, changes: readonly TextEditorChange[]): boolean {
for (const change of changes) {
// If the change is a delete, skip it
if (change.kind === TextEditorDiffKind.Deletion) {
if (change.kind === TextEditorChangeKind.Deletion) {
continue;
}
@ -29,30 +29,30 @@ function isLineChanged(lineNumber: number, changes: readonly TextEditorDiff[]):
return false;
}
function mapLineNumber(lineNumber: number, changes: readonly TextEditorDiff[]): number {
function mapLineNumber(lineNumber: number, changes: readonly TextEditorChange[]): number {
if (changes.length === 0) {
return lineNumber;
}
for (const change of changes) {
// Line number is before the change
if ((change.kind === TextEditorDiffKind.Addition && lineNumber < change.modifiedStartLineNumber) ||
(change.kind === TextEditorDiffKind.Modification && lineNumber < change.modifiedStartLineNumber) ||
(change.kind === TextEditorDiffKind.Deletion && lineNumber < change.originalStartLineNumber)) {
if ((change.kind === TextEditorChangeKind.Addition && lineNumber < change.modifiedStartLineNumber) ||
(change.kind === TextEditorChangeKind.Modification && lineNumber < change.modifiedStartLineNumber) ||
(change.kind === TextEditorChangeKind.Deletion && lineNumber < change.originalStartLineNumber)) {
break;
}
// Update line number
switch (change.kind) {
case TextEditorDiffKind.Addition:
case TextEditorChangeKind.Addition:
lineNumber = lineNumber - (change.modifiedEndLineNumber - change.originalStartLineNumber);
break;
case TextEditorDiffKind.Modification:
case TextEditorChangeKind.Modification:
if (change.originalStartLineNumber !== change.modifiedStartLineNumber || change.originalEndLineNumber !== change.modifiedEndLineNumber) {
lineNumber = lineNumber - (change.modifiedEndLineNumber - change.originalEndLineNumber);
}
break;
case TextEditorDiffKind.Deletion:
case TextEditorChangeKind.Deletion:
lineNumber = lineNumber + (change.originalEndLineNumber - change.originalStartLineNumber) + 1;
break;
}
@ -158,13 +158,13 @@ export class GitBlameController {
const decorations: DecorationOptions[] = [];
for (const lineNumber of textEditor.selections.map(s => s.active.line)) {
// Check if the line is in an add/edit change
if (isLineChanged(lineNumber + 1, diffInformation.diff)) {
if (isLineChanged(lineNumber + 1, diffInformation.changes)) {
decorations.push(this._createDecoration(lineNumber, l10n.t('Not Committed Yet')));
continue;
}
// Recalculate the line number factoring in the diff information
const lineNumberWithDiff = mapLineNumber(lineNumber + 1, diffInformation.diff);
const lineNumberWithDiff = mapLineNumber(lineNumber + 1, diffInformation.changes);
const blameInformation = blameInformationCollection.find(blameInformation => {
return blameInformation.ranges.find(range => {
return lineNumberWithDiff >= range.startLineNumber && lineNumberWithDiff <= range.endLineNumber;

Просмотреть файл

@ -1560,7 +1560,7 @@ export class CommandCenter {
const textEditor = window.activeTextEditor;
this.logger.debug('[CommandCenter][stageSelectedChanges] changes:', changes);
this.logger.debug('[CommandCenter][stageSelectedChanges] diffInformation.diff:', textEditor?.diffInformation?.diff);
this.logger.debug('[CommandCenter][stageSelectedChanges] diffInformation.changes:', textEditor?.diffInformation?.changes);
this.logger.debug('[CommandCenter][stageSelectedChanges] diffInformation.isStale:', textEditor?.diffInformation?.isStale);
if (!textEditor || !textEditor.diffInformation || textEditor.diffInformation.isStale) {
@ -1569,8 +1569,8 @@ export class CommandCenter {
const modifiedDocument = textEditor.document;
const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
const selectedChanges = textEditor.diffInformation.diff
.map(diff => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, diff, range), null))
const selectedChanges = textEditor.diffInformation.changes
.map(change => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, change, range), null))
.filter(d => !!d) as LineChange[];
if (!selectedChanges.length) {
@ -1746,7 +1746,7 @@ export class CommandCenter {
const textEditor = window.activeTextEditor;
this.logger.debug('[CommandCenter][revertSelectedRanges] changes:', changes);
this.logger.debug('[CommandCenter][revertSelectedRanges] diffInformation.diff:', textEditor?.diffInformation?.diff);
this.logger.debug('[CommandCenter][revertSelectedRanges] diffInformation.changes:', textEditor?.diffInformation?.changes);
this.logger.debug('[CommandCenter][revertSelectedRanges] diffInformation.isStale:', textEditor?.diffInformation?.isStale);
if (!textEditor || !textEditor.diffInformation || textEditor.diffInformation.isStale) {
@ -1755,12 +1755,12 @@ export class CommandCenter {
const modifiedDocument = textEditor.document;
const selections = textEditor.selections;
const selectedChanges = textEditor.diffInformation.diff.filter(change => {
const selectedChanges = textEditor.diffInformation.changes.filter(change => {
const modifiedRange = getModifiedRange(modifiedDocument, change);
return selections.every(selection => !selection.intersection(modifiedRange));
});
if (selectedChanges.length === textEditor.diffInformation.diff.length) {
if (selectedChanges.length === textEditor.diffInformation.changes.length) {
window.showInformationMessage(l10n.t('The selection range does not contain any changes.'));
return;
}
@ -1827,7 +1827,7 @@ export class CommandCenter {
const textEditor = window.activeTextEditor;
this.logger.debug('[CommandCenter][unstageSelectedRanges] changes:', changes);
this.logger.debug('[CommandCenter][unstageSelectedRanges] diffInformation.diff:', textEditor?.diffInformation?.diff);
this.logger.debug('[CommandCenter][unstageSelectedRanges] diffInformation.changes:', textEditor?.diffInformation?.changes);
this.logger.debug('[CommandCenter][unstageSelectedRanges] diffInformation.isStale:', textEditor?.diffInformation?.isStale);
if (!textEditor || !textEditor.diffInformation || textEditor.diffInformation.isStale) {
@ -1850,9 +1850,9 @@ export class CommandCenter {
const originalUri = toGitUri(modifiedUri, 'HEAD');
const originalDocument = await workspace.openTextDocument(originalUri);
const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
const selectedDiffs = textEditor.diffInformation.diff
.map(diff => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, diff, range), null))
.filter(d => !!d) as LineChange[];
const selectedDiffs = textEditor.diffInformation.changes
.map(change => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, change, range), null))
.filter(c => !!c) as LineChange[];
if (!selectedDiffs.length) {
window.showInformationMessage(l10n.t('The selection range does not contain any changes.'));

Просмотреть файл

@ -24,6 +24,7 @@ import { IGuidesTextModelPart } from './textModelGuides.js';
import { ITokenizationTextModelPart } from './tokenizationTextModelPart.js';
import { UndoRedoGroup } from '../../platform/undoRedo/common/undoRedo.js';
import { TokenArray } from './tokens/tokenArray.js';
import { IEditorModel } from './editorCommon.js';
/**
* Vertical Lane in the overview ruler of the editor.
@ -1328,6 +1329,13 @@ export interface ITextModel {
readonly tokenization: ITokenizationTextModelPart;
}
/**
* @internal
*/
export function isITextModel(obj: IEditorModel): obj is ITextModel {
return Boolean(obj && (obj as ITextModel).uri);
}
/**
* @internal
*/

Просмотреть файл

@ -379,7 +379,7 @@ export interface ITextEditorOptions extends IEditorOptions {
selectionSource?: TextEditorSelectionSource | string;
}
export type ITextEditorDiff = [
export type ITextEditorChange = [
originalStartLineNumber: number,
originalEndLineNumber: number,
modifiedStartLineNumber: number,
@ -390,7 +390,7 @@ export interface ITextEditorDiffInformation {
readonly documentVersion: number;
readonly original: URI | undefined;
readonly modified: URI;
readonly diff: readonly ITextEditorDiff[];
readonly changes: readonly ITextEditorChange[];
}
export function isTextEditorDiffInformationEqual(
@ -400,7 +400,7 @@ export function isTextEditorDiffInformationEqual(
return diff1?.documentVersion === diff2?.documentVersion &&
uriIdentityService.extUri.isEqual(diff1?.original, diff2?.original) &&
uriIdentityService.extUri.isEqual(diff1?.modified, diff2?.modified) &&
equals<ITextEditorDiff>(diff1?.diff, diff2?.diff, (a, b) => {
equals<ITextEditorChange>(diff1?.changes, diff2?.changes, (a, b) => {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
});
}

Просмотреть файл

@ -10,10 +10,10 @@ import { URI, UriComponents } from '../../../base/common/uri.js';
import { ICodeEditorService } from '../../../editor/browser/services/codeEditorService.js';
import { IRange } from '../../../editor/common/core/range.js';
import { ISelection } from '../../../editor/common/core/selection.js';
import { IDecorationOptions, IDecorationRenderOptions, IDiffEditorModel } from '../../../editor/common/editorCommon.js';
import { IDecorationOptions, IDecorationRenderOptions } from '../../../editor/common/editorCommon.js';
import { ISingleEditOperation } from '../../../editor/common/core/editOperation.js';
import { CommandsRegistry } from '../../../platform/commands/common/commands.js';
import { ITextEditorOptions, IResourceEditorInput, EditorActivation, EditorResolution, ITextEditorDiffInformation, isTextEditorDiffInformationEqual, ITextEditorDiff } from '../../../platform/editor/common/editor.js';
import { ITextEditorOptions, IResourceEditorInput, EditorActivation, EditorResolution, ITextEditorDiffInformation, isTextEditorDiffInformationEqual, ITextEditorChange } from '../../../platform/editor/common/editor.js';
import { ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';
import { MainThreadTextEditor } from './mainThreadEditor.js';
import { ExtHostContext, ExtHostEditorsShape, IApplyEditsOptions, ITextDocumentShowOptions, ITextEditorConfigurationUpdate, ITextEditorPositionData, IUndoStopOptions, MainThreadTextEditorsShape, TextEditorRevealType } from '../common/extHost.protocol.js';
@ -32,7 +32,7 @@ import { DirtyDiffContribution } from '../../contrib/scm/browser/dirtydiffDecora
import { IDirtyDiffModelService } from '../../contrib/scm/browser/diff.js';
import { autorun, constObservable, derived, derivedOpts, IObservable, observableFromEvent } from '../../../base/common/observable.js';
import { IUriIdentityService } from '../../../platform/uriIdentity/common/uriIdentity.js';
import { ITextModel } from '../../../editor/common/model.js';
import { isITextModel } from '../../../editor/common/model.js';
export interface IMainThreadEditorLocator {
getEditor(id: string): MainThreadTextEditor | undefined;
@ -140,29 +140,29 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
d.getOriginalEditor().getId() === codeEditor.getId() ||
d.getModifiedEditor().getId() === codeEditor.getId());
const codeEditorTextModelObs = diffEditor
const editorModelObs = diffEditor
? observableFromEvent(this, diffEditor.onDidChangeModel, () => diffEditor.getModel())
: observableFromEvent(this, codeEditor.onDidChangeModel, () => codeEditor.getModel());
const changesObs = derived<IObservable<{ original: URI; modified: URI; changes: IChange[] } | undefined>>(reader => {
const codeEditorTextModel = codeEditorTextModelObs.read(reader);
if (!codeEditorTextModel) {
const editorChangesObs = derived<IObservable<{ original: URI; modified: URI; changes: IChange[] } | undefined>>(reader => {
const editorModel = editorModelObs.read(reader);
if (!editorModel) {
return constObservable(undefined);
}
// DiffEditor
if (diffEditor) {
if (!isITextModel(editorModel)) {
return observableFromEvent(diffEditor.onDidUpdateDiff, () => {
return {
original: (codeEditorTextModel as IDiffEditorModel).original.uri,
modified: (codeEditorTextModel as IDiffEditorModel).modified.uri,
original: editorModel.original.uri,
modified: editorModel.modified.uri,
changes: diffEditor.getLineChanges() ?? []
};
});
}
// TextEditor
const dirtyDiffModel = this._dirtyDiffModelService.getOrCreateModel((codeEditorTextModel as ITextModel).uri);
const dirtyDiffModel = this._dirtyDiffModelService.getOrCreateModel(editorModel.uri);
if (!dirtyDiffModel) {
return constObservable(undefined);
}
@ -178,7 +178,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
return {
original: scmQuickDiff.originalResource,
modified: (codeEditorTextModel as ITextModel).uri,
modified: editorModel.uri,
changes
};
});
@ -188,17 +188,17 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
owner: this,
equalsFn: (diff1, diff2) => isTextEditorDiffInformationEqual(this._uriIdentityService, diff1, diff2)
}, reader => {
const codeEditorTextModel = codeEditorTextModelObs.read(reader);
const changes = changesObs.read(reader).read(reader);
if (!codeEditorTextModel || !changes) {
const editorModel = editorModelObs.read(reader);
const editorChanges = editorChangesObs.read(reader).read(reader);
if (!editorModel || !editorChanges) {
return undefined;
}
const documentVersion = diffEditor
? (codeEditorTextModel as IDiffEditorModel).modified.getVersionId()
: (codeEditorTextModel as ITextModel).getVersionId();
const documentVersion = isITextModel(editorModel)
? editorModel.getVersionId()
: editorModel.modified.getVersionId();
const diff: ITextEditorDiff[] = changes.changes
const changes: ITextEditorChange[] = editorChanges.changes
.map(change => [
change.originalStartLineNumber,
change.originalEndLineNumber,
@ -208,9 +208,9 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
return {
documentVersion,
original: changes.original,
modified: changes.modified,
diff
original: editorChanges.original,
modified: editorChanges.modified,
changes
};
});
}

Просмотреть файл

@ -1671,7 +1671,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
TextEdit: extHostTypes.TextEdit,
SnippetTextEdit: extHostTypes.SnippetTextEdit,
TextEditorCursorStyle: TextEditorCursorStyle,
TextEditorDiffKind: extHostTypes.TextEditorDiffKind,
TextEditorChangeKind: extHostTypes.TextEditorChangeKind,
TextEditorLineNumbersStyle: extHostTypes.TextEditorLineNumbersStyle,
TextEditorRevealType: extHostTypes.TextEditorRevealType,
TextEditorSelectionChangeKind: extHostTypes.TextEditorSelectionChangeKind,

Просмотреть файл

@ -1821,7 +1821,7 @@ export interface ITextEditorPositionData {
[id: string]: EditorGroupColumn;
}
export type ITextEditorDiff = [
export type ITextEditorChange = [
originalStartLineNumber: number,
originalEndLineNumber: number,
modifiedStartLineNumber: number,
@ -1832,7 +1832,7 @@ export interface ITextEditorDiffInformation {
readonly documentVersion: number;
readonly original: UriComponents | undefined;
readonly modified: UriComponents;
readonly diff: readonly ITextEditorDiff[];
readonly changes: readonly ITextEditorChange[];
}
export interface IEditorPropertiesChangeData {

Просмотреть файл

@ -12,7 +12,7 @@ import { ExtHostEditorsShape, IEditorPropertiesChangeData, IMainContext, ITextDo
import { ExtHostDocumentsAndEditors } from './extHostDocumentsAndEditors.js';
import { ExtHostTextEditor, TextEditorDecorationType } from './extHostTextEditor.js';
import * as TypeConverters from './extHostTypeConverters.js';
import { TextEditorSelectionChangeKind, TextEditorDiffKind } from './extHostTypes.js';
import { TextEditorSelectionChangeKind, TextEditorChangeKind } from './extHostTypes.js';
import * as vscode from 'vscode';
export class ExtHostEditors extends Disposable implements ExtHostEditorsShape {
@ -178,11 +178,11 @@ export class ExtHostEditors extends Disposable implements ExtHostEditorsShape {
const original = URI.revive(diffInformation.original);
const modified = URI.revive(diffInformation.modified);
const diff = diffInformation.diff.map(diff => {
const [originalStartLineNumber, originalEndLineNumber, modifiedStartLineNumber, modifiedEndLineNumber] = diff;
const changes = diffInformation.changes.map(change => {
const [originalStartLineNumber, originalEndLineNumber, modifiedStartLineNumber, modifiedEndLineNumber] = change;
const kind = originalEndLineNumber === 0 ? TextEditorDiffKind.Addition :
modifiedEndLineNumber === 0 ? TextEditorDiffKind.Deletion : TextEditorDiffKind.Modification;
const kind = originalEndLineNumber === 0 ? TextEditorChangeKind.Addition :
modifiedEndLineNumber === 0 ? TextEditorChangeKind.Deletion : TextEditorChangeKind.Modification;
return {
originalStartLineNumber,
@ -190,7 +190,7 @@ export class ExtHostEditors extends Disposable implements ExtHostEditorsShape {
modifiedStartLineNumber,
modifiedEndLineNumber,
kind
} satisfies vscode.TextEditorDiff;
} satisfies vscode.TextEditorChange;
});
const that = this;
@ -198,7 +198,7 @@ export class ExtHostEditors extends Disposable implements ExtHostEditorsShape {
documentVersion: diffInformation.documentVersion,
original,
modified,
diff,
changes,
get isStale(): boolean {
const document = that._extHostDocumentsAndEditors.getDocument(modified);
return document?.version !== diffInformation.documentVersion;

Просмотреть файл

@ -1909,7 +1909,7 @@ export enum TextEditorSelectionChangeKind {
Command = 3
}
export enum TextEditorDiffKind {
export enum TextEditorChangeKind {
Addition = 1,
Deletion = 2,
Modification = 3

Просмотреть файл

@ -6,25 +6,25 @@
declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/84899
export enum TextEditorDiffKind {
export enum TextEditorChangeKind {
Addition = 1,
Deletion = 2,
Modification = 3
}
export interface TextEditorDiff {
export interface TextEditorChange {
readonly originalStartLineNumber: number;
readonly originalEndLineNumber: number;
readonly modifiedStartLineNumber: number;
readonly modifiedEndLineNumber: number;
readonly kind: TextEditorDiffKind;
readonly kind: TextEditorChangeKind;
}
export interface TextEditorDiffInformation {
readonly documentVersion: number;
readonly original: Uri | undefined;
readonly modified: Uri;
readonly diff: readonly TextEditorDiff[];
readonly changes: readonly TextEditorChange[];
readonly isStale: boolean;
}