|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 |
| -import { LRUCachedFunction } from '../../../../../../base/common/cache.js'; |
7 |
| -import { CancellationToken } from '../../../../../../base/common/cancellation.js'; |
8 |
| -import { equalsIfDefined, itemEquals } from '../../../../../../base/common/equals.js'; |
9 | 6 | import { createHotClass } from '../../../../../../base/common/hotReloadHelpers.js';
|
10 | 7 | import { Disposable } from '../../../../../../base/common/lifecycle.js';
|
11 |
| -import { derivedDisposable, ObservablePromise, derived, IObservable, derivedOpts, ISettableObservable } from '../../../../../../base/common/observable.js'; |
| 8 | +import { derived, IObservable, ISettableObservable } from '../../../../../../base/common/observable.js'; |
12 | 9 | import { IInstantiationService } from '../../../../../../platform/instantiation/common/instantiation.js';
|
13 | 10 | import { ICodeEditor } from '../../../../../browser/editorBrowser.js';
|
14 |
| -import { IDiffProviderFactoryService } from '../../../../../browser/widget/diffEditor/diffProviderFactoryService.js'; |
15 | 11 | import { SingleLineEdit } from '../../../../../common/core/lineEdit.js';
|
16 | 12 | import { Position } from '../../../../../common/core/position.js';
|
17 | 13 | import { Range } from '../../../../../common/core/range.js';
|
18 | 14 | import { SingleTextEdit, TextEdit, AbstractText } from '../../../../../common/core/textEdit.js';
|
19 |
| -import { TextLength } from '../../../../../common/core/textLength.js'; |
20 | 15 | import { Command } from '../../../../../common/languages.js';
|
21 | 16 | import { TextModelText } from '../../../../../common/model/textModelText.js';
|
22 |
| -import { IModelService } from '../../../../../common/services/model.js'; |
23 | 17 | import { InlineCompletionsModel } from '../../model/inlineCompletionsModel.js';
|
24 | 18 | import { InlineEdit } from '../../model/inlineEdit.js';
|
25 | 19 | import { InlineCompletionItem } from '../../model/provideInlineCompletions.js';
|
26 | 20 | import { InlineEditsView } from './view.js';
|
27 |
| -import { UniqueUriGenerator } from './utils.js'; |
28 | 21 |
|
29 |
| -export class InlineEditsViewAndDiffProducer extends Disposable { |
| 22 | +export class InlineEditsViewAndDiffProducer extends Disposable { // TODO: This class is no longer a diff producer. Rename it or get rid of it |
30 | 23 | public static readonly hot = createHotClass(InlineEditsViewAndDiffProducer);
|
31 | 24 |
|
32 |
| - private readonly _modelUriGenerator = new UniqueUriGenerator('inline-edits'); |
33 |
| - |
34 |
| - private readonly _originalModel = derivedDisposable(() => this._modelService.createModel( |
35 |
| - '', null, this._modelUriGenerator.getUniqueUri())).keepObserved(this._store); |
36 |
| - private readonly _modifiedModel = derivedDisposable(() => this._modelService.createModel( |
37 |
| - '', null, this._modelUriGenerator.getUniqueUri())).keepObserved(this._store); |
38 |
| - |
39 |
| - private readonly _differ = new LRUCachedFunction({ getCacheKey: JSON.stringify }, (arg: { original: string; modified: string }) => { |
40 |
| - this._originalModel.get().setValue(arg.original); |
41 |
| - this._modifiedModel.get().setValue(arg.modified); |
42 |
| - |
43 |
| - const diffAlgo = this._diffProviderFactoryService.createDiffProvider({ diffAlgorithm: 'advanced' }); |
44 |
| - |
45 |
| - return ObservablePromise.fromFn(async () => { |
46 |
| - const result = await diffAlgo.computeDiff(this._originalModel.get(), this._modifiedModel.get(), { |
47 |
| - computeMoves: false, |
48 |
| - ignoreTrimWhitespace: false, |
49 |
| - maxComputationTimeMs: 1000, |
50 |
| - extendToSubwords: true, |
51 |
| - }, CancellationToken.None); |
52 |
| - return result; |
53 |
| - }); |
54 |
| - }); |
55 |
| - |
56 |
| - private readonly _inlineEditPromise = derived<IObservable<InlineEditWithChanges | undefined> | undefined>(this, (reader) => { |
| 25 | + private readonly _inlineEdit = derived<InlineEditWithChanges | undefined>(this, (reader) => { |
57 | 26 | const model = this._model.read(reader);
|
58 | 27 | if (!model) { return undefined; }
|
59 | 28 | const inlineEdit = this._edit.read(reader);
|
60 | 29 | if (!inlineEdit) { return undefined; }
|
| 30 | + const textModel = this._editor.getModel(); |
| 31 | + if (!textModel) { return undefined; } |
| 32 | + |
| 33 | + const editOffset = model.inlineEditState.get()?.inlineCompletion.inlineEdit.read(reader); |
| 34 | + if (!editOffset) { return undefined; } |
| 35 | + |
| 36 | + const edits = editOffset.edits.map(e => { |
| 37 | + const innerEditRange = Range.fromPositions( |
| 38 | + textModel.getPositionAt(e.replaceRange.start), |
| 39 | + textModel.getPositionAt(e.replaceRange.endExclusive) |
| 40 | + ); |
| 41 | + return new SingleTextEdit(innerEditRange, e.newText); |
| 42 | + }); |
61 | 43 |
|
62 |
| - //if (inlineEdit.text.trim() === '') { return undefined; } |
63 |
| - const text = new TextModelText(this._editor.getModel()!); |
64 |
| - const edit = inlineEdit.edit.extendToFullLine(text); |
65 |
| - |
66 |
| - const diffResult = this._differ.get({ original: this._editor.getModel()!.getValueInRange(edit.range), modified: edit.text }); |
67 |
| - |
68 |
| - return diffResult.promiseResult.map(p => { |
69 |
| - if (!p || !p.data) { |
70 |
| - return undefined; |
71 |
| - } |
72 |
| - const result = p.data; |
73 |
| - |
74 |
| - const rangeStartPos = edit.range.getStartPosition(); |
75 |
| - const innerChanges = result.changes.flatMap(c => c.innerChanges!); |
76 |
| - if (innerChanges.length === 0) { |
77 |
| - // there are no changes |
78 |
| - return undefined; |
79 |
| - } |
80 |
| - |
81 |
| - function addRangeToPos(pos: Position, range: Range): Range { |
82 |
| - const start = TextLength.fromPosition(range.getStartPosition()); |
83 |
| - return TextLength.ofRange(range).createRange(start.addToPosition(pos)); |
84 |
| - } |
85 |
| - |
86 |
| - const edits = innerChanges.map(c => new SingleTextEdit( |
87 |
| - addRangeToPos(rangeStartPos, c.originalRange), |
88 |
| - this._modifiedModel.get()!.getValueInRange(c.modifiedRange) |
89 |
| - )); |
90 |
| - const diffEdits = new TextEdit(edits); |
| 44 | + const diffEdits = new TextEdit(edits); |
| 45 | + const text = new TextModelText(textModel); |
91 | 46 |
|
92 |
| - return new InlineEditWithChanges(text, diffEdits, inlineEdit.isCollapsed, model.primaryPosition.get(), inlineEdit.renderExplicitly, inlineEdit.commands, inlineEdit.inlineCompletion); //inlineEdit.showInlineIfPossible); |
93 |
| - }); |
| 47 | + return new InlineEditWithChanges(text, diffEdits, inlineEdit.isCollapsed, model.primaryPosition.get(), inlineEdit.renderExplicitly, inlineEdit.commands, inlineEdit.inlineCompletion); |
94 | 48 | });
|
95 | 49 |
|
96 |
| - private readonly _inlineEdit = derivedOpts({ owner: this, equalsFn: equalsIfDefined(itemEquals()) }, reader => this._inlineEditPromise.read(reader)?.read(reader)); |
97 |
| - |
98 | 50 | constructor(
|
99 | 51 | private readonly _editor: ICodeEditor,
|
100 | 52 | private readonly _edit: IObservable<InlineEdit | undefined>,
|
101 | 53 | private readonly _model: IObservable<InlineCompletionsModel | undefined>,
|
102 | 54 | private readonly _focusIsInMenu: ISettableObservable<boolean>,
|
103 | 55 | @IInstantiationService private readonly _instantiationService: IInstantiationService,
|
104 |
| - @IDiffProviderFactoryService private readonly _diffProviderFactoryService: IDiffProviderFactoryService, |
105 |
| - @IModelService private readonly _modelService: IModelService |
106 | 56 | ) {
|
107 | 57 | super();
|
108 | 58 |
|
|
0 commit comments