Skip to content

Commit

Permalink
feat(punctuation): 支持输入右单引号和右双引号
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Oct 26, 2023
1 parent 257c4da commit d3eb429
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 35 deletions.
6 changes: 5 additions & 1 deletion Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
45B76CA92AEA6042009AFABD /* PunctuationConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */; };
6753419E2AB54A3A00757F76 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6753419D2AB54A3A00757F76 /* main.cpp */; };
675341A72AB54AEA00757F76 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 675341A52AB54AEA00757F76 /* sqlite3.c */; };
67AA47FF2AB733860073AC86 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 67C9A0232AB53ED3000B5281 /* Assets.xcassets */; };
Expand Down Expand Up @@ -67,6 +68,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PunctuationConversion.swift; sourceTree = "<group>"; };
6753419B2AB54A3A00757F76 /* TableBuilder */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TableBuilder; sourceTree = BUILT_PRODUCTS_DIR; };
6753419D2AB54A3A00757F76 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
675341A52AB54AEA00757F76 /* sqlite3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sqlite3.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -196,6 +198,7 @@
67C9A0062AB53E83000B5281 /* Fire.swift */,
67C9A0222AB53ED3000B5281 /* FireInputServer.swift */,
67C9A0072AB53E83000B5281 /* FireInputController.swift */,
45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */,
67C9A0022AB53E83000B5281 /* InputSource.swift */,
67C9A0012AB53E83000B5281 /* MainMenu.xib */,
67C9A0042AB53E83000B5281 /* StatusBar.swift */,
Expand Down Expand Up @@ -432,6 +435,7 @@
67C9A0422AB53ED3000B5281 /* ThesaurusPane.swift in Sources */,
67C9A0002AB53E62000B5281 /* ModifierKeyUpChecker.swift in Sources */,
67C9A0442AB53ED3000B5281 /* CandidatesWindow.swift in Sources */,
45B76CA92AEA6042009AFABD /* PunctuationConversion.swift in Sources */,
67C9A0362AB53ED3000B5281 /* DictManager.swift in Sources */,
67C9A0582AB5429B000B5281 /* sqlite3.c in Sources */,
67C99FFD2AB53E62000B5281 /* ToastWindow.swift in Sources */,
Expand Down Expand Up @@ -619,7 +623,7 @@
CURRENT_PROJECT_VERSION = 1;
DEPLOYMENT_LOCATION = YES;
DEVELOPMENT_TEAM = T68XK6867P;
DSTROOT = "/Library/Input Methods";
DSTROOT = "$HOME/Library/Input Methods";
ENABLE_HARDENED_RUNTIME = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down
18 changes: 0 additions & 18 deletions Fire/Fire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ class Fire: NSObject {

var inputMode: InputMode = .zhhans

func transformPunctuation(_ origin: String) -> String? {
let isPunctuation = punctuation.keys.contains(origin)
if !isPunctuation {
return nil
}
let mode = Defaults[.punctuationMode]
if mode == .enUs {
return origin
}
if mode == .zhhans {
return punctuation[origin]
}
if mode == .custom {
return Defaults[.customPunctuationSettings][origin]
}
return nil
}

override init() {
super.init()
_ = InputSource.shared.onSelectChanged { selected in
Expand Down
2 changes: 1 addition & 1 deletion Fire/FireInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class FireInputController: IMKInputController {
}

// 如果输入的字符是标点符号,转换标点符号为中文符号
if inputMode == .zhhans, let result = Fire.shared.transformPunctuation(string) {
if inputMode == .zhhans, let result = PunctuationConversion.shared.conversion(string) {
insertText(result)
return true
}
Expand Down
60 changes: 60 additions & 0 deletions Fire/PunctuationConversion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// PunctuationConversion.swift
// Fire
//
// Created by 杨永榜 on 2023/10/26.
//

import Foundation
import Defaults

protocol Conversion {
func conversion(_ origin: String) -> String?
}

class PunctuationConversion: Conversion {
// 左引号暂存栈
private var parisPunctuationStack: [String] = []
private let MAX_STACK_SIZE = 30 // 暂存栈大小,防止随着使用出现内存上涨

private func transformResult(_ result: String) -> String {
let resultMap = [
"": "",
"": ""
]
// 存在需要待匹配的左侧引号,并且当前输出和待匹配的引号一致,把结果转为对应的右侧引号
if resultMap.keys.contains(result) && result == parisPunctuationStack.last {
_ = parisPunctuationStack.popLast()
return resultMap[result] ?? result
}
// 没有待匹配的引号,并且输入了左侧引号,存入待匹配区
if resultMap.keys.contains(result) {
parisPunctuationStack.append(result)
if parisPunctuationStack.count > MAX_STACK_SIZE {
parisPunctuationStack.removeFirst()
}
return result
}
return result
}

func conversion(_ origin: String) -> String? {
let isPunctuation = punctuation.keys.contains(origin)
if !isPunctuation {
return nil
}
let mode = Defaults[.punctuationMode]
if mode == .enUs {
return origin
}
if mode == .zhhans {
return punctuation[origin] == nil ? nil : transformResult(punctuation[origin]!)
}
if mode == .custom {
return Defaults[.customPunctuationSettings][origin]
}
return nil
}

static let shared = PunctuationConversion()
}
30 changes: 15 additions & 15 deletions Fire/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,28 @@ let punctuation: [String: String] = [
"/": "",
";": "",
"'": "",
"[": "",
"]": "",
"`": "",
"[": "",
"]": "",
"`": "·",
"!": "",
"@": "",
"#": "",
"@": "@",
"#": "#",
"$": "",
"%": "",
"%": "%",
"^": "……",
"&": "",
"*": "×",
"&": "&",
"*": "*",
"(": "",
")": "",
"-": "",
"-": "-",
"_": "——",
"+": "",
"=": "",
"~": "",
"{": "",
"+": "+",
"=": "=",
"~": "~",
"{": "",
"\\": "",
"|": "",
"}": "",
"|": "|",
"}": "",
":": "",
"\"": "",
"<": "",
Expand Down

0 comments on commit d3eb429

Please sign in to comment.