Skip to content

Commit

Permalink
Merge pull request #119 from qwertyyb/feature/quote-paris-punctuation
Browse files Browse the repository at this point in the history
feat: 支持输出右半单双引号;支持输出「『』」符号
  • Loading branch information
qwertyyb authored Oct 27, 2023
2 parents 257c4da + a84085d commit 4bc0674
Show file tree
Hide file tree
Showing 5 changed files with 107 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
86 changes: 86 additions & 0 deletions Fire/PunctuationConversion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// 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 quoteCount = [
"": 0,
"": 0,
]
private var squareBracketsCount = [
"": 0,
"": 0
]

// 转换单双引号
// 基本思路: 第一次按引号输入左引号,第二次按输入右引号
private func transformQuoteResult(_ result: String) -> String {
if !quoteCount.keys.contains(result) {
return result
}
let resultMap = [
"": "",
"": ""
]
quoteCount[result] = (quoteCount[result]! + 1) % 2
if quoteCount[result] == 0 {
return resultMap[result]!
}
return result
}

// 转换方括号
// 基本思路: 第一次按{输出「,第二次按{输出『,按}时,以左括号为优先进行匹配
private func transformSquareBrackets(_ result: String) -> String {
if !squareBracketsCount.keys.contains(result) {
return result
}
let resultMap = [
"": "",
"": ""
]

squareBracketsCount[result] = (squareBracketsCount[result]! + 1) % 2
if result == "" {
squareBracketsCount[""] = (squareBracketsCount[result]! + 1) % 2
}
if squareBracketsCount[result] == 0 {
return resultMap[result]!
}
return result
}

private func transformResult(_ result: String) -> String {
return transformQuoteResult(transformSquareBrackets(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 4bc0674

Please sign in to comment.