Skip to content

Commit

Permalink
Fixed selection bug and added source mark to copy lines (#384)
Browse files Browse the repository at this point in the history
* Fix so clone up moves selection

* Added source mark to copy lines

* Added tests for clone linesg

* Updated tests
  • Loading branch information
AndreasArvidsson authored Dec 12, 2021
1 parent 0712ee8 commit 9df55b6
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 82 deletions.
64 changes: 44 additions & 20 deletions src/actions/CopyLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ class CopyLines implements Action {
return ranges.map(({ range, isParagraph }) => {
const delimiter = isParagraph ? "\n\n" : "\n";
let text = editor.document.getText(range);
text = this.isUp ? `${text}${delimiter}` : `${delimiter}${text}`;
const length = text.length;
text = this.isUp ? `${delimiter}${text}` : `${text}${delimiter}`;
const newRange = this.isUp
? new Range(range.start, range.start)
: new Range(range.end, range.end);
? new Range(range.end, range.end)
: new Range(range.start, range.start);
return {
editor,
range: newRange,
text,
edit: {
editor,
range: newRange,
text,
isReplace: this.isUp,
},
offset: delimiter.length,
length,
};
});
}
Expand All @@ -62,27 +68,44 @@ class CopyLines implements Action {
const results = flatten(
await runOnTargetsForEachEditor(targets, async (editor, targets) => {
const ranges = this.getRanges(editor, targets);
const edits = this.getEdits(editor, ranges);
const editWrappers = this.getEdits(editor, ranges);
const rangeSelections = ranges.map(
({ range }) => new Selection(range.start, range.end)
);

const [updatedSelections, copySelections] =
const [editorSelections, copySelections] =
await performEditsAndUpdateSelections(
this.graph.rangeUpdater,
editor,
edits,
[
targets.map((target) => target.selection.selection),
ranges.map(({ range }) => new Selection(range.start, range.end)),
]
editWrappers.map((wrapper) => wrapper.edit),
[editor.selections, rangeSelections]
);

editor.revealRange(updatedSelections[0]);
editor.selections = editorSelections;
editor.revealRange(copySelections[0]);

let sourceSelections;
if (this.isUp) {
sourceSelections = editWrappers.map((wrapper) => {
const startIndex =
editor.document.offsetAt(wrapper.edit.range.start) +
wrapper.offset;
const endIndex = startIndex + wrapper.length;
return new Selection(
editor.document.positionAt(startIndex),
editor.document.positionAt(endIndex)
);
});
} else {
sourceSelections = rangeSelections;
}

return {
thatMark: updatedSelections.map((selection) => ({
sourceMark: sourceSelections.map((selection) => ({
editor,
selection,
})),
copySelections: copySelections.map((selection) => ({
thatMark: copySelections.map((selection) => ({
editor,
selection,
})),
Expand All @@ -91,24 +114,25 @@ class CopyLines implements Action {
);

await displayPendingEditDecorationsForSelection(
results.flatMap((result) => result.copySelections),
results.flatMap((result) => result.thatMark),
this.graph.editStyles.justAdded.token
);

const sourceMark = results.flatMap((result) => result.sourceMark);
const thatMark = results.flatMap((result) => result.thatMark);

return { thatMark };
return { sourceMark, thatMark };
}
}

export class CopyLinesUp extends CopyLines {
constructor(graph: Graph) {
super(graph, false);
super(graph, true);
}
}

export class CopyLinesDown extends CopyLines {
constructor(graph: Graph) {
super(graph, true);
super(graph, false);
}
}
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneHarp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone harp
action: insertCopyAfter
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: h}
initialState:
documentContents: hello
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
marks:
default.h:
start: {line: 0, character: 0}
end: {line: 0, character: 5}
finalState:
documentContents: |-
hello
hello
selections:
- anchor: {line: 1, character: 5}
active: {line: 1, character: 5}
thatMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 5}
sourceMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 5}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneHarp2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone harp
action: insertCopyAfter
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: h}
initialState:
documentContents: hello
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.h:
start: {line: 0, character: 0}
end: {line: 0, character: 5}
finalState:
documentContents: |-
hello
hello
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
thatMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 5}
sourceMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 5}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneUpHarp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone up harp
action: insertCopyBefore
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: h}
initialState:
documentContents: hello
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
marks:
default.h:
start: {line: 0, character: 0}
end: {line: 0, character: 5}
finalState:
documentContents: |-
hello
hello
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 5}
sourceMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 5}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneUpHarp2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone up harp
action: insertCopyBefore
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: h}
initialState:
documentContents: hello
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks:
default.h:
start: {line: 0, character: 0}
end: {line: 0, character: 5}
finalState:
documentContents: |-
hello
hello
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 5}
sourceMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 5}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneUpVest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone up vest
action: insertCopyBefore
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: v}
initialState:
documentContents: " const value = \"Hello world\";"
selections:
- anchor: {line: 0, character: 15}
active: {line: 0, character: 15}
marks:
default.v:
start: {line: 0, character: 10}
end: {line: 0, character: 15}
finalState:
documentContents: |2-
const value = "Hello world";
const value = "Hello world";
selections:
- anchor: {line: 0, character: 15}
active: {line: 0, character: 15}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 32}
sourceMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 32}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: v}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/actions/cloneVest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: plaintext
command:
version: 1
spokenForm: clone vest
action: insertCopyAfter
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: v}
initialState:
documentContents: " const value = \"Hello world\";"
selections:
- anchor: {line: 0, character: 15}
active: {line: 0, character: 15}
marks:
default.v:
start: {line: 0, character: 10}
end: {line: 0, character: 15}
finalState:
documentContents: |2-
const value = "Hello world";
const value = "Hello world";
selections:
- anchor: {line: 1, character: 15}
active: {line: 1, character: 15}
thatMark:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 32}
sourceMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 32}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: v}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: identity}}]
31 changes: 0 additions & 31 deletions src/test/suite/fixtures/recorded/actions/dupeUpVest.yml

This file was deleted.

31 changes: 0 additions & 31 deletions src/test/suite/fixtures/recorded/actions/dupeVest.yml

This file was deleted.

0 comments on commit 9df55b6

Please sign in to comment.