Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pickers for empty chunks #691

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 97 additions & 41 deletions packages/nbdime/src/common/mergeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@
* Effect for adding a gutter marker
*/
const addGutterMarkerEffect = StateEffect.define<{
pos: number;
from: number;
block: boolean;
on: boolean;
type: string;
}>({
map: (val, mapping) => ({
pos: mapping.mapPos(val.pos),
from: mapping.mapPos(val.from),
block: val.block,
on: val.on,
type: val.type,
}),
Expand All @@ -257,10 +259,30 @@
map: val => ({ type: val.type }),
});

class MergeMarker extends GutterMarker {
constructor(options: { symbol: string; className: string; block: boolean }) {
super();
this._symbol = options.symbol;
this._className = options.className;
this._block = options.block;
}
get isBlock() {
return this._block;

Check warning on line 270 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L269-L270

Added lines #L269 - L270 were not covered by tests
}
toDOM() {
let pickerMarker = elt('div', this._symbol);
pickerMarker.className = this._className;
return pickerMarker;
}
private _symbol: string;
private _className: string;
private _block: boolean;
}

/**
* StateField storing information about gutter markers (picker and conflict ones)
*/
const gutterMarkerField = StateField.define<RangeSet<GutterMarker>>({
const gutterMarkerField = StateField.define<RangeSet<MergeMarker>>({
create: () => {
return RangeSet.empty;
},
Expand All @@ -269,9 +291,15 @@
for (let e of transaction.effects) {
if (e.is(addGutterMarkerEffect)) {
if (e.value.on) {
const marker: GutterMarker =
e.value.type === 'picker' ? pickerMarker : conflictMarker;
gutters = gutters.update({ add: [marker.range(e.value.pos)] });
const marker: MergeMarker =
e.value.type === 'picker'
? e.value.block
? pickerBlockMarker
: pickerMarker

Check warning on line 298 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L297-L298

Added lines #L297 - L298 were not covered by tests
: e.value.block
? conflictBlockMarker
: conflictMarker;
gutters = gutters.update({ add: [marker.range(e.value.from)] });

Check warning on line 302 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L300-L302

Added lines #L300 - L302 were not covered by tests
}
}
if (e.is(removeGutterMarkerEffect)) {
Expand All @@ -281,27 +309,34 @@
return gutters;
},
});

/**
* Picker gutter marker DOM Element ➭
*/
const pickerMarker = new (class extends GutterMarker {
toDOM() {
let pickerMarker = elt('div', PICKER_SYMBOL);
pickerMarker.className = GUTTER_PICKER_CLASS;
return pickerMarker;
}
})();
const pickerMarker = new MergeMarker({
symbol: PICKER_SYMBOL,
className: GUTTER_PICKER_CLASS,
block: false,
});
const pickerBlockMarker = new MergeMarker({
symbol: PICKER_SYMBOL,
className: GUTTER_PICKER_CLASS,
block: true,
});

/**
* Conflict gutter marker DOM Element ⚠
*/
const conflictMarker = new (class extends GutterMarker {
toDOM() {
let conflictMarker = elt('div', CONFLICT_MARKER);
conflictMarker.className = GUTTER_CONFLICT_CLASS;
return conflictMarker;
}
})();
const conflictMarker = new MergeMarker({
symbol: CONFLICT_MARKER,
className: GUTTER_CONFLICT_CLASS,
block: false,
});
const conflictBlockMarker = new MergeMarker({
symbol: CONFLICT_MARKER,
className: GUTTER_CONFLICT_CLASS,
block: true,
});

/**
* Effect for adding a mapping between a line and a chunk
Expand Down Expand Up @@ -598,22 +633,23 @@
private createGutterEffects(
editor: EditorView,
chunk: Chunk,
pos: number,
on: true,
type: string,
from: number,
type: 'picker' | 'conflict',
block: boolean = false,
) {
let effects: StateEffect<unknown>[] = [];

let gutterEffect = addGutterMarkerEffect.of({
pos: pos,
on: on,
from: from,
on: true,
type: type,
block: block,
});
effects.push(gutterEffect);

effects.push(
addLineChunkMappingEffect.of({
line: offsetToPos(editor.state.doc, pos).line,
line: offsetToPos(editor.state.doc, from).line,
chunk: chunk,
type: type,
}),
Expand Down Expand Up @@ -676,13 +712,7 @@
if (!decorationKey.includes('Merge')) {
// For all editors except merge editor, add a picker button
effects = effects.concat(
this.createGutterEffects(
editor,
chunk,
startingOffset,
true,
'picker',
),
this.createGutterEffects(editor, chunk, startingOffset, 'picker'),
);
} else if (editor === this._baseEditorWidget.cm) {
for (let s of chunk.sources) {
Expand All @@ -691,13 +721,12 @@
!hasEntries(s.decision.localDiff) &&
!hasEntries(s.decision.remoteDiff)
) {
// We have a custom decision, add picker on base only!*/
// We have a custom decision, add picker on base only!
effects = effects.concat(
this.createGutterEffects(
editor,
chunk,
startingOffset,
true,
'picker',
),
);
Expand All @@ -709,7 +738,6 @@
editor,
chunk,
startingOffset,
true,
'conflict',
),
);
Expand All @@ -727,6 +755,7 @@
}
}
if (chunkFirstLine === chunkLastLine) {
// When the chunk is empty, make sure a horizontal line shows up
const startingOffset = posToOffset(editor.state.doc, {
line: chunkFirstLine,
column: 0,
Expand All @@ -744,19 +773,20 @@
this.createGutterEffects(
editor,
chunk,
chunkLastLine,
true,
startingOffset,
'picker',
true,
),
);
} else if (conflict) {
// Add conflict markers on editor, if conflicted
effects = effects.concat(
this.createGutterEffects(
editor,
chunk,
startingOffset,
'conflict',
true,
'picker',
),
);
}
Expand Down Expand Up @@ -1124,7 +1154,32 @@
gutterMarkerField,
gutter({
class: 'cm-gutter',
markers: editor => editor.state.field(gutterMarkerField),
markers: view => {
return view.state.field(gutterMarkerField).update({
filter: (_from, _to, value: MergeMarker) => !value.isBlock,

Check warning on line 1159 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1159

Added line #L1159 was not covered by tests
});
},
widgetMarker: (
view: EditorView,
widget: WidgetType,
block: BlockInfo,
): GutterMarker | null => {

Check warning on line 1166 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1166

Added line #L1166 was not covered by tests
if (!(widget instanceof PaddingWidget)) {
return null;

Check warning on line 1168 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1168

Added line #L1168 was not covered by tests
}
const markers = view.state.field(gutterMarkerField).update({

Check warning on line 1170 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1170

Added line #L1170 was not covered by tests
filter: (from, _to, value: MergeMarker) =>
value.isBlock && block.from === from,
});
if (markers.size > 1) {
throw Error('More than one block gutter widget matched');

Check warning on line 1175 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1175

Added line #L1175 was not covered by tests
}
if (markers.size === 1) {
const cursor = markers.iter();
return cursor.value;

Check warning on line 1179 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1178-L1179

Added lines #L1178 - L1179 were not covered by tests
}
return null;

Check warning on line 1181 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1181

Added line #L1181 was not covered by tests
},
initialSpacer: () => pickerMarker,
domEventHandlers: {
mousedown: (editor, line) => {
Expand Down Expand Up @@ -1455,9 +1510,10 @@
}
effects.push(
addGutterMarkerEffect.of({
pos: line.from,
from: line.from,
on: false,
type: 'picker',
block: false,
}),
);
} else {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading