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
26 changes: 18 additions & 8 deletions docs/en/features/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ Implicit navigation requires an unambiguous source token — patterns where the

Navigate from a symbol usage or definition to its declaration. In C++, many entities have separate declarations and definitions; go-to-declaration always targets the declaration side.

- [ ] Functions — from usage or out-of-line definition to the declaration/prototype
clice returns the declaration locations plus the definition: symbols defined inline have no separate declaration, and navigating to the definition is what clients expect in that case.

- [x] Index-based cross-TU go-to-declaration
- [x] Functions — from usage or out-of-line definition to the declaration/prototype

```cpp
// widget.h
Expand Down Expand Up @@ -289,7 +292,9 @@ Navigate from a symbol usage or definition to its declaration. In C++, many enti

## Go to Implementation

- [ ] Virtual method → all override implementations
- [x] Index-based go-to-implementation (direct overrides; each level of a
deep chain navigates to its own overriders)
- [x] Virtual method → all override implementations

```cpp
struct Base { virtual void draw(); };
Expand Down Expand Up @@ -339,7 +344,11 @@ Navigate from a symbol usage or definition to its declaration. In C++, many enti

Navigate to the type definition of a symbol. Applicable to variables, parameters, fields, and any other named entity that has a type. When the type is a type alias or a pointer-like wrapper, navigation should unwrap to the underlying/pointee type.

- [ ] Local variables and parameters
- [x] Index-based go-to-type-definition for declared entities (variables,
parameters, fields). Known limitations: `auto`-deduced variables have no
type relation yet, and alias-typed variables navigate to the `using`
declaration rather than unwrapping to the underlying type.
- [x] Local variables and parameters

```cpp
void process(Widget w) {
Expand Down Expand Up @@ -575,8 +584,9 @@ Highlight all references to the symbol under cursor within the current file (`te

## Changelog

| Date | Change | PR |
| ---- | ------------------------------------------------ | --- |
| — | Index-based go-to-definition and find references | — |
| — | Call hierarchy (incoming/outgoing) | — |
| — | Type hierarchy (supertypes/subtypes) | — |
| Date | Change | PR |
| ---- | -------------------------------------------------------------------------------------------------- | --- |
| — | declaration / implementation / typeDefinition; references includeDeclaration includes declarations | — |
| — | Index-based go-to-definition and find references | — |
| — | Call hierarchy (incoming/outgoing) | — |
| — | Type hierarchy (supertypes/subtypes) | — |
34 changes: 27 additions & 7 deletions src/server/compiler/indexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,9 @@ Indexer::CursorHit Indexer::resolve_cursor(llvm::StringRef path,
return hit;
}

// Fallback to MergedIndex, using session text for position -> offset.
if(!session)
return {};
auto offset = session->line_map().to_offset(position);
if(!offset)
return {};

// Fallback to MergedIndex. Position -> offset uses the session text when
// one exists (open but not yet compiled); for closed files the shard's
// own stored content provides the mapping.
auto proj_it = workspace.project_index.path_pool.find(path);
if(proj_it == workspace.project_index.path_pool.cache.end())
return {};
Expand All @@ -354,6 +350,10 @@ Indexer::CursorHit Indexer::resolve_cursor(llvm::StringRef path,
if(ls.empty())
return {};
lsp::LineMap map(merged_index.content(), ls);

auto offset = session ? session->line_map().to_offset(position) : map.to_offset(position);
if(!offset)
return {};
CursorHit hit;
merged_index.lookup(*offset, [&](const index::Occurrence& o) {
auto range = map.to_range(o.range.begin, o.range.end);
Expand Down Expand Up @@ -414,6 +414,26 @@ std::vector<protocol::Location> Indexer::query_relations(llvm::StringRef path,
return locations;
}

std::vector<protocol::Location> Indexer::query_symbol_targets(llvm::StringRef path,
const protocol::Position& position,
RelationKind kind,
Session* session) {
auto hit = resolve_cursor(path, position, session);
if(hit.hash == 0)
return {};

llvm::SmallVector<index::SymbolHash> targets;
collect_unique_targets(hit.hash, kind, targets);

std::vector<protocol::Location> locations;
for(auto target: targets) {
if(auto info = resolve_symbol(target)) {
locations.push_back({std::move(info->uri), info->range});
}
}
return locations;
}

std::optional<SymbolInfo> Indexer::lookup_symbol(const std::string& uri,
llvm::StringRef path,
const protocol::Position& position,
Expand Down
10 changes: 10 additions & 0 deletions src/server/compiler/indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ class Indexer {
RelationKind kind,
Session* session);

/// Query between-symbol relations (Implementation, TypeDefinition, ...)
/// for the symbol at cursor and resolve each target symbol to its
/// definition location — the two-hop query behind go-to-implementation
/// and go-to-type-definition.
/// @param session Active Session for this file, or nullptr.
std::vector<protocol::Location> query_symbol_targets(llvm::StringRef path,
const protocol::Position& position,
RelationKind kind,
Session* session);

/// Look up symbol info (hash, name, kind, range) at a cursor position.
/// @param session Active Session for this file, or nullptr.
std::optional<SymbolInfo> lookup_symbol(const std::string& uri,
Expand Down
73 changes: 49 additions & 24 deletions src/server/service/lsp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ LSPClient::LSPClient(MasterServer& server, kota::ipc::JsonPeer& peer) : server(s
return this->server.indexer.query_relations(path, pos, kind, session);
};

auto query_targets_at = [this,
resolve_uri](const std::string& uri,
const protocol::Position& pos,
RelationKind kind) -> std::vector<protocol::Location> {
auto [path, path_id, session] = resolve_uri(uri);
return this->server.indexer.query_symbol_targets(path, pos, kind, session);
};

auto resolve_item =
[this,
resolve_uri](const std::string& uri,
Expand Down Expand Up @@ -423,38 +431,55 @@ LSPClient::LSPClient(MasterServer& server, kota::ipc::JsonPeer& peer) : server(s
pos);
});

peer.on_request([this, query_at](RequestContext& ctx,
const protocol::ReferenceParams& params) -> RawResult {
// The navigation handlers below are index-only: closed documents are
// fully serveable from the index, and an empty result is a real answer,
// returned as [] — never an error.
peer.on_request(
[query_at](RequestContext& ctx, const protocol::ReferenceParams& params) -> RawResult {
auto& uri = params.text_document_position_params.text_document.uri;
auto& pos = params.text_document_position_params.position;

auto locations = query_at(uri, pos, RelationKind::Reference);

if(params.context.include_declaration) {
for(auto kind: {RelationKind::Declaration, RelationKind::Definition}) {
auto extra = query_at(uri, pos, kind);
locations.insert(locations.end(),
std::make_move_iterator(extra.begin()),
std::make_move_iterator(extra.end()));
}
}

co_return to_raw(locations);
});

peer.on_request([query_targets_at](RequestContext& ctx,
const protocol::TypeDefinitionParams& params) -> RawResult {
auto& uri = params.text_document_position_params.text_document.uri;
auto& pos = params.text_document_position_params.position;
co_return to_raw(query_targets_at(uri, pos, RelationKind::TypeDefinition));
});

auto locations = query_at(uri, pos, RelationKind::Reference);
peer.on_request([query_targets_at](RequestContext& ctx,
const protocol::ImplementationParams& params) -> RawResult {
auto& uri = params.text_document_position_params.text_document.uri;
auto& pos = params.text_document_position_params.position;
co_return to_raw(query_targets_at(uri, pos, RelationKind::Implementation));
});

if(params.context.include_declaration) {
// Declarations plus the definition: symbols defined inline have no
// separate Declaration relation, and navigating to the definition is
// what every client expects in that case.
peer.on_request(
[query_at](RequestContext& ctx, const protocol::DeclarationParams& params) -> RawResult {
auto& uri = params.text_document_position_params.text_document.uri;
auto& pos = params.text_document_position_params.position;
auto locations = query_at(uri, pos, RelationKind::Declaration);
auto defs = query_at(uri, pos, RelationKind::Definition);
locations.insert(locations.end(),
std::make_move_iterator(defs.begin()),
std::make_move_iterator(defs.end()));
}

if(locations.empty())
co_return serde_raw{"null"};
co_return to_raw(locations);
});

peer.on_request(
[this](RequestContext& ctx, const protocol::TypeDefinitionParams& params) -> RawResult {
co_return serde_raw{"null"};
});

peer.on_request(
[this](RequestContext& ctx, const protocol::ImplementationParams& params) -> RawResult {
co_return serde_raw{"null"};
});

peer.on_request(
[this](RequestContext& ctx, const protocol::DeclarationParams& params) -> RawResult {
co_return serde_raw{"null"};
co_return to_raw(locations);
});

peer.on_request([this](RequestContext& ctx,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/index_features/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.20)
project(index_features CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(main main.cpp)
add_executable(main main.cpp nav.cpp)
19 changes: 19 additions & 0 deletions tests/data/index_features/nav.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "nav.h"

int area(const Shape& s) {
return s.width * s.height;
}

Shape global_shape;

int shape_area() {
return area(global_shape);
}

void GLRenderer::render() {}

void DebugGLRenderer::render() {}

Shape make_unit_shape() {
return {1, 1};
}
26 changes: 26 additions & 0 deletions tests/data/index_features/nav.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

struct Shape;

int area(const Shape& s);

struct Shape {
int width;
int height;
};

struct Renderer {
virtual void render() = 0;

virtual ~Renderer() = default;
};

struct GLRenderer : Renderer {
void render() override;
};

struct DebugGLRenderer : GLRenderer {
void render() override;
};

Shape make_unit_shape();
Loading