Created
March 8, 2022 05:51
-
-
Save UltraInstinct05/63f5dac75fe02b75c83464bb8311809a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
class SelectDictionaryCommand(sublime_plugin.WindowCommand): | |
def run(self): | |
available_dictionaries = [x for x in sublime.find_resources("*") if x.endswith("dic")] | |
current_dictionaries = self.window.active_view().settings().get("dictionary") | |
items = [ | |
sublime.QuickPanelItem( | |
trigger = x.split("/")[2], | |
details = x, | |
kind=self.kind_information(x), | |
annotation = "Already set" if x in current_dictionaries else "" | |
) | |
for x in available_dictionaries | |
] | |
self.window.show_quick_panel( | |
items = items, | |
on_select = lambda id: self.on_select(id, available_dictionaries), | |
placeholder = "Select a dictionary to set ...", | |
flags = sublime.KEEP_OPEN_ON_FOCUS_LOST | sublime.MONOSPACE_FONT | |
) | |
def on_select(self, id, available_dictionaries): | |
if id < 0: | |
return | |
settings = sublime.load_settings("Preferences.sublime-settings") | |
settings.set("dictionary", self.construct_dictionary_setting(available_dictionaries[id])) | |
sublime.save_settings("Preferences.sublime-settings") | |
def kind_information(self, dictionary): | |
builtin_dictionaries = ["en_GB", "en_US"] | |
for x in builtin_dictionaries: | |
if dictionary.find(x) >= 0: | |
return (sublime.KIND_ID_COLOR_YELLOWISH, "D", "System Dictionary") | |
return (sublime.KIND_ID_COLOR_REDISH, "I", "Installed Dictionary") | |
def construct_dictionary_setting(self, selected_dictionary): | |
current_dictionaries = self.window.active_view().settings().get("dictionary") | |
if type(current_dictionaries) == str: | |
if selected_dictionary in current_dictionaries: | |
sublime.status_message("Reverting to default dictionary value.") | |
return selected_dictionary | |
return [current_dictionaries, selected_dictionary] | |
if selected_dictionary in current_dictionaries: | |
current_dictionaries.remove(selected_dictionary) | |
return current_dictionaries | |
current_dictionaries.append(selected_dictionary) | |
return current_dictionaries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment