193 lines
7.8 KiB
Rust
193 lines
7.8 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! The open buffers: installing, closing and finding them, and which
|
|
//! text — the buffer's or the disk's — speaks for a path when the index
|
|
//! or a refactoring asks.
|
|
|
|
use std::fs;
|
|
use std::path::Path;
|
|
|
|
use lsp_types::Uri;
|
|
use lucisv_core::walk::dir_id;
|
|
use vlog_semantic::FileFacts;
|
|
|
|
use super::discover::{oversize, Found};
|
|
use super::{Document, Workspace};
|
|
use crate::uri::uri_to_path;
|
|
|
|
impl Workspace {
|
|
pub fn document(&self, uri: &Uri) -> Option<&Document> {
|
|
self.docs.get(uri)
|
|
}
|
|
|
|
/// The open URI and document backed by `path` (exact path first, else
|
|
/// matching file identity without basename gating), if there is one.
|
|
pub fn open_document_for_path(&self, path: &Path) -> Option<(&Uri, &Document)> {
|
|
if let Some(found) = self.docs.iter().find(|(_, doc)| doc.path.as_deref() == Some(path)) {
|
|
return Some(found);
|
|
}
|
|
if self.docs.is_empty() {
|
|
return None;
|
|
}
|
|
let target = dir_id(path)?;
|
|
self.docs
|
|
.iter()
|
|
.filter(|(_, doc)| {
|
|
doc.path.as_deref().is_some_and(|p| {
|
|
!case_only_mismatch(p, path) && dir_id(p).as_ref() == Some(&target)
|
|
})
|
|
})
|
|
.min_by_key(|(_, doc)| doc.path.as_deref())
|
|
}
|
|
|
|
/// The open document backed by `path` (exact path first, else matching file
|
|
/// identity without basename gating), if there is one.
|
|
pub fn document_for_path(&self, path: &Path) -> Option<&Document> {
|
|
self.open_document_for_path(path).map(|(_, doc)| doc)
|
|
}
|
|
|
|
/// Whether another currently open URI (distinct from `uri`) covers `path`
|
|
/// or its file identity.
|
|
pub(crate) fn has_other_document_for(&self, uri: &Uri, path: &Path) -> bool {
|
|
if self.docs.len() <= 1 {
|
|
return false;
|
|
}
|
|
if self.docs.iter().any(|(other, doc)| *other != *uri && doc.path.as_deref() == Some(path))
|
|
{
|
|
return true;
|
|
}
|
|
let Some(target) = dir_id(path) else { return false };
|
|
self.docs.iter().any(|(other, doc)| {
|
|
*other != *uri
|
|
&& doc.path.as_deref().is_some_and(|p| {
|
|
!case_only_mismatch(p, path) && dir_id(p).as_ref() == Some(&target)
|
|
})
|
|
})
|
|
}
|
|
|
|
/// Every open document and its URI.
|
|
pub fn open_documents(&self) -> impl Iterator<Item = (&Uri, &Document)> {
|
|
self.docs.iter()
|
|
}
|
|
|
|
/// Replace a buffer's contents and re-analyze it.
|
|
///
|
|
/// Also the one place a directory enters the folder scope: a buffer with a
|
|
/// path puts its own directory on the list `scope_dirs` raises. An
|
|
/// `untitled:` buffer has no path and contributes none.
|
|
///
|
|
/// And the one place the **large-file fast path** is chosen: a text over
|
|
/// `lucisv.largeFile.threshold` is line-indexed and nothing more. Because
|
|
/// the decision lives here it is re-taken on every `didOpen`, `didChange`
|
|
/// and text-carrying `didSave`, which is what makes a buffer edited past
|
|
/// the threshold — or back under it — change mode at that moment. A
|
|
/// *changed threshold* reaches the buffers already open through here too:
|
|
/// the settings applier re-installs each one the new value classifies
|
|
/// differently (`Server::reclassify_large_buffers`;
|
|
/// `docs/features/large-file-mode/README.md`).
|
|
pub fn set_document(&mut self, uri: Uri, text: String) {
|
|
let path = uri_to_path(&uri);
|
|
if let Some(dir) = path.as_deref().and_then(Path::parent) {
|
|
if !self.opened_dirs.iter().any(|seen| seen == dir) {
|
|
self.opened_dirs.push(dir.to_path_buf());
|
|
self.opened_dirs.sort();
|
|
}
|
|
}
|
|
// This path now has a buffer speaking for it, so whatever the view
|
|
// cache holds for it is both unwanted and possibly older than the
|
|
// edits the buffer carries.
|
|
if let Some(path) = path.as_deref() {
|
|
self.forget_view(path);
|
|
}
|
|
let doc = match self.is_large(&text) {
|
|
true => Document::large(path, text),
|
|
false => Document::new(path, text, &self.index),
|
|
};
|
|
self.docs.insert(uri, doc);
|
|
}
|
|
|
|
/// Whether a buffer of this text takes the fast path. `0` disables it, and
|
|
/// the comparison is strict, so a buffer of exactly the threshold is still
|
|
/// analyzed in full.
|
|
pub fn is_large(&self, text: &str) -> bool {
|
|
let threshold = self.client.large_file_threshold;
|
|
threshold != 0 && text.len() as u64 > threshold
|
|
}
|
|
|
|
/// Whether any open buffer is on the large-file fast path.
|
|
///
|
|
/// The gate in `server.rs` runs before every request, and this is what
|
|
/// keeps that from costing a URI parse per request in the session where
|
|
/// nothing is large — which is every session but one.
|
|
pub fn has_large_documents(&self) -> bool {
|
|
self.docs.values().any(|doc| doc.large)
|
|
}
|
|
|
|
pub fn close_document(&mut self, uri: &Uri) {
|
|
let closed = self.docs.remove(uri);
|
|
// Disk is the authority again, and a view made before this buffer was
|
|
// opened predates every save it made.
|
|
if let Some(path) = closed.and_then(|doc| doc.path) {
|
|
self.forget_view(&path);
|
|
}
|
|
}
|
|
|
|
/// Every open buffer's URI, for republishing diagnostics after a rebuild.
|
|
pub fn open_uris(&self) -> Vec<Uri> {
|
|
self.docs.keys().cloned().collect()
|
|
}
|
|
|
|
/// The text of a workspace file: the open buffer if there is one, else
|
|
/// what is on disk. Same precedence the index rebuild uses, so a location
|
|
/// is never computed against text the index did not see.
|
|
pub fn text_of(&self, path: &Path) -> Option<String> {
|
|
match self.document_for_path(path) {
|
|
Some(doc) => Some(doc.text.clone()),
|
|
None => fs::read_to_string(path).ok(),
|
|
}
|
|
}
|
|
|
|
/// The text one in-scope file contributes right now, by `collect_sources`'s
|
|
/// rules — `None` when disk is over `maxFileSize` or cannot be read.
|
|
///
|
|
/// An open buffer on the large-file fast path (`doc.large`) keeps its
|
|
/// on-disk index record subject to `maxFileSize` (Topo #63, Owner
|
|
/// decision 2), falling through to disk rather than suppressing it.
|
|
pub(super) fn current_text(&self, path: &Path) -> Option<String> {
|
|
if let Some(doc) = self.document_for_path(path) {
|
|
if !doc.large {
|
|
return Some(doc.text.clone());
|
|
}
|
|
}
|
|
if oversize(&Found::of(path), self.client.max_file_size, &mut 0).is_some() {
|
|
return None;
|
|
}
|
|
fs::read_to_string(path).ok()
|
|
}
|
|
|
|
/// The index facts and the completion words of the open buffer backing
|
|
/// `path`, read off the analysis it already holds — `None` when no buffer
|
|
/// does, or when the one that does is on the large-file fast path and has
|
|
/// no analysis of its own text to read.
|
|
///
|
|
/// The buffer is found the same way [`Workspace::current_text`] finds the
|
|
/// text, so the two always speak for the same `Document`, and facts are
|
|
/// stamped with canonical `path`.
|
|
pub(super) fn analysis_of(&self, path: &Path) -> Option<(FileFacts, Vec<String>)> {
|
|
let doc = self.document_for_path(path)?;
|
|
(!doc.large).then(|| (doc.facts(path.to_path_buf()), doc.words()))
|
|
}
|
|
}
|
|
|
|
/// On case-insensitive filesystems, two paths differing only in ASCII case
|
|
/// are not symlinks and should not cross-match an open buffer to disk.
|
|
pub(super) fn case_only_mismatch(a: &Path, b: &Path) -> bool {
|
|
if cfg!(any(target_os = "macos", target_os = "windows")) {
|
|
a != b && a.to_string_lossy().eq_ignore_ascii_case(&b.to_string_lossy())
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|