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
1 change: 1 addition & 0 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ or removing a command fails until the table changes in the same commit.
| `lsp.classifyNavigationTarget` | Classify a language-server jump target as workspace, external read-only or rejected |
| `lsp.declarationOrUsages` | Decide whether a go-to-declaration gesture opens the declaration or shows its usages |
| `editor.lineEdit` | Apply a deterministic line-level text transform and return the replacement plus selection to restore |
| `editor.copyReferenceLocation` | Format a one-based line reference from normalized document symbols; fall back to a file path when no containing symbol exists |
| `editor.find` | Find text or plans bounded replacements in a borrowed document |
| `editor.preferences.resolve` | Resolve persisted editor presentation preferences |
| `editor.recent.reduce` | Reduce and queries bounded recent-file history |
Expand Down
5 changes: 5 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@ pub enum EditorCommand {
/// Applies a deterministic line-level editing transform
/// (`editor.lineEdit`).
LineEdit,
/// Formats a symbol or file reference for one caret position (`editor.copyReferenceLocation`).
CopyReferenceLocation,
/// Finds text or plans bounded replacements in a borrowed document (`editor.find`).
Find,
/// Resolves persisted editor presentation preferences (`editor.preferences.resolve`).
Expand Down Expand Up @@ -1326,6 +1328,9 @@ impl CoreCommand {
"lsp.classifyNavigationTarget" => Some(Self::Lsp(LspCommand::ClassifyNavigationTarget)),
"lsp.declarationOrUsages" => Some(Self::Lsp(LspCommand::DeclarationOrUsages)),
"editor.lineEdit" => Some(Self::Editor(EditorCommand::LineEdit)),
"editor.copyReferenceLocation" => {
Some(Self::Editor(EditorCommand::CopyReferenceLocation))
}
"editor.find" => Some(Self::Editor(EditorCommand::Find)),
"editor.preferences.resolve" => Some(Self::Editor(EditorCommand::PreferencesResolve)),
"editor.recent.reduce" => Some(Self::Editor(EditorCommand::RecentReduce)),
Expand Down
5 changes: 5 additions & 0 deletions rust/crates/core/src/runtime/dispatcher/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub(super) fn dispatch(
EditorCommand::GroupLayoutDropTargets => {
plugin_response(id, payload, lithe_editor_layout::drop_targets_command)
}
EditorCommand::CopyReferenceLocation => plugin_response(
id,
payload,
lithe_editor::reference_location::reference_command,
),
EditorCommand::Find => match lithe_editor::find::command(payload, document) {
Ok(data) => CoreResponse::success(id, data),
Err(error) => CoreResponse::failure(id, error),
Expand Down
17 changes: 17 additions & 0 deletions rust/crates/core/src/tests/editor_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,20 @@ fn navigation_history_keeps_only_the_newest_entries() {
assert_eq!(forward.len() as u64, limit);
assert_eq!(forward[0]["line"], 5);
}

#[test]
fn copy_reference_location_matches_shared_fixture() {
let fixture: Value = serde_json::from_str(include_str!(
"../../../../fixtures/editor/copy-reference-location-v1.json"
))
.unwrap();
for case in fixture["cases"].as_array().unwrap() {
let response: Value = serde_json::from_str(&crate::execute_json(
&json!({"command":"editor.copyReferenceLocation","payload":case["request"]})
.to_string(),
))
.unwrap();
assert_eq!(response["ok"], true, "{}: {response}", case["name"]);
assert_eq!(response["data"], case["expected"], "{}", case["name"]);
}
}
1 change: 1 addition & 0 deletions rust/crates/editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod navigation_input;
pub mod preferences;
pub mod recent;
pub mod recovery;
pub mod reference_location;
pub mod text_edit_plan;

pub use line_edit::{line_edit, LineEditRequest};
Expand Down
242 changes: 242 additions & 0 deletions rust/crates/editor/src/reference_location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
//! Builds a portable reference for the caret from language-server document symbols.
//!
//! The host owns the document path and actual caret, while Core owns the common
//! formatting and symbol-range selection rule. No language-specific source parsing is
//! performed: unknown or incomplete symbols fall back to an explicit file location.

use lithe_protocol::{CoreError, ErrorCode};
use serde::Deserialize;
use serde_json::{json, Value};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Request {
path: String,
line: u32,
utf16_column: u32,
#[serde(default)]
symbols: Vec<Symbol>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Position {
line: u32,
utf16_column: u32,
}

#[derive(Deserialize)]
struct Range {
start: Position,
end: Position,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Symbol {
name: String,
kind: u32,
range: Range,
#[serde(default)]
detail: Option<String>,
#[serde(default)]
container_name: Option<String>,
#[serde(default)]
children: Option<Vec<Symbol>>,
}

/// Formats the server's innermost containing symbol, with a one-based source line.
/// A namespace, type or method is used only when the server reports it: the
/// caller never guesses a package or method signature from the file name.
pub fn reference_command(payload: Value) -> Result<Value, CoreError> {
let request: Request = serde_json::from_value(payload).map_err(|_| {
CoreError::new(
ErrorCode::InvalidRequest,
"Invalid reference location request",
)
})?;
if request.path.is_empty() {
return Err(CoreError::new(
ErrorCode::InvalidRequest,
"Reference location requires a file path",
));
}
let line = u64::from(request.line) + 1;
let position = Position {
line: request.line,
utf16_column: request.utf16_column,
};
let symbol = reference_symbol(&request.symbols, position);
let (reference, source) = match symbol {
Some(symbol) => (format!("{symbol}#L{line}"), "symbol"),
None => (format!("{}#L{line}", request.path), "file"),
};
Ok(json!({"reference":reference, "source":source}))
}

fn contains(range: &Range, position: Position) -> bool {
range.start <= position && position < range.end
}

fn extent(range: &Range) -> (u32, u32) {
(
range.end.line.saturating_sub(range.start.line),
range
.end
.utf16_column
.saturating_sub(range.start.utf16_column),
)
}

// Prefer a nested symbol chain over a flat entry, and a smaller enclosing range
// when a server returns overlapping flat SymbolInformation entries.
fn enclosing(symbols: &[Symbol], position: Position) -> Vec<&Symbol> {
let mut best = Vec::new();
let mut best_extent = (u32::MAX, u32::MAX);
for symbol in symbols {
if !contains(&symbol.range, position) {
continue;
}
let mut candidate = vec![symbol];
candidate.extend(enclosing(
symbol.children.as_deref().unwrap_or(&[]),
position,
));
let span = extent(&symbol.range);
if candidate.len() > best.len() || (candidate.len() == best.len() && span < best_extent) {
best = candidate;
best_extent = span;
}
}
best
}

fn clean(value: &str) -> Option<&str> {
let value = value.trim();
(!value.is_empty() && !value.chars().any(char::is_control)).then_some(value)
}

fn callable_name(symbol: &Symbol) -> Option<String> {
let name = clean(&symbol.name)?;
// LSP implementations may put a method's exact signature in its name,
// or put only the parameter list in detail. Never invent the types.
if name.contains('(') {
return Some(name.to_owned());
}
if let Some(detail) = symbol.detail.as_deref().and_then(clean) {
if detail.starts_with('(') {
if let Some(end) = detail.find(')') {
return Some(format!("{}{}", name, &detail[..=end]));
}
}
}
Some(name.to_owned())
}

fn reference_symbol(symbols: &[Symbol], position: Position) -> Option<String> {
let path = enclosing(symbols, position);
let mut namespaces = Vec::new();
let mut types = Vec::new();
let mut callable = None;
for symbol in &path {
let Some(name) = clean(&symbol.name) else {
continue;
};
match symbol.kind {
// LSP SymbolKind: Module, Namespace, Package
2..=4 => namespaces.push(name.to_owned()),
// Class, Enum, Interface, Struct
5 | 10 | 11 | 23 => types.push(name.to_owned()),
// Method, Constructor, Function
6 | 9 | 12 => callable = callable_name(symbol),
_ => {}
}
}
// Flat SymbolInformation includes containerName instead of nested children.
// Use the server's name as-is; it may already be fully qualified.
if types.is_empty() {
if let Some(container) = path
.last()
.and_then(|s| s.container_name.as_deref())
.and_then(clean)
{
types.push(container.to_owned());
}
}
let mut owner = namespaces.join(".");
if !types.is_empty() {
if !owner.is_empty() {
owner.push('.');
}
owner.push_str(&types.join("."));
}
match (owner.is_empty(), callable) {
(true, None) => None,
(true, Some(method)) => Some(method),
(false, Some(method)) => Some(format!("{owner}#{method}")),
(false, None) => Some(owner),
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn server_symbol_tree_and_detail_are_preserved() {
let symbols = json!([{
"name":"com.example", "kind":4,
"range":{"start":{"line":0,"utf16Column":0},"end":{"line":200,"utf16Column":0}},
"children":[{
"name":"DemoClass","kind":5,
"range":{"start":{"line":1,"utf16Column":0},"end":{"line":190,"utf16Column":0}},
"children":[{
"name":"goHome","kind":6,"detail":"(String,Integer) : void",
"range":{"start":{"line":130,"utf16Column":0},"end":{"line":144,"utf16Column":0}}
}]
}]
}]);
let data = reference_command(
json!({"path":"/tmp/DemoClass.java","line":136,"utf16Column":4,"symbols":symbols}),
)
.unwrap();
assert_eq!(
data["reference"],
"com.example.DemoClass#goHome(String,Integer)#L137"
);
assert_eq!(data["source"], "symbol");
}

#[test]
fn missing_or_nonmatching_symbols_fall_back_to_path() {
for symbols in [
json!([]),
json!([{
"name":"DemoClass","kind":5,
"range":{"start":{"line":10,"utf16Column":0},"end":{"line":12,"utf16Column":0}}
}]),
] {
let data = reference_command(json!({"path":"C:/src/DemoClass.java","line":136,"utf16Column":4,"symbols":symbols})).unwrap();
assert_eq!(data["reference"], "C:/src/DemoClass.java#L137");
assert_eq!(data["source"], "file");
}
}

#[test]
fn flat_method_uses_its_server_container_and_exact_name() {
let symbols = json!([{
"name":"goHome(String,Integer)","kind":6,"containerName":"DemoClass",
"range":{"start":{"line":6,"utf16Column":0},"end":{"line":9,"utf16Column":0}}
}]);
let data = reference_command(
json!({"path":"file.java","line":7,"utf16Column":0,"symbols":symbols}),
)
.unwrap();
assert_eq!(data["reference"], "DemoClass#goHome(String,Integer)#L8");
}

#[test]
fn rejects_empty_path_and_invalid_positions() {
assert!(reference_command(json!({"path":"","line":0,"utf16Column":0})).is_err());
assert!(reference_command(json!({"path":"a.java","line":-1,"utf16Column":0})).is_err());
}
}
Loading
Loading