Skip to content
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
8 changes: 8 additions & 0 deletions crates/completion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "completion"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
14 changes: 14 additions & 0 deletions crates/completion/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
16 changes: 16 additions & 0 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "database"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
fst = "0.4.7"
lsp-types = "0.93.1"
salsa = "0.17.0-pre.2"
tree-sitter = "0.20.9"

types = {path = "../types", version = "0.1.0"}
queries = { path = "../queries", version = "0.1.0" }
parking_lot = "0.12.1"
File renamed without changes.
36 changes: 36 additions & 0 deletions crates/database/src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Here we have our database

use std::fmt::Debug;

use lsp_types::Url;
use types::SourceFile;

// input query group
#[salsa::query_group(SourceDatabaseStorage)]
pub trait SourceDatabase: salsa::Database {
// query
#[salsa::input]
fn source(&self, name: Url) -> SourceFile;
}

#[salsa::database(SourceDatabaseStorage)]
#[derive(Default)]
pub struct RootDatabase {
pub storage: salsa::Storage<Self>,
}

impl salsa::Database for RootDatabase {}

impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(RootDatabase {
storage: self.storage.snapshot(),
})
}
}

impl Debug for RootDatabase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RootDatabase").finish()
}
}
4 changes: 4 additions & 0 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod db;
mod state;

pub use self::{db::*, state::*};
56 changes: 56 additions & 0 deletions crates/database/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{collections::HashMap, sync::Arc};

use lsp_types::{Diagnostic, Url};
use parking_lot::Mutex;
use queries::errors::build_diagnostics;
use salsa::ParallelDatabase;
use tree_sitter::Tree;
use types::SourceFile;

use crate::db::{RootDatabase, SourceDatabase};

// Here is our global state
#[derive(Default, Debug)]
pub struct GlobalState {
pub database: RootDatabase,

// store all the asts into a hashmap
pub asts: Arc<Mutex<HashMap<Url, Tree>>>,

// We store the diagnostics in a hashmap for fast lookup
pub diagnostics: Arc<Mutex<HashMap<Url, Vec<Diagnostic>>>>,
}

impl GlobalState {
pub fn new() -> Self {
Self::default()
}

// store the content of the file in the database
pub fn set_source_inputs(&mut self, url: Url, content: SourceFile) {
self.database.set_source(url, content);
}

pub fn update_diagnostics(&mut self, url: Url) {
let binding = self.asts.lock();
let root_node = binding.get(&url).unwrap().root_node();
self.diagnostics.lock().insert(
url.clone(),
build_diagnostics(self.database.source(url).text.into_bytes(), &root_node),
);
}

pub fn snapshot(&self) -> GlobalStateSnapshot {
GlobalStateSnapshot {
db: self.database.snapshot(),
asts: Arc::clone(&self.asts),
diagnostics: Arc::clone(&self.diagnostics),
}
}
}

pub struct GlobalStateSnapshot {
pub db: salsa::Snapshot<RootDatabase>,
pub asts: Arc<Mutex<HashMap<Url, Tree>>>,
pub diagnostics: Arc<Mutex<HashMap<Url, Vec<Diagnostic>>>>,
}
3 changes: 3 additions & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ libloading = "0.7.2"

queries = { path = "../queries", version = "0.1.0" }
helper = { path = "../helper", version = "0.1.0" }
database = { path = "../database", version = "0.1.0" }
types = { path = "../types", version = "0.1.0" }


4 changes: 0 additions & 4 deletions crates/server/src/global_state/mod.rs

This file was deleted.

153 changes: 0 additions & 153 deletions crates/server/src/global_state/state.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/server/src/handler/completion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use database::GlobalState;
use helper::types::Symbol;
use log::debug;
use lsp_server::{ErrorCode::InternalError, RequestId, Response};
Expand All @@ -6,8 +7,6 @@ use lsp_types::{
};
use queries::utils::get_smallest_scope_id_by_position;

use crate::global_state::GlobalState;

pub fn completion(id: RequestId, params: CompletionParams, state: GlobalState) -> Response {
debug!("got completion request #{}: {:?}", id, params);

Expand Down
Loading