-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin.rs
More file actions
176 lines (149 loc) · 4.45 KB
/
Copy pathwin.rs
File metadata and controls
176 lines (149 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::Error;
use std::ffi::OsStr;
use std::iter;
use std::os::windows::ffi::OsStrExt;
use windows::{
core::{HSTRING, PWSTR},
Win32::{
Foundation::S_FALSE,
Globalization::{
IEnumSpellingError, ISpellChecker, ISpellCheckerFactory, ISpellingError,
SpellCheckerFactory,
},
System::Com::{
CoCreateInstance, CoInitializeEx, CoTaskMemFree, CLSCTX_INPROC_SERVER,
COINIT_MULTITHREADED,
},
},
};
fn wide_string(s: &str) -> Vec<u16> {
OsStr::new(s).encode_wide().chain(iter::once(0)).collect()
}
fn utf16_offset_to_utf8(s: &str, utf16_units: usize) -> usize {
let mut units = 0;
for (byte_idx, ch) in s.char_indices() {
if units >= utf16_units {
return byte_idx;
}
units += ch.len_utf16();
}
s.len()
}
fn open_for_language(bcp47: &str) -> Result<ISpellChecker, Error> {
// S_OK / S_FALSE (already initialized) both succeed via windows::Result
let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
let factory: ISpellCheckerFactory = unsafe {
CoCreateInstance(&SpellCheckerFactory, None, CLSCTX_INPROC_SERVER)
}
.map_err(|_| Error::Unavailable)?;
let tag = HSTRING::from(bcp47);
unsafe { factory.CreateSpellChecker(&tag) }.map_err(|_| Error::Unavailable)
}
#[derive(Debug)]
pub struct Checker {
checker: ISpellChecker,
}
impl Checker {
pub fn new() -> Result<Self, Error> {
Self::with_locale("en_US", "en-US")
}
pub fn with_locale(_hunspell: &str, bcp47: &str) -> Result<Self, Error> {
Ok(Checker {
checker: open_for_language(bcp47)?,
})
}
pub fn suggest(&self, word: &str) -> Vec<String> {
const MAX: usize = 10;
if word.is_empty() {
return Vec::new();
}
let Ok(enum_str) = (unsafe { self.checker.Suggest(&HSTRING::from(word)) }) else {
return Vec::new();
};
let mut out = Vec::new();
while out.len() < MAX {
let mut item = [PWSTR::null()];
let mut fetched = 0u32;
let hr = unsafe { enum_str.Next(&mut item, Some(&mut fetched)) };
if fetched == 0 || item[0].is_null() {
break;
}
if let Ok(s) = unsafe { item[0].to_string() } {
out.push(s);
}
unsafe {
CoTaskMemFree(Some(item[0].as_ptr() as *const _));
}
if hr.is_err() && hr != S_FALSE {
break;
}
}
out
}
pub fn check(&mut self, text: &str) -> impl Iterator<Item = SpellingError> {
if text.is_empty() {
return ErrorIter {
original: String::new(),
text: vec![],
iter: None,
};
}
let original = text.to_owned();
let wide = wide_string(text);
let iter = unsafe { self.checker.ComprehensiveCheck(&HSTRING::from(text)) }.ok();
ErrorIter {
original,
text: wide,
iter,
}
}
pub fn ignore(&mut self, word: &str) {
if word.is_empty() {
return;
}
let _ = unsafe { self.checker.Ignore(&HSTRING::from(word)) };
}
}
struct ErrorIter {
original: String,
text: Vec<u16>,
iter: Option<IEnumSpellingError>,
}
impl Iterator for ErrorIter {
type Item = SpellingError;
fn next(&mut self) -> Option<SpellingError> {
let iter = self.iter.as_ref()?;
let mut err: Option<ISpellingError> = None;
let hr = unsafe { iter.Next(&mut err) };
if hr == S_FALSE {
return None;
}
let err = err?;
let start = unsafe { err.StartIndex() }.ok()? as usize;
let length = unsafe { err.Length() }.ok()? as usize;
let err_text = String::from_utf16(&self.text[start..start + length]).ok()?;
let byte_start = utf16_offset_to_utf8(&self.original, start);
let byte_end = utf16_offset_to_utf8(&self.original, start + length);
Some(SpellingError {
text: err_text,
start: byte_start,
end: byte_end,
})
}
}
pub struct SpellingError {
text: String,
start: usize,
end: usize,
}
impl SpellingError {
pub fn text(&self) -> &str {
&self.text
}
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
}