Skip to content

Commit 4fc6161

Browse files
committed
[IMP] server: refactor ast_indexes into new node_index support from Ruff
Drop our home-made ast_indexes that help us indexing statements and use built-in node_indexes that are now directly on Nodes in last version of Ruff_python_ast.
1 parent 411c63b commit 4fc6161

15 files changed

+334
-192
lines changed

server/src/core/file_mgr.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use lsp_types::notification::{Notification, PublishDiagnostics};
22
use ropey::Rope;
3-
use ruff_python_ast::Mod;
3+
use ruff_python_ast::{AnyRootNodeRef, Mod, ModModule, PySourceType, Stmt};
44
use ruff_python_parser::{Mode, ParseOptions, Parsed, Token, TokenKind};
55
use lsp_types::{Diagnostic, DiagnosticSeverity, MessageType, NumberOrString, Position, PublishDiagnosticsParams, Range, TextDocumentContentChangeEvent};
66
use tracing::{error, warn};
@@ -9,9 +9,11 @@ use std::collections::HashSet;
99
use std::hash::{Hash, Hasher};
1010
use std::path::PathBuf;
1111
use std::str::FromStr;
12+
use std::sync::Arc;
1213
use std::{collections::HashMap, fs};
1314
use crate::core::config::DiagnosticFilter;
1415
use crate::core::diagnostics::{create_diagnostic, DiagnosticCode, DiagnosticSetting};
16+
use crate::features::node_index_ast::IndexedModule;
1517
use crate::threads::SessionInfo;
1618
use crate::utils::PathSanitizer;
1719
use std::rc::Rc;
@@ -58,10 +60,16 @@ pub enum AstType {
5860
pub struct FileInfoAst {
5961
pub text_hash: u64,
6062
pub text_rope: Option<ropey::Rope>,
61-
pub ast: Option<Vec<ruff_python_ast::Stmt>>,
63+
pub indexed_module: Option<Arc<IndexedModule>>,
6264
pub ast_type: AstType,
6365
}
6466

67+
impl FileInfoAst {
68+
pub fn get_stmts(&self) -> Option<&Vec<Stmt>> {
69+
self.indexed_module.as_ref().map(|module| &module.parsed.syntax().body)
70+
}
71+
}
72+
6573
#[derive(Debug)]
6674
pub struct FileInfo {
6775
pub version: i32,
@@ -87,7 +95,7 @@ impl FileInfo {
8795
file_info_ast: Rc::new(RefCell::new(FileInfoAst {
8896
text_hash: 0,
8997
text_rope: None,
90-
ast: None,
98+
indexed_module: None,
9199
ast_type: AstType::Python,
92100
})),
93101
diagnostics: HashMap::new(),
@@ -159,14 +167,20 @@ impl FileInfo {
159167
let content = &fia.text_rope.as_ref().unwrap().slice(..);
160168
let source = content.to_string(); //cast to string to get a version with all changes
161169
drop(fia);
162-
let ast = ruff_python_parser::parse_unchecked(source.as_str(), ParseOptions::from(Mode::Module));
170+
let mut python_source_type = PySourceType::Python;
171+
if self.uri.ends_with(".pyi") {
172+
python_source_type = PySourceType::Stub;
173+
} else if self.uri.ends_with(".ipynb") {
174+
python_source_type = PySourceType::Ipynb;
175+
}
176+
let parsed_module = ruff_python_parser::parse_unchecked_source(source.as_str(), python_source_type);
163177
if in_workspace {
164178
self.noqas_blocs.clear();
165179
self.noqas_lines.clear();
166-
self.extract_tokens(&ast, &source);
180+
self.extract_tokens(&parsed_module, &source);
167181
}
168182
self.valid = true;
169-
for error in ast.errors().iter() {
183+
for error in parsed_module.errors().iter() {
170184
self.valid = false;
171185
if let Some(diagnostic_base) = create_diagnostic(&session, DiagnosticCode::OLS01000, &[]) {
172186
diagnostics.push(Diagnostic {
@@ -179,15 +193,7 @@ impl FileInfo {
179193
});
180194
}
181195
}
182-
match ast.into_syntax() {
183-
Mod::Expression(_expr) => {
184-
warn!("No support for expression-file only");
185-
self.file_info_ast.borrow_mut().ast = None
186-
},
187-
Mod::Module(module) => {
188-
self.file_info_ast.borrow_mut().ast = Some(module.body);
189-
}
190-
}
196+
self.file_info_ast.borrow_mut().indexed_module = Some(IndexedModule::new(parsed_module));
191197
self.replace_diagnostics(BuildSteps::SYNTAX, diagnostics);
192198
}
193199

@@ -209,11 +215,11 @@ impl FileInfo {
209215
self._build_ast(session, session.sync_odoo.get_file_mgr().borrow().is_in_workspace(&self.uri));
210216
}
211217

212-
pub fn extract_tokens(&mut self, ast: &Parsed<Mod>, source: &String) {
218+
pub fn extract_tokens(&mut self, parsed_module: &Parsed<ModModule>, source: &String) {
213219
let mut is_first_expr: bool = true;
214220
let mut noqa_to_add = None;
215221
let mut previous_token: Option<&Token> = None;
216-
for token in ast.tokens().iter() {
222+
for token in parsed_module.tokens().iter() {
217223
match token.kind() {
218224
TokenKind::Comment => {
219225
let text = &source[token.range()];

server/src/core/odoo.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,13 +1172,13 @@ impl Odoo {
11721172
if let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(session, &PathBuf::from(path.clone())) {
11731173
let file_info = session.sync_odoo.get_file_mgr().borrow_mut().get_file_info(&path);
11741174
if let Some(file_info) = file_info {
1175-
if file_info.borrow().file_info_ast.borrow().ast.is_none() {
1175+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_none() {
11761176
file_info.borrow_mut().prepare_ast(session);
11771177
}
11781178
let ast_type = file_info.borrow().file_info_ast.borrow().ast_type.clone();
11791179
match ast_type {
11801180
AstType::Python => {
1181-
if file_info.borrow_mut().file_info_ast.borrow().ast.is_some() {
1181+
if file_info.borrow_mut().file_info_ast.borrow().indexed_module.is_some() {
11821182
return Ok(HoverFeature::hover_python(session, &file_symbol, &file_info, params.text_document_position_params.position.line, params.text_document_position_params.position.character));
11831183
}
11841184
},
@@ -1209,13 +1209,13 @@ impl Odoo {
12091209
if let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(session, &PathBuf::from(path.clone())) {
12101210
let file_info = session.sync_odoo.get_file_mgr().borrow().get_file_info(&path);
12111211
if let Some(file_info) = file_info {
1212-
if file_info.borrow().file_info_ast.borrow().ast.is_none() {
1212+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_none() {
12131213
file_info.borrow_mut().prepare_ast(session);
12141214
}
12151215
let ast_type = file_info.borrow().file_info_ast.borrow().ast_type.clone();
12161216
match ast_type {
12171217
AstType::Python => {
1218-
if file_info.borrow().file_info_ast.borrow().ast.is_some() {
1218+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_some() {
12191219
return Ok(DefinitionFeature::get_location(session, &file_symbol, &file_info, params.text_document_position_params.position.line, params.text_document_position_params.position.character));
12201220
}
12211221
},
@@ -1246,13 +1246,13 @@ impl Odoo {
12461246
if let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(session, &PathBuf::from(path.clone())) {
12471247
let file_info = session.sync_odoo.get_file_mgr().borrow_mut().get_file_info(&path);
12481248
if let Some(file_info) = file_info {
1249-
if file_info.borrow().file_info_ast.borrow().ast.is_none() {
1249+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_none() {
12501250
file_info.borrow_mut().prepare_ast(session);
12511251
}
12521252
let ast_type = file_info.borrow().file_info_ast.borrow().ast_type.clone();
12531253
match ast_type {
12541254
AstType::Python => {
1255-
if file_info.borrow_mut().file_info_ast.borrow().ast.is_some() {
1255+
if file_info.borrow_mut().file_info_ast.borrow().indexed_module.is_some() {
12561256
return Ok(ReferenceFeature::get_references(session, &file_symbol, &file_info, params.text_document_position.position.line, params.text_document_position.position.character));
12571257
}
12581258
},
@@ -1284,10 +1284,10 @@ impl Odoo {
12841284
if let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(session, &PathBuf::from(path.clone())) {
12851285
let file_info = session.sync_odoo.get_file_mgr().borrow_mut().get_file_info(&path);
12861286
if let Some(file_info) = file_info {
1287-
if file_info.borrow().file_info_ast.borrow().ast.is_none() {
1287+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_none() {
12881288
file_info.borrow_mut().prepare_ast(session);
12891289
}
1290-
if file_info.borrow_mut().file_info_ast.borrow().ast.is_some() {
1290+
if file_info.borrow_mut().file_info_ast.borrow().indexed_module.is_some() {
12911291
return Ok(CompletionFeature::autocomplete(session, &file_symbol, &file_info, params.text_document_position.position.line, params.text_document_position.position.character));
12921292
}
12931293
}
@@ -1598,7 +1598,7 @@ impl Odoo {
15981598
if uri.ends_with(".py") || uri.ends_with(".pyi") || uri.ends_with(".xml") || uri.ends_with(".csv") {
15991599
let file_info = session.sync_odoo.get_file_mgr().borrow().get_file_info(&path);
16001600
if let Some(file_info) = file_info {
1601-
if file_info.borrow().file_info_ast.borrow().ast.is_none() {
1601+
if file_info.borrow().file_info_ast.borrow().indexed_module.is_none() {
16021602
file_info.borrow_mut().prepare_ast(session);
16031603
}
16041604
return Ok(DocumentSymbolFeature::get_symbols(session, &file_info));

0 commit comments

Comments
 (0)