feat(typing): add English autocomplete and Chinese candidate expand panel

Offline English lexicon suggestions with autocapitalization, plus a same-height Chinese more-candidates grid over the key area.
This commit is contained in:
Rocky
2026-08-03 18:30:20 +08:00
parent d1e5fed964
commit 38e5ad570d
35 changed files with 5086 additions and 179 deletions
+244 -10
View File
@@ -12,11 +12,14 @@ static RimeApi_stdbool *OSGRimeAPI(void) {
@implementation OSGRimeCandidate
- (instancetype)initWithText:(NSString *)text comment:(NSString *)comment {
- (instancetype)initWithText:(NSString *)text
comment:(NSString *)comment
index:(NSInteger)index {
self = [super init];
if (self) {
_text = [text copy];
_comment = [comment copy];
_index = index;
}
return self;
}
@@ -53,6 +56,178 @@ static RimeApi_stdbool *OSGRimeAPI(void) {
static BOOL sSetupComplete = NO;
static BOOL sInitialized = NO;
/// Longest-prefix match against Mandarin pinyin syllables (no tones).
static NSString *OSGLongestPinyinSyllablePrefix(NSString *input) {
static NSSet<NSString *> *syllables;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Keep longest forms available; matcher tries length 6→1.
syllables = [NSSet setWithArray:@[
@"chuang", @"shuang", @"zhuang",
@"chang", @"cheng", @"chong", @"chuai", @"chuan", @"chuo",
@"hang", @"heng", @"hong", @"huai", @"huan", @"huang", @"huo",
@"jiang", @"jiong", @"kuang", @"liang", @"niang", @"qiang", @"qiong",
@"shang", @"sheng", @"shuai", @"shuan", @"shuo",
@"xiang", @"xiong", @"zhang", @"zheng", @"zhong", @"zhuai", @"zhuan", @"zhuo",
@"bang", @"beng", @"bian", @"biao", @"bing",
@"cang", @"ceng", @"chai", @"chao", @"chen", @"chou", @"chua", @"chui", @"chun", @"cong", @"cuan", @"cui", @"cun", @"cuo",
@"dang", @"deng", @"dian", @"diao", @"ding", @"dong", @"dou", @"duan", @"dui", @"dun", @"duo",
@"fang", @"feng", @"fiao",
@"gang", @"geng", @"gong", @"guai", @"guan", @"guang", @"guo",
@"kang", @"keng", @"kong", @"kuai", @"kuan", @"kuo",
@"lang", @"leng", @"lian", @"liao", @"ling", @"long", @"luan", @"lue", @"lun", @"luo", @"lüe",
@"mang", @"meng", @"mian", @"miao", @"ming", @"miu",
@"nang", @"neng", @"nian", @"niao", @"ning", @"nong", @"nuan", @"nue", @"nun", @"nuo", @"nüe",
@"pang", @"peng", @"pian", @"piao", @"ping",
@"rang", @"reng", @"rong", @"ruan", @"rui", @"run", @"ruo",
@"sang", @"seng", @"shai", @"shan", @"shao", @"shei", @"shen", @"shou", @"shua", @"shui", @"shun", @"song", @"suan", @"sui", @"sun", @"suo",
@"tang", @"teng", @"tian", @"tiao", @"ting", @"tong", @"tou", @"tuan", @"tui", @"tun", @"tuo",
@"wang", @"weng",
@"yang", @"ying", @"yong", @"yuan",
@"zang", @"zeng", @"zhai", @"zhan", @"zhao", @"zhei", @"zhen", @"zhou", @"zhua", @"zhui", @"zhun", @"zong", @"zuan", @"zui", @"zun", @"zuo",
@"ang", @"eng", @"ong",
@"bai", @"ban", @"bao", @"bei", @"ben", @"bie", @"bin", @"bo", @"bu",
@"cai", @"can", @"cao", @"ce", @"cen", @"cha", @"che", @"chi", @"chu", @"ci", @"cong", @"cou", @"cu",
@"dai", @"dan", @"dao", @"de", @"dei", @"den", @"di", @"dia", @"die", @"diu", @"dong", @"dou", @"du",
@"ei", @"en", @"er",
@"fa", @"fan", @"fei", @"fen", @"fo", @"fou", @"fu",
@"gai", @"gan", @"gao", @"ge", @"gei", @"gen", @"gou", @"gu",
@"hai", @"han", @"hao", @"he", @"hei", @"hen", @"hou", @"hu",
@"ji", @"jia", @"jie", @"jin", @"jiu", @"ju", @"juan", @"jue", @"jun",
@"kai", @"kan", @"kao", @"ke", @"ken", @"kou", @"ku",
@"lai", @"lan", @"lao", @"le", @"lei", @"leng", @"li", @"lia", @"lie", @"lin", @"liu", @"long", @"lou", @"lu", @"luan", @"lun", @"lü",
@"mai", @"man", @"mao", @"me", @"mei", @"men", @"mi", @"mie", @"min", @"miu", @"mo", @"mou", @"mu",
@"nai", @"nan", @"nao", @"ne", @"nei", @"nen", @"ni", @"nie", @"nin", @"niu", @"nong", @"nou", @"nu", @"nü",
@"ou",
@"pai", @"pan", @"pao", @"pei", @"pen", @"pi", @"pie", @"pin", @"po", @"pou", @"pu",
@"qi", @"qia", @"qie", @"qin", @"qiu", @"qu", @"quan", @"que", @"qun",
@"ran", @"rao", @"re", @"ren", @"ri", @"rou", @"ru",
@"sai", @"san", @"sao", @"se", @"sen", @"sha", @"she", @"shi", @"shu", @"si", @"sou", @"su",
@"tai", @"tan", @"tao", @"te", @"tei", @"ten", @"ti", @"tie", @"tou", @"tu",
@"wa", @"wai", @"wan", @"wei", @"wen", @"wo", @"wu",
@"xi", @"xia", @"xie", @"xin", @"xiu", @"xu", @"xuan", @"xue", @"xun",
@"ya", @"yan", @"yao", @"ye", @"yi", @"yin", @"yo", @"you", @"yu", @"yue", @"yun",
@"zai", @"zan", @"zao", @"ze", @"zei", @"zen", @"zha", @"zhe", @"zhi", @"zhou", @"zhu", @"zi", @"zou", @"zu",
@"ai", @"an", @"ao",
@"ba", @"bi", @"bu",
@"ca", @"ce", @"ci", @"cu",
@"da", @"de", @"di", @"du",
@"fa", @"fo", @"fu",
@"ga", @"ge", @"gu",
@"ha", @"he", @"hu",
@"ji", @"ju",
@"ka", @"ke", @"ku",
@"la", @"le", @"li", @"lo", @"lu", @"lü",
@"ma", @"me", @"mi", @"mo", @"mu",
@"na", @"ne", @"ni", @"nu", @"nü",
@"pa", @"pi", @"po", @"pu",
@"qi", @"qu",
@"re", @"ri", @"ru",
@"sa", @"se", @"si", @"su",
@"ta", @"te", @"ti", @"tu",
@"wa", @"wo", @"wu",
@"xi", @"xu",
@"ya", @"ye", @"yi", @"yo", @"yu",
@"za", @"ze", @"zi", @"zu",
@"a", @"e", @"o",
]];
});
NSInteger maxLen = MIN((NSInteger)6, (NSInteger)input.length);
for (NSInteger len = maxLen; len >= 1; len--) {
NSString *prefix = [input substringToIndex:(NSUInteger)len];
if ([syllables containsObject:prefix]) {
return prefix;
}
}
return @"";
}
/// First spelling unit: pinyin syllable, or 2-key double-pinyin fallback.
static NSString *OSGFirstSpellingUnit(NSString *rawInput) {
if (rawInput.length == 0) {
return @"";
}
NSString *syllable = OSGLongestPinyinSyllablePrefix(rawInput);
if (syllable.length > 0) {
return syllable;
}
// Microsoft / Sogou double pinyin: typical syllable is two keys.
if (rawInput.length > 2) {
return [rawInput substringToIndex:2];
}
return @"";
}
/// When input continues past the first spelling unit, prefer multi-character
/// phrases first, then single-character first-syllable backups (PC-IME style).
static NSArray<OSGRimeCandidate *> *OSGAssembleDisplayCandidates(
NSArray<OSGRimeCandidate *> *scanned,
NSString *rawInput,
NSInteger limit
) {
if (limit <= 0 || scanned.count == 0) {
return @[];
}
NSString *firstUnit = OSGFirstSpellingUnit(rawInput);
BOOL hasRemainder = firstUnit.length > 0 && rawInput.length > firstUnit.length;
if (!hasRemainder) {
NSInteger count = MIN(limit, (NSInteger)scanned.count);
return [scanned subarrayWithRange:NSMakeRange(0, (NSUInteger)count)];
}
NSMutableArray<OSGRimeCandidate *> *phrases = [NSMutableArray array];
NSMutableArray<OSGRimeCandidate *> *characters = [NSMutableArray array];
for (OSGRimeCandidate *candidate in scanned) {
if (candidate.text.length >= 2) {
[phrases addObject:candidate];
} else if (candidate.text.length == 1) {
[characters addObject:candidate];
}
}
// Reserve roughly a quarter of the pool (min 24) for first-syllable chars.
NSInteger charSlots = MIN((NSInteger)characters.count, MAX((NSInteger)24, limit / 4));
NSInteger phraseSlots = MAX((NSInteger)0, limit - charSlots);
NSMutableArray<OSGRimeCandidate *> *assembled = [NSMutableArray array];
NSMutableSet<NSString *> *seen = [NSMutableSet set];
void (^appendUnique)(OSGRimeCandidate *, NSInteger) =
^(OSGRimeCandidate *candidate, NSInteger cap) {
if ((NSInteger)assembled.count >= cap) {
return;
}
if (candidate.text.length == 0 || [seen containsObject:candidate.text]) {
return;
}
[seen addObject:candidate.text];
[assembled addObject:candidate];
};
for (OSGRimeCandidate *candidate in phrases) {
appendUnique(candidate, phraseSlots);
if ((NSInteger)assembled.count >= phraseSlots) {
break;
}
}
for (OSGRimeCandidate *candidate in characters) {
appendUnique(candidate, limit);
if ((NSInteger)assembled.count >= limit) {
return assembled;
}
}
// Fill any leftover slots with remaining phrases.
for (OSGRimeCandidate *candidate in phrases) {
appendUnique(candidate, limit);
if ((NSInteger)assembled.count >= limit) {
break;
}
}
return assembled;
}
- (instancetype)initWithSharedDataDirectory:(NSString *)sharedDataDirectory
userDataDirectory:(NSString *)userDataDirectory
distributionVersion:(NSString *)distributionVersion {
@@ -224,25 +399,84 @@ static BOOL sInitialized = NO;
NSString *preedit = @"";
BOOL composing = NO;
NSMutableArray<OSGRimeCandidate *> *candidates = [NSMutableArray array];
RIME_STRUCT(RimeContext_stdbool, context);
if (api->get_context(self.session, &context)) {
composing = context.composition.length > 0;
if (context.composition.preedit) {
preedit = [NSString stringWithUTF8String:context.composition.preedit];
}
NSInteger count = MIN((NSInteger)context.menu.num_candidates, MAX((NSInteger)0, limit));
for (NSInteger index = 0; index < count; index++) {
RimeCandidate item = context.menu.candidates[index];
NSString *text = item.text ? [NSString stringWithUTF8String:item.text] : @"";
NSString *comment = item.comment ? [NSString stringWithUTF8String:item.comment] : @"";
[candidates addObject:[[OSGRimeCandidate alloc] initWithText:text comment:comment]];
}
api->free_context(&context);
}
NSString *rawInput = [self rawInput];
NSInteger safeLimit = MAX((NSInteger)0, limit);
NSString *firstUnit = OSGFirstSpellingUnit(rawInput);
BOOL hasRemainder = firstUnit.length > 0 && rawInput.length > firstUnit.length;
NSMutableArray<OSGRimeCandidate *> *scanned = [NSMutableArray array];
// Deep-scan only when a trailing spelling unit exists so we can still
// surface first-syllable characters buried under completion flood.
NSInteger scanCap = hasRemainder ? MAX(safeLimit * 4, (NSInteger)400) : safeLimit;
if (RIME_API_AVAILABLE(api, candidate_list_begin) &&
RIME_API_AVAILABLE(api, candidate_list_next) &&
RIME_API_AVAILABLE(api, candidate_list_end)) {
RimeCandidateListIterator iterator = {0};
if (api->candidate_list_begin(self.session, &iterator)) {
NSInteger phrasesSeen = 0;
NSInteger charsSeen = 0;
NSInteger charQuota = hasRemainder ? MAX((NSInteger)24, safeLimit / 4) : 0;
while ((NSInteger)scanned.count < scanCap && api->candidate_list_next(&iterator)) {
NSString *text = iterator.candidate.text
? [NSString stringWithUTF8String:iterator.candidate.text]
: @"";
NSString *comment = iterator.candidate.comment
? [NSString stringWithUTF8String:iterator.candidate.comment]
: @"";
[scanned addObject:[[OSGRimeCandidate alloc] initWithText:text
comment:comment
index:iterator.index]];
if (text.length >= 2) {
phrasesSeen += 1;
} else if (text.length == 1) {
charsSeen += 1;
}
if (!hasRemainder && (NSInteger)scanned.count >= safeLimit) {
break;
}
if (hasRemainder && phrasesSeen >= safeLimit && charsSeen >= charQuota) {
break;
}
}
api->candidate_list_end(&iterator);
}
}
// Fallback: current menu page only (older librime / API hole).
if (scanned.count == 0) {
RIME_STRUCT(RimeContext_stdbool, menuContext);
if (api->get_context(self.session, &menuContext)) {
NSInteger count = MIN((NSInteger)menuContext.menu.num_candidates, safeLimit);
for (NSInteger index = 0; index < count; index++) {
RimeCandidate item = menuContext.menu.candidates[index];
NSString *text = item.text ? [NSString stringWithUTF8String:item.text] : @"";
NSString *comment = item.comment ? [NSString stringWithUTF8String:item.comment] : @"";
// Menu indices are page-local; absolute index ≈ page_no * page_size + i.
NSInteger absolute = (NSInteger)menuContext.menu.page_no
* (NSInteger)menuContext.menu.page_size
+ index;
[scanned addObject:[[OSGRimeCandidate alloc] initWithText:text
comment:comment
index:absolute]];
}
api->free_context(&menuContext);
}
}
NSArray<OSGRimeCandidate *> *display =
OSGAssembleDisplayCandidates(scanned, rawInput, safeLimit);
return [[OSGRimeSnapshot alloc] initWithPreedit:preedit
candidates:candidates
candidates:display
commitText:commitText
composing:composing];
}
+5 -1
View File
@@ -6,7 +6,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface OSGRimeCandidate : NSObject
@property(nonatomic, copy, readonly) NSString *text;
@property(nonatomic, copy, readonly) NSString *comment;
- (instancetype)initWithText:(NSString *)text comment:(NSString *)comment;
/// Absolute librime candidate index for `select_candidate` (stable across UI reorder).
@property(nonatomic, readonly) NSInteger index;
- (instancetype)initWithText:(NSString *)text
comment:(NSString *)comment
index:(NSInteger)index;
@end
/// Immutable runtime snapshot consumed by SwiftUI.