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

"Input Method" Support on Account Name #75

Merged
merged 2 commits into from
Oct 10, 2022
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
54 changes: 35 additions & 19 deletions src/completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from 'vscode';
import {Extension} from './extension';
import {EOL} from 'os';
import {countOccurrences} from './utils';
import {InputMethod} from './inputMethods/inputMethod';
import {countOccurrences, pushIfEmpty} from './utils';
import {InputMethod, InputMethodConfig} from './inputMethods/inputMethod';
import {Pinyin} from './inputMethods/pinyin';

interface Account {
Expand Down Expand Up @@ -96,6 +96,12 @@ implements vscode.CompletionItemProvider, vscode.HoverProvider {
}
}

findLetterExpression (text: string, config?: InputMethodConfig) : string[] {
return this.inputMethods
.map((inputMethod) => inputMethod.getLetterRepresentation(text, config))
.filter((text) => text.length > 0)
};

provideHover(
document: vscode.TextDocument,
position: vscode.Position,
Expand Down Expand Up @@ -204,24 +210,22 @@ implements vscode.CompletionItemProvider, vscode.HoverProvider {
kind: CompletionItemKind,
suffix: string,
) => {
let findOne = false;
for (const inputMethod of this.inputMethods) {
const letters = inputMethod.getLetterRepresentation(text);
if (letters.length > 0) {
findOne = true;
const item = new CompletionItem(
const lettersExpressions : string[] = this.findLetterExpression(text);
const completionItems = lettersExpressions.map((letters) => {
const item = new CompletionItem(
letters + '(' + text + ')',
kind,
);
item.insertText = text + suffix;
list.push(item);
}
}
if (!findOne) {
return item;
})
if (completionItems.length === 0) {
const item = new CompletionItem(text, kind);
item.insertText = text + suffix;
list.push(item);
completionItems.push(item)
}
list.push(...completionItems);
};
const list: CompletionItem[] = [];
if (numQuotes === 1) {
Expand Down Expand Up @@ -278,19 +282,31 @@ implements vscode.CompletionItemProvider, vscode.HoverProvider {
if (account.close !== null && account.close !== '') {
continue;
}
const item = new CompletionItem(
accountName,
CompletionItemKind.EnumMember,
);
item.documentation = this.describeAccount(accountName);
item.range = wordRange;
list.push(item);
const labelToItemFunc = (accountName: string, label? : string) : CompletionItem => {
const item = new CompletionItem(
label ? `${label}(${accountName})` : accountName,
CompletionItemKind.EnumMember,
);
item.documentation = this.describeAccount(accountName);
item.range = wordRange;
item.insertText = accountName;
return item;
}
list.push(
...pushIfEmpty(
this.findLetterExpression(accountName, { keepPunctuation: true })
.map((letters) => {
const item = labelToItemFunc(accountName, letters);
return item;
}), labelToItemFunc(accountName))
);
}
this.commodities.forEach((v, i, a) => {
const item = new CompletionItem(v, CompletionItemKind.Unit);
item.range = wordRange;
list.push(item);
});
console.log('[VSCode Beancount Log]', 'provideCompleteionItems', 'case default', 'completion items list', list);
resolve(list);
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/inputMethods/inputMethod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface InputMethod {
getLetterRepresentation(w: string): string;
getLetterRepresentation(w: string, config?: InputMethodConfig): string;
}

export class InputMethodConfig {
keepPunctuation: boolean = false;
}
10 changes: 6 additions & 4 deletions src/inputMethods/pinyin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {InputMethod} from './inputMethod';
import {InputMethod, InputMethodConfig} from './inputMethod';
import {readFileSync} from 'fs';

export class Pinyin implements InputMethod {
Expand All @@ -14,14 +14,16 @@ export class Pinyin implements InputMethod {
}
}

getLetterRepresentation(w: string): string {
getLetterRepresentation(w: string, config?: InputMethodConfig): string {
const punctuationReg = /[,.\:\-\\/!?]/i;
const reg = /[0-9a-zA-Z]/i;
const result = [];
for (let str of w) {
if (!str.match(reg)) {
str = this._pyData.get(str) || '';
let defatultStr = config?.keepPunctuation && str.match(punctuationReg) ? str : '';
str = this._pyData.get(str) || defatultStr;
}
result.push(str)
result.push(str);
}
return result.join('');
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ export function runCmd(
export function countOccurrences(s: string, c: RegExp) {
return (s.match(c) || []).length;
}

export function pushIfEmpty <T> (array: T[], defaultValue: T) : T[] {
if (array.length === 0) {
array.push(defaultValue);
}
return array;
}