sudachi/plugin/input_text/
mod.rs

1/*
2 * Copyright (c) 2021-2024 Works Applications Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17pub mod default_input_text;
18mod ignore_yomigana;
19mod prolonged_sound_mark;
20
21use serde_json::Value;
22
23use crate::config::Config;
24use crate::dic::grammar::Grammar;
25use crate::input_text::{InputBuffer, InputEditor};
26use crate::plugin::input_text::default_input_text::DefaultInputTextPlugin;
27use crate::plugin::input_text::ignore_yomigana::IgnoreYomiganaPlugin;
28use crate::plugin::input_text::prolonged_sound_mark::ProlongedSoundMarkPlugin;
29use crate::plugin::loader::PluginCategory;
30use crate::prelude::*;
31
32/// Trait of plugin to modify the input text before tokenization
33pub trait InputTextPlugin: Sync + Send {
34    /// Loads necessary information for the plugin
35    fn set_up(&mut self, settings: &Value, config: &Config, grammar: &Grammar)
36        -> SudachiResult<()>;
37
38    /// Whether the rewrite process uses chars
39    fn uses_chars(&self) -> bool {
40        false
41    }
42
43    /// Perform rewrites
44    fn rewrite(&self, input: &mut InputBuffer) -> SudachiResult<()> {
45        if self.uses_chars() {
46            input.refresh_chars()
47        }
48        input.with_editor(|b, r| {
49            // deprecation is to discourage calling the work function
50            #[allow(deprecated)]
51            self.rewrite_impl(b, r)
52        })
53    }
54
55    /// Actual implementation of rewriting. Call `apply_rewrite` instead.
56    #[deprecated(note = "call rewrite instead")]
57    fn rewrite_impl<'a>(
58        &'a self,
59        input: &InputBuffer,
60        edit: InputEditor<'a>,
61    ) -> SudachiResult<InputEditor<'a>>;
62}
63
64impl PluginCategory for dyn InputTextPlugin {
65    type BoxType = Box<dyn InputTextPlugin + Sync + Send>;
66    type InitFnType = unsafe fn() -> SudachiResult<Self::BoxType>;
67    fn configurations(cfg: &Config) -> &[Value] {
68        &cfg.input_text_plugins
69    }
70
71    fn bundled_impl(name: &str) -> Option<Self::BoxType> {
72        match name {
73            "IgnoreYomiganaPlugin" => Some(Box::<IgnoreYomiganaPlugin>::default()),
74            "DefaultInputTextPlugin" => Some(Box::<DefaultInputTextPlugin>::default()),
75            "ProlongedSoundMarkPlugin" => Some(Box::<ProlongedSoundMarkPlugin>::default()),
76            _ => None,
77        }
78    }
79
80    fn do_setup(
81        ptr: &mut Self::BoxType,
82        settings: &Value,
83        config: &Config,
84        grammar: &mut Grammar,
85    ) -> SudachiResult<()> {
86        ptr.set_up(settings, config, grammar)
87    }
88}