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

feat: put db con behind feature gate #268

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
49 changes: 44 additions & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/pgt_analyse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ version = "0.0.0"
pgt_console.workspace = true
pgt_diagnostics.workspace = true
pgt_query_ext.workspace = true
pgt_schema_cache.workspace = true
rustc-hash = { workspace = true }

biome_deserialize = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pgt_flags = { workspace = true }
pgt_fs = { workspace = true }
pgt_lsp = { workspace = true }
pgt_text_edit = { workspace = true }
pgt_workspace = { workspace = true }
pgt_workspace = { workspace = true, features = ["db-connection"] }
quick-junit = "0.5.0"
rayon = { workspace = true }
rustc-hash = { workspace = true }
Expand Down
16 changes: 2 additions & 14 deletions crates/pgt_completions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@ name = "pgt_completions"
repository.workspace = true
version = "0.0.0"


[dependencies]
async-std = "1.12.0"

pgt_text_size.workspace = true


pgt_schema_cache.workspace = true
pgt_text_size.workspace = true
pgt_treesitter_queries.workspace = true
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tree-sitter.workspace = true
tree_sitter_sql.workspace = true

sqlx.workspace = true

tokio = { version = "1.41.1", features = ["full"] }

[dev-dependencies]
pgt_test_utils.workspace = true
tokio = { version = "1.41.1", features = ["full"] }

[lib]
doctest = false

[features]
schema = ["dep:schemars"]
6 changes: 2 additions & 4 deletions crates/pgt_completions/src/complete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use pgt_text_size::TextSize;
use serde::{Deserialize, Serialize};

use crate::{
builder::CompletionBuilder,
Expand All @@ -18,10 +17,9 @@ pub struct CompletionParams<'a> {
pub tree: Option<&'a tree_sitter::Tree>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Default)]
pub struct CompletionResult {
pub(crate) items: Vec<CompletionItem>,
pub items: Vec<CompletionItem>,
}

impl IntoIterator for CompletionResult {
Expand Down
11 changes: 3 additions & 8 deletions crates/pgt_completions/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[derive(Debug, PartialEq, Eq)]
pub enum CompletionItemKind {
Table,
Function,
Column,
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug)]
pub struct CompletionItem {
pub label: String,
pub(crate) score: i32,
pub score: i32,
pub description: String,
pub preselected: bool,
pub kind: CompletionItemKind,
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pgt_fs = { workspace = true }
pgt_lsp_converters = { workspace = true }
pgt_text_edit = { workspace = true }
pgt_text_size.workspace = true
pgt_workspace = { workspace = true }
pgt_workspace = { workspace = true, features = ["db-connection"] }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
12 changes: 8 additions & 4 deletions crates/pgt_lsp/src/handlers/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ pub fn get_completions(
}

fn to_lsp_types_completion_item_kind(
pg_comp_kind: pgt_completions::CompletionItemKind,
pg_comp_kind: pgt_workspace::workspace::CompletionItemKind,
) -> lsp_types::CompletionItemKind {
match pg_comp_kind {
pgt_completions::CompletionItemKind::Function => lsp_types::CompletionItemKind::FUNCTION,
pgt_completions::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS,
pgt_completions::CompletionItemKind::Column => lsp_types::CompletionItemKind::FIELD,
pgt_workspace::workspace::CompletionItemKind::Function => {
lsp_types::CompletionItemKind::FUNCTION
}
pgt_workspace::workspace::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS,
pgt_workspace::workspace::CompletionItemKind::Column => {
lsp_types::CompletionItemKind::FIELD
}
}
}
44 changes: 44 additions & 0 deletions crates/pgt_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
authors.workspace = true
categories.workspace = true
description = "<DESCRIPTION>"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "pgt_wasm"
repository.workspace = true
version = "0.0.0"

[dependencies]
pgt_console = { workspace = true }
pgt_diagnostics = { workspace = true }
pgt_workspace = { workspace = true }
js-sys = "0.3.72"
serde = { workspace = true }
serde-wasm-bindgen = "0.6.5"
wasm-bindgen = { version = "0.2.95", features = ["serde-serialize"] }
getrandom = { version = "0.2.15", features = ["js"] }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }

[build-dependencies]
biome_js_factory = { workspace = true }
biome_rowan = { workspace = true }
biome_js_formatter = { workspace = true }
pgt_workspace = { workspace = true, features = ["schema"] }
quote = "1.0.14"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[package.metadata.wasm-pack.profile.profiling]
wasm-opt = false

102 changes: 102 additions & 0 deletions crates/pgt_wasm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::{env, fs, io, path::PathBuf};

use quote::{format_ident, quote};

use biome_js_factory::syntax::JsFileSource;
use biome_js_factory::{
make,
syntax::{AnyJsDeclaration, AnyJsModuleItem, AnyJsStatement},
};
use biome_js_formatter::{context::JsFormatOptions, format_node};
use biome_rowan::AstNode;
use pgt_workspace::workspace_types::{generate_type, methods, ModuleQueue};

fn main() -> io::Result<()> {
let methods = methods();

let mut items = Vec::new();
let mut queue = ModuleQueue::default();

for method in &methods {
generate_type(&mut items, &mut queue, &method.params);
generate_type(&mut items, &mut queue, &method.result);
}

let module = make::js_module(
make::js_directive_list(None),
make::js_module_item_list(items.into_iter().map(|(decl, _)| {
AnyJsModuleItem::AnyJsStatement(match decl {
AnyJsDeclaration::JsClassDeclaration(decl) => {
AnyJsStatement::JsClassDeclaration(decl)
}
AnyJsDeclaration::JsFunctionDeclaration(decl) => {
AnyJsStatement::JsFunctionDeclaration(decl)
}
AnyJsDeclaration::JsVariableDeclaration(decl) => {
AnyJsStatement::JsVariableStatement(make::js_variable_statement(decl).build())
}
AnyJsDeclaration::TsDeclareFunctionDeclaration(decl) => {
AnyJsStatement::TsDeclareFunctionDeclaration(decl)
}
AnyJsDeclaration::TsEnumDeclaration(decl) => {
AnyJsStatement::TsEnumDeclaration(decl)
}
AnyJsDeclaration::TsExternalModuleDeclaration(decl) => {
AnyJsStatement::TsExternalModuleDeclaration(decl)
}
AnyJsDeclaration::TsGlobalDeclaration(decl) => {
AnyJsStatement::TsGlobalDeclaration(decl)
}
AnyJsDeclaration::TsImportEqualsDeclaration(decl) => {
AnyJsStatement::TsImportEqualsDeclaration(decl)
}
AnyJsDeclaration::TsInterfaceDeclaration(decl) => {
AnyJsStatement::TsInterfaceDeclaration(decl)
}
AnyJsDeclaration::TsModuleDeclaration(decl) => {
AnyJsStatement::TsModuleDeclaration(decl)
}
AnyJsDeclaration::TsTypeAliasDeclaration(decl) => {
AnyJsStatement::TsTypeAliasDeclaration(decl)
}
})
})),
make::eof(),
)
.build();

// Wasm-bindgen will paste the generated TS code as-is into the final .d.ts file,
// ensure it looks good by running it through the formatter
let formatted = format_node(JsFormatOptions::new(JsFileSource::ts()), module.syntax()).unwrap();
let printed = formatted.print().unwrap();
let definitions = printed.into_code();

// Generate wasm-bindgen extern type imports for all the types defined in the TS code
let types = queue.visited().iter().map(|name| {
let ident = format_ident!("I{name}");
quote! {
#[wasm_bindgen(typescript_type = #name)]
#[allow(non_camel_case_types)]
pub type #ident;
}
});

let tokens = quote! {
#[wasm_bindgen(typescript_custom_section)]
const TS_TYPEDEFS: &'static str = #definitions;

#[wasm_bindgen]
extern "C" {
#( #types )*
}
};

let out_dir = env::var("OUT_DIR").unwrap();
fs::write(
PathBuf::from(out_dir).join("ts_types.rs"),
tokens.to_string(),
)?;

Ok(())
}

Loading
Loading