-
-
Notifications
You must be signed in to change notification settings - Fork 79
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
Improved update selections #306
Changes from 19 commits
ea84754
82620c0
e5661eb
1f64fd6
f90f0da
12c71b5
3b39f22
8a28109
e73911a
3af3419
e25f7c9
d3e12da
758af7e
a32e74b
fd48593
244ce51
842d51e
ab7d5d4
99c6a04
fd837d8
e925391
c8b2404
e5ad5c5
811023f
83abf91
538e993
02f4f1d
0c2ed1b
c5f01ff
240d99e
07803b1
dcf9521
8499d82
0bb155d
1dd8f3c
33f6c4f
e1f94dc
e9e90d0
96854ec
12d8874
74ce565
2fd2e2d
e057c59
801a994
c1b4077
c60ff42
2f591fc
46b787a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import { Selection } from "vscode"; | ||
import { DecorationRangeBehavior, Selection } from "vscode"; | ||
import { flatten } from "lodash"; | ||
import { | ||
Action, | ||
ActionPreferences, | ||
ActionReturnValue, | ||
Edit, | ||
Graph, | ||
SelectionWithEditor, | ||
TypedSelection, | ||
} from "../typings/Types"; | ||
import { runOnTargetsForEachEditor } from "../util/targetUtils"; | ||
import { decorationSleep } from "../util/editDisplayUtils"; | ||
import { performEditsAndUpdateSelections } from "../util/updateSelections"; | ||
import { selectionWithEditorFromPositions } from "../util/selectionUtils"; | ||
import { FullSelectionInfo } from "../typings/updateSelections"; | ||
import { | ||
getSelectionInfo, | ||
performEditsAndUpdateFullSelectionInfos, | ||
} from "../core/updateSelections/updateSelections"; | ||
|
||
export default class Wrap implements Action { | ||
getTargetPreferences: () => ActionPreferences[] = () => [ | ||
|
@@ -31,62 +35,88 @@ export default class Wrap implements Action { | |
await runOnTargetsForEachEditor<SelectionWithEditor[]>( | ||
targets, | ||
async (editor, targets) => { | ||
const edits = targets.flatMap((target) => [ | ||
const { document } = editor; | ||
const boundaries = targets.map((target) => ({ | ||
start: new Selection( | ||
target.selection.selection.start, | ||
target.selection.selection.start | ||
), | ||
end: new Selection( | ||
target.selection.selection.end, | ||
target.selection.selection.end | ||
), | ||
})); | ||
|
||
const edits: Edit[] = boundaries.flatMap(({ start, end }) => [ | ||
{ | ||
text: left, | ||
range: new Selection( | ||
target.selection.selection.start, | ||
target.selection.selection.start | ||
), | ||
range: start, | ||
}, | ||
{ | ||
text: right, | ||
dontMoveOnEqualStart: true, | ||
range: new Selection( | ||
target.selection.selection.end, | ||
target.selection.selection.end | ||
), | ||
range: end, | ||
isReplace: true, | ||
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. We use |
||
}, | ||
]); | ||
|
||
const [updatedOriginalSelections, updatedTargetsSelections] = | ||
await performEditsAndUpdateSelections(editor, edits, [ | ||
editor.selections, | ||
targets.map((target) => target.selection.selection), | ||
]); | ||
const delimiterSelectionInfos: FullSelectionInfo[] = | ||
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. I think this file turned out really nicely. Notice how we now just give a few ranges to the selection updater and it takes care of all the complex logic:
|
||
boundaries.flatMap(({ start, end }) => { | ||
return [ | ||
getSelectionInfo( | ||
document, | ||
start, | ||
DecorationRangeBehavior.OpenClosed | ||
), | ||
getSelectionInfo( | ||
document, | ||
end, | ||
DecorationRangeBehavior.ClosedOpen | ||
), | ||
]; | ||
}); | ||
|
||
editor.selections = updatedOriginalSelections; | ||
const cursorSelectionInfos = editor.selections.map((selection) => | ||
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.
|
||
getSelectionInfo( | ||
document, | ||
selection, | ||
DecorationRangeBehavior.ClosedClosed | ||
) | ||
); | ||
|
||
const updatedSelections = updatedTargetsSelections.flatMap( | ||
({ start, end }) => [ | ||
new Selection( | ||
start.translate({ characterDelta: -left.length }), | ||
start | ||
), | ||
new Selection( | ||
end, | ||
end.translate({ characterDelta: right.length }) | ||
), | ||
] | ||
const thatMarkSelectionInfos = targets.map( | ||
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. For the |
||
({ selection: { selection } }) => | ||
getSelectionInfo( | ||
document, | ||
selection, | ||
DecorationRangeBehavior.OpenOpen | ||
) | ||
); | ||
|
||
const [delimiterSelections, cursorSelections, thatMarkSelections] = | ||
await performEditsAndUpdateFullSelectionInfos( | ||
this.graph.selectionUpdater, | ||
editor, | ||
edits, | ||
[ | ||
delimiterSelectionInfos, | ||
cursorSelectionInfos, | ||
thatMarkSelectionInfos, | ||
] | ||
); | ||
|
||
editor.selections = cursorSelections; | ||
|
||
editor.setDecorations( | ||
this.graph.editStyles.justAdded.token, | ||
updatedSelections | ||
delimiterSelections | ||
); | ||
await decorationSleep(); | ||
|
||
editor.setDecorations(this.graph.editStyles.justAdded.token, []); | ||
|
||
return targets.map((target, index) => { | ||
const start = updatedSelections[index * 2].start; | ||
const end = updatedSelections[index * 2 + 1].end; | ||
return selectionWithEditorFromPositions( | ||
target.selection, | ||
start, | ||
end | ||
); | ||
}); | ||
return thatMarkSelections.map((selection) => ({ | ||
editor, | ||
selection, | ||
})); | ||
} | ||
) | ||
); | ||
|
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.
Just specify here that the target range is open on both ends and it will then automatically expand to include inserted text