Skip to content

Commit 9bfb495

Browse files
committed
fix: workspace wide ops should respect unsaved changes
1 parent 3a528af commit 9bfb495

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

crates/nu-lsp/src/workspace.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use nu_protocol::{
1818
use std::{
1919
collections::{BTreeMap, HashMap, HashSet},
2020
fs,
21-
path::{Path, PathBuf},
21+
path::Path,
2222
sync::Arc,
2323
};
2424

@@ -428,12 +428,12 @@ impl LanguageServer {
428428
let (data_sender, data_receiver) = crossbeam_channel::unbounded::<InternalMessage>();
429429
let (cancel_sender, cancel_receiver) = crossbeam_channel::bounded::<bool>(1);
430430
let engine_state = Arc::new(engine_state);
431-
let docs = self.docs.clone();
431+
let text_documents = self.docs.clone();
432432
self.send_progress_begin(token.clone(), message)?;
433433

434434
std::thread::spawn(move || -> Result<()> {
435435
let mut working_set = StateWorkingSet::new(&engine_state);
436-
let scripts: HashSet<PathBuf> = match find_nu_scripts_in_folder(&workspace_uri) {
436+
let mut scripts: HashSet<_> = match find_nu_scripts_in_folder(&workspace_uri) {
437437
Ok(it) => it,
438438
Err(_) => {
439439
data_sender
@@ -444,6 +444,20 @@ impl LanguageServer {
444444
}
445445
.filter_map(|p| p.ok())
446446
.collect();
447+
448+
// For unsaved new files
449+
let mut opened_scripts = HashSet::new();
450+
let docs = match text_documents.lock() {
451+
Ok(it) => it,
452+
Err(err) => return Err(miette!(err.to_string())),
453+
};
454+
for uri in docs.documents().keys() {
455+
let fp = uri_to_path(uri);
456+
opened_scripts.insert(fp.clone());
457+
scripts.insert(fp);
458+
}
459+
drop(docs);
460+
447461
let len = scripts.len();
448462
let definition_span = Self::find_definition_span_by_id(&working_set, &id_tracker.id);
449463
let bytes_to_search = id_tracker.name.to_owned();
@@ -461,12 +475,18 @@ impl LanguageServer {
461475
}
462476
let percentage = (i * 100 / len) as u32;
463477
let uri = path_to_uri(fp);
464-
let docs = match docs.lock() {
465-
Ok(it) => it,
466-
Err(err) => return Err(miette!(err.to_string())),
467-
};
468-
let file = if let Some(file) = docs.get_document(&uri) {
469-
file
478+
let file = if opened_scripts.contains(fp) {
479+
let docs = match text_documents.lock() {
480+
Ok(it) => it,
481+
Err(err) => return Err(miette!(err.to_string())),
482+
};
483+
let Some(file) = docs.get_document(&uri) else {
484+
continue;
485+
};
486+
let doc_copy =
487+
FullTextDocument::new("nu".to_string(), 0, file.get_content(None).into());
488+
drop(docs);
489+
doc_copy
470490
} else {
471491
let file_bytes = match fs::read(fp) {
472492
Ok(it) => it,
@@ -483,15 +503,15 @@ impl LanguageServer {
483503
.into_diagnostic()?;
484504
continue;
485505
}
486-
&FullTextDocument::new(
506+
FullTextDocument::new(
487507
"nu".to_string(),
488508
0,
489509
String::from_utf8_lossy(&file_bytes).into(),
490510
)
491511
};
492512
let ranges = Self::find_reference_in_file(
493513
&mut working_set,
494-
file,
514+
&file,
495515
fp,
496516
&mut id_tracker,
497517
definition_span,

0 commit comments

Comments
 (0)