-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathserver.rs
513 lines (445 loc) · 17.7 KB
/
server.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use std::{fs, panic::RefUnwindSafe, path::Path, sync::RwLock};
use analyser::AnalyserVisitorBuilder;
use async_helper::run_async;
use dashmap::DashMap;
use db_connection::DbConnection;
use document::Document;
use futures::{StreamExt, stream};
use parsed_document::{
AsyncDiagnosticsMapper, CursorPositionFilter, DefaultMapper, ExecuteStatementMapper,
ParsedDocument, SyncDiagnosticsMapper,
};
use pgt_analyse::{AnalyserOptions, AnalysisFilter};
use pgt_analyser::{Analyser, AnalyserConfig, AnalyserContext};
use pgt_diagnostics::{
Diagnostic, DiagnosticExt, Error, Severity, serde::Diagnostic as SDiagnostic,
};
use pgt_fs::{ConfigName, PgTPath};
use pgt_typecheck::TypecheckParams;
use schema_cache_manager::SchemaCacheManager;
use sqlx::Executor;
use tracing::info;
use crate::{
WorkspaceError,
configuration::to_analyser_rules,
features::{
code_actions::{
self, CodeAction, CodeActionKind, CodeActionsResult, CommandAction,
CommandActionCategory, ExecuteStatementParams, ExecuteStatementResult,
},
completions::{CompletionsResult, GetCompletionsParams, get_statement_for_completions},
diagnostics::{PullDiagnosticsParams, PullDiagnosticsResult},
},
settings::{Settings, SettingsHandle, SettingsHandleMut},
};
use super::{
GetFileContentParams, IsPathIgnoredParams, OpenFileParams, ServerInfo, UpdateSettingsParams,
Workspace,
};
pub use statement_identifier::StatementId;
mod analyser;
mod annotation;
mod async_helper;
mod change;
mod db_connection;
pub(crate) mod document;
mod migration;
pub(crate) mod parsed_document;
mod pg_query;
mod schema_cache_manager;
mod sql_function;
mod statement_identifier;
mod tree_sitter;
pub(super) struct WorkspaceServer {
/// global settings object for this workspace
settings: RwLock<Settings>,
/// Stores the schema cache for this workspace
schema_cache: SchemaCacheManager,
parsed_documents: DashMap<PgTPath, ParsedDocument>,
connection: RwLock<DbConnection>,
}
/// The `Workspace` object is long-lived, so we want it to be able to cross
/// unwind boundaries.
/// In return, we have to make sure operations on the workspace either do not
/// panic, of that panicking will not result in any broken invariant (it would
/// not result in any undefined behavior as catching an unwind is safe, but it
/// could lead too hard to debug issues)
impl RefUnwindSafe for WorkspaceServer {}
impl WorkspaceServer {
/// Create a new [Workspace]
///
/// This is implemented as a crate-private method instead of using
/// [Default] to disallow instances of [Workspace] from being created
/// outside a [crate::App]
pub(crate) fn new() -> Self {
Self {
settings: RwLock::default(),
parsed_documents: DashMap::default(),
schema_cache: SchemaCacheManager::default(),
connection: RwLock::default(),
}
}
/// Provides a reference to the current settings
fn settings(&self) -> SettingsHandle {
SettingsHandle::new(&self.settings)
}
fn settings_mut(&self) -> SettingsHandleMut {
SettingsHandleMut::new(&self.settings)
}
fn is_ignored_by_migration_config(&self, path: &Path) -> bool {
let set = self.settings();
set.as_ref()
.migrations
.as_ref()
.and_then(|migration_settings| {
let ignore_before = migration_settings.after.as_ref()?;
let migrations_dir = migration_settings.path.as_ref()?;
let migration = migration::get_migration(path, migrations_dir)?;
Some(&migration.sequence_number <= ignore_before)
})
.unwrap_or(false)
}
/// Check whether a file is ignored in the top-level config `files.ignore`/`files.include`
fn is_ignored(&self, path: &Path) -> bool {
let file_name = path.file_name().and_then(|s| s.to_str());
// Never ignore Postgres Tools's config file regardless `include`/`ignore`
(file_name != Some(ConfigName::pgt_jsonc())) &&
// Apply top-level `include`/`ignore
(self.is_ignored_by_top_level_config(path) || self.is_ignored_by_migration_config(path))
}
/// Check whether a file is ignored in the top-level config `files.ignore`/`files.include`
fn is_ignored_by_top_level_config(&self, path: &Path) -> bool {
let set = self.settings();
let settings = set.as_ref();
let is_included = settings.files.included_files.is_empty()
|| is_dir(path)
|| settings.files.included_files.matches_path(path);
!is_included
|| settings.files.ignored_files.matches_path(path)
|| settings.files.git_ignore.as_ref().is_some_and(|ignore| {
// `matched_path_or_any_parents` panics if `source` is not under the gitignore root.
// This checks excludes absolute paths that are not a prefix of the base root.
if !path.has_root() || path.starts_with(ignore.path()) {
// Because Postgres Tools passes a list of paths,
// we use `matched_path_or_any_parents` instead of `matched`.
ignore
.matched_path_or_any_parents(path, path.is_dir())
.is_ignore()
} else {
false
}
})
}
}
impl Workspace for WorkspaceServer {
/// Update the global settings for this workspace
///
/// ## Panics
/// This function may panic if the internal settings mutex has been poisoned
/// by another thread having previously panicked while holding the lock
#[tracing::instrument(level = "trace", skip(self), err)]
fn update_settings(&self, params: UpdateSettingsParams) -> Result<(), WorkspaceError> {
tracing::info!("Updating settings in workspace");
self.settings_mut().as_mut().merge_with_configuration(
params.configuration,
params.workspace_directory,
params.vcs_base_path,
params.gitignore_matches.as_slice(),
)?;
tracing::info!("Updated settings in workspace");
if !params.skip_db {
self.connection
.write()
.unwrap()
.set_conn_settings(&self.settings().as_ref().db);
}
tracing::info!("Updated Db connection settings");
Ok(())
}
/// Add a new file to the workspace
#[tracing::instrument(level = "info", skip_all, fields(path = params.path.as_path().as_os_str().to_str()), err)]
fn open_file(&self, params: OpenFileParams) -> Result<(), WorkspaceError> {
self.parsed_documents
.entry(params.path.clone())
.or_insert_with(|| {
ParsedDocument::new(params.path.clone(), params.content, params.version)
});
Ok(())
}
/// Remove a file from the workspace
fn close_file(&self, params: super::CloseFileParams) -> Result<(), WorkspaceError> {
self.parsed_documents
.remove(¶ms.path)
.ok_or_else(WorkspaceError::not_found)?;
Ok(())
}
/// Change the content of an open file
#[tracing::instrument(level = "debug", skip_all, fields(
path = params.path.as_os_str().to_str(),
version = params.version
), err)]
fn change_file(&self, params: super::ChangeFileParams) -> Result<(), WorkspaceError> {
let mut parser =
self.parsed_documents
.entry(params.path.clone())
.or_insert(ParsedDocument::new(
params.path.clone(),
"".to_string(),
params.version,
));
parser.apply_change(params);
Ok(())
}
fn server_info(&self) -> Option<&ServerInfo> {
None
}
fn get_file_content(&self, params: GetFileContentParams) -> Result<String, WorkspaceError> {
let document = self
.parsed_documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
Ok(document.get_document_content().to_string())
}
fn is_path_ignored(&self, params: IsPathIgnoredParams) -> Result<bool, WorkspaceError> {
Ok(self.is_ignored(params.pgt_path.as_path()))
}
fn pull_code_actions(
&self,
params: code_actions::CodeActionsParams,
) -> Result<code_actions::CodeActionsResult, WorkspaceError> {
let parser = self
.parsed_documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let settings = self
.settings
.read()
.expect("Unable to read settings for Code Actions");
let disabled_reason: Option<String> = if settings.db.allow_statement_executions {
None
} else {
Some("Statement execution not allowed against database.".into())
};
let actions = parser
.iter_with_filter(
DefaultMapper,
CursorPositionFilter::new(params.cursor_position),
)
.map(|(stmt, _, txt)| {
let title = format!(
"Execute Statement: {}...",
txt.chars().take(50).collect::<String>()
);
CodeAction {
title,
kind: CodeActionKind::Command(CommandAction {
category: CommandActionCategory::ExecuteStatement(stmt),
}),
disabled_reason: disabled_reason.clone(),
}
})
.collect();
Ok(CodeActionsResult { actions })
}
fn execute_statement(
&self,
params: ExecuteStatementParams,
) -> Result<ExecuteStatementResult, WorkspaceError> {
let parser = self
.parsed_documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let stmt = parser.find(params.statement_id, ExecuteStatementMapper);
if stmt.is_none() {
return Ok(ExecuteStatementResult {
message: "Statement was not found in document.".into(),
});
};
let (_id, _range, content, ast) = stmt.unwrap();
if ast.is_none() {
return Ok(ExecuteStatementResult {
message: "Statement is invalid.".into(),
});
};
let conn = self.connection.read().unwrap();
let pool = match conn.get_pool() {
Some(p) => p,
None => {
return Ok(ExecuteStatementResult {
message: "Not connected to database.".into(),
});
}
};
let result = run_async(async move { pool.execute(sqlx::query(&content)).await })??;
Ok(ExecuteStatementResult {
message: format!(
"Successfully executed statement. Rows affected: {}",
result.rows_affected()
),
})
}
fn pull_diagnostics(
&self,
params: PullDiagnosticsParams,
) -> Result<PullDiagnosticsResult, WorkspaceError> {
let settings = self.settings();
// create analyser for this run
// first, collect enabled and disabled rules from the workspace settings
let (enabled_rules, disabled_rules) = AnalyserVisitorBuilder::new(settings.as_ref())
.with_linter_rules(¶ms.only, ¶ms.skip)
.finish();
// then, build a map that contains all options
let options = AnalyserOptions {
rules: to_analyser_rules(settings.as_ref()),
};
// next, build the analysis filter which will be used to match rules
let filter = AnalysisFilter {
categories: params.categories,
enabled_rules: Some(enabled_rules.as_slice()),
disabled_rules: &disabled_rules,
};
// finally, create the analyser that will be used during this run
let analyser = Analyser::new(AnalyserConfig {
options: &options,
filter,
});
let parser = self
.parsed_documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let mut diagnostics: Vec<SDiagnostic> = parser.document_diagnostics().to_vec();
if let Some(pool) = self
.connection
.read()
.expect("DbConnection RwLock panicked")
.get_pool()
{
let path_clone = params.path.clone();
let input = parser.iter(AsyncDiagnosticsMapper).collect::<Vec<_>>();
let async_results = run_async(async move {
stream::iter(input)
.map(|(_id, range, content, ast, cst)| {
let pool = pool.clone();
let path = path_clone.clone();
async move {
if let Some(ast) = ast {
pgt_typecheck::check_sql(TypecheckParams {
conn: &pool,
sql: &content,
ast: &ast,
tree: &cst,
})
.await
.map(|d| {
d.map(|d| {
let r = d.location().span.map(|span| span + range.start());
d.with_file_path(path.as_path().display().to_string())
.with_file_span(r.unwrap_or(range))
})
})
} else {
Ok(None)
}
}
})
.buffer_unordered(10)
.collect::<Vec<_>>()
.await
})?;
for result in async_results.into_iter() {
let result = result?;
if let Some(diag) = result {
diagnostics.push(SDiagnostic::new(diag));
}
}
}
diagnostics.extend(parser.iter(SyncDiagnosticsMapper).flat_map(
|(_id, range, ast, diag)| {
let mut errors: Vec<Error> = vec![];
if let Some(diag) = diag {
errors.push(diag.into());
}
if let Some(ast) = ast {
errors.extend(
analyser
.run(AnalyserContext { root: &ast })
.into_iter()
.map(Error::from)
.collect::<Vec<pgt_diagnostics::Error>>(),
);
}
errors
.into_iter()
.map(|d| {
let severity = d
.category()
.filter(|category| category.name().starts_with("lint/"))
.map_or_else(
|| d.severity(),
|category| {
settings
.as_ref()
.get_severity_from_rule_code(category)
.unwrap_or(Severity::Warning)
},
);
SDiagnostic::new(
d.with_file_path(params.path.as_path().display().to_string())
.with_file_span(range)
.with_severity(severity),
)
})
.collect::<Vec<_>>()
},
));
let errors = diagnostics
.iter()
.filter(|d| d.severity() == Severity::Error || d.severity() == Severity::Fatal)
.count();
info!("Pulled {:?} diagnostic(s)", diagnostics.len());
Ok(PullDiagnosticsResult {
diagnostics,
errors,
skipped_diagnostics: 0,
})
}
#[tracing::instrument(level = "debug", skip_all, fields(
path = params.path.as_os_str().to_str(),
position = params.position.to_string()
), err)]
fn get_completions(
&self,
params: GetCompletionsParams,
) -> Result<CompletionsResult, WorkspaceError> {
let parsed_doc = self
.parsed_documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let pool = match self.connection.read().unwrap().get_pool() {
Some(pool) => pool,
None => {
tracing::debug!("No connection to database. Skipping completions.");
return Ok(CompletionsResult::default());
}
};
let schema_cache = self.schema_cache.load(pool)?;
match get_statement_for_completions(&parsed_doc, params.position) {
None => Ok(CompletionsResult::default()),
Some((_id, range, content, cst)) => {
let position = params.position - range.start();
let items = pgt_completions::complete(pgt_completions::CompletionParams {
position,
schema: schema_cache.as_ref(),
tree: &cst,
text: content,
});
Ok(CompletionsResult { items })
}
}
}
}
/// Returns `true` if `path` is a directory or
/// if it is a symlink that resolves to a directory.
fn is_dir(path: &Path) -> bool {
path.is_dir() || (path.is_symlink() && fs::read_link(path).is_ok_and(|path| path.is_dir()))
}