Skip to content
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

Merged
merged 48 commits into from
Nov 7, 2021
Merged

Conversation

pokey
Copy link
Member

@pokey pokey commented Oct 31, 2021

This pull request is a complete rework of the way that we update ranges in response to document changes. There is now a rangeUpdater component on the graph with which you can register ranges that you'd like it to keep up-to-date.

Closes #138
Closes #298

  • Add tests

This was referenced Nov 3, 2021
getSelectionInfo(
editor.document,
originalSelection.selection.selection,
DecorationRangeBehavior.OpenOpen
Copy link
Member Author

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

editor.selections,
targets.map((target) => target.selection.selection),
]);
const delimiterSelectionInfos: FullSelectionInfo[] =
Copy link
Member Author

Choose a reason for hiding this comment

The 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:

  • For the delimiters that we'll be highlighting, we give it an empty range on the left, that is open at the left, so that it will expand to include the left delimiter, but not the right delimiter (in the case that we're wrapping an empty range). We give it an empty range on the right that is open on the right for the right delimiter highlight


editor.selections = updatedOriginalSelections;
const cursorSelectionInfos = editor.selections.map((selection) =>
Copy link
Member Author

Choose a reason for hiding this comment

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

  • For the new cursor selection, we give it a closed range so it won't include delimiters

end.translate({ characterDelta: right.length })
),
]
const thatMarkSelectionInfos = targets.map(
Copy link
Member Author

Choose a reason for hiding this comment

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

For the that mark we give it same range as cursor, but make it open so that it will include both delimiters

target.selection.selection.end
),
range: end,
isReplace: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

We use isReplace to signal that it shouldn't push empty selections to the right

Comment on lines +43 to +45
default.w:
start: {line: 0, character: 0}
end: {line: 0, character: 0}
Copy link
Member Author

Choose a reason for hiding this comment

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

Notice how clobbering the range just leaves the mark at the beginning of the deleted range

Comment on lines +41 to +43
default.w:
start: {line: 0, character: 2}
end: {line: 0, character: 5}
Copy link
Member Author

Choose a reason for hiding this comment

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

This behaviour is what we agreed upon in discussion: although technically the whale token has joined with the harp token, we preserve only the part of whale that remains; we don't join it with harp

Comment on lines +45 to +47
default.w:
start: {line: 0, character: 3}
end: {line: 0, character: 8}
Copy link
Member Author

Choose a reason for hiding this comment

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

This test is kinda bananas, but basically we've deleted first 2 characters of whale, then added stuff with a space in it to the end of whale. We preserve those last 3 chars of whale and pick up the test that was added until the space on the end! 😅

@@ -0,0 +1,35 @@
spokenForm: bring fine
Copy link
Member Author

Choose a reason for hiding this comment

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

The tests in this directory are all selection based, because we now control cursor in response to a bring. For example, in this one we check that the selection is moved to the right

Comment on lines +23 to +25
selections:
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this behaviour is kinda cool. If we say "bring air after this", then the cursor doesn't move the way it usually does when you say "bring air"

@pokey pokey marked this pull request as ready for review November 4, 2021 18:33
@AndreasArvidsson AndreasArvidsson merged commit 6543066 into main Nov 7, 2021
@AndreasArvidsson AndreasArvidsson deleted the improved-update-selections branch November 7, 2021 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants