Skip to content

New interface for performing edits and updating ranges #2551

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 12 commits into from
Jul 24, 2024

Conversation

AndreasArvidsson
Copy link
Member

@AndreasArvidsson AndreasArvidsson commented Jul 23, 2024

This introduces a new interface to perform updates. The requirements I had was:

  1. Support both selections and ranges
  2. Support both edits and callback
  3. Support setting range expansion behavior per range/selection
  4. Support updating selection internally

What this pr doesn't try to do:

  1. Improve the range updater logic

Example

const {
  selections: [updatedContentSelections],
  ranges: [updatedEditRanges]
} = await new EditsUpdater(rangeUpdater, editableEditor, edits)
  .selections(contentSelections)
  .ranges(editRanges, RangeExpansionBehavior.openOpen)
  .updateEditorSelections()
  .run();

Ponderings

  1. Should updating editor selections be opt out instead?
  2. Here I'm using two separate classes EditsUpdater and CallbackUpdater. I'm a bit tempted to just use a single class and a polymorphic argument. editsOrCallback: Edit[] | () => Promise<void>. Something like that.
  3. Strict necessary we don't actually need the two separate return lists. Internally the range updater works with selections so we converting ranges to selections internally. We could just return a single list of selections and since all selections are also ranges I don't think any of the actions would actually care?

If we embrace this polymorphic nature it could look something like this:

const [updatedEditSelections, updatedContentSelections] =
  await new EditsUpdater(rangeUpdater, editableEditor, edits)
    .add(contentSelections)
    .add(editRanges, RangeExpansionBehavior.openOpen)
    .updateEditorSelections()
    .run();

Personally I am a fan of the above since it became cleaner and simpler, but I know that @pokey generally doesn't prefer union arguments if it can be avoided.

We could of course get rid of new if we wanted to make it even cleaner.

const [updatedEditSelections, updatedContentSelections] =
  await performUpdates(rangeUpdater, editableEditor, edits)
    .add(contentSelections)
    .add(editRanges, RangeExpansionBehavior.openOpen)
    .updateEditorSelections()
    .run();

Related to #729

Checklist

  • [/] I have added tests
  • [/] I have updated the docs and cheatsheet
  • [/] I have not broken the cheatsheet

@AndreasArvidsson AndreasArvidsson requested a review from pokey as a code owner July 23, 2024 01:32
@AndreasArvidsson AndreasArvidsson changed the title Edits updater New interface for performing edits and updating ranges Jul 23, 2024
@pokey
Copy link
Member

pokey commented Jul 23, 2024

Proposed new interface:

const {
  sourceEditRanges: updatedSourceEditRanges,
  destinationEditRanges: updatedDestinationEditRanges,
} = await performEditsAndUpdateSelections({
  rangeUpdator,
  editor: editableEditor,
  edits: filteredEdits.map(({ edit }) => edit),
  // callback: async () => {...do whatever},
  selections: {
    sourceEditRanges,
    destinationEditRanges: {
      selections: destinationEditRanges,
      behavior: RangeExpansionBehavior.openOpen,
    },
  },
  // preserveEditorSelections: true,
});

In the future we can do something fancier eg put it on the ide or on the editableEditor or only ever have editableEditor as something that gets passed into the callback function, etc

Note that the values in the map passed to selections can be Range | Selection | {selections: Range | Selection, behavior: RangeExpansionBehavior}

@AndreasArvidsson
Copy link
Member Author

@pokey Updated according to the new interface now

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bunch of very minor nits. Feel free to merge once you've addressed the ones that seem important enough to address 😅

Happy with how the interface turned out tho; nice to rip out all those extra functions

Comment on lines +30 to +38
const { contentRanges: updatedRanges } =
await performEditsAndUpdateSelections({
rangeUpdater: this.rangeUpdater,
editor: editableEditor,
edits,
selections: {
contentRanges,
},
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempting to have shorthand

Suggested change
const { contentRanges: updatedRanges } =
await performEditsAndUpdateSelections({
rangeUpdater: this.rangeUpdater,
editor: editableEditor,
edits,
selections: {
contentRanges,
},
});
const updatedRanges =
await performEditsAndUpdateSelections({
rangeUpdater: this.rangeUpdater,
editor: editableEditor,
edits,
selections: contentRanges,
});

but that might be getting a bit too overloaded 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that is to much

@AndreasArvidsson AndreasArvidsson added this pull request to the merge queue Jul 24, 2024
Merged via the queue into main with commit 17a8f27 Jul 24, 2024
15 checks passed
@AndreasArvidsson AndreasArvidsson deleted the editsUpdater branch July 24, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants