Skip to content

Commit 3c34a95

Browse files
committed
progress
1 parent 11a75ce commit 3c34a95

6 files changed

Lines changed: 362 additions & 121 deletions

File tree

crates/pgt_workspace/src/settings.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ use tracing::trace;
1212

1313
use ignore::gitignore::{Gitignore, GitignoreBuilder};
1414
use pgt_configuration::{
15-
ConfigurationDiagnostic, LinterConfiguration, PartialConfiguration,
1615
database::PartialDatabaseConfiguration,
1716
diagnostics::InvalidIgnorePattern,
1817
files::FilesConfiguration,
1918
migrations::{MigrationsConfiguration, PartialMigrationsConfiguration},
19+
ConfigurationDiagnostic, LinterConfiguration, PartialConfiguration,
2020
};
21-
use pgt_fs::{FileSystem, PgTPath};
21+
use pgt_fs::PgTPath;
2222

2323
use crate::{
24-
DynRef, WorkspaceError,
2524
matcher::Matcher,
2625
workspace::{ProjectKey, WorkspaceData},
26+
WorkspaceError,
2727
};
2828

2929
#[derive(Debug, Default)]
@@ -49,6 +49,16 @@ impl WorkspaceSettings {
4949
self.current_project
5050
}
5151

52+
pub fn get_current_project_path(&self) -> Option<&PgTPath> {
53+
trace!("Current key {:?}", self.current_project);
54+
let data = self.data.get(self.current_project);
55+
if let Some(data) = data {
56+
Some(&data.path)
57+
} else {
58+
None
59+
}
60+
}
61+
5262
pub fn get_current_project_data_mut(&mut self) -> &mut ProjectData {
5363
self.data
5464
.get_mut(self.current_project)
@@ -123,7 +133,8 @@ impl WorkspaceSettings {
123133
for (key, path_to_settings) in iter {
124134
trace!(
125135
"Workspace path {:?}, file path {:?}",
126-
path_to_settings.path, path
136+
path_to_settings.path,
137+
path
127138
);
128139
trace!("Iter key: {:?}", key);
129140
if key == self.current_project {
@@ -145,6 +156,51 @@ impl WorkspaceSettings {
145156
}
146157
}
147158

159+
#[derive(Debug)]
160+
pub struct WorkspaceSettingsHandle<'a> {
161+
inner: RwLockReadGuard<'a, WorkspaceSettings>,
162+
}
163+
164+
impl<'a> WorkspaceSettingsHandle<'a> {
165+
pub(crate) fn new(settings: &'a RwLock<WorkspaceSettings>) -> Self {
166+
Self {
167+
inner: settings.read().unwrap(),
168+
}
169+
}
170+
171+
pub(crate) fn settings(&self) -> Option<&Settings> {
172+
self.inner.get_current_settings()
173+
}
174+
175+
pub(crate) fn path(&self) -> Option<&PgTPath> {
176+
self.inner.get_current_project_path()
177+
}
178+
}
179+
180+
impl<'a> AsRef<WorkspaceSettings> for WorkspaceSettingsHandle<'a> {
181+
fn as_ref(&self) -> &WorkspaceSettings {
182+
&self.inner
183+
}
184+
}
185+
186+
pub struct WorkspaceSettingsHandleMut<'a> {
187+
inner: RwLockWriteGuard<'a, WorkspaceSettings>,
188+
}
189+
190+
impl<'a> WorkspaceSettingsHandleMut<'a> {
191+
pub(crate) fn new(settings: &'a RwLock<WorkspaceSettings>) -> Self {
192+
Self {
193+
inner: settings.write().unwrap(),
194+
}
195+
}
196+
}
197+
198+
impl<'a> AsMut<WorkspaceSettings> for WorkspaceSettingsHandleMut<'a> {
199+
fn as_mut(&mut self) -> &mut WorkspaceSettings {
200+
&mut self.inner
201+
}
202+
}
203+
148204
/// Global settings for the entire workspace
149205
#[derive(Debug, Default)]
150206
pub struct Settings {

crates/pgt_workspace/src/workspace.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ use pgt_configuration::{PartialConfiguration, RuleSelector};
66
use pgt_fs::PgTPath;
77
use pgt_text_size::TextRange;
88
use serde::{Deserialize, Serialize};
9-
use slotmap::{DenseSlotMap, new_key_type};
9+
use slotmap::{new_key_type, DenseSlotMap};
1010

1111
use crate::{
12-
WorkspaceError,
1312
features::{
1413
code_actions::{
1514
CodeActionsParams, CodeActionsResult, ExecuteStatementParams, ExecuteStatementResult,
1615
},
1716
completions::{CompletionsResult, GetCompletionsParams},
1817
diagnostics::{PullDiagnosticsParams, PullDiagnosticsResult},
1918
},
19+
WorkspaceError,
2020
};
2121

2222
mod client;
2323
mod server;
2424

25-
pub use server::StatementId;
2625
pub(crate) use server::parsed_document::*;
26+
pub use server::StatementId;
2727

2828
#[derive(Debug, serde::Serialize, serde::Deserialize)]
2929
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
@@ -93,6 +93,21 @@ pub struct ServerInfo {
9393
pub version: Option<String>,
9494
}
9595

96+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
97+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
98+
#[serde(rename_all = "camelCase")]
99+
pub struct RegisterProjectFolderParams {
100+
pub path: Option<PathBuf>,
101+
pub set_as_current_workspace: bool,
102+
}
103+
104+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
105+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
106+
#[serde(rename_all = "camelCase")]
107+
pub struct UnregisterProjectFolderParams {
108+
pub path: PgTPath,
109+
}
110+
96111
pub trait Workspace: Send + Sync + RefUnwindSafe {
97112
/// Retrieves the list of diagnostics associated to a file
98113
fn pull_diagnostics(
@@ -111,6 +126,18 @@ pub trait Workspace: Send + Sync + RefUnwindSafe {
111126
params: GetCompletionsParams,
112127
) -> Result<CompletionsResult, WorkspaceError>;
113128

129+
/// Register a possible workspace project folder. Returns the key of said project. Use this key when you want to switch to different projects.
130+
fn register_project_folder(
131+
&self,
132+
params: RegisterProjectFolderParams,
133+
) -> Result<ProjectKey, WorkspaceError>;
134+
135+
/// Unregister a workspace project folder. The settings that belong to that project are deleted.
136+
fn unregister_project_folder(
137+
&self,
138+
params: UnregisterProjectFolderParams,
139+
) -> Result<(), WorkspaceError>;
140+
114141
/// Update the global settings for this workspace
115142
fn update_settings(&self, params: UpdateSettingsParams) -> Result<(), WorkspaceError>;
116143

crates/pgt_workspace/src/workspace/client.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::workspace::ServerInfo;
22
use crate::{TransportError, Workspace, WorkspaceError};
3-
use serde::{Deserialize, Serialize, de::DeserializeOwned};
3+
use serde::{de::DeserializeOwned, Deserialize, Serialize};
44
use serde_json::json;
55
use std::{
66
panic::RefUnwindSafe,
@@ -103,6 +103,20 @@ where
103103
self.request("pgt/execute_statement", params)
104104
}
105105

106+
fn register_project_folder(
107+
&self,
108+
params: RegisterProjectFolderParams,
109+
) -> Result<ProjectKey, WorkspaceError> {
110+
self.request("pgt/register_project_folder", params)
111+
}
112+
113+
fn unregister_project_folder(
114+
&self,
115+
params: UnregisterProjectFolderParams,
116+
) -> Result<(), WorkspaceError> {
117+
self.request("pgt/unregister_project_folder", params)
118+
}
119+
106120
fn open_file(&self, params: OpenFileParams) -> Result<(), WorkspaceError> {
107121
self.request("pgt/open_file", params)
108122
}

0 commit comments

Comments
 (0)