Skip to content
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

Migrate the LS to per-crate compiler plugins #42

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
196 changes: 136 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

70 changes: 38 additions & 32 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[package]
name = "cairo-language-server"
version = "2.9.1"
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 Expand Up @@ -86,34 +92,34 @@ test-log = "0.2.16"
# on some of them directly.
# This ensures no duplicate instances of Cairo crates are pulled in by mistake.
[patch.crates-io]
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-doc = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-eq-solver = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-formatter = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-parser = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-proc-macros = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-project = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-sierra-type-size = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-syntax-codegen = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-test-plugin = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", rev = "0b86ece404b0922b76caca5d07a94ed41407f174" }
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-doc = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-eq-solver = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-formatter = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-parser = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-proc-macros = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-project = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-sierra-type-size = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-syntax-codegen = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-test-plugin = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", branch = "spr/main/f0989263" }

# The profile used for CI in pull requests.
# External dependencies are built with optimisation enabled,
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
Loading