|
1 | 1 | use glob::glob;
|
2 | 2 | use lsp_types::{Diagnostic, DiagnosticTag, Position, Range};
|
| 3 | +use ruff_python_ast::name::Name; |
3 | 4 | use tracing::error;
|
4 | 5 | use std::collections::{HashMap, HashSet};
|
5 | 6 | use std::rc::Rc;
|
6 | 7 | use std::cell::RefCell;
|
7 | 8 | use std::path::{Path, PathBuf};
|
8 | 9 |
|
9 |
| -use ruff_text_size::TextRange; |
| 10 | +use ruff_text_size::{TextRange, TextSize}; |
10 | 11 | use ruff_python_ast::{Alias, Identifier};
|
11 | 12 | use crate::{constants::*, oyarn, Sy, S};
|
12 | 13 | use crate::core::diagnostics::{create_diagnostic, DiagnosticCode};
|
@@ -51,15 +52,32 @@ fn resolve_import_stmt_hook(alias: &Alias, from_symbol: &Option<Rc<RefCell<Symbo
|
51 | 52 | }
|
52 | 53 | }
|
53 | 54 |
|
| 55 | +/** |
| 56 | + * Helper to manually import a symbol. Do not forget to use level instead of '.' in the from_stmt parameter. |
| 57 | + */ |
| 58 | +pub fn manual_import(session: &mut SessionInfo, source_file_symbol: &Rc<RefCell<Symbol>>, from_stmt:Option<String>, name: &str, asname: Option<String>, level: Option<u32>, diagnostics: &mut Option<&mut Vec<Diagnostic>>) -> Vec<ImportResult> { |
| 59 | + let name_aliases = vec![Alias { |
| 60 | + name: Identifier { id: Name::new(name), range: TextRange::new(TextSize::new(0), TextSize::new(0)) }, |
| 61 | + asname: match asname { |
| 62 | + Some(asname_inner) => Some(Identifier { id: Name::new(asname_inner), range: TextRange::new(TextSize::new(0), TextSize::new(0)) }), |
| 63 | + None => None, |
| 64 | + }, |
| 65 | + range: TextRange::new(TextSize::new(0), TextSize::new(0)), |
| 66 | + }]; |
| 67 | + let from_stmt = match from_stmt { |
| 68 | + Some(from_stmt_inner) => Some(Identifier { id: Name::new(from_stmt_inner), range: TextRange::new(TextSize::new(0), TextSize::new(0)) }), |
| 69 | + None => None, |
| 70 | + }; |
| 71 | + resolve_import_stmt(session, source_file_symbol, from_stmt.as_ref(), &name_aliases, level, diagnostics) |
| 72 | +} |
| 73 | + |
54 | 74 | pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<RefCell<Symbol>>, from_stmt: Option<&Identifier>, name_aliases: &[Alias], level: Option<u32>, diagnostics: &mut Option<&mut Vec<Diagnostic>>) -> Vec<ImportResult> {
|
55 | 75 | //A: search base of different imports
|
56 | 76 | let source_root = source_file_symbol.borrow().get_root().as_ref().unwrap().upgrade().unwrap();
|
57 | 77 | let entry = source_root.borrow().get_entry().unwrap();
|
58 | 78 | let _source_file_symbol_lock = source_file_symbol.borrow_mut();
|
59 | 79 | let file_tree = _resolve_packages(
|
60 |
| - &_source_file_symbol_lock.paths()[0].clone(), |
61 |
| - &_source_file_symbol_lock.get_tree(), |
62 |
| - &_source_file_symbol_lock.typ(), |
| 80 | + &_source_file_symbol_lock, |
63 | 81 | level,
|
64 | 82 | from_stmt);
|
65 | 83 | drop(_source_file_symbol_lock);
|
@@ -206,20 +224,20 @@ pub fn find_module(session: &mut SessionInfo, odoo_addons: Rc<RefCell<Symbol>>,
|
206 | 224 | None
|
207 | 225 | }
|
208 | 226 |
|
209 |
| -fn _resolve_packages(file_path: &String, file_tree: &Tree, file_sym_type: &SymType, level: Option<u32>, from_stmt: Option<&Identifier>) -> Vec<OYarn> { |
| 227 | +fn _resolve_packages(from_file: &Symbol, level: Option<u32>, from_stmt: Option<&Identifier>) -> Vec<OYarn> { |
210 | 228 | let mut first_part_tree: Vec<OYarn> = vec![];
|
211 | 229 | if level.is_some() && level.unwrap() > 0 {
|
212 | 230 | let mut lvl = level.unwrap();
|
213 |
| - if lvl > Path::new(file_path).components().count() as u32 { |
| 231 | + if lvl > Path::new(&from_file.paths()[0]).components().count() as u32 { |
214 | 232 | panic!("Level is too high!")
|
215 | 233 | }
|
216 |
| - if matches!(*file_sym_type, SymType::PACKAGE(_)) { |
| 234 | + if matches!(from_file.typ(), SymType::PACKAGE(_)) { |
217 | 235 | lvl -= 1;
|
218 | 236 | }
|
219 | 237 | if lvl == 0 {
|
220 |
| - first_part_tree = file_tree.0.clone(); |
| 238 | + first_part_tree = from_file.get_tree().0.clone(); |
221 | 239 | } else {
|
222 |
| - let tree = file_tree; |
| 240 | + let tree = from_file.get_tree(); |
223 | 241 | if lvl > tree.0.len() as u32 {
|
224 | 242 | error!("Level is too high and going out of scope");
|
225 | 243 | first_part_tree = vec![];
|
@@ -417,9 +435,7 @@ pub fn get_all_valid_names(session: &mut SessionInfo, source_file_symbol: &Rc<Re
|
417 | 435 | let entry = source_root.borrow().get_entry().unwrap();
|
418 | 436 | let _source_file_symbol_lock = source_file_symbol.borrow_mut();
|
419 | 437 | let file_tree = _resolve_packages(
|
420 |
| - &_source_file_symbol_lock.paths()[0].clone(), |
421 |
| - &_source_file_symbol_lock.get_tree(), |
422 |
| - &_source_file_symbol_lock.typ(), |
| 438 | + &_source_file_symbol_lock, |
423 | 439 | level,
|
424 | 440 | from_stmt);
|
425 | 441 | drop(_source_file_symbol_lock);
|
|
0 commit comments