-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathworkspace.rs
296 lines (255 loc) · 9.47 KB
/
workspace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::{panic::RefUnwindSafe, path::PathBuf, sync::Arc};
pub use self::client::{TransportRequest, WorkspaceClient, WorkspaceTransport};
use pgt_analyse::RuleCategories;
use pgt_configuration::{PartialConfiguration, RuleSelector};
use pgt_fs::PgTPath;
use pgt_text_size::{TextRange, TextSize};
use serde::{Deserialize, Serialize};
use crate::WorkspaceError;
mod client;
mod server;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct OpenFileParams {
pub path: PgTPath,
pub content: String,
pub version: i32,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CloseFileParams {
pub path: PgTPath,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ChangeFileParams {
pub path: PgTPath,
pub version: i32,
pub changes: Vec<ChangeParams>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PullDiagnosticsParams {
pub path: PgTPath,
pub categories: RuleCategories,
pub max_diagnostics: u64,
pub only: Vec<RuleSelector>,
pub skip: Vec<RuleSelector>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct GetCompletionsParams {
/// The File for which a completion is requested.
pub path: PgTPath,
/// The Cursor position in the file for which a completion is requested.
pub position: TextSize,
}
#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CompletionResult {
pub(crate) items: Vec<CompletionItem>,
}
#[cfg(feature = "db-connection")]
impl IntoIterator for CompletionResult {
type Item = CompletionItem;
type IntoIter = <Vec<CompletionItem> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
#[cfg(feature = "db-connection")]
impl From<pgt_completions::CompletionResult> for CompletionResult {
fn from(external: pgt_completions::CompletionResult) -> Self {
CompletionResult {
items: external.items.into_iter().map(Into::into).collect(),
}
}
}
#[cfg(feature = "db-connection")]
impl From<pgt_completions::CompletionItem> for CompletionItem {
fn from(external: pgt_completions::CompletionItem) -> Self {
CompletionItem {
label: external.label,
score: external.score,
description: external.description,
preselected: external.preselected,
kind: external.kind.into(),
}
}
}
#[cfg(feature = "db-connection")]
impl From<pgt_completions::CompletionItemKind> for CompletionItemKind {
fn from(external: pgt_completions::CompletionItemKind) -> Self {
match external {
pgt_completions::CompletionItemKind::Table => CompletionItemKind::Table,
pgt_completions::CompletionItemKind::Function => CompletionItemKind::Function,
pgt_completions::CompletionItemKind::Column => CompletionItemKind::Column,
}
}
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub enum CompletionItemKind {
Table,
Function,
Column,
}
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CompletionItem {
pub label: String,
pub(crate) score: i32,
pub description: String,
pub preselected: bool,
pub kind: CompletionItemKind,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PullDiagnosticsResult {
pub diagnostics: Vec<pgt_diagnostics::serde::Diagnostic>,
pub errors: usize,
pub skipped_diagnostics: u64,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ChangeParams {
/// The range of the file that changed. If `None`, the whole file changed.
pub range: Option<TextRange>,
pub text: String,
}
impl ChangeParams {
pub fn overwrite(text: String) -> Self {
Self { range: None, text }
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct IsPathIgnoredParams {
pub pgt_path: PgTPath,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct UpdateSettingsParams {
pub configuration: PartialConfiguration,
pub vcs_base_path: Option<PathBuf>,
pub gitignore_matches: Vec<String>,
pub workspace_directory: Option<PathBuf>,
pub skip_db: bool,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct GetFileContentParams {
pub path: PgTPath,
}
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ServerInfo {
/// The name of the server as defined by the server.
pub name: String,
/// The server's version as defined by the server.
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
}
pub trait Workspace: Send + Sync + RefUnwindSafe {
/// Retrieves the list of diagnostics associated to a file
fn pull_diagnostics(
&self,
params: PullDiagnosticsParams,
) -> Result<PullDiagnosticsResult, WorkspaceError>;
fn get_completions(
&self,
params: GetCompletionsParams,
) -> Result<CompletionResult, WorkspaceError>;
/// Update the global settings for this workspace
fn update_settings(&self, params: UpdateSettingsParams) -> Result<(), WorkspaceError>;
/// Add a new file to the workspace
fn open_file(&self, params: OpenFileParams) -> Result<(), WorkspaceError>;
/// Remove a file from the workspace
fn close_file(&self, params: CloseFileParams) -> Result<(), WorkspaceError>;
/// Change the content of an open file
fn change_file(&self, params: ChangeFileParams) -> Result<(), WorkspaceError>;
/// Returns information about the server this workspace is connected to or `None` if the workspace isn't connected to a server.
fn server_info(&self) -> Option<&ServerInfo>;
/// Return the content of a file
fn get_file_content(&self, params: GetFileContentParams) -> Result<String, WorkspaceError>;
/// Checks if the current path is ignored by the workspace.
///
/// Takes as input the path of the file that workspace is currently processing and
/// a list of paths to match against.
///
/// If the file path matches, then `true` is returned, and it should be considered ignored.
fn is_path_ignored(&self, params: IsPathIgnoredParams) -> Result<bool, WorkspaceError>;
}
/// Convenience function for constructing a server instance of [Workspace]
pub fn server() -> Box<dyn Workspace> {
Box::new(server::WorkspaceServer::new())
}
/// Convenience function for constructing a server instance of [Workspace]
pub fn server_sync() -> Arc<dyn Workspace> {
Arc::new(server::WorkspaceServer::new())
}
// Convenience function for constructing a client instance of [Workspace]
pub fn client<T>(transport: T) -> Result<Box<dyn Workspace>, WorkspaceError>
where
T: WorkspaceTransport + RefUnwindSafe + Send + Sync + 'static,
{
Ok(Box::new(client::WorkspaceClient::new(transport)?))
}
/// [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization)
/// guard for an open file in a workspace, takes care of closing the file
/// automatically on drop
pub struct FileGuard<'app, W: Workspace + ?Sized> {
workspace: &'app W,
path: PgTPath,
}
impl<'app, W: Workspace + ?Sized> FileGuard<'app, W> {
pub fn open(workspace: &'app W, params: OpenFileParams) -> Result<Self, WorkspaceError> {
let path = params.path.clone();
workspace.open_file(params)?;
Ok(Self { workspace, path })
}
pub fn change_file(
&self,
version: i32,
changes: Vec<ChangeParams>,
) -> Result<(), WorkspaceError> {
self.workspace.change_file(ChangeFileParams {
path: self.path.clone(),
version,
changes,
})
}
pub fn get_file_content(&self) -> Result<String, WorkspaceError> {
self.workspace.get_file_content(GetFileContentParams {
path: self.path.clone(),
})
}
pub fn pull_diagnostics(
&self,
categories: RuleCategories,
max_diagnostics: u32,
only: Vec<RuleSelector>,
skip: Vec<RuleSelector>,
) -> Result<PullDiagnosticsResult, WorkspaceError> {
self.workspace.pull_diagnostics(PullDiagnosticsParams {
path: self.path.clone(),
categories,
max_diagnostics: max_diagnostics.into(),
only,
skip,
})
}
}
impl<W: Workspace + ?Sized> Drop for FileGuard<'_, W> {
fn drop(&mut self) {
self.workspace
.close_file(CloseFileParams {
path: self.path.clone(),
})
// `close_file` can only error if the file was already closed, in
// this case it's generally better to silently matcher the error
// than panic (especially in a drop handler)
.ok();
}
}