Skip to content

Commit

Permalink
refactor: simplify codes
Browse files Browse the repository at this point in the history
To load pinyin data from Mandarin.dat

Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Oct 3, 2020
1 parent e832adf commit ca0ba1b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25,565 deletions.
27 changes: 25 additions & 2 deletions ace_jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sublime
import sublime_plugin

from typing import Any, Dict, List, Union, cast
from typing import Any, Dict, List, Optional, Union, cast

from .libs import char_width_converter
from .libs.xpinyin import Pinyin
Expand All @@ -12,6 +12,7 @@

SETTINGS_FILENAME = "AceJump.sublime-settings"
SYTNAX_FILENAME = "Packages/{}/AceJump.sublime-syntax".format(PACKAGE_NAME)
XPINYIN_DICT_PATH = "Packages/{}/libs/xpinyin/Mandarin.dat".format(PACKAGE_NAME)

CHINESE_REGEX_OBJ = re.compile("[\u4E00-\u9FD5]+", re.U)

Expand All @@ -34,7 +35,7 @@
HINTING_MODE_INLINE_PHANTOM = 2
HINTING_MODE_DEFAULT = HINTING_MODE_REPLACE_CHAR

xpy = Pinyin()
xpy = None # type: Optional[Pinyin]
last_index = 0
hints = [] # type: List[sublime.Region]
phantom_sets = {} # type: Dict[int, sublime.PhantomSet]
Expand All @@ -43,6 +44,24 @@
ace_jump_active = False


def plugin_loaded() -> None:
init_xpy()


def init_xpy() -> None:
global xpy

pinyin_map = {} # type: Dict[str, str]
for line_num0, line in enumerate(sublime.load_resource(XPINYIN_DICT_PATH).splitlines()):
try:
k, v = line.split("\t")
pinyin_map[k] = v
except ValueError:
print_msg("Malformed pinyin data line {}: `{}`".format(line_num0 + 1, line))

xpy = Pinyin(pinyin_map)


def get_active_views(window: sublime.Window, current_buffer_only: bool) -> List[sublime.View]:
"""Returns all currently visible views"""

Expand Down Expand Up @@ -135,6 +154,10 @@ def set_plugin_mode(_mode: int) -> None:
sublime.status_message(msg)


def print_msg(msg: str) -> None:
print("[{}] {}".format(PACKAGE_NAME, msg))


# set the default plugin mode
set_plugin_mode(MODE_DEFAULT)

Expand Down
Loading

0 comments on commit ca0ba1b

Please sign in to comment.