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

fix phantom space after double quotes #842

Merged
merged 3 commits into from
Jun 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1112,32 +1112,32 @@ private void handleSeparatorEvent(final Event event, final InputTransaction inpu
mConnection.commitCodePoint(codePoint);
}
} else {
if ((SpaceState.PHANTOM == inputTransaction.getMSpaceState()
&& settingsValues.isUsuallyFollowedBySpace(codePoint))
|| (Constants.CODE_DOUBLE_QUOTE == codePoint
&& isInsideDoubleQuoteOrAfterDigit)) {
if (SpaceState.PHANTOM == inputTransaction.getMSpaceState()
&& (settingsValues.isUsuallyFollowedBySpace(codePoint) || isInsideDoubleQuoteOrAfterDigit)) {
// If we are in phantom space state, and the user presses a separator, we want to
// stay in phantom space state so that the next keypress has a chance to add the
// space. For example, if I type "Good dat", pick "day" from the suggestion strip
// then insert a comma and go on to typing the next word, I want the space to be
// inserted automatically before the next word, the same way it is when I don't
// input the comma. A double quote behaves like it's usually followed by space if
// we're inside a double quote.
// input the comma. Also when closing a quote the phantom state should be preserved.
// The case is a little different if the separator is a space stripper. Such a
// separator does not normally need a space on the right (that's the difference
// between swappers and strippers), so we should not stay in phantom space state if
// the separator is a stripper. Hence the additional test above.
mSpaceState = SpaceState.PHANTOM;
} else
} else {
// mSpaceState is still SpaceState.NONE, but some characters should typically
// be followed by space. Set phantom space state for such characters if the user
// enabled the setting and was not composing a word. The latter avoids setting
// phantom space state when typing decimal numbers, with the drawback of not
// setting phantom space state after ending a sentence with a non-word.
// A double quote behaves like it's usually followed by space if we're inside
// a double quote.
if (wasComposingWord
&& settingsValues.mAutospaceAfterPunctuationEnabled
&& settingsValues.isUsuallyFollowedBySpace(codePoint)) {
&& (settingsValues.isUsuallyFollowedBySpace(codePoint) || isInsideDoubleQuoteOrAfterDigit)) {
mSpaceState = SpaceState.PHANTOM;
}
}

mConnection.commitCodePoint(codePoint);
Expand Down
42 changes: 42 additions & 0 deletions app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,50 @@ class InputLogicTest {
assertEquals("hello ", text)
}

@Test fun `no weird space inside multi-"`() {
reset()
chainInput("\"\"\"")
assertEquals("\"\"\"", text)

reset()
DeviceProtectedUtils.getSharedPreferences(latinIME).edit { putBoolean(Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION, true) }
chainInput("\"\"\"")
assertEquals("\"\"\"", text)
}

@Test fun `autospace still happens after "`() {
reset()
DeviceProtectedUtils.getSharedPreferences(latinIME).edit { putBoolean(Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION, true) }
chainInput("\"hello\"you")
assertEquals("\"hello\" you", text)
}

@Test fun `autospace still happens after " if next word is in quotes`() {
reset()
DeviceProtectedUtils.getSharedPreferences(latinIME).edit { putBoolean(Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION, true) }
chainInput("\"hello\"\"you\"")
assertEquals("\"hello\" \"you\"", text)
}

@Test fun `autospace propagates over "`() {
reset()
input('"')
pickSuggestion("hello")
assertEquals(spaceState, SpaceState.PHANTOM) // picking a suggestion sets phantom space state
chainInput("\"you")
assertEquals("\"hello\" you", text)
}

@Test fun `autospace still happens after " if nex word is in " and after comma`() {
reset()
DeviceProtectedUtils.getSharedPreferences(latinIME).edit { putBoolean(Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION, true) }
chainInput("\"hello\",\"you\"")
assertEquals("\"hello\", \"you\"", text)
}

@Test fun `autospace in json editor`() {
reset()
DeviceProtectedUtils.getSharedPreferences(latinIME).edit { putBoolean(Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION, true) }
chainInput("{\"label\":\"")
assertEquals("{\"label\": \"", text)
input('c')
Expand Down