Skip to content

Commit 8a0cf60

Browse files
init
1 parent ff3ae5a commit 8a0cf60

File tree

14 files changed

+194
-210
lines changed

14 files changed

+194
-210
lines changed

.cargo/config.toml

-3
This file was deleted.

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgt_configuration/src/database.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use biome_deserialize::StringSet;
12
use biome_deserialize_macros::{Merge, Partial};
23
use bpaf::Bpaf;
34
use serde::{Deserialize, Serialize};
@@ -28,6 +29,9 @@ pub struct DatabaseConfiguration {
2829
#[partial(bpaf(long("database")))]
2930
pub database: String,
3031

32+
#[partial(bpaf(long("allow_statement_executions_against")))]
33+
pub allow_statement_executions_against: StringSet,
34+
3135
/// The connection timeout in seconds.
3236
#[partial(bpaf(long("conn_timeout_secs"), fallback(Some(10)), debug_fallback))]
3337
pub conn_timeout_secs: u16,
@@ -41,6 +45,7 @@ impl Default for DatabaseConfiguration {
4145
username: "postgres".to_string(),
4246
password: "postgres".to_string(),
4347
database: "postgres".to_string(),
48+
allow_statement_executions_against: Default::default(),
4449
conn_timeout_secs: 10,
4550
}
4651
}

crates/pgt_configuration/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl PartialConfiguration {
111111
password: Some("postgres".to_string()),
112112
database: Some("postgres".to_string()),
113113
conn_timeout_secs: Some(10),
114+
allow_statement_executions_against: Default::default(),
114115
}),
115116
}
116117
}

crates/pgt_workspace/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pgt_schema_cache = { workspace = true }
2828
pgt_statement_splitter = { workspace = true }
2929
pgt_text_size.workspace = true
3030
pgt_typecheck = { workspace = true }
31+
pgt_commands = { workspace = true }
3132
rustc-hash = { workspace = true }
3233
schemars = { workspace = true, optional = true }
3334
serde = { workspace = true, features = ["derive"] }
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use pgt_configuration::RuleSelector;
2+
use pgt_fs::PgTPath;
3+
use pgt_text_size::TextRange;
4+
5+
use crate::Workspace;
6+
7+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
8+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9+
pub struct CodeActionsParams {
10+
pub path: PgTPath,
11+
pub range: Option<TextRange>,
12+
pub only: Vec<RuleSelector>,
13+
pub skip: Vec<RuleSelector>,
14+
}
15+
16+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
17+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
18+
pub struct CodeActionsResult {
19+
pub actions: Vec<CodeAction>,
20+
}
21+
22+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
23+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24+
#[serde(rename_all = "camelCase")]
25+
pub struct CodeAction {
26+
pub category: CodeActionCategory,
27+
}
28+
29+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
30+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
31+
pub enum CodeActionCategory {
32+
ExecuteCommand(String),
33+
}
34+
35+
impl CodeActionCategory {
36+
fn perform(self, workspace: &dyn Workspace) -> Result<(), String> {
37+
match self {
38+
Self::ExecuteCommand(stmt) => {}
39+
}
40+
Ok(())
41+
}
42+
}

crates/pgt_workspace/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::{Deref, DerefMut};
33
use pgt_console::Console;
44
use pgt_fs::{FileSystem, OsFileSystem};
55

6+
mod code_actions;
67
pub mod configuration;
78
pub mod diagnostics;
89
pub mod dome;

crates/pgt_workspace/src/workspace.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{panic::RefUnwindSafe, path::PathBuf, sync::Arc};
22

33
pub use self::client::{TransportRequest, WorkspaceClient, WorkspaceTransport};
4+
use crate::code_actions::*;
45
use pgt_analyse::RuleCategories;
56
use pgt_configuration::{PartialConfiguration, RuleSelector};
67
use pgt_fs::PgTPath;
@@ -115,6 +116,12 @@ pub trait Workspace: Send + Sync + RefUnwindSafe {
115116
params: PullDiagnosticsParams,
116117
) -> Result<PullDiagnosticsResult, WorkspaceError>;
117118

119+
/// Retrieves a list of available code_actions for a file/cursor_position
120+
fn pull_code_actions(
121+
&self,
122+
params: CodeActionsParams,
123+
) -> Result<CodeActionsResult, WorkspaceError>;
124+
118125
fn get_completions(
119126
&self,
120127
params: GetCompletionsParams,

crates/pgt_workspace/src/workspace/client.rs

+7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ impl<T> Workspace for WorkspaceClient<T>
8989
where
9090
T: WorkspaceTransport + RefUnwindSafe + Send + Sync,
9191
{
92+
fn pull_code_actions(
93+
&self,
94+
params: crate::code_actions::CodeActionsParams,
95+
) -> Result<crate::code_actions::CodeActionsResult, WorkspaceError> {
96+
self.request("pgt/code_actions", params)
97+
}
98+
9299
fn open_file(&self, params: OpenFileParams) -> Result<(), WorkspaceError> {
93100
self.request("pgt/open_file", params)
94101
}

crates/pgt_workspace/src/workspace/server.rs

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use tree_sitter::TreeSitterStore;
1919

2020
use crate::{
2121
WorkspaceError,
22+
code_actions::{CodeAction, CodeActionCategory, CodeActionsResult},
2223
configuration::to_analyser_rules,
2324
settings::{Settings, SettingsHandle, SettingsHandleMut},
2425
workspace::PullDiagnosticsResult,
@@ -253,6 +254,35 @@ impl Workspace for WorkspaceServer {
253254
Ok(self.is_ignored(params.pgt_path.as_path()))
254255
}
255256

257+
fn pull_code_actions(
258+
&self,
259+
params: crate::code_actions::CodeActionsParams,
260+
) -> Result<crate::code_actions::CodeActionsResult, WorkspaceError> {
261+
let cursor_position = params.range;
262+
263+
let doc = self
264+
.documents
265+
.get(&params.path)
266+
.ok_or(WorkspaceError::not_found())?;
267+
268+
let eligible_statements = doc.iter_statements_with_text_and_range().filter(
269+
|(_, range, _)| match cursor_position {
270+
None => true,
271+
Some(r) => range.contains(r),
272+
},
273+
);
274+
275+
let mut actions: Vec<CodeAction> = vec![];
276+
277+
for (stmt, range, txt) in eligible_statements {
278+
actions.push(CodeAction {
279+
category: CodeActionCategory::ExecuteCommand(txt.into()),
280+
});
281+
}
282+
283+
Ok(CodeActionsResult { actions })
284+
}
285+
256286
fn pull_diagnostics(
257287
&self,
258288
params: super::PullDiagnosticsParams,

docs/index.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ postgrestools init
3838

3939
After running the `init` command, you’ll have a `postgrestools.jsonc` file in your directory:
4040

41-
[//]: # (BEGIN DEFAULT_CONFIGURATION)
41+
[//]: # "BEGIN DEFAULT_CONFIGURATION"
4242

4343
```json
4444
{
@@ -63,12 +63,13 @@ After running the `init` command, you’ll have a `postgrestools.jsonc` file in
6363
"username": "postgres",
6464
"password": "postgres",
6565
"database": "postgres",
66-
"connTimeoutSecs": 10
66+
"connTimeoutSecs": 10,
67+
"allowStatementExecutionsAgainst": ["127.0.0.1/*", "localhost/*"]
6768
}
6869
}
6970
```
7071

71-
[//]: # (END DEFAULT_CONFIGURATION)
72+
[//]: # "END DEFAULT_CONFIGURATION"
7273

7374
Make sure to point the database connection settings at your local development database. To see what else can be configured, run `--help`.
7475

@@ -91,9 +92,7 @@ We recommend installing an editor plugin to get the most out of Postgres Languag
9192
> [!NOTE]
9293
> We will update this section once we have published the binaries.
9394
94-
9595
## CI Setup
9696

9797
> [!NOTE]
9898
> We will update this section once we have published the binaries.
99-

0 commit comments

Comments
 (0)