Repository navigation
数字の区切り記号と中黒にピリオド・スラッシュ候補を追加する #367
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
Closed
sinoda1114
wants to merge
2
commits into
azooKey:main
from
sinoda1114:codex/numeric-separator-candidates
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Core/Sources/Core/InputUtils/NumericSeparatorCandidates.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| enum NumericSeparatorCandidates { | ||
| /// 数字の区切りと単独の中黒に、記号の別表記を追加する。 | ||
| static func variants(for reading: String) -> [String] { | ||
| if reading == "・" { | ||
| return ["/", "/"] | ||
| } | ||
| let separator: Character | ||
| let replacement: String | ||
| if reading.contains("。") { | ||
| separator = "。" | ||
| replacement = "." | ||
| } else if reading.contains("・") { | ||
| separator = "・" | ||
| replacement = "/" | ||
| } else { | ||
| return [] | ||
| } | ||
| let groups = reading.split(separator: separator, omittingEmptySubsequences: false) | ||
| var normalized: [String] = [] | ||
| for group in groups { | ||
| guard !group.isEmpty else { | ||
| return [] | ||
| } | ||
| var digits = "" | ||
| for scalar in group.unicodeScalars { | ||
| switch scalar.value { | ||
| case 0x30...0x39: | ||
| digits.unicodeScalars.append(scalar) | ||
| case 0xFF10...0xFF19: | ||
| digits.unicodeScalars.append(UnicodeScalar(scalar.value - 0xFEE0)!) | ||
| default: | ||
| return [] | ||
| } | ||
| } | ||
| normalized.append(digits) | ||
| } | ||
| return [normalized.joined(separator: replacement)] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Core/Tests/CoreTests/InputUtilsTests/NumericSeparatorCandidatesTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| @testable import Core | ||
| import Foundation | ||
| import KanaKanjiConverterModuleWithDefaultDictionary | ||
| import Testing | ||
|
|
||
| @MainActor | ||
| struct NumericSeparatorCandidatesTests { | ||
| @Test func numericGroupsAndStandaloneMiddleDot() { | ||
| #expect(NumericSeparatorCandidates.variants(for: "5。1") == ["5.1"]) | ||
| #expect(NumericSeparatorCandidates.variants(for: "5。1") == ["5.1"]) | ||
| #expect(NumericSeparatorCandidates.variants(for: "10・10") == ["10/10"]) | ||
| #expect(NumericSeparatorCandidates.variants(for: "2026・9・05") == ["2026/9/05"]) | ||
| #expect(NumericSeparatorCandidates.variants(for: "1。2。3") == ["1.2.3"]) | ||
| #expect(NumericSeparatorCandidates.variants(for: "・") == ["/", "/"]) | ||
| for input in ["", "。", "5。", "。1", "10・・10", "1。2・3", "文章。", "名前・名前", "10/10", "5.1", "10", "5 。1"] { | ||
| #expect(NumericSeparatorCandidates.variants(for: input).isEmpty) | ||
| } | ||
| } | ||
|
|
||
| @Test(arguments: [("5。1", "5.1"), ("10・10", "10/10"), ("・", "/"), ("・", "/"), ("5。1", "5.1")]) | ||
| func candidatesPreserveInputAndCommitWholeValue(input: String, expected: String) throws { | ||
| let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) | ||
| try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: directory) } | ||
| let manager = SegmentsManager(kanaKanjiConverter: .withDefaultDictionary(), applicationDirectoryURL: directory, | ||
| containerURL: nil, context: .init(useZenzai: false)) | ||
| manager.insertAtCursorPosition(input, inputStyle: .direct) | ||
| for rich in [false, true] { | ||
| manager.update(requestRichCandidates: rich) | ||
| manager.requestSetCandidateWindowState(visible: true) | ||
| guard case .selecting(let choices, _) = manager.getCurrentCandidateWindow(inputState: .selecting) else { | ||
| Issue.record("Expected conversion candidates") | ||
| return | ||
| } | ||
| #expect(manager.convertTarget == input) | ||
| #expect(choices.contains { $0.text == input }) | ||
| #expect(choices.filter { $0.text == expected }.count == 1) | ||
| if rich { | ||
| let row = try #require(choices.firstIndex { $0.text == expected }) | ||
| manager.requestSelectingRow(row) | ||
| let candidate = try #require(manager.selectedCandidate) | ||
| manager.prefixCandidateCommited(candidate, leftSideContext: "") | ||
| #expect(manager.isEmpty) | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
After the user adjusts the conversion segment (for example, shrinking
10・10to a prefix with Control-I),convertTargetstill contains the entire composition while the converter'smainResultsrepresent the edited prefix. This code nevertheless creates10/10from the whole target and gives it an input count covering the whole composition, so selecting it ignores the requested segment boundary and commits all remaining text. Derive the variant and composing count from the active conversion range, or suppress these synthetic candidates while segment editing is active.Useful? React with 👍 / 👎.