|
| 1 | +use crate::{ |
| 2 | + builtins::{PyStr, PyStrRef}, |
| 3 | + exceptions::types::PyBaseExceptionRef, |
| 4 | + sliceable::PySliceableSequence, |
| 5 | + IdProtocol, PyObjectRef, TypeProtocol, VirtualMachine, |
| 6 | +}; |
| 7 | +use std::cell::RefCell; |
| 8 | +use std::iter::ExactSizeIterator; |
| 9 | +use std::thread_local; |
| 10 | + |
| 11 | +const MAX_CANDIDATE_ITEMS: usize = 750; |
| 12 | +const MAX_STRING_SIZE: usize = 40; |
| 13 | + |
| 14 | +const MOVE_COST: usize = 2; |
| 15 | +const CASE_COST: usize = 1; |
| 16 | + |
| 17 | +fn substitution_cost(mut a: u8, mut b: u8) -> usize { |
| 18 | + if (a & 31) != (b & 31) { |
| 19 | + return MOVE_COST; |
| 20 | + } |
| 21 | + if a == b { |
| 22 | + return 0usize; |
| 23 | + } |
| 24 | + if (b'A'..=b'Z').contains(&a) { |
| 25 | + a += b'a' - b'A'; |
| 26 | + } |
| 27 | + if (b'A'..=b'Z').contains(&b) { |
| 28 | + b += b'a' - b'A'; |
| 29 | + } |
| 30 | + if a == b { |
| 31 | + CASE_COST |
| 32 | + } else { |
| 33 | + MOVE_COST |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize { |
| 38 | + thread_local! { |
| 39 | + static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = RefCell::new([0usize; MAX_STRING_SIZE]); |
| 40 | + } |
| 41 | + |
| 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 | + BUFFER.with(|buffer| { |
| 78 | + let mut buffer = buffer.borrow_mut(); |
| 79 | + for i in 0..a_end { |
| 80 | + buffer[i] = (i + 1) * MOVE_COST; |
| 81 | + } |
| 82 | + |
| 83 | + let mut result = 0usize; |
| 84 | + for (b_index, b_code) in b_bytes[b_begin..(b_begin + b_end)].iter().enumerate() { |
| 85 | + result = b_index * MOVE_COST; |
| 86 | + let mut distance = result; |
| 87 | + let mut minimum = usize::MAX; |
| 88 | + for (a_index, a_code) in a_bytes[a_begin..(a_begin + a_end)].iter().enumerate() { |
| 89 | + let substitute = distance + substitution_cost(*b_code, *a_code); |
| 90 | + distance = buffer[a_index]; |
| 91 | + let insert_delete = usize::min(result, distance) + MOVE_COST; |
| 92 | + result = usize::min(insert_delete, substitute); |
| 93 | + |
| 94 | + buffer[a_index] = result; |
| 95 | + if result < minimum { |
| 96 | + minimum = result; |
| 97 | + } |
| 98 | + } |
| 99 | + if minimum > max_cost { |
| 100 | + return max_cost + 1; |
| 101 | + } |
| 102 | + } |
| 103 | + result |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +fn calculate_suggestions<'a>( |
| 108 | + dir_iter: impl ExactSizeIterator<Item = &'a PyObjectRef>, |
| 109 | + name: &PyObjectRef, |
| 110 | +) -> Option<PyStrRef> { |
| 111 | + if dir_iter.len() >= MAX_CANDIDATE_ITEMS { |
| 112 | + return None; |
| 113 | + } |
| 114 | + |
| 115 | + let mut suggestion: Option<&PyStrRef> = None; |
| 116 | + let mut suggestion_distance = usize::MAX; |
| 117 | + let name = name.downcast_ref::<PyStr>()?; |
| 118 | + |
| 119 | + for item in dir_iter { |
| 120 | + let item_name = item.downcast_ref::<PyStr>()?; |
| 121 | + if name.as_str() == item_name.as_str() { |
| 122 | + continue; |
| 123 | + } |
| 124 | + let max_distance = usize::min( |
| 125 | + (name.len() + item_name.len() + 3) * MOVE_COST / 6, |
| 126 | + suggestion_distance - 1, |
| 127 | + ); |
| 128 | + let current_distance = |
| 129 | + levenshtein_distance(name.as_str(), item_name.as_str(), max_distance); |
| 130 | + if current_distance > max_distance { |
| 131 | + continue; |
| 132 | + } |
| 133 | + if suggestion.is_none() || current_distance < suggestion_distance { |
| 134 | + suggestion = Some(item_name); |
| 135 | + suggestion_distance = current_distance; |
| 136 | + } |
| 137 | + } |
| 138 | + suggestion.cloned() |
| 139 | +} |
| 140 | + |
| 141 | +pub fn offer_suggestions(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> Option<PyStrRef> { |
| 142 | + if exc.class().is(&vm.ctx.exceptions.attribute_error) { |
| 143 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 144 | + let obj = vm.get_attribute(exc.as_object().clone(), "obj").unwrap(); |
| 145 | + |
| 146 | + calculate_suggestions(vm.dir(Some(obj)).ok()?.borrow_vec().iter(), &name) |
| 147 | + } else if exc.class().is(&vm.ctx.exceptions.name_error) { |
| 148 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 149 | + let mut tb = exc.traceback().unwrap(); |
| 150 | + while let Some(traceback) = tb.next.clone() { |
| 151 | + tb = traceback; |
| 152 | + } |
| 153 | + |
| 154 | + let varnames = tb.frame.code.clone().co_varnames(vm); |
| 155 | + if let Some(suggestions) = calculate_suggestions(varnames.as_slice().iter(), &name) { |
| 156 | + return Some(suggestions); |
| 157 | + }; |
| 158 | + |
| 159 | + let globals = vm.extract_elements(tb.frame.globals.as_object()).ok()?; |
| 160 | + if let Some(suggestions) = calculate_suggestions(globals.as_slice().iter(), &name) { |
| 161 | + return Some(suggestions); |
| 162 | + }; |
| 163 | + |
| 164 | + let builtins = vm.extract_elements(tb.frame.builtins.as_object()).ok()?; |
| 165 | + calculate_suggestions(builtins.as_slice().iter(), &name) |
| 166 | + } else { |
| 167 | + None |
| 168 | + } |
| 169 | +} |
0 commit comments