-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add rewrap action #365
Merged
Merged
Add rewrap action #365
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,65 @@ | ||
import { TextEditor } from "vscode"; | ||
import { | ||
Action, | ||
ActionPreferences, | ||
ActionReturnValue, | ||
Graph, | ||
SelectionWithContext, | ||
TypedSelection, | ||
} from "../typings/Types"; | ||
import { repeat } from "../util/array"; | ||
|
||
export default class Rewrap implements Action { | ||
getTargetPreferences: () => ActionPreferences[] = () => [ | ||
{ | ||
insideOutsideType: "inside", | ||
modifier: { | ||
type: "surroundingPair", | ||
delimiter: "any", | ||
delimiterInclusion: undefined, | ||
}, | ||
}, | ||
]; | ||
|
||
constructor(private graph: Graph) { | ||
this.run = this.run.bind(this); | ||
} | ||
|
||
run( | ||
[targets]: [TypedSelection[]], | ||
left: string, | ||
right: string | ||
): Promise<ActionReturnValue> { | ||
const boundaries: TypedSelection[] = targets.flatMap((target) => { | ||
const boundary = target.selectionContext.boundary; | ||
|
||
if (boundary == null || boundary.length !== 2) { | ||
throw Error("Target must have an opening and closing delimiter"); | ||
} | ||
|
||
return boundary.map((edge) => | ||
constructSimpleTypedSelection(target.selection.editor, edge) | ||
); | ||
}); | ||
|
||
const replacementTexts = repeat([left, right], targets.length); | ||
|
||
return this.graph.actions.replace.run([boundaries], replacementTexts); | ||
} | ||
} | ||
|
||
function constructSimpleTypedSelection( | ||
editor: TextEditor, | ||
selection: SelectionWithContext | ||
): TypedSelection { | ||
return { | ||
selection: { | ||
selection: selection.selection, | ||
editor, | ||
}, | ||
selectionType: "token", | ||
selectionContext: selection.context, | ||
insideOutsideType: null, | ||
position: "contents", | ||
}; | ||
} |
This file contains 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 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 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
38 changes: 38 additions & 0 deletions
38
src/test/suite/fixtures/recorded/actions/curlyRepackRound.yml
This file contains 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,38 @@ | ||
languageId: plaintext | ||
command: | ||
version: 1 | ||
spokenForm: curly repack round | ||
action: rewrapWithPairedDelimiter | ||
targets: | ||
- type: primitive | ||
modifier: {type: surroundingPair, delimiter: parentheses} | ||
extraArgs: ['{', '}'] | ||
initialState: | ||
documentContents: |- | ||
([hello]) | ||
(there) | ||
selections: | ||
- anchor: {line: 0, character: 5} | ||
active: {line: 0, character: 5} | ||
- anchor: {line: 1, character: 5} | ||
active: {line: 1, character: 5} | ||
marks: {} | ||
finalState: | ||
documentContents: |- | ||
{[hello]} | ||
{there} | ||
selections: | ||
- anchor: {line: 0, character: 5} | ||
active: {line: 0, character: 5} | ||
- anchor: {line: 1, character: 5} | ||
active: {line: 1, character: 5} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 1} | ||
- anchor: {line: 0, character: 8} | ||
active: {line: 0, character: 9} | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 1} | ||
- anchor: {line: 1, character: 6} | ||
active: {line: 1, character: 7} | ||
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: parentheses}}] |
31 changes: 31 additions & 0 deletions
31
src/test/suite/fixtures/recorded/actions/squareRepackHarp.yml
This file contains 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,31 @@ | ||
languageId: plaintext | ||
command: | ||
version: 1 | ||
spokenForm: square repack harp | ||
action: rewrapWithPairedDelimiter | ||
targets: | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: h} | ||
extraArgs: ['[', ']'] | ||
initialState: | ||
documentContents: | | ||
(hello) | ||
selections: | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 0} | ||
marks: | ||
default.h: | ||
start: {line: 0, character: 1} | ||
end: {line: 0, character: 6} | ||
finalState: | ||
documentContents: | | ||
[hello] | ||
selections: | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 0} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 1} | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 7} | ||
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}] |
31 changes: 31 additions & 0 deletions
31
src/test/suite/fixtures/recorded/actions/squareRepackLeper.yml
This file contains 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,31 @@ | ||
languageId: plaintext | ||
command: | ||
version: 1 | ||
spokenForm: square repack leper | ||
action: rewrapWithPairedDelimiter | ||
targets: | ||
- type: primitive | ||
mark: {type: decoratedSymbol, symbolColor: default, character: (} | ||
extraArgs: ['[', ']'] | ||
initialState: | ||
documentContents: | | ||
(hello) | ||
selections: | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 0} | ||
marks: | ||
default.(: | ||
start: {line: 0, character: 0} | ||
end: {line: 0, character: 1} | ||
finalState: | ||
documentContents: | | ||
[hello] | ||
selections: | ||
- anchor: {line: 1, character: 0} | ||
active: {line: 1, character: 0} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 1} | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 7} | ||
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: (}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}] |
28 changes: 28 additions & 0 deletions
28
src/test/suite/fixtures/recorded/actions/squareRepackPair.yml
This file contains 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,28 @@ | ||
languageId: plaintext | ||
command: | ||
version: 1 | ||
spokenForm: square repack pair | ||
action: rewrapWithPairedDelimiter | ||
targets: | ||
- type: primitive | ||
modifier: {type: surroundingPair, delimiter: any} | ||
extraArgs: ['[', ']'] | ||
initialState: | ||
documentContents: | | ||
(hello) | ||
selections: | ||
- anchor: {line: 0, character: 2} | ||
active: {line: 0, character: 2} | ||
marks: {} | ||
finalState: | ||
documentContents: | | ||
[hello] | ||
selections: | ||
- anchor: {line: 0, character: 2} | ||
active: {line: 0, character: 2} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 1} | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 7} | ||
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}] |
26 changes: 26 additions & 0 deletions
26
src/test/suite/fixtures/recorded/actions/squareRepackThis.yml
This file contains 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,26 @@ | ||
languageId: plaintext | ||
command: | ||
version: 1 | ||
spokenForm: square repack this | ||
action: rewrapWithPairedDelimiter | ||
targets: | ||
- type: primitive | ||
mark: {type: cursor} | ||
extraArgs: ['[', ']'] | ||
initialState: | ||
documentContents: (hello) | ||
selections: | ||
- anchor: {line: 0, character: 4} | ||
active: {line: 0, character: 4} | ||
marks: {} | ||
finalState: | ||
documentContents: "[hello]" | ||
selections: | ||
- anchor: {line: 0, character: 4} | ||
active: {line: 0, character: 4} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 1} | ||
- anchor: {line: 0, character: 6} | ||
active: {line: 0, character: 7} | ||
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}] |
29 changes: 29 additions & 0 deletions
29
src/test/suite/fixtures/recorded/surroundingPair/textual/chuckPairHarp.yml
This file contains 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,29 @@ | ||
languageId: plaintext | ||
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. Testing that |
||
command: | ||
version: 1 | ||
spokenForm: chuck pair harp | ||
action: remove | ||
targets: | ||
- type: primitive | ||
modifier: {type: surroundingPair, delimiter: any} | ||
mark: {type: decoratedSymbol, symbolColor: default, character: h} | ||
initialState: | ||
documentContents: | | ||
(hello) (there) | ||
selections: | ||
- anchor: {line: 0, character: 13} | ||
active: {line: 0, character: 13} | ||
marks: | ||
default.h: | ||
start: {line: 0, character: 1} | ||
end: {line: 0, character: 6} | ||
finalState: | ||
documentContents: | | ||
(there) | ||
selections: | ||
- anchor: {line: 0, character: 5} | ||
active: {line: 0, character: 5} | ||
thatMark: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: surroundingPair, delimiter: any}}] |
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.
This way
processToken
won't clobber things it doesn't know about