Skip to content

Master master fda #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ harness = false

[dev-dependencies]
iai-callgrind = "0.14.0"

[features]
default = ["debug_yarn"]
debug_yarn = [] # use a readable structure instead of Yarn for debug purpose
32 changes: 27 additions & 5 deletions server/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,40 @@ use byteyarn::{yarn, Yarn};
pub const EXTENSION_NAME: &str = "Odoo";
pub const EXTENSION_VERSION: &str = "0.4.1";

pub const DEBUG_ODOO_BUILDER: bool = false;
pub const DEBUG_ODOO_BUILDER: bool = true;
pub const DEBUG_MEMORY: bool = false;
pub const DEBUG_THREADS: bool = false;
pub const DEBUG_STEPS: bool = false;
pub const DEBUG_STEPS: bool = true;

pub type Tree = (Vec<Yarn>, Vec<Yarn>);
pub type Tree = (Vec<OYarn>, Vec<OYarn>);

//type DebugYarn = String;

#[macro_export]
#[cfg(not(all(feature="debug_yarn", debug_assertions)))]
macro_rules! oyarn {
($($args:tt)*) => {
byteyarn::yarn!($($args)*)
};
}
#[macro_export]
#[cfg(all(feature="debug_yarn", debug_assertions))]
macro_rules! oyarn {
($($args:tt)*) => {
format!($($args)*)
};
}

#[cfg(all(feature="debug_yarn", debug_assertions))]
pub type OYarn = String;
#[cfg(not(all(feature="debug_yarn", debug_assertions)))]
pub type OYarn = Yarn;

pub fn tree(a: Vec<&str>, b: Vec<&str>) -> Tree {
(a.iter().map(|x| yarn!("{}", *x)).collect(), b.iter().map(|x| yarn!("{}", *x)).collect())
(a.iter().map(|x| oyarn!("{}", *x)).collect(), b.iter().map(|x| oyarn!("{}", *x)).collect())
}

pub fn flatten_tree(tree: &Tree) -> Vec<Yarn> {
pub fn flatten_tree(tree: &Tree) -> Vec<OYarn> {
[tree.0.clone(), tree.1.clone()].concat()
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/core/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use byteyarn::Yarn;
use tracing::{error, info};
use weak_table::PtrWeakHashSet;

use crate::{constants::{flatten_tree, BuildSteps, PackageType, SymType, Tree}, threads::SessionInfo, utils::PathSanitizer};
use crate::{constants::{flatten_tree, BuildSteps, OYarn, PackageType, SymType, Tree}, threads::SessionInfo, utils::PathSanitizer};

use super::{odoo::SyncOdoo, symbols::symbol::Symbol};

Expand Down Expand Up @@ -102,7 +102,7 @@ impl EntryPointMgr {
/* Create a new entry to public.
return the disk_dir symbol of the last FOLDER of the path
*/
pub fn add_entry_to_addons(&mut self, path: String, related: Option<Rc<RefCell<EntryPoint>>>, related_addition: Option<Vec<Yarn>>) -> Option<Rc<RefCell<Symbol>>> {
pub fn add_entry_to_addons(&mut self, path: String, related: Option<Rc<RefCell<EntryPoint>>>, related_addition: Option<Vec<OYarn>>) -> Option<Rc<RefCell<Symbol>>> {
info!("Adding new addon entry point: {}", path);
let entry_point_tree = PathBuf::from(&path).to_tree();
let mut addon_to_odoo_path = None;
Expand Down Expand Up @@ -316,16 +316,16 @@ pub enum EntryPointType {
#[derive(Debug, Clone)]
pub struct EntryPoint {
pub path: String,
pub tree: Vec<Yarn>,
pub tree: Vec<OYarn>,
pub typ: EntryPointType,
pub addon_to_odoo_path: Option<String>, //contains the odoo path if this is an addon entry point
pub addon_to_odoo_tree: Option<Vec<Yarn>>, //contains the odoo tree if this is an addon entry point
pub addon_to_odoo_tree: Option<Vec<OYarn>>, //contains the odoo tree if this is an addon entry point
pub root: Rc<RefCell<Symbol>>,
pub not_found_symbols: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
pub to_delete: bool,
}
impl EntryPoint {
pub fn new(path: String, tree: Vec<Yarn>, typ:EntryPointType, addon_to_odoo_path: Option<String>, addon_to_odoo_tree: Option<Vec<Yarn>>) -> Rc<RefCell<Self>> {
pub fn new(path: String, tree: Vec<OYarn>, typ:EntryPointType, addon_to_odoo_path: Option<String>, addon_to_odoo_tree: Option<Vec<OYarn>>) -> Rc<RefCell<Self>> {
let root = Symbol::new_root();
root.borrow_mut().as_root_mut().weak_self = Some(Rc::downgrade(&root)); // manually set weakself for root symbols
let res = Rc::new(RefCell::new(Self { path,
Expand Down
58 changes: 38 additions & 20 deletions server/src/core/import_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use byteyarn::{yarn, Yarn};
use glob::glob;
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString, Position, Range};
use tracing::{error, info};
Expand All @@ -9,7 +8,7 @@ use std::path::Path;

use ruff_text_size::TextRange;
use ruff_python_ast::{Alias, Identifier};
use crate::{constants::*, Sy, S};
use crate::{constants::*, oyarn, Sy, S};
use crate::threads::SessionInfo;
use crate::utils::{is_dir_cs, is_file_cs, PathSanitizer};

Expand All @@ -18,7 +17,7 @@ use super::odoo::SyncOdoo;
use super::symbols::symbol::Symbol;

pub struct ImportResult {
pub name: Yarn,
pub name: OYarn,
pub found: bool,
pub symbol: Rc<RefCell<Symbol>>,
pub file_tree: Tree,
Expand All @@ -29,8 +28,8 @@ pub struct ImportResult {
//It means of course than a modification during the build will not be taken into account, but it should be ok because reloaded after the build
#[derive(Debug)]
pub struct ImportCache {
pub modules: HashMap<Yarn, Option<Rc<RefCell<Symbol>>>>,
pub main_modules: HashMap<Yarn, Option<Rc<RefCell<Symbol>>>>,
pub modules: HashMap<OYarn, Option<Rc<RefCell<Symbol>>>>,
pub main_modules: HashMap<OYarn, Option<Rc<RefCell<Symbol>>>>,
}

fn resolve_import_stmt_hook(alias: &Alias, from_symbol: &Option<Rc<RefCell<Symbol>>>, session: &mut SessionInfo, source_file_symbol: &Rc<RefCell<Symbol>>, from_stmt: Option<&Identifier>, level: Option<u32>, diagnostics: &mut Option<&mut Vec<Diagnostic>>) -> Option<ImportResult>{
Expand Down Expand Up @@ -84,7 +83,7 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
let mut result = vec![];
for alias in name_aliases {
result.push(ImportResult{
name: Yarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
name: OYarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
found: false,
symbol: fallback_sym.as_ref().unwrap_or(&source_root).clone(),
file_tree: (file_tree.clone(), vec![]),
Expand All @@ -97,7 +96,10 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re

let mut name_index: i32 = -1;
for alias in name_aliases.iter() {
let name = yarn!("{}", alias.name);
let name = oyarn!("{}", alias.name);
if name == "odoo.init" {
println!("here");
}
name_index += 1;
if let Some(hook_result) = resolve_import_stmt_hook(alias, &from_symbol, session, source_file_symbol, from_stmt, level, diagnostics){
result[name_index as usize] = hook_result;
Expand All @@ -119,7 +121,7 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
&entry,
source_path.as_str(),
from_symbol.clone(),
&vec![name.split(".").map(|s| yarn!("{}", s)).next().unwrap()],
&vec![name.split(".").map(|s| oyarn!("{}", s)).next().unwrap()],
None,
None);
if name_symbol.is_none() {
Expand All @@ -133,14 +135,27 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
continue;
}
}
result[name_index as usize].name = name.split(".").map(|s| yarn!("{}", s)).next().unwrap();
result[name_index as usize].name = name.split(".").map(|s| oyarn!("{}", s)).next().unwrap();
result[name_index as usize].found = true;
result[name_index as usize].symbol = name_symbol.as_ref().unwrap().clone();
//we found name_symbol and will return it. But in the case of "import A.B.C", even if we will return A, we need to effectively import B then
//C as it can have effects on the codebase
let name_split: Vec<OYarn> = name.split(".").map(|s| oyarn!("{}", s)).collect();
if name_split.len() > 1 {
_get_or_create_symbol(
session,
&entry,
source_path.as_str(),
Some(name_symbol.as_ref().unwrap().clone()),
&Vec::from_iter(name_split[1..name_split.len()].iter().cloned()),
None,
None);
}
continue;
}
let name_split: Vec<Yarn> = name.split(".").map(|s| yarn!("{}", s)).collect();
let name_first_part: Vec<Yarn> = Vec::from_iter(name_split[0..name_split.len()-1].iter().cloned());
let name_last_name: Vec<Yarn> = vec![name_split.last().unwrap().clone()];
let name_split: Vec<OYarn> = name.split(".").map(|s| oyarn!("{}", s)).collect();
let name_first_part: Vec<OYarn> = Vec::from_iter(name_split[0..name_split.len()-1].iter().cloned());
let name_last_name: Vec<OYarn> = vec![name_split.last().unwrap().clone()];

// get the full file_tree, including the first part of the name import stmt. (os in import os.path)
let (next_symbol, fallback_sym) = _get_or_create_symbol(
Expand Down Expand Up @@ -182,7 +197,7 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
return result;
}

pub fn find_module(session: &mut SessionInfo, odoo_addons: Rc<RefCell<Symbol>>, name: &Yarn) -> Option<Rc<RefCell<Symbol>>> {
pub fn find_module(session: &mut SessionInfo, odoo_addons: Rc<RefCell<Symbol>>, name: &OYarn) -> Option<Rc<RefCell<Symbol>>> {
let paths = (*odoo_addons).borrow().paths().clone();
for path in paths.iter() {
let full_path = Path::new(path.as_str()).join(name.as_str());
Expand All @@ -208,8 +223,8 @@ pub fn find_module(session: &mut SessionInfo, odoo_addons: Rc<RefCell<Symbol>>,
None
}

fn _resolve_packages(file_path: &String, file_tree: &Tree, file_sym_type: &SymType, level: Option<u32>, from_stmt: Option<&Identifier>) -> Vec<Yarn> {
let mut first_part_tree: Vec<Yarn> = vec![];
fn _resolve_packages(file_path: &String, file_tree: &Tree, file_sym_type: &SymType, level: Option<u32>, from_stmt: Option<&Identifier>) -> Vec<OYarn> {
let mut first_part_tree: Vec<OYarn> = vec![];
if level.is_some() && level.unwrap() > 0 {
let mut lvl = level.unwrap();
if lvl > Path::new(file_path).components().count() as u32 {
Expand All @@ -234,15 +249,15 @@ fn _resolve_packages(file_path: &String, file_tree: &Tree, file_sym_type: &SymTy
Some(from_stmt_inner) => {
let split = from_stmt_inner.as_str().split(".");
for i in split {
first_part_tree.push(yarn!("{}", i));
first_part_tree.push(oyarn!("{}", i));
}
},
None => ()
}
first_part_tree
}

fn _get_or_create_symbol(session: &mut SessionInfo, for_entry: &Rc<RefCell<EntryPoint>>, from_path: &str, symbol: Option<Rc<RefCell<Symbol>>>, names: &Vec<Yarn>, asname: Option<String>, level: Option<u32>) -> (Option<Rc<RefCell<Symbol>>>, Option<Rc<RefCell<Symbol>>>) {
fn _get_or_create_symbol(session: &mut SessionInfo, for_entry: &Rc<RefCell<EntryPoint>>, from_path: &str, symbol: Option<Rc<RefCell<Symbol>>>, names: &Vec<OYarn>, asname: Option<String>, level: Option<u32>) -> (Option<Rc<RefCell<Symbol>>>, Option<Rc<RefCell<Symbol>>>) {
let mut sym: Option<Rc<RefCell<Symbol>>> = symbol.clone();
let mut last_symbol = symbol.clone();
for branch in names.iter() {
Expand Down Expand Up @@ -339,7 +354,7 @@ fn _get_or_create_symbol(session: &mut SessionInfo, for_entry: &Rc<RefCell<Entry
return (sym, last_symbol)
}

fn _resolve_new_symbol(session: &mut SessionInfo, parent: Rc<RefCell<Symbol>>, name: &Yarn, asname: Option<String>) -> Result<Rc<RefCell<Symbol>>, String> {
fn _resolve_new_symbol(session: &mut SessionInfo, parent: Rc<RefCell<Symbol>>, name: &OYarn, asname: Option<String>) -> Result<Rc<RefCell<Symbol>>, String> {
let sym_name: String = match asname {
Some(asname_inner) => asname_inner.clone(),
None => name.to_string()
Expand Down Expand Up @@ -368,6 +383,9 @@ fn _resolve_new_symbol(session: &mut SessionInfo, parent: Rc<RefCell<Symbol>>, n
let _arc_symbol = Symbol::create_from_path(session, &full_path.with_extension("py"), parent.clone(), false);
if _arc_symbol.is_some() {
let _arc_symbol = _arc_symbol.unwrap();
if full_path.ends_with("init.py") {
println!("here");
}
SyncOdoo::rebuild_arch_now(session, &_arc_symbol);
return Ok(_arc_symbol);
}
Expand Down Expand Up @@ -411,7 +429,7 @@ fn _resolve_new_symbol(session: &mut SessionInfo, parent: Rc<RefCell<Symbol>>, n
return Err("Symbol not found".to_string())
}

pub fn get_all_valid_names(session: &mut SessionInfo, source_file_symbol: &Rc<RefCell<Symbol>>, from_stmt: Option<&Identifier>, base_name: String, level: Option<u32>) -> HashSet<Yarn> {
pub fn get_all_valid_names(session: &mut SessionInfo, source_file_symbol: &Rc<RefCell<Symbol>>, from_stmt: Option<&Identifier>, base_name: String, level: Option<u32>) -> HashSet<OYarn> {
//A: search base of different imports
let source_root = source_file_symbol.borrow().get_root().as_ref().unwrap().upgrade().unwrap();
let entry = source_root.borrow().get_entry().unwrap();
Expand Down Expand Up @@ -443,7 +461,7 @@ pub fn get_all_valid_names(session: &mut SessionInfo, source_file_symbol: &Rc<Re
let from_symbol = from_symbol.unwrap();

let mut sym: Option<Rc<RefCell<Symbol>>> = Some(from_symbol.clone());
let mut names = vec![base_name.split(".").map(|s| yarn!("{}", s)).next().unwrap()];
let mut names = vec![base_name.split(".").map(|s| oyarn!("{}", s)).next().unwrap()];
if base_name.ends_with(".") {
names.push(Sy!(""));
}
Expand Down
16 changes: 8 additions & 8 deletions server/src/core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use std::rc::Weak;
use byteyarn::Yarn;
use lsp_types::MessageType;
use weak_table::PtrWeakHashSet;
use std::collections::HashSet;

use crate::constants::BuildStatus;
use crate::constants::BuildSteps;
use crate::constants::OYarn;
use crate::constants::SymType;
use crate::threads::SessionInfo;

Expand All @@ -17,9 +17,9 @@ use super::symbols::symbol::Symbol;

#[derive(Debug)]
pub struct ModelData {
pub name: Yarn,
pub inherit: Vec<Yarn>,
pub inherits: Vec<(Yarn, Yarn)>,
pub name: OYarn,
pub inherit: Vec<OYarn>,
pub inherits: Vec<(OYarn, OYarn)>,

pub description: String,
pub auto: bool,
Expand All @@ -42,7 +42,7 @@ pub struct ModelData {
impl ModelData {
pub fn new() -> Self {
Self {
name: Yarn::new(""),
name: OYarn::from(""),
inherit: Vec::new(),
inherits: Vec::new(),
description: String::new(),
Expand All @@ -67,13 +67,13 @@ impl ModelData {

#[derive(Debug)]
pub struct Model {
name: Yarn,
name: OYarn,
symbols: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
pub dependents: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
}

impl Model {
pub fn new(name: Yarn, symbol: Rc<RefCell<Symbol>>) -> Self {
pub fn new(name: OYarn, symbol: Rc<RefCell<Symbol>>) -> Self {
let mut res = Self {
name,
symbols: PtrWeakHashSet::new(),
Expand Down Expand Up @@ -182,7 +182,7 @@ impl Model {
/* Return all symbols that build this model.
It returns the symbol and an optional string that represents the module name that should be added to dependencies to be used.
*/
pub fn all_symbols(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> Vec<(Rc<RefCell<Symbol>>, Option<Yarn>)> {
pub fn all_symbols(&self, session: &mut SessionInfo, from_module: Option<Rc<RefCell<Symbol>>>) -> Vec<(Rc<RefCell<Symbol>>, Option<OYarn>)> {
let mut symbol = Vec::new();
for s in self.symbols.iter() {
if let Some(from_module) = from_module.as_ref() {
Expand Down
20 changes: 10 additions & 10 deletions server/src/core/odoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::path::{Path, PathBuf};
use std::env;
use std::cmp;
use regex::Regex;
use crate::{constants::*, Sy};
use crate::{constants::*, oyarn, Sy};
use super::config::{DiagMissingImportsMode, RefreshMode};
use super::entry_point::{EntryPoint, EntryPointMgr};
use super::file_mgr::FileMgr;
Expand Down Expand Up @@ -62,12 +62,12 @@ pub struct SyncOdoo {
pub has_main_entry:bool,
pub has_odoo_main_entry: bool,
pub has_valid_python: bool,
pub main_entry_tree: Vec<Yarn>,
pub main_entry_tree: Vec<OYarn>,
pub stubs_dirs: Vec<String>,
pub stdlib_dir: String,
file_mgr: Rc<RefCell<FileMgr>>,
pub modules: HashMap<Yarn, Weak<RefCell<Symbol>>>,
pub models: HashMap<Yarn, Rc<RefCell<Model>>>,
pub modules: HashMap<OYarn, Weak<RefCell<Symbol>>>,
pub models: HashMap<OYarn, Rc<RefCell<Model>>>,
pub interrupt_rebuild: Arc<AtomicBool>,
pub terminate_rebuild: Arc<AtomicBool>,
pub watched_file_updates: Arc<AtomicU32>,
Expand Down Expand Up @@ -324,10 +324,10 @@ impl SyncOdoo {
panic!("Odoo root symbol not found")
};
odoo_sym.borrow_mut().set_is_external(false);
let odoo_odoo = Symbol::create_from_path(session, &config_odoo_path.join("odoo"), odoo_sym.clone(), false);
if odoo_odoo.is_none() {
panic!("Not able to find odoo with given path. Aborting...");
}
//let odoo_odoo = Symbol::create_from_path(session, &config_odoo_path.join("odoo"), odoo_sym.clone(), false);
//Force the creation of "odoo" to be a package, even if it is a namespace
let path_str = config_odoo_path.join("odoo").sanitize();
let odoo_odoo = Some(odoo_sym.borrow_mut().add_new_python_package(session, &S!("odoo"), &path_str));
let odoo_typ = odoo_odoo.as_ref().unwrap().borrow().typ().clone();
match odoo_typ {
SymType::PACKAGE(PackageType::PYTHON_PACKAGE) => {
Expand Down Expand Up @@ -408,7 +408,7 @@ impl SyncOdoo {
for item in PathBuf::from(addon_path).read_dir().expect("Unable to browse and odoo addon directory") {
match item {
Ok(item) => {
if item.file_type().unwrap().is_dir() && !session.sync_odoo.modules.contains_key(&yarn!("{}", item.file_name().to_str().unwrap())) {
if item.file_type().unwrap().is_dir() && !session.sync_odoo.modules.contains_key(&oyarn!("{}", item.file_name().to_str().unwrap())) {
let module_symbol = Symbol::create_from_path(session, &item.path(), addons_symbol.clone(), true);
if module_symbol.is_some() {
session.sync_odoo.add_to_rebuild_arch(module_symbol.unwrap());
Expand Down Expand Up @@ -818,7 +818,7 @@ impl SyncOdoo {
false
}

pub fn is_in_main_entry(session: &mut SessionInfo, path: &Vec<Yarn>) -> bool{
pub fn is_in_main_entry(session: &mut SessionInfo, path: &Vec<OYarn>) -> bool{
path.starts_with(session.sync_odoo.main_entry_tree.as_slice())
}

Expand Down
Loading