Skip to content

Add a SelectAndDelete Token deletion mode #243

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ All tokens will turn into the partial completion text they replaced

Tokens will be replaced with the toString value of the objects they represent when they are deleted

#### TokenCompleteTextView.TokenDeleteStyle.SelectThenDelete

This mode will first select the token and when pressing the 'delete' key again it'll delete the selected token

Restoring the view state
========================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public enum TokenDeleteStyle {
_Parent, //...do the parent behavior, not recommended
Clear, //...clear the underlying text
PartialCompletion, //...return the original text used for completion
ToString //...replace the token with toString of the token object
ToString, //...replace the token with toString of the token object
SelectThenDelete //...first backspace selects the token and another backspace deletes it
}

//When the user clicks on a token...
Expand Down Expand Up @@ -835,6 +836,7 @@ protected CharSequence convertSelectionToString(Object object) {
//if the token gets deleted, this text will get put in the field instead
switch (deletionStyle) {
case Clear:
case SelectThenDelete:
return "";
case PartialCompletion:
return currentCompletionText();
Expand Down Expand Up @@ -1517,7 +1519,23 @@ public boolean canDeleteSelection(int beforeLength) {
int endTokenSelection = text.getSpanEnd(span);

// moving on, no need to check this token
if (isTokenRemovable(span.token)) continue;
if (isTokenRemovable(span.token)) {
if (deletionStyle == TokenDeleteStyle.SelectThenDelete) {
if (span.view.isSelected()) {
// If a token is already selected then we can just delete it
return true;
}
// If we're not selecting multiple characters and we want to remove the current token
if (startSelection == endSelection && endTokenSelection + 1 == endSelection && !span.view.isSelected()) {
// Just select it and don't delete it, the next backspace will delete it
clearSelections();
span.view.setSelected(true);
invalidate();
return false;
}
}
continue;
}

if (startSelection == endSelection) {
// Delete single
Expand Down Expand Up @@ -1552,10 +1570,8 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
//Shouldn't be able to delete prefix, so don't do anything
if (getSelectionStart() <= prefix.length()) {
beforeLength = 0;
return deleteSelectedObject(false) || super.deleteSurroundingText(beforeLength, afterLength);
}

return super.deleteSurroundingText(beforeLength, afterLength);
return deleteSelectedObject(false) || super.deleteSurroundingText(beforeLength, afterLength);
}
}
}