-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial attempt * Initial working version * Add tests * Add another test Co-authored-by: Andreas Arvidsson <andreas.arvidsson87@gmail.com>
- Loading branch information
1 parent
d855232
commit b6b22f8
Showing
13 changed files
with
322 additions
and
39 deletions.
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 | ||
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.