Skip to content

Commit

Permalink
Fixing incorrect color range calculation (#200626)
Browse files Browse the repository at this point in the history
* fixing incorrect color new range calculation
  • Loading branch information
aiday-mar authored Dec 12, 2023
1 parent cb0ff84 commit b1cffad
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/vs/editor/contrib/colorPicker/browser/colorHoverParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AsyncIterableObject } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Color, RGBA } from 'vs/base/common/color';
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Range } from 'vs/editor/common/core/range';
import { IModelDecoration, ITextModel, TrackedRangeStickiness } from 'vs/editor/common/model';
Expand Down Expand Up @@ -207,7 +207,7 @@ function renderHoverParts(participant: ColorHoverParticipant | StandaloneColorPi
disposables.add(model.onColorFlushed(async (color: Color) => {
await _updateColorPresentations(editorModel, model, color, range, colorHover);
editorUpdatedByColorPicker = true;
range = _updateEditorModel(editor, range, model, context);
range = _updateEditorModel(editor, range, model);
}));
}
disposables.add(model.onDidChangeColor((color: Color) => {
Expand All @@ -224,30 +224,19 @@ function renderHoverParts(participant: ColorHoverParticipant | StandaloneColorPi
return disposables;
}

function _updateEditorModel(editor: ICodeEditor, range: Range, model: ColorPickerModel, context?: IEditorHoverRenderContext) {
let newRange: Range;
function _updateEditorModel(editor: IActiveCodeEditor, range: Range, model: ColorPickerModel): Range {
const textEdits: ISingleEditOperation[] = [];
if (model.presentation.textEdit) {
textEdits.push(model.presentation.textEdit);
newRange = new Range(
model.presentation.textEdit.range.startLineNumber,
model.presentation.textEdit.range.startColumn,
model.presentation.textEdit.range.endLineNumber,
model.presentation.textEdit.range.endColumn
);
const trackedRange = editor.getModel()!._setTrackedRange(null, newRange, TrackedRangeStickiness.GrowsOnlyWhenTypingAfter);
newRange = editor.getModel()!._getTrackedRange(trackedRange) || newRange;
} else {
textEdits.push({ range, text: model.presentation.label, forceMoveMarkers: false });
newRange = range.setEndPosition(range.endLineNumber, range.startColumn + model.presentation.label.length);
}
const edit = model.presentation.textEdit ?? { range, text: model.presentation.label, forceMoveMarkers: false };
textEdits.push(edit);

if (model.presentation.additionalTextEdits) {
textEdits.push(...model.presentation.additionalTextEdits);
}
const replaceRange = Range.lift(edit.range);
const trackedRange = editor.getModel()._setTrackedRange(null, replaceRange, TrackedRangeStickiness.GrowsOnlyWhenTypingAfter);
editor.executeEdits('colorpicker', textEdits);
editor.pushUndoStop();
return newRange;
return editor.getModel()._getTrackedRange(trackedRange) ?? replaceRange;
}

async function _updateColorPresentations(editorModel: ITextModel, colorPickerModel: ColorPickerModel, color: Color, range: Range, colorHover: ColorHover | StandaloneColorPickerHover) {
Expand Down

0 comments on commit b1cffad

Please sign in to comment.