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
22 changes: 19 additions & 3 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ or removing a command fails until the table changes in the same commit.
| `lsp.builtinNavigation` | Return lightweight current-file definition/reference locations |
| `lsp.mergeCompletions` | Choose which candidates survive when several prioritized providers complete at one position |
| `lsp.activationPolicy` | Decide whether a language server may start automatically after failures, using a platform monotonic clock |
| `lsp.problems.query` | Filter, order, and group Problems tool-window diagnostics and count them by severity |
| `lsp.problems.query` | Filter, order, and group Problems tool-window diagnostics, count them by severity, and lay the tree rows out |
| `lsp.references.query` | Group Find Usages locations by file and lay out the References tool window's visible rows, selection, and previous/next usages |
| `lsp.references.lines` | Show the lines of References tool-window usages with highlighted usage ranges, as search results show theirs |
| `lsp.startServer` | Start one Rust-owned process/session and begin initialization |
Expand Down Expand Up @@ -2979,8 +2979,24 @@ stable `key`, `indices` into the submitted list, and per-severity `counts`),
`files` (file summaries in file order regardless of `groupBy`),
`visibleCounts`, and `totalCounts`. `fileKey` is the platform's native file
identity; `displayPath` is the workspace-relative path and orders files with
the Project view's natural file-name order. Severity is the LSP number;
missing or out-of-range values are unknown (`0`) and rank last. Cases:
the Project view's natural file-name order; file groups also carry `name`
(its last segment) and `folder` (the rest, empty at the root). Severity is the
LSP number; missing or out-of-range values are unknown (`0`) and rank last.
The request also carries the platform-held tree state: `collapsed` (the keys
of closed group nodes; unknown keys are ignored) and the `selected` row key.
The response lays the tree out the way IntelliJ IDEA's Problems window does:
`rows` are the rows that show from top to bottom, each with `key`, `kind`
(`group`, `diagnostic`), `depth`, `expanded` (group nodes only), `group`, and
`shows` (the diagnostic the row previews and opens: itself, or the first under
a node). Group nodes (`file:<fileKey>`, `severity:<n>`) head their diagnostics
at depth 1; with `groupBy` `none` there are no nodes and the diagnostics sit at
depth 0. A diagnostic's key is `diagnostic:<line>:<column>:<ordinal>:<fileKey>`,
where `ordinal` counts the earlier submitted diagnostics at the same file and
position, so a selection stays on a diagnostic the server republishes at
another index. `selected` and `selectedRow` name the selected row: an absent
or unknown key selects nothing, a diagnostic a closed node hides moves the
selection to that node, and a diagnostic that is gone moves it to its file's
node when the list is grouped by file and the file still shows. Cases:
`rust/fixtures/lsp/problems-query-v1.json`.

`lsp.references.query` accepts the Find Usages answer as
Expand Down
3 changes: 2 additions & 1 deletion rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ pub enum LspCommand {
MergeCompletions,
/// Decides whether a language server may start automatically (`lsp.activationPolicy`).
ActivationPolicy,
/// Filters, orders, and groups Problems tool-window diagnostics (`lsp.problems.query`).
/// Filters, orders, and groups Problems tool-window diagnostics and lays the tree rows out
/// (`lsp.problems.query`).
ProblemsQuery,
/// Groups Find Usages locations by file and lays out the References tool
/// window's rows (`lsp.references.query`).
Expand Down
4 changes: 4 additions & 0 deletions rust/crates/core/src/tests/lsp_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ fn malformed_requests_fail_with_invalid_request() {
"lsp.problems.query",
json!({"diagnostics": [], "groupBy": "type"}),
),
(
"lsp.problems.query",
json!({"diagnostics": [], "collapsed": "file:a"}),
),
(
"lsp.references.query",
json!({"locations": [{"fileKey": "a"}]}),
Expand Down
198 changes: 188 additions & 10 deletions rust/crates/lsp-policy/src/problems.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
//! Problems tool-window filtering, ordering, and grouping (`lsp.problems.query`).
//! Problems tool-window filtering, ordering, grouping, and tree rows
//! (`lsp.problems.query`).
//!
//! Platforms submit the diagnostics they display together with the active
//! view options. Core returns indices into the submitted list, so the large
//! message payload never crosses the boundary a second time. File paths are
//! compared with the IDEA natural file-name order that the Project view uses,
//! which keeps `File2` before `File10` on both platforms.
//!
//! The tool window shows the groups the way IntelliJ IDEA's Problems window
//! does: a node per file (or severity) over its diagnostics. Platforms keep
//! which nodes are collapsed and which row is selected, and ask Core again
//! after a change; Core returns the rows that show and moves a selection that
//! a collapsed node hides to that node. A diagnostic's row key names its file,
//! position, and place among the diagnostics at that position rather than its
//! index, so the selection stays on it when the server republishes the list.

use lithe_core_rules::file_names::FileNameKey;
use lithe_protocol::{CoreError, ErrorCode};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};

/// LSP severities in display order; unknown (`0`) ranks after hints.
const SEVERITY_RANKS: [u8; 5] = [1, 2, 3, 4, 0];
Expand All @@ -26,6 +35,12 @@ struct Request {
sort_by: SortBy,
#[serde(default)]
filter: Filter,
/// The keys of the group nodes the user closed; unknown keys are ignored.
#[serde(default)]
collapsed: Vec<String>,
/// The key of the selected row; absent or unknown selects nothing.
#[serde(default)]
selected: Option<String>,
}

/// One displayed diagnostic. Only the fields that affect filtering and order
Expand Down Expand Up @@ -142,13 +157,48 @@ struct Group {
file_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
display_path: Option<String>,
/// The last segment of a file group's `displayPath`.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
/// A file group's `displayPath` without its last segment; empty at the
/// workspace root.
#[serde(skip_serializing_if = "Option::is_none")]
folder: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
severity: Option<u8>,
/// Indices into the submitted `diagnostics`, in display order.
indices: Vec<usize>,
counts: SeverityCounts,
}

#[derive(Serialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
enum RowKind {
Group,
Diagnostic,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Row {
/// The group's key, or `diagnostic:<line>:<column>:<ordinal>:<fileKey>`
/// where `ordinal` counts the earlier submitted diagnostics at the same
/// file and position; stable while nodes open and close.
key: String,
kind: RowKind,
/// 0 for a group node and for a diagnostic of the ungrouped list, 1 for a
/// diagnostic under a node.
depth: u8,
/// Whether a node's diagnostics show; absent on a diagnostic.
#[serde(skip_serializing_if = "Option::is_none")]
expanded: Option<bool>,
/// The row's group.
group: usize,
/// The diagnostic the row previews and opens: itself, or the first
/// diagnostic under a node.
shows: usize,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct FileSummary {
Expand All @@ -167,6 +217,15 @@ struct Response {
files: Vec<FileSummary>,
visible_counts: SeverityCounts,
total_counts: SeverityCounts,
/// The rows that show, top to bottom; empty when nothing is visible.
rows: Vec<Row>,
/// The key of the selected row, always one of `rows`; absent when nothing
/// is selected.
#[serde(skip_serializing_if = "Option::is_none")]
selected: Option<String>,
/// The position of the selected row in `rows`.
#[serde(skip_serializing_if = "Option::is_none")]
selected_row: Option<usize>,
}

struct Entry<'a> {
Expand Down Expand Up @@ -273,14 +332,23 @@ fn query(request: &Request) -> Response {
let groups = match request.group_by {
GroupBy::File => files
.iter()
.map(|file| Group {
key: format!("file:{}", file.facts.file_key),
kind: "file",
file_key: Some(file.facts.file_key.clone()),
display_path: Some(file.facts.display_path.clone()),
severity: None,
indices: file.indices.clone(),
counts: file.counts,
.map(|file| {
let path = file.facts.display_path.as_str();
let (folder, name) = match path.rfind('/') {
Some(at) => (&path[..at], &path[at + 1..]),
None => ("", path),
};
Group {
key: file_group_key(&file.facts.file_key),
kind: "file",
file_key: Some(file.facts.file_key.clone()),
display_path: Some(path.to_string()),
name: Some(name.to_string()),
folder: Some(folder.to_string()),
severity: None,
indices: file.indices.clone(),
counts: file.counts,
}
})
.collect(),
GroupBy::Severity => SEVERITY_RANKS
Expand All @@ -299,6 +367,8 @@ fn query(request: &Request) -> Response {
kind: "severity",
file_key: None,
display_path: None,
name: None,
folder: None,
severity: Some(severity),
indices,
counts,
Expand All @@ -312,6 +382,8 @@ fn query(request: &Request) -> Response {
kind: "all",
file_key: None,
display_path: None,
name: None,
folder: None,
severity: None,
indices: visible.iter().map(|entry| entry.index).collect(),
counts: visible_counts,
Expand All @@ -327,14 +399,120 @@ fn query(request: &Request) -> Response {
counts: file.counts,
})
.collect();
let keys = diagnostic_keys(&request.diagnostics);
let (rows, selected) = tree(request, &groups, &keys);
let selected_row = selected
.as_ref()
.and_then(|key| rows.iter().position(|row| &row.key == key));
Response {
groups,
files,
visible_counts,
total_counts,
rows,
selected,
selected_row,
}
}

/// The rows that show and the key of the selected one. Group nodes head
/// their diagnostics except in the ungrouped list, whose diagnostics sit at
/// the top level. A selected diagnostic that a closed node hides moves to
/// that node; one that is gone moves to its file's node when the list is
/// grouped by file and the file still shows, and is dropped otherwise.
fn tree(request: &Request, groups: &[Group], keys: &[String]) -> (Vec<Row>, Option<String>) {
let headed = request.group_by != GroupBy::None;
let collapsed: BTreeSet<&str> = request.collapsed.iter().map(String::as_str).collect();
let expanded: Vec<bool> = groups
.iter()
.map(|group| !headed || !collapsed.contains(group.key.as_str()))
.collect();
let mut rows = Vec::new();
for (slot, group) in groups.iter().enumerate() {
if headed {
rows.push(Row {
key: group.key.clone(),
kind: RowKind::Group,
depth: 0,
expanded: Some(expanded[slot]),
group: slot,
shows: group.indices[0],
});
}
if !expanded[slot] {
continue;
}
for index in &group.indices {
rows.push(Row {
key: keys[*index].clone(),
kind: RowKind::Diagnostic,
depth: u8::from(headed),
expanded: None,
group: slot,
shows: *index,
});
}
}

let Some(wanted) = request.selected.as_deref() else {
return (rows, None);
};
if headed && groups.iter().any(|group| group.key == wanted) {
return (rows, Some(wanted.to_string()));
}
if let Some(index) = keys.iter().position(|key| key == wanted) {
if let Some(slot) = groups
.iter()
.position(|group| group.indices.contains(&index))
{
let shown = if expanded[slot] {
wanted.to_string()
} else {
groups[slot].key.clone()
};
return (rows, Some(shown));
}
}
let file = (request.group_by == GroupBy::File)
.then(|| diagnostic_file(wanted))
.flatten()
.map(file_group_key)
.filter(|key| groups.iter().any(|group| &group.key == key));
(rows, file)
}

/// Each submitted diagnostic's row key, in submission order.
fn diagnostic_keys(diagnostics: &[DiagnosticFacts]) -> Vec<String> {
let mut seen = BTreeMap::<(&str, u64, u64), usize>::new();
diagnostics
.iter()
.map(|facts| {
let ordinal = seen
.entry((facts.file_key.as_str(), facts.line, facts.column))
.or_insert(0);
let key = format!(
"diagnostic:{}:{}:{}:{}",
facts.line, facts.column, ordinal, facts.file_key
);
*ordinal += 1;
key
})
.collect()
}

/// The `fileKey` a diagnostic row key names, or `None` for another key.
fn diagnostic_file(key: &str) -> Option<&str> {
let mut parts = key.strip_prefix("diagnostic:")?.splitn(4, ':');
for _ in 0..3 {
parts.next()?.parse::<u64>().ok()?;
}
parts.next()
}

fn file_group_key(file_key: &str) -> String {
format!("file:{file_key}")
}

struct FileGroup<'a> {
/// The first visible diagnostic of the file names the group.
facts: &'a DiagnosticFacts,
Expand Down
Loading
Loading