Skip to content

Commit

Permalink
LS: Replace RootDatabase with custom LS-specific AnalysisDatabase (#5899
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mkaput committed Jun 26, 2024
1 parent 87e83ff commit cdaceb9
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 102 deletions.
8 changes: 3 additions & 5 deletions crates/cairo-lang-language-server/src/ide/code_actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_syntax::node::SyntaxNode;
use tower_lsp::lsp_types::{
CodeAction, CodeActionOrCommand, CodeActionParams, CodeActionResponse, Diagnostic,
NumberOrString,
};
use tracing::debug;

use crate::lang::db::LsSyntaxGroup;
use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup};
use crate::lang::lsp::{LsProtoGroup, ToCairo};

mod rename_unused_variable;
Expand All @@ -19,7 +17,7 @@ mod rename_unused_variable;
skip_all,
fields(uri = %params.text_document.uri)
)]
pub fn code_actions(params: CodeActionParams, db: &RootDatabase) -> Option<CodeActionResponse> {
pub fn code_actions(params: CodeActionParams, db: &AnalysisDatabase) -> Option<CodeActionResponse> {
let mut actions = Vec::with_capacity(params.context.diagnostics.len());
let file_id = db.file_for_url(&params.text_document.uri)?;
let node = db.find_syntax_node_at_position(file_id, params.range.start.to_cairo())?;
Expand All @@ -46,7 +44,7 @@ pub fn code_actions(params: CodeActionParams, db: &RootDatabase) -> Option<CodeA
///
/// A vector of [`CodeAction`] objects that can be applied to resolve the diagnostic.
fn get_code_actions_for_diagnostic(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
node: &SyntaxNode,
diagnostic: &Diagnostic,
params: &CodeActionParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::collections::HashMap;

use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_syntax::node::SyntaxNode;
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::{CodeAction, Diagnostic, TextEdit, Url, WorkspaceEdit};

use crate::lang::db::AnalysisDatabase;

/// Create a code action that prefixes an unused variable with an `_`.
#[tracing::instrument(level = "trace", skip_all)]
pub fn rename_unused_variable(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
node: &SyntaxNode,
diagnostic: Diagnostic,
uri: Url,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
LanguageElementId, LookupItemId, ModuleFileId, ModuleId, NamedLanguageElementId,
TopLevelLanguageElementId, TraitFunctionId,
};
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::TextOffset;
use cairo_lang_semantic::db::SemanticGroup;
Expand All @@ -19,16 +21,16 @@ use cairo_lang_semantic::types::peel_snapshots;
use cairo_lang_semantic::{ConcreteTypeId, Pattern, TypeLongId};
use cairo_lang_syntax::node::ast::PathSegment;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::LookupIntern;
use cairo_lang_utils::{LookupIntern, Upcast};
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, Position, Range, TextEdit};
use tracing::debug;

use crate::lang::db::LsSemanticGroup;
use crate::lang::db::{AnalysisDatabase, LsSemanticGroup};
use crate::lang::lsp::ToLsp;

#[tracing::instrument(level = "trace", skip_all)]
pub fn generic_completions(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
module_file_id: ModuleFileId,
lookup_items: Vec<LookupItemId>,
) -> Vec<CompletionItem> {
Expand Down Expand Up @@ -106,7 +108,7 @@ fn resolved_generic_item_completion_kind(item: ResolvedGenericItem) -> Completio

#[tracing::instrument(level = "trace", skip_all)]
pub fn colon_colon_completions(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
module_file_id: ModuleFileId,
lookup_items: Vec<LookupItemId>,
segments: Vec<PathSegment>,
Expand Down Expand Up @@ -181,7 +183,7 @@ pub fn colon_colon_completions(

#[tracing::instrument(level = "trace", skip_all)]
pub fn dot_completions(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
file_id: FileId,
lookup_items: Vec<LookupItemId>,
expr: ast::ExprBinary,
Expand Down Expand Up @@ -256,7 +258,7 @@ pub fn dot_completions(
/// Returns a completion item for a method.
#[tracing::instrument(level = "trace", skip_all)]
fn completion_for_method(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
module_id: ModuleId,
trait_function: TraitFunctionId,
position: Position,
Expand Down Expand Up @@ -292,7 +294,7 @@ fn completion_for_method(
/// Checks if a module has a trait in scope.
#[tracing::instrument(level = "trace", skip_all)]
fn module_has_trait(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
module_id: ModuleId,
trait_id: cairo_lang_defs::ids::TraitId,
) -> Option<bool> {
Expand All @@ -310,7 +312,7 @@ fn module_has_trait(
/// Finds all methods that can be called on a type.
#[tracing::instrument(level = "trace", skip_all)]
fn find_methods_for_type(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
mut resolver: Resolver<'_>,
ty: cairo_lang_semantic::TypeId,
stable_ptr: cairo_lang_syntax::node::ids::SyntaxStablePtrId,
Expand Down
9 changes: 4 additions & 5 deletions crates/cairo-lang-language-server/src/ide/completion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_semantic::items::us::get_use_segments;
use cairo_lang_semantic::resolve::AsSegments;
use cairo_lang_syntax::node::ast::PathSegment;
Expand All @@ -10,7 +9,7 @@ use tower_lsp::lsp_types::{CompletionParams, CompletionResponse, CompletionTrigg
use tracing::debug;

use self::completions::{colon_colon_completions, dot_completions, generic_completions};
use crate::lang::db::{LsSemanticGroup, LsSyntaxGroup};
use crate::lang::db::{AnalysisDatabase, LsSemanticGroup, LsSyntaxGroup};
use crate::lang::lsp::{LsProtoGroup, ToCairo};

mod completions;
Expand All @@ -21,7 +20,7 @@ mod completions;
skip_all,
fields(uri = %params.text_document_position.text_document.uri)
)]
pub fn complete(params: CompletionParams, db: &RootDatabase) -> Option<CompletionResponse> {
pub fn complete(params: CompletionParams, db: &AnalysisDatabase) -> Option<CompletionResponse> {
let text_document_position = params.text_document_position;
let file_id = db.file_for_url(&text_document_position.text_document.uri)?;
let mut position = text_document_position.position;
Expand Down Expand Up @@ -63,7 +62,7 @@ enum CompletionKind {
}

#[tracing::instrument(level = "trace", skip_all)]
fn completion_kind(db: &RootDatabase, node: SyntaxNode) -> CompletionKind {
fn completion_kind(db: &AnalysisDatabase, node: SyntaxNode) -> CompletionKind {
debug!("node.kind: {:#?}", node.kind(db));
match node.kind(db) {
SyntaxKind::TerminalDot => {
Expand Down Expand Up @@ -152,7 +151,7 @@ fn completion_kind(db: &RootDatabase, node: SyntaxNode) -> CompletionKind {
}

#[tracing::instrument(level = "trace", skip_all)]
fn completion_kind_from_path_node(db: &RootDatabase, parent: SyntaxNode) -> CompletionKind {
fn completion_kind_from_path_node(db: &AnalysisDatabase, parent: SyntaxNode) -> CompletionKind {
debug!("completion_kind_from_path_node: {}", parent.clone().get_text_without_trivia(db));
let expr = ast::ExprPath::from_syntax_node(db, parent);
debug!("has_tail: {}", expr.has_tail(db));
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-language-server/src/ide/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_formatter::{get_formatted_file, FormatterConfig};
use cairo_lang_parser::db::ParserGroup;
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::{DocumentFormattingParams, Position, Range, TextEdit};
use tracing::error;

use crate::lang::db::AnalysisDatabase;
use crate::lang::lsp::LsProtoGroup;

/// Format a whole document.
Expand All @@ -14,7 +14,7 @@ use crate::lang::lsp::LsProtoGroup;
skip_all,
fields(uri = %params.text_document.uri)
)]
pub fn format(params: DocumentFormattingParams, db: &RootDatabase) -> Option<Vec<TextEdit>> {
pub fn format(params: DocumentFormattingParams, db: &AnalysisDatabase) -> Option<Vec<TextEdit>> {
let file_uri = params.text_document.uri;
let file = db.file_for_url(&file_uri)?;

Expand Down
5 changes: 2 additions & 3 deletions crates/cairo-lang-language-server/src/ide/hover/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cairo_lang_compiler::db::RootDatabase;
use tower_lsp::lsp_types::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind};

use crate::lang::db::LsSyntaxGroup;
use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup};
use crate::lang::lsp::{LsProtoGroup, ToCairo};
use crate::markdown::Markdown;

Expand All @@ -13,7 +12,7 @@ mod render;
skip_all,
fields(uri = %params.text_document_position_params.text_document.uri)
)]
pub fn hover(params: HoverParams, db: &RootDatabase) -> Option<Hover> {
pub fn hover(params: HoverParams, db: &AnalysisDatabase) -> Option<Hover> {
let file_id = db.file_for_url(&params.text_document_position_params.text_document.uri)?;
let position = params.text_document_position_params.position.to_cairo();
let identifier = db.find_identifier_at_position(file_id, position)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_syntax::node::ast::TerminalIdentifier;
use cairo_lang_syntax::node::TypedSyntaxNode;
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::Hover;

use crate::ide::hover::markdown_contents;
use crate::lang::db::AnalysisDatabase;
use crate::lang::inspect::defs::SymbolDef;
use crate::lang::lsp::ToLsp;
use crate::markdown::Markdown;

/// Get declaration and documentation "definition" of an item referred by the given identifier.
#[tracing::instrument(level = "trace", skip_all)]
pub fn definition(
db: &RootDatabase,
db: &AnalysisDatabase,
identifier: &TerminalIdentifier,
file_id: FileId,
) -> Option<Hover> {
Expand Down
16 changes: 8 additions & 8 deletions crates/cairo-lang-language-server/src/ide/hover/render/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::ids::{FunctionWithBodyId, LookupItemId};
use cairo_lang_diagnostics::ToOption;
use cairo_lang_semantic::db::SemanticGroup;
Expand All @@ -8,17 +7,18 @@ use cairo_lang_semantic::Mutability;
use cairo_lang_syntax::node::ast::{Expr, Pattern, TerminalIdentifier};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{SyntaxNode, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
use itertools::Itertools;
use tower_lsp::lsp_types::Hover;

use crate::ide::hover::markdown_contents;
use crate::lang::db::LsSemanticGroup;
use crate::lang::db::{AnalysisDatabase, LsSemanticGroup};
use crate::markdown::Markdown;

/// Legacy hover rendering backported from Cairo 2.6.3 codebase.
///
/// This logic is meant for gradual replacement with new-style hovers and eventually be removed.
pub fn legacy(db: &RootDatabase, identifier: &TerminalIdentifier) -> Option<Hover> {
pub fn legacy(db: &AnalysisDatabase, identifier: &TerminalIdentifier) -> Option<Hover> {
let node = identifier.as_syntax_node();
let lookup_item_id = db.find_lookup_item(&node)?;
let function_id = lookup_item_id.function_with_body()?;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn legacy(db: &RootDatabase, identifier: &TerminalIdentifier) -> Option<Hove

/// If the node is an identifier, retrieves a hover hint for it.
fn get_identifier_hint(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
lookup_item_id: LookupItemId,
node: SyntaxNode,
) -> Option<Markdown> {
Expand All @@ -64,7 +64,7 @@ fn get_identifier_hint(

/// If the node is an expression, retrieves a hover hint for it.
fn get_expr_hint(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
function_id: FunctionWithBodyId,
node: SyntaxNode,
) -> Option<Markdown> {
Expand Down Expand Up @@ -107,7 +107,7 @@ fn get_expr_hint(

/// Returns the semantic expression for the current node.
fn nearest_semantic_expr(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
mut node: SyntaxNode,
function_id: FunctionWithBodyId,
) -> Option<cairo_lang_semantic::Expr> {
Expand All @@ -128,7 +128,7 @@ fn nearest_semantic_expr(

/// If the node is a pattern, retrieves a hover hint for it.
fn get_pattern_hint(
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
function_id: FunctionWithBodyId,
node: SyntaxNode,
) -> Option<Markdown> {
Expand All @@ -139,7 +139,7 @@ fn get_pattern_hint(

/// Returns the semantic pattern for the current node.
fn nearest_semantic_pat(
db: &dyn SemanticGroup,
db: &AnalysisDatabase,
mut node: SyntaxNode,
function_id: FunctionWithBodyId,
) -> Option<cairo_lang_semantic::Pattern> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::{GotoDefinitionParams, GotoDefinitionResponse, Location};

use crate::get_definition_location;
use crate::lang::db::AnalysisDatabase;
use crate::lang::lsp::{LsProtoGroup, ToCairo, ToLsp};

/// Get the definition location of a symbol at a given text document position.
Expand All @@ -13,7 +13,7 @@ use crate::lang::lsp::{LsProtoGroup, ToCairo, ToLsp};
)]
pub fn goto_definition(
params: GotoDefinitionParams,
db: &RootDatabase,
db: &AnalysisDatabase,
) -> Option<GotoDefinitionResponse> {
let file = db.file_for_url(&params.text_document_position_params.text_document.uri)?;
let position = params.text_document_position_params.position.to_cairo();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_filesystem::span::TextOffset;
use cairo_lang_parser::db::ParserGroup;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_syntax as syntax;
use cairo_lang_syntax::node::ast::{self};
use cairo_lang_syntax::node::kind::SyntaxKind;
Expand All @@ -13,6 +11,7 @@ use tracing::error;

use self::encoder::{EncodedToken, TokenEncoder};
pub use self::token_kind::SemanticTokenKind;
use crate::lang::db::AnalysisDatabase;
use crate::lang::lsp::LsProtoGroup;

mod encoder;
Expand All @@ -26,7 +25,7 @@ mod token_kind;
)]
pub fn semantic_highlight_full(
params: SemanticTokensParams,
db: &RootDatabase,
db: &AnalysisDatabase,
) -> Option<SemanticTokensResult> {
let file_uri = params.text_document.uri;
let file = db.file_for_url(&file_uri)?;
Expand All @@ -53,7 +52,7 @@ struct SemanticTokensTraverser {
impl SemanticTokensTraverser {
pub fn find_semantic_tokens(
&mut self,
db: &(dyn SemanticGroup + 'static),
db: &AnalysisDatabase,
data: &mut Vec<SemanticToken>,
node: SyntaxNode,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use cairo_lang_semantic::resolve::{ResolvedConcreteItem, ResolvedGenericItem};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::utils::grandparent_kind;
use cairo_lang_syntax::node::{ast, SyntaxNode, Terminal, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::SemanticTokenType;

use crate::lang::db::LsSemanticGroup;
use crate::lang::db::{AnalysisDatabase, LsSemanticGroup};

#[allow(dead_code)]
pub enum SemanticTokenKind {
Expand All @@ -34,10 +35,7 @@ pub enum SemanticTokenKind {
GenericParamImpl,
}
impl SemanticTokenKind {
pub fn from_syntax_node(
db: &(dyn SemanticGroup + 'static),
mut node: SyntaxNode,
) -> Option<Self> {
pub fn from_syntax_node(db: &AnalysisDatabase, mut node: SyntaxNode) -> Option<Self> {
let syntax_db = db.upcast();
let mut expr_path_ptr = None;
let kind = node.kind(syntax_db);
Expand Down
Loading

0 comments on commit cdaceb9

Please sign in to comment.