-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathserver.rs
578 lines (502 loc) · 20.2 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::{fs, panic::RefUnwindSafe, path::Path, sync::RwLock};
use analyser::AnalyserVisitorBuilder;
use async_helper::run_async;
use change::StatementChange;
use dashmap::DashMap;
use db_connection::DbConnection;
pub(crate) use document::StatementId;
use document::{Document, Statement};
use futures::{StreamExt, stream};
use pg_query::PgQueryStore;
use pgt_analyse::{AnalyserOptions, AnalysisFilter};
use pgt_analyser::{Analyser, AnalyserConfig, AnalyserContext};
use pgt_diagnostics::{Diagnostic, DiagnosticExt, 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 tree_sitter::TreeSitterStore;
use crate::{
WorkspaceError,
configuration::to_analyser_rules,
features::{
code_actions::{
self, CodeAction, CodeActionKind, CodeActionsResult, CommandAction,
CommandActionCategory, ExecuteStatementParams, ExecuteStatementResult,
},
completions::{CompletionsResult, GetCompletionsParams},
diagnostics::{PullDiagnosticsParams, PullDiagnosticsResult},
},
settings::{Settings, SettingsHandle, SettingsHandleMut},
};
use super::{
GetFileContentParams, IsPathIgnoredParams, OpenFileParams, ServerInfo, UpdateSettingsParams,
Workspace,
};
mod analyser;
mod async_helper;
mod change;
mod db_connection;
mod document;
mod migration;
mod pg_query;
mod schema_cache_manager;
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,
/// Stores the document (text content + version number) associated with a URL
documents: DashMap<PgTPath, Document>,
tree_sitter: TreeSitterStore,
pg_query: PgQueryStore,
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(),
documents: DashMap::default(),
tree_sitter: TreeSitterStore::new(),
pg_query: PgQueryStore::new(),
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");
tracing::debug!("Updated settings are {:#?}", self.settings());
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> {
let doc = Document::new(params.path.clone(), params.content, params.version);
doc.iter_statements_with_text().for_each(|(stmt, content)| {
self.tree_sitter.add_statement(&stmt, content);
self.pg_query.add_statement(&stmt, content);
});
self.documents.insert(params.path, doc);
Ok(())
}
/// Remove a file from the workspace
fn close_file(&self, params: super::CloseFileParams) -> Result<(), WorkspaceError> {
let (_, doc) = self
.documents
.remove(¶ms.path)
.ok_or_else(WorkspaceError::not_found)?;
for stmt in doc.iter_statements() {
self.tree_sitter.remove_statement(&stmt);
self.pg_query.remove_statement(&stmt);
}
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 doc = self
.documents
.entry(params.path.clone())
.or_insert(Document::new(
params.path.clone(),
"".to_string(),
params.version,
));
for c in &doc.apply_file_change(¶ms) {
match c {
StatementChange::Added(added) => {
tracing::debug!(
"Adding statement: id:{:?}, path:{:?}, text:{:?}",
added.stmt.id,
added.stmt.path.as_os_str().to_str(),
added.text
);
self.tree_sitter.add_statement(&added.stmt, &added.text);
self.pg_query.add_statement(&added.stmt, &added.text);
}
StatementChange::Deleted(s) => {
tracing::debug!(
"Deleting statement: id:{:?}, path:{:?}",
s.id,
s.path.as_os_str()
);
self.tree_sitter.remove_statement(s);
self.pg_query.remove_statement(s);
}
StatementChange::Modified(s) => {
tracing::debug!(
"Modifying statement with id {:?} (new id {:?}) in {:?}. Range {:?}, Changed from '{:?}' to '{:?}', changed text: {:?}",
s.old_stmt.id,
s.new_stmt.id,
s.old_stmt.path.as_os_str().to_str(),
s.change_range,
s.old_stmt_text,
s.new_stmt_text,
s.change_text
);
self.tree_sitter.modify_statement(s);
self.pg_query.modify_statement(s);
}
}
}
Ok(())
}
fn server_info(&self) -> Option<&ServerInfo> {
None
}
fn get_file_content(&self, params: GetFileContentParams) -> Result<String, WorkspaceError> {
let document = self
.documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
Ok(document.content.clone())
}
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 doc = self
.documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let eligible_statements = doc
.iter_statements_with_text_and_range()
.filter(|(_, range, _)| range.contains(params.cursor_position));
let mut actions: Vec<code_actions::CodeAction> = vec![];
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())
};
for (stmt, _, txt) in eligible_statements {
let title = format!(
"Execute Statement: {}...",
txt.chars().take(50).collect::<String>()
);
actions.push(CodeAction {
title,
kind: CodeActionKind::Command(CommandAction {
category: CommandActionCategory::ExecuteStatement(stmt.id),
}),
disabled_reason: disabled_reason.clone(),
});
}
Ok(CodeActionsResult { actions })
}
fn execute_statement(
&self,
params: ExecuteStatementParams,
) -> Result<ExecuteStatementResult, WorkspaceError> {
let doc = self
.documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
if self
.pg_query
.get_ast(&Statement {
path: params.path,
id: params.statement_id,
})
.is_none()
{
return Ok(ExecuteStatementResult {
message: "Statement is invalid.".into(),
});
};
let sql: String = match doc.get_txt(params.statement_id) {
Some(txt) => txt,
None => {
return Ok(ExecuteStatementResult {
message: "Statement was not found in document.".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(&sql)).await })??;
Ok(ExecuteStatementResult {
message: format!(
"Successfully executed statement. Rows affected: {}",
result.rows_affected()
),
})
}
fn pull_diagnostics(
&self,
params: PullDiagnosticsParams,
) -> Result<PullDiagnosticsResult, WorkspaceError> {
// get all statements form the requested document and pull diagnostics out of every
// source
let doc = self
.documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
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 mut diagnostics: Vec<SDiagnostic> = doc.diagnostics().to_vec();
if let Some(pool) = self
.connection
.read()
.expect("DbConnection RwLock panicked")
.get_pool()
{
let typecheck_params: Vec<_> = doc
.iter_statements_with_text_and_range()
.map(|(stmt, range, text)| {
let ast = self.pg_query.get_ast(&stmt);
let tree = self.tree_sitter.get_parse_tree(&stmt);
(text.to_string(), ast, tree, *range)
})
.collect();
// run diagnostics for each statement in parallel if its mostly i/o work
let path_clone = params.path.clone();
let async_results = run_async(async move {
stream::iter(typecheck_params)
.map(|(text, ast, tree, range)| {
let pool = pool.clone();
let path = path_clone.clone();
async move {
if let Some(ast) = ast {
pgt_typecheck::check_sql(TypecheckParams {
conn: &pool,
sql: &text,
ast: &ast,
tree: tree.as_deref(),
})
.await
.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 {
None
}
}
})
.buffer_unordered(10)
.collect::<Vec<_>>()
.await
})?;
for result in async_results.into_iter().flatten() {
diagnostics.push(SDiagnostic::new(result));
}
}
diagnostics.extend(doc.iter_statements_with_range().flat_map(|(stmt, r)| {
let mut stmt_diagnostics = self.pg_query.get_diagnostics(&stmt);
let ast = self.pg_query.get_ast(&stmt);
if let Some(ast) = ast {
stmt_diagnostics.extend(
analyser
.run(AnalyserContext { root: &ast })
.into_iter()
.map(SDiagnostic::new)
.collect::<Vec<_>>(),
);
}
stmt_diagnostics
.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(r)
.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 pool = match self.connection.read().unwrap().get_pool() {
Some(pool) => pool,
None => return Ok(CompletionsResult::default()),
};
let doc = self
.documents
.get(¶ms.path)
.ok_or(WorkspaceError::not_found())?;
let (statement, stmt_range, text) = match doc
.iter_statements_with_text_and_range()
.find(|(_, r, _)| r.contains(params.position))
{
Some(s) => s,
None => return Ok(CompletionsResult::default()),
};
// `offset` is the position in the document,
// but we need the position within the *statement*.
let position = params.position - stmt_range.start();
let tree = self.tree_sitter.get_parse_tree(&statement);
tracing::debug!(
"Found the statement. We're looking for position {:?}. Statement Range {:?} to {:?}. Statement: {:?}",
position,
stmt_range.start(),
stmt_range.end(),
text
);
let schema_cache = self.schema_cache.load(pool)?;
let items = pgt_completions::complete(pgt_completions::CompletionParams {
position,
schema: schema_cache.as_ref(),
tree: tree.as_deref(),
text: text.to_string(),
});
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()))
}