diff --git a/src/completer.ts b/src/completer.ts index a7afcd7..f71ce49 100644 --- a/src/completer.ts +++ b/src/completer.ts @@ -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 { @@ -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, @@ -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) { @@ -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; } diff --git a/src/inputMethods/inputMethod.ts b/src/inputMethods/inputMethod.ts index 74ac06d..d3bdd0f 100644 --- a/src/inputMethods/inputMethod.ts +++ b/src/inputMethods/inputMethod.ts @@ -1,3 +1,7 @@ export interface InputMethod { - getLetterRepresentation(w: string): string; + getLetterRepresentation(w: string, config?: InputMethodConfig): string; } + +export class InputMethodConfig { + keepPunctuation: boolean = false; +} \ No newline at end of file diff --git a/src/inputMethods/pinyin.ts b/src/inputMethods/pinyin.ts index 7b3f7af..a960ac4 100644 --- a/src/inputMethods/pinyin.ts +++ b/src/inputMethods/pinyin.ts @@ -1,4 +1,4 @@ -import {InputMethod} from './inputMethod'; +import {InputMethod, InputMethodConfig} from './inputMethod'; import {readFileSync} from 'fs'; export class Pinyin implements InputMethod { @@ -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(''); } diff --git a/src/utils.ts b/src/utils.ts index 2b5744a..c493780 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -24,3 +24,10 @@ export function runCmd( export function countOccurrences(s: string, c: RegExp) { return (s.match(c) || []).length; } + +export function pushIfEmpty (array: T[], defaultValue: T) : T[] { + if (array.length === 0) { + array.push(defaultValue); + } + return array; +}