|
| 1 | +use crate::common::lock::PyRwLock; |
| 2 | +use crate::{ |
| 3 | + builtins::{PyBaseObject, PyList, PyStrRef}, |
| 4 | + exceptions::types::PyBaseExceptionRef, |
| 5 | + sliceable::PySliceableSequence, |
| 6 | + IdProtocol, PyObjectRef, TryFromObject, TypeProtocol, VirtualMachine, |
| 7 | +}; |
| 8 | + |
| 9 | +const MAX_CANDIDATE_ITEMS: usize = 750; |
| 10 | +const MAX_STRING_SIZE: usize = 40; |
| 11 | + |
| 12 | +const MOVE_COST: usize = 2; |
| 13 | +const CASE_COST: usize = 1; |
| 14 | + |
| 15 | +fn substitution_cost(mut a: u8, mut b: u8) -> usize { |
| 16 | + if (a & 31) != (b & 31) { |
| 17 | + return MOVE_COST; |
| 18 | + } |
| 19 | + if a == b { |
| 20 | + return 0usize; |
| 21 | + } |
| 22 | + if (b'A'..=b'Z').contains(&a) { |
| 23 | + a += b'a' - b'A'; |
| 24 | + } |
| 25 | + if (b'A'..=b'Z').contains(&b) { |
| 26 | + b += b'a' - b'A'; |
| 27 | + } |
| 28 | + if a == b { |
| 29 | + return CASE_COST; |
| 30 | + } |
| 31 | + MOVE_COST |
| 32 | +} |
| 33 | + |
| 34 | +fn global_levelshtein_buffer() -> &'static PyRwLock<[usize; MAX_STRING_SIZE]> { |
| 35 | + rustpython_common::static_cell! { |
| 36 | + static BUFFER: PyRwLock<[usize; MAX_STRING_SIZE]>; |
| 37 | + }; |
| 38 | + BUFFER.get_or_init(|| PyRwLock::new([0usize; MAX_STRING_SIZE])) |
| 39 | +} |
| 40 | + |
| 41 | +fn levelshtein_distance(a: &str, b: &str, max_cost: usize) -> usize { |
| 42 | + if a == b { |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + let (mut a_bytes, mut b_bytes) = (a.as_bytes(), b.as_bytes()); |
| 47 | + let (mut a_begin, mut a_end) = (0usize, a.len()); |
| 48 | + let (mut b_begin, mut b_end) = (0usize, b.len()); |
| 49 | + |
| 50 | + while a_end > 0 && b_end > 0 && a_bytes[a_begin] == b_bytes[b_begin] { |
| 51 | + a_begin += 1; |
| 52 | + b_begin += 1; |
| 53 | + a_end -= 1; |
| 54 | + b_end -= 1; |
| 55 | + } |
| 56 | + while a_end > 0 && b_end > 0 && a_bytes[a_begin + a_end - 1] == b_bytes[b_begin + b_end - 1] { |
| 57 | + a_end -= 1; |
| 58 | + b_end -= 1; |
| 59 | + } |
| 60 | + if a_end == 0 || b_end == 0 { |
| 61 | + return (a_end + b_end) * MOVE_COST; |
| 62 | + } |
| 63 | + if a_end > MAX_STRING_SIZE || b_end > MAX_STRING_SIZE { |
| 64 | + return max_cost + 1; |
| 65 | + } |
| 66 | + |
| 67 | + if b_end < a_end { |
| 68 | + std::mem::swap(&mut a_bytes, &mut b_bytes); |
| 69 | + std::mem::swap(&mut a_begin, &mut b_begin); |
| 70 | + std::mem::swap(&mut a_end, &mut b_end); |
| 71 | + } |
| 72 | + |
| 73 | + if (b_end - a_end) * MOVE_COST > max_cost { |
| 74 | + return max_cost + 1; |
| 75 | + } |
| 76 | + |
| 77 | + for i in 0..a_end { |
| 78 | + global_levelshtein_buffer().write()[i] = (i + 1) * MOVE_COST; |
| 79 | + } |
| 80 | + |
| 81 | + let mut result = 0usize; |
| 82 | + for (b_index, b_code) in b_bytes[b_begin..(b_begin + b_end)].iter().enumerate() { |
| 83 | + result = b_index * MOVE_COST; |
| 84 | + let mut distance = result; |
| 85 | + let mut minimum = usize::MAX; |
| 86 | + for (a_index, a_code) in a_bytes[a_begin..(a_begin + a_end)].iter().enumerate() { |
| 87 | + let substitute = distance + substitution_cost(*b_code, *a_code); |
| 88 | + distance = global_levelshtein_buffer().read()[a_index]; |
| 89 | + let insert_delete = usize::min(result, distance) + MOVE_COST; |
| 90 | + result = usize::min(insert_delete, substitute); |
| 91 | + |
| 92 | + global_levelshtein_buffer().write()[a_index] = result; |
| 93 | + if result < minimum { |
| 94 | + minimum = result; |
| 95 | + } |
| 96 | + } |
| 97 | + if minimum > max_cost { |
| 98 | + return max_cost + 1; |
| 99 | + } |
| 100 | + } |
| 101 | + result |
| 102 | +} |
| 103 | + |
| 104 | +fn calculate_suggestions(dir: PyList, name: &PyObjectRef, vm: &VirtualMachine) -> Option<PyStrRef> { |
| 105 | + let dir = dir.borrow_vec(); |
| 106 | + if dir.len() >= MAX_CANDIDATE_ITEMS { |
| 107 | + return None; |
| 108 | + } |
| 109 | + |
| 110 | + let mut suggestion: Option<PyStrRef> = None; |
| 111 | + let mut suggestion_distance = usize::MAX; |
| 112 | + let name = if let Ok(name) = PyStrRef::try_from_object(vm, name.clone()) { |
| 113 | + name |
| 114 | + } else { |
| 115 | + return None; |
| 116 | + }; |
| 117 | + for item in dir.iter() { |
| 118 | + let item_str = if let Ok(item_str) = PyStrRef::try_from_object(vm, item.clone()) { |
| 119 | + item_str |
| 120 | + } else { |
| 121 | + return None; |
| 122 | + }; |
| 123 | + if name.to_string() == item_str.to_string() { |
| 124 | + continue; |
| 125 | + } |
| 126 | + let max_distance = usize::min( |
| 127 | + (name.len() + item_str.len() + 3) * MOVE_COST / 6, |
| 128 | + suggestion_distance - 1, |
| 129 | + ); |
| 130 | + let current_distance = levelshtein_distance(name.as_str(), item_str.as_str(), max_distance); |
| 131 | + if current_distance > max_distance { |
| 132 | + continue; |
| 133 | + } |
| 134 | + if suggestion.is_none() || current_distance < suggestion_distance { |
| 135 | + suggestion = Some(item_str); |
| 136 | + suggestion_distance = current_distance; |
| 137 | + } |
| 138 | + } |
| 139 | + suggestion |
| 140 | +} |
| 141 | + |
| 142 | +pub fn offer_suggestions(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> Option<PyStrRef> { |
| 143 | + if exc.class().is(&vm.ctx.exceptions.attribute_error) { |
| 144 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 145 | + let obj = vm.get_attribute(exc.as_object().clone(), "obj").unwrap(); |
| 146 | + |
| 147 | + calculate_suggestions(PyBaseObject::dir(obj, vm).unwrap(), &name, vm) |
| 148 | + } else if exc.class().is(&vm.ctx.exceptions.name_error) { |
| 149 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 150 | + let mut tb = exc.traceback().unwrap(); |
| 151 | + |
| 152 | + while let Some(traceback) = tb.next.clone() { |
| 153 | + tb = traceback; |
| 154 | + } |
| 155 | + |
| 156 | + let frame = tb.frame.clone(); |
| 157 | + let code = frame.code.clone(); |
| 158 | + |
| 159 | + let dir = PyList::from(code.co_varnames(vm).as_slice().to_vec()); |
| 160 | + if let Some(suggestions) = calculate_suggestions(dir, &name, vm) { |
| 161 | + return Some(suggestions); |
| 162 | + }; |
| 163 | + |
| 164 | + let dir = PyList::from(vm.extract_elements(frame.globals.as_object()).unwrap()); |
| 165 | + if let Some(suggestions) = calculate_suggestions(dir, &name, vm) { |
| 166 | + return Some(suggestions); |
| 167 | + }; |
| 168 | + |
| 169 | + let dir = PyList::from(vm.extract_elements(frame.builtins.as_object()).unwrap()); |
| 170 | + calculate_suggestions(dir, &name, vm) |
| 171 | + } else { |
| 172 | + None |
| 173 | + } |
| 174 | +} |
0 commit comments