Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixd/Controller: support "goto definition" for select expressions #549

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions nixd/lib/Controller/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ nixd::Selector nixd::mkSelector(const nixf::ExprSelect &Select,
return BaseSelector;
}

nixd::Selector nixd::mkSelector(const nixf::ExprSelect &Sel,
const nixf::VariableLookupAnalysis &VLA,
const nixf::ParentMapAnalysis &PM) {
if (Sel.expr().kind() != Node::NK_ExprVar)
throw NotVariableSelect();

const auto &Var = static_cast<ExprVar &>(Sel.expr());

auto BaseSelector = mkIdiomSelector(Var, VLA, PM);

return mkSelector(Sel, std::move(BaseSelector));
}

std::pair<std::vector<std::string>, std::string>
nixd::getScopeAndPrefix(const Node &N, const ParentMapAnalysis &PM) {
if (N.kind() != Node::NK_Identifer)
Expand Down
28 changes: 24 additions & 4 deletions nixd/lib/Controller/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "nixd/Protocol/AttrSet.h"

#include <nixf/Basic/Nodes/Expr.h>
#include <nixf/Sema/ParentMap.h>
#include <nixf/Sema/VariableLookup.h>

Expand Down Expand Up @@ -49,6 +50,8 @@ upEnv(const nixf::Node &Desc, const nixf::VariableLookupAnalysis &VLA,
std::pair<std::vector<std::string>, std::string>
getScopeAndPrefix(const nixf::Node &N, const nixf::ParentMapAnalysis &PM);

struct IdiomException : std::exception {};

/// \brief Exceptions scoped in nixd::mkIdiomSelector
struct IdiomSelectorException : std::exception {};

Expand All @@ -59,13 +62,21 @@ struct NotAnIdiomException : IdiomSelectorException {
}
};

struct VLAException : std::exception {};

/// \brief No such variable.
struct NoSuchVarException : IdiomSelectorException {
struct NoSuchVarException : VLAException {
[[nodiscard]] const char *what() const noexcept override {
return "no such variable";
}
};

struct UndefinedVarException : VLAException {
[[nodiscard]] const char *what() const noexcept override {
return "undefined variable";
}
};

/// \brief Construct a nixd::Selector from \p Var.
///
/// Try to heuristically find a selector of a variable, based on some known
Expand All @@ -74,22 +85,31 @@ Selector mkIdiomSelector(const nixf::ExprVar &Var,
const nixf::VariableLookupAnalysis &VLA,
const nixf::ParentMapAnalysis &PM);

struct SelectorException : std::exception {};

/// \brief The attrpath has a dynamic name, thus it cannot be trivially
/// transformed to "static" selector.
struct DynamicNameException : SelectorException {
struct DynamicNameException : IdiomSelectorException {
[[nodiscard]] const char *what() const noexcept override {
return "dynamic attribute path encountered";
}
};

struct NotVariableSelect : IdiomSelectorException {
[[nodiscard]] const char *what() const noexcept override {
return "the base expression of the select is not a variable";
}
};

/// \brief Construct a nixd::Selector from \p AP.
Selector mkSelector(const nixf::AttrPath &AP, Selector BaseSelector);

/// \brief Construct a nixd::Selector from \p Select.
Selector mkSelector(const nixf::ExprSelect &Select, Selector BaseSelector);

/// \brief Construct a nixd::Selector from \p Select.
Selector mkSelector(const nixf::ExprSelect &Select,
const nixf::VariableLookupAnalysis &VLA,
const nixf::ParentMapAnalysis &PM);

enum class FindAttrPathResult {
OK,
Inherit,
Expand Down
8 changes: 1 addition & 7 deletions nixd/lib/Controller/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,11 @@ void completeSelect(const nixf::ExprSelect &Select, AttrSetClient &Client,
// Ask nixpkgs provider to get idioms completion.
NixpkgsCompletionProvider NCP(Client);

const auto Handler = [](std::exception &E) noexcept {
log(DBG "skipped, reason: {0}", E.what());
};
try {
Selector Sel = mkSelector(Select, mkIdiomSelector(Var, VLA, PM));
NCP.completePackages(mkParams(Sel, IsComplete), List);
} catch (IdiomSelectorException &E) {
Handler(E);
return;
} catch (SelectorException &E) {
Handler(E);
log(DBG "skipped, reason: {0}", E.what());
inclyc marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
Loading
Loading