-
-
Notifications
You must be signed in to change notification settings - Fork 84
Support delimiter insertion for "paste after" #771
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { commands, DecorationRangeBehavior, window } from "vscode"; | ||
import { | ||
callFunctionAndUpdateSelections, | ||
callFunctionAndUpdateSelectionsWithBehavior, | ||
} from "../core/updateSelections/updateSelections"; | ||
import { Target } from "../typings/target.types"; | ||
import { Graph } from "../typings/Types"; | ||
import { | ||
focusEditor, | ||
setSelectionsWithoutFocusingEditor, | ||
} from "../util/setSelectionsAndFocusEditor"; | ||
import { ensureSingleEditor } from "../util/targetUtils"; | ||
import { ActionReturnValue } from "./actions.types"; | ||
|
||
export class Paste { | ||
constructor(private graph: Graph) {} | ||
|
||
async run([targets]: [Target[]]): Promise<ActionReturnValue> { | ||
const targetEditor = ensureSingleEditor(targets); | ||
const originalEditor = window.activeTextEditor; | ||
|
||
// First call editNew in order to insert delimiters if necessary and leave | ||
// the cursor in the right position. Note that this action will focus the | ||
// editor containing the targets | ||
const [originalCursorSelections] = await callFunctionAndUpdateSelections( | ||
this.graph.rangeUpdater, | ||
async () => { | ||
await this.graph.actions.editNew.run([targets]); | ||
}, | ||
targetEditor.document, | ||
[targetEditor.selections] | ||
); | ||
|
||
// Then use VSCode paste command, using open ranges at the place where we | ||
// paste in order to capture the pasted text for highlights and `that` mark | ||
const [updatedCursorSelections, updatedTargetSelections] = | ||
await callFunctionAndUpdateSelectionsWithBehavior( | ||
this.graph.rangeUpdater, | ||
() => commands.executeCommand("editor.action.clipboardPasteAction"), | ||
targetEditor.document, | ||
[ | ||
{ | ||
selections: originalCursorSelections, | ||
}, | ||
{ | ||
selections: targetEditor.selections, | ||
rangeBehavior: DecorationRangeBehavior.OpenOpen, | ||
}, | ||
] | ||
); | ||
|
||
// Reset cursors on the editor where the edits took place. | ||
// NB: We don't focus the editor here because we want to focus the original | ||
// editor, not the one where the edits took place | ||
setSelectionsWithoutFocusingEditor(targetEditor, updatedCursorSelections); | ||
|
||
// If necessary focus back original editor | ||
if (originalEditor != null && originalEditor !== window.activeTextEditor) { | ||
// NB: We just do one editor focus at the end, instead of using | ||
// setSelectionsAndFocusEditor because the command might operate on | ||
// multiple editors, so we just do one focus at the end. | ||
await focusEditor(originalEditor); | ||
} | ||
|
||
this.graph.editStyles.displayPendingEditDecorationsForRanges( | ||
updatedTargetSelections.map((selection) => ({ | ||
editor: targetEditor, | ||
range: selection, | ||
})), | ||
this.graph.editStyles.justAdded, | ||
true | ||
); | ||
|
||
return { | ||
thatMark: updatedTargetSelections.map((selection) => ({ | ||
editor: targetEditor, | ||
selection, | ||
})), | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/suite/fixtures/recorded/actions/pasteAfterArgueBat.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
languageId: typescript | ||
command: | ||
spokenForm: paste after argue bat | ||
version: 2 | ||
targets: | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: b} | ||
modifiers: | ||
- {type: position, position: after} | ||
- type: containingScope | ||
scopeType: {type: argumentOrParameter} | ||
usePrePhraseSnapshot: true | ||
action: {name: pasteFromClipboard} | ||
initialState: | ||
documentContents: foo(bar, baz, bongo) | ||
selections: | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 6} | ||
marks: | ||
default.b: | ||
start: {line: 0, character: 14} | ||
end: {line: 0, character: 19} | ||
clipboard: baz | ||
finalState: | ||
documentContents: foo(bar, baz, bongo, baz) | ||
selections: | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 6} | ||
thatMark: | ||
- anchor: {line: 0, character: 21} | ||
active: {line: 0, character: 24} | ||
decorations: | ||
- name: justAddedBackground | ||
type: token | ||
start: {line: 0, character: 21} | ||
end: {line: 0, character: 24} | ||
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: b}, modifiers: [{type: position, position: after}, {type: containingScope, scopeType: {type: argumentOrParameter}}]}] |
82 changes: 82 additions & 0 deletions
82
.../suite/fixtures/recorded/actions/pasteAfterLineSpunAndAfterBlockLookAndBeforeLineSpun.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
languageId: plaintext | ||
command: | ||
spokenForm: paste after line spun and after block look and before line spun | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shockingly, Cursorless handled this one with no problem, inserting the right delimiters and leaving cursors in the right spot 😯 |
||
version: 2 | ||
targets: | ||
- type: list | ||
elements: | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: s} | ||
modifiers: | ||
- {type: position, position: after} | ||
- type: containingScope | ||
scopeType: {type: line} | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: l} | ||
modifiers: | ||
- {type: position, position: after} | ||
- type: containingScope | ||
scopeType: {type: paragraph} | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: s} | ||
modifiers: | ||
- {type: position, position: before} | ||
- type: containingScope | ||
scopeType: {type: line} | ||
usePrePhraseSnapshot: true | ||
action: {name: pasteFromClipboard} | ||
initialState: | ||
documentContents: |- | ||
testing | ||
|
||
hello | ||
there | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
- anchor: {line: 3, character: 5} | ||
active: {line: 3, character: 5} | ||
marks: | ||
default.s: | ||
start: {line: 0, character: 0} | ||
end: {line: 0, character: 7} | ||
default.l: | ||
start: {line: 2, character: 0} | ||
end: {line: 2, character: 5} | ||
clipboard: baz | ||
finalState: | ||
documentContents: |- | ||
baz | ||
testing | ||
baz | ||
|
||
hello | ||
there | ||
|
||
baz | ||
selections: | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 0} | ||
- anchor: {line: 5, character: 5} | ||
active: {line: 5, character: 5} | ||
thatMark: | ||
- anchor: {line: 2, character: 0} | ||
active: {line: 2, character: 3} | ||
- anchor: {line: 7, character: 0} | ||
active: {line: 7, character: 3} | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 3} | ||
decorations: | ||
- name: justAddedBackground | ||
type: token | ||
start: {line: 2, character: 0} | ||
end: {line: 2, character: 3} | ||
- name: justAddedBackground | ||
type: token | ||
start: {line: 7, character: 0} | ||
end: {line: 7, character: 3} | ||
- name: justAddedBackground | ||
type: token | ||
start: {line: 0, character: 0} | ||
end: {line: 0, character: 3} | ||
fullTargets: [{type: list, elements: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, modifiers: [{type: position, position: after}, {type: containingScope, scopeType: {type: line}}]}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: l}, modifiers: [{type: position, position: after}, {type: containingScope, scopeType: {type: paragraph}}]}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, modifiers: [{type: position, position: before}, {type: containingScope, scopeType: {type: line}}]}]}] |
49 changes: 49 additions & 0 deletions
49
src/test/suite/fixtures/recorded/actions/pasteAfterLineTrapAndAfterBlockTrap.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
languageId: python | ||
command: | ||
spokenForm: paste after line trap and after block trap | ||
version: 2 | ||
targets: | ||
- type: list | ||
elements: | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: t} | ||
modifiers: | ||
- {type: position, position: after} | ||
- type: containingScope | ||
scopeType: {type: line} | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: t} | ||
modifiers: | ||
- {type: position, position: after} | ||
- type: containingScope | ||
scopeType: {type: paragraph} | ||
usePrePhraseSnapshot: true | ||
action: {name: pasteFromClipboard} | ||
initialState: | ||
documentContents: | | ||
if True: | ||
pass | ||
selections: | ||
- anchor: {line: 2, character: 0} | ||
active: {line: 2, character: 0} | ||
marks: | ||
default.t: | ||
start: {line: 0, character: 3} | ||
end: {line: 0, character: 7} | ||
clipboard: print("hello") | ||
finalState: | ||
documentContents: | | ||
if True: | ||
print("hello") | ||
pass | ||
|
||
print("hello") | ||
selections: | ||
- anchor: {line: 5, character: 0} | ||
active: {line: 5, character: 0} | ||
thatMark: | ||
- anchor: {line: 1, character: 4} | ||
active: {line: 1, character: 18} | ||
- anchor: {line: 4, character: 4} | ||
active: {line: 4, character: 18} | ||
fullTargets: [{type: list, elements: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, modifiers: [{type: position, position: after}, {type: containingScope, scopeType: {type: line}}]}, {type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, modifiers: [{type: position, position: after}, {type: containingScope, scopeType: {type: paragraph}}]}]}] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this so that paste and insert snippet can use it; could be useful to map a spoken form at some point 🤷. Kinda like a fancy "change" that will insert delimiters if you give it a positional modifier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then maybe we should just merge this with change and allowed that option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my suggestion at meet-up, but the issue is that highlighting becomes a bit complex, as the targets will need to own a bit more. I'm not opposed to it, but maybe out of scope for this PR? We'll need to think a bit about giving targets more autonomy wrt highlighting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do that in a follow up pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean we already have removal highlight range in the targets so we could just add content highlight range if we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is a fairly clean solution here that would also simplify the highlight graph component, but it's worth pairing on and I think out of scope
If you prefer for this PR I can just have the paste action keep its own
EditNew
object; that was my original solution