Skip to content

refactor: move PgLspPath into pg_fs crate #146

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

Merged
merged 2 commits into from
Nov 23, 2024
Merged
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
59 changes: 59 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ rust-version = "1.71"
line_index = { path = "./lib/line_index", version = "0.0.0" }
tree_sitter_sql = { path = "./lib/tree_sitter_sql", version = "0.0.0" }
tree-sitter = "0.20.10"
tracing = "0.1.40"

# postgres specific crates
pg_lexer = { path = "./crates/pg_lexer", version = "0.0.0" }
pg_fs = { path = "./crates/pg_fs", version = "0.0.0" }
pg_diagnostics = { path = "./crates/pg_diagnostics", version = "0.0.0" }
pg_lexer_codegen = { path = "./crates/pg_lexer_codegen", version = "0.0.0" }
pg_statement_splitter = { path = "./crates/pg_statement_splitter", version = "0.0.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/pg_base_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ text-size = "1.1.1"
line_index.workspace = true

pg_statement_splitter.workspace = true
pg_fs.workspace = true

[dev-dependencies]

Expand Down
3 changes: 2 additions & 1 deletion crates/pg_base_db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ impl DocumentChange {
mod tests {
use text_size::{TextRange, TextSize};

use crate::{change::Change, document::StatementRef, Document, DocumentChange, PgLspPath};
use crate::{change::Change, document::StatementRef, Document, DocumentChange};
use pg_fs::PgLspPath;

#[test]
fn test_document_apply_changes() {
Expand Down
5 changes: 3 additions & 2 deletions crates/pg_base_db/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{hash::Hash, hash::Hasher, ops::RangeBounds};
use line_index::LineIndex;
use text_size::{TextRange, TextSize};

use crate::PgLspPath;
use pg_fs::PgLspPath;

extern crate test;

Expand Down Expand Up @@ -163,8 +163,9 @@ impl Document {
mod tests {

use text_size::{TextRange, TextSize};
use pg_fs::PgLspPath;

use crate::{Document, PgLspPath};
use crate::{Document};

#[test]
fn test_statements_at_range() {
Expand Down
2 changes: 0 additions & 2 deletions crates/pg_base_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

mod change;
mod document;
mod path;

pub use change::{Change, ChangedStatement, DocumentChange, StatementChange};
pub use document::{Document, StatementRef};
pub use path::PgLspPath;
15 changes: 15 additions & 0 deletions crates/pg_fs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pg_fs"
version = "0.0.0"
edition = "2021"

[dependencies]
directories = "5.0.1"
tracing = { workspace = true }

[dev-dependencies]

[lib]
doctest = false

[features]
21 changes: 21 additions & 0 deletions crates/pg_fs/src/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use directories::ProjectDirs;
use std::{env, fs, path::PathBuf};
use tracing::warn;

pub fn ensure_cache_dir() -> PathBuf {
if let Some(proj_dirs) = ProjectDirs::from("dev", "supabase-community", "pglsp") {
// Linux: /home/alice/.cache/biome
// Win: C:\Users\Alice\AppData\Local\biomejs\biome\cache
// Mac: /Users/Alice/Library/Caches/dev.biomejs.biome
let cache_dir = proj_dirs.cache_dir().to_path_buf();
if let Err(err) = fs::create_dir_all(&cache_dir) {
let temp_dir = env::temp_dir();
warn!("Failed to create local cache directory {cache_dir:?} due to error: {err}, fallback to {temp_dir:?}");
temp_dir
} else {
cache_dir
}
} else {
env::temp_dir()
}
}
8 changes: 8 additions & 0 deletions crates/pg_fs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! # pg_fs

mod dir;
mod path;

pub use dir::ensure_cache_dir;
pub use path::PgLspPath;

3 changes: 2 additions & 1 deletion crates/pg_base_db/src/path.rs → crates/pg_fs/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ops::Deref, path::PathBuf};

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct PgLspPath {
path: PathBuf,
}
Expand All @@ -20,3 +20,4 @@ impl PgLspPath {
}
}
}

1 change: 1 addition & 0 deletions crates/pg_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ text-size = "1.1.1"
line_index.workspace = true

pg_hover.workspace = true
pg_fs.workspace = true
pg_completions.workspace = true
pg_inlay_hints.workspace = true
pg_commands.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/pg_lsp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod to_proto;
use std::path::PathBuf;

use lsp_types;
use pg_base_db::PgLspPath;
use pg_fs::PgLspPath;

/// Convert a `lsp_types::Url` to a `PgLspPath`.
pub(crate) fn file_path(url: &lsp_types::Url) -> PgLspPath {
Expand Down
3 changes: 2 additions & 1 deletion crates/pg_lsp/src/utils/line_index_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl LineIndexExt for LineIndex {

#[cfg(test)]
mod tests {
use pg_base_db::{Document, PgLspPath};
use pg_base_db::Document;
use pg_fs::PgLspPath;
use text_size::{TextRange, TextSize};

use crate::utils::line_index_ext::LineIndexExt;
Expand Down
1 change: 1 addition & 0 deletions crates/pg_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ async-std = "1.12.0"
sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] }

pg_base_db.workspace = true
pg_fs.workspace = true
pg_diagnostics.workspace = true
pg_query_ext.workspace = true
pg_lint.workspace = true
Expand Down
6 changes: 4 additions & 2 deletions crates/pg_workspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::sync::{RwLock, RwLockWriteGuard};

use dashmap::{DashMap, DashSet};
use lint::Linter;
use pg_base_db::{Document, DocumentChange, PgLspPath, StatementRef};
use pg_base_db::{Document, DocumentChange, StatementRef};
use pg_fs::PgLspPath;
use pg_query::PgQueryParser;
use pg_schema_cache::SchemaCache;
use sqlx::PgPool;
Expand Down Expand Up @@ -184,7 +185,8 @@ mod tests {
use pg_diagnostics::Diagnostic;
use text_size::{TextRange, TextSize};

use crate::{PgLspPath, Workspace};
use crate::Workspace;
use pg_fs::PgLspPath;

#[test]
fn test_apply_change() {
Expand Down
Loading