Skip to content

Commit

Permalink
Migrated the LS to per-crate plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Dec 20, 2024
1 parent 2304720 commit 10ad607
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 98 deletions.
79 changes: 77 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ name = "cairo-language-server"
version = "2.9.2"
edition = "2021"

authors = ["Software Mansion <[email protected]>", "StarkWare <[email protected]>"]
authors = [
"Software Mansion <[email protected]>",
"StarkWare <[email protected]>",
]
description = "The Cairo Language Server"
license = "Apache-2.0"
repository = "https://github.com/software-mansion/cairols"
Expand Down Expand Up @@ -48,7 +51,10 @@ cairo-lang-test-plugin = "*"
cairo-lang-utils = "*"
convert_case = "0.6.0"
crossbeam = "0.8.4"
governor = { version = "0.7.0", default-features = false, features = ["std", "quanta"] }
governor = { version = "0.7.0", default-features = false, features = [
"std",
"quanta",
] }
indent = "0.1.1"
indoc = "2"
itertools = "0.13.0"
Expand All @@ -57,7 +63,7 @@ lsp-server = "0.7.7"
lsp-types = "=0.95.0"
salsa = { package = "rust-analyzer-salsa", version = "0.17.0-pre.6" }
scarb-metadata = "1.13"
scarb-proc-macro-server-types = "0.1"
scarb-proc-macro-server-types = { git = "https://github.com/software-mansion/scarb", branch = "integraledelebesgue/crate-plugins" }
scarb-stable-hash = "1"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1.0.116"
Expand Down
10 changes: 9 additions & 1 deletion src/ide/hover/render/definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::plugin::InlineMacroExprPlugin;
use cairo_lang_doc::db::DocGroup;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_syntax::node::TypedSyntaxNode;
Expand Down Expand Up @@ -45,8 +46,15 @@ pub fn definition(

SymbolDef::Variable(var) => fenced_code_block(&var.signature(db)),
SymbolDef::ExprInlineMacro(macro_name) => {
let crate_id = db.file_modules(file_id).ok()?.first()?.owning_crate(db);

let mut md = fenced_code_block(macro_name);
if let Some(doc) = db.inline_macro_plugins().get(macro_name)?.documentation() {
if let Some(doc) = db
.crate_inline_macro_plugins(crate_id)
.get(macro_name)
.map(|&id| db.lookup_intern_inline_macro_plugin(id))?
.documentation()
{
md += RULE;
md += &doc;
}
Expand Down
16 changes: 11 additions & 5 deletions src/ide/macros/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::VecDeque;
use std::sync::Arc;

use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::plugin::MacroPluginMetadata;
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin, MacroPluginMetadata};
use cairo_lang_diagnostics::DiagnosticsBuilder;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::{FileId, FileKind, FileLongId, VirtualFile};
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn expand_macro(db: &AnalysisDatabase, params: &TextDocumentPositionParams)

let metadata = MacroPluginMetadata {
cfg_set: &cfg_set,
declared_derives: &db.declared_derives(),
declared_derives: &db.declared_derives(crate_id),
allowed_features: &Default::default(),
edition,
};
Expand Down Expand Up @@ -98,11 +98,14 @@ fn expanded_macro_files(
let mut module_queue = VecDeque::from([(module_file, vec![item])]);
let mut files = VecDeque::new();

let crate_id = db.file_modules(module_file).ok()?.first()?.owning_crate(db);

while let Some((module_file, item_asts)) = module_queue.pop_front() {
files.push_back(module_file);

for item_ast in item_asts {
for plugin in db.macro_plugins() {
for &plugin_id in db.crate_macro_plugins(crate_id).iter() {
let plugin = db.lookup_intern_macro_plugin(plugin_id);
let result = plugin.generate_code(db.upcast(), item_ast.clone(), metadata);

if let Some(generated) = result.code {
Expand Down Expand Up @@ -251,7 +254,9 @@ fn expand_inline_macros_in_single_file(
output: &mut String,
mut config: FileProcessorConfig,
) -> Option<()> {
let plugins = db.inline_macro_plugins();
let crate_id = db.file_modules(file).ok()?.first()?.owning_crate(db);

let plugins = db.crate_inline_macro_plugins(crate_id);

if config.macros.is_empty() {
append_file_with_header(db, file, &config.content, output);
Expand All @@ -260,7 +265,8 @@ fn expand_inline_macros_in_single_file(
for node in config.macros.into_iter().rev() {
let inline_macro = ExprInlineMacro::from_syntax_node(db, node.clone());
let code = plugins
.get(&inline_macro.path(db).as_syntax_node().get_text_without_trivia(db))?
.get(&inline_macro.path(db).as_syntax_node().get_text_without_trivia(db))
.map(|&id| db.lookup_intern_inline_macro_plugin(id))?
.generate_code(db, &inline_macro, metadata)
.code?
.content;
Expand Down
Loading

0 comments on commit 10ad607

Please sign in to comment.