Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,8 @@ For `workspace.listDirectories`, `rust/fixtures/workspace/directory-listing-leav
shows the difference between an unloaded child in its parent's listing and the empty
`children` array returned when that same directory is listed directly.

`workspace.projectTree.rows` accepts `{ nodes, selection?, top? }` and returns
`{ rows, selection, selectionMoved, topRow, targets }`. `nodes` are the project tree
`workspace.projectTree.rows` accepts `{ nodes, selection?, top?, compact? }` and returns
`{ rows, selection, selectionMoved, topRow, targets, heads?, depths? }`. `nodes` are the project tree
nodes the host loaded with `workspace.listDirectories`, in preorder, each
`{ path, depth, open? }`: the first is the root `{ path: "", depth: 0 }`, and every
other node is one name inside the nearest node above it at one less depth; `open`
Expand All @@ -1083,6 +1083,21 @@ above it, a path listed twice, more than 2^20 nodes and the selection errors of
`workspace.rowSelection.reduce` return `invalid_request`. See
`rust/fixtures/workspace/project-tree-rows-v1.json`.

With `compact: true`, nodes may also say `folder: true` (the node is a folder) and
`listed: true` (the host listed its children), and a listed folder other than the
root whose only loaded child is a folder shares that folder's row, as IntelliJ IDEA
and VS Code compact folder chains. The chain's last folder is the row, and the
response adds `heads`, one per row: the position of the chain's first folder (the
row itself when it heads no chain), and `depths`, one per row: how deep the row is
indented, its node's `depth` less the folders above it that share their row with
the folder inside them, so a chain takes one level whatever its length. The head's
`open` decides whether the row is open; the `open` of the folders after it is
ignored. A selection or `top` on a
folder of a chain goes to the chain's row, and `selectionMoved` says so. A folder
not listed yet ends a chain, so hosts list the row's folder when its head opens.
Without `compact`, `folder` and `listed` are ignored and `heads` and `depths` are
absent.

`workspace.repositories.repositories` is ordered with the containing workspace
repository first when present, then repositories under the opened workspace by
workspace containment, depth, and path. Each entry contains an absolute native
Expand Down Expand Up @@ -2092,7 +2107,7 @@ an unreferenced fragment. Requests over 100,000 commits, the largest Git Log a
`rust/fixtures/git/graph-idea/` holds the pinned IntelliJ oracles.

`git.changedFileGroups` groups the changed files of a commit, a stash or a shelf
by folder; it never starts Git. The request is `{ "paths": string[] }`, the
by folder; it never starts Git. The request is `{ "paths": string[], "nest"? }`, the
repository-relative `/`-separated paths in the order Git listed them, at most
200,000. The response is `{ "folders": [{ "path", "files" }] }`: every path lands
in the folder it names before its last `/` (`""` for the top of the repository),
Expand All @@ -2102,6 +2117,15 @@ and file names compare by name, both in the project tree's natural order
before the folders inside it and `v0.1.4.md` before `v0.1.12.md`; names the
natural order cannot tell apart fall back to their raw text, then to request
order. Hosts group once per loaded list and own expansion, rows and drawing.

The optional `nest: { compact? }` asks for the folders as a tree: every folder
above a changed file is listed too, with empty `files`, in the same order, and
each folder adds `depth` (how many folder rows it is under, the top of the
repository and the folders right under it being 0) and `label` (what its row
shows, `""` for the top). With `compact: true`, a folder other than the top that
holds no file and exactly one folder is left out and its row goes to that folder,
whose `label` joins the chain's names with `/`, as IntelliJ IDEA and VS Code
compact folder chains. Without `nest`, `depth` and `label` are absent.
`rust/fixtures/git/changed-file-groups-v1.json` defines the rules.

`git.worktreeCreationPreview` derives what the worktree creation dialog shows;
Expand Down
116 changes: 109 additions & 7 deletions rust/crates/git/src/changed_file_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
//! project tree's natural order (`lithe_core_rules::file_names`), so `v0.1.4` comes before
//! `v0.1.12` and a folder comes right before its subfolders. Which folders are open, the rows
//! they make and how they draw stay in the views.
//!
//! With `nest`, the folders form the tree a folder view shows: every folder above a changed
//! file is listed, those without files of their own too, each with its depth under the top of
//! the repository and the label its row shows. With `nest.compact`, a folder other than the
//! top that holds no file and one folder shares its row with that folder, as IntelliJ IDEA and
//! VS Code compact such chains: the row is the last folder of the chain and its label the names
//! of the chain joined by `/`.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use lithe_core_rules::file_names::FileNameKey;
use lithe_protocol::{CoreError, ErrorCode};
Expand All @@ -24,6 +31,18 @@ const MAX_PATHS: usize = 200_000;
struct Request {
/// Repository-relative paths with `/` separators, in the order Git listed them.
paths: Vec<String>,
/// Lists the folders as a tree with depths and labels; absent keeps the flat grouping.
#[serde(default)]
nest: Option<Nest>,
}

/// How the folders of a nested grouping are laid out.
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Nest {
/// A folder that holds no file and one folder shares its row with that folder.
#[serde(default)]
compact: bool,
}

/// One folder and the files directly in it.
Expand All @@ -34,6 +53,14 @@ struct Folder {
path: String,
/// Indices into the request's `paths` of the files in the folder, in name order.
files: Vec<usize>,
/// With `nest`, how many rows of folders the folder's row is under: 0 for the top of the
/// repository and for the folders right under it.
#[serde(skip_serializing_if = "Option::is_none")]
depth: Option<usize>,
/// With `nest`, what the folder's row shows: its name, or the names of the compact chain
/// it ends joined by `/`; `""` for the top of the repository.
#[serde(skip_serializing_if = "Option::is_none")]
label: Option<String>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -95,14 +122,89 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
keyed.sort_by(|(left_key, left, _), (right_key, right, _)| {
compare_segments(left_key, right_key).then_with(|| left.cmp(right))
});
let folders = keyed
.into_iter()
.map(|(_, folder, files)| Folder {
path: folder.to_string(),
files,
let folders = match &request.nest {
None => keyed
.into_iter()
.map(|(_, folder, files)| Folder {
path: folder.to_string(),
files,
depth: None,
label: None,
})
.collect(),
Some(nest) => nested(keyed, nest.compact),
};
Ok(serde_json::to_value(Response { folders }).expect("changed file groups encode"))
}

/// The folders of `keyed`, in its order, as a tree: the folders above them without files of
/// their own are added, each folder gets its depth and label, and with `compact` a folder that
/// holds no file and one folder gives its row to that folder.
fn nested(keyed: Vec<(Vec<FileNameKey>, &str, Vec<usize>)>, compact: bool) -> Vec<Folder> {
let mut files: HashMap<&str, Vec<usize>> = HashMap::new();
let mut all: Vec<(Vec<FileNameKey>, &str)> = Vec::new();
for (key, folder, indices) in keyed {
let mut above = folder;
while !above.is_empty() {
above = folder_of(above);
if above.is_empty() || files.contains_key(above) {
break;
}
files.insert(above, Vec::new());
let segments = above.split('/').map(FileNameKey::new).collect();
all.push((segments, above));
}
files.insert(folder, indices);
all.push((key, folder));
}
all.sort_by(|(left_key, left), (right_key, right)| {
compare_segments(left_key, right_key).then_with(|| left.cmp(right))
});
let mut inside: HashMap<&str, usize> = HashMap::new();
for (_, folder) in &all {
if !folder.is_empty() {
*inside.entry(folder_of(folder)).or_default() += 1;
}
}
let joined: HashSet<&str> = all
.iter()
.map(|(_, folder)| *folder)
.filter(|folder| {
compact
&& !folder.is_empty()
&& files[folder].is_empty()
&& inside.get(folder).copied() == Some(1)
})
.collect();
Ok(serde_json::to_value(Response { folders }).expect("changed file groups encode"))
let joins = |folder: &str| joined.contains(folder);
// The depth and label of each folder's row; a folder that joins the one inside it hands its
// label on to that folder.
let mut rows: HashMap<&str, (usize, String)> = HashMap::new();
let mut out = Vec::new();
for (_, folder) in all {
let (depth, label) = if folder.is_empty() {
(0, String::new())
} else {
let above = folder_of(folder);
let name = name_of(folder);
match rows.get(above) {
Some((depth, label)) if joins(above) => (*depth, format!("{label}/{name}")),
Some((depth, _)) if !above.is_empty() => (depth + 1, name.to_string()),
_ => (0, name.to_string()),
}
};
rows.insert(folder, (depth, label.clone()));
if joins(folder) {
continue;
}
out.push(Folder {
path: folder.to_string(),
files: files.remove(folder).unwrap_or_default(),
depth: Some(depth),
label: Some(label),
});
}
out
}

/// The folder `path` names before its last `/`, or `""` for a file at the top.
Expand Down
Loading
Loading