-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixd/AST: support construct attrpaths from AST
- Loading branch information
Showing
7 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "nixd/AST/ParseAST.h" | ||
|
||
#include "lspserver/Protocol.h" | ||
|
||
#include <map> | ||
|
||
namespace nixd { | ||
|
||
class AttrLocator { | ||
std::map<lspserver::Range, const nix::Expr *> Attrs; | ||
|
||
public: | ||
AttrLocator(const ParseAST &AST); | ||
|
||
/// Locate a nix attribute, \return the pointer to the expression | ||
/// { a = "1"; } | ||
/// ^ ^~~ | ||
/// Pos pointer | ||
[[nodiscard]] const nix::Expr *locate(lspserver::Position Pos) const; | ||
}; | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "nixd/AST/AttrLocator.h" | ||
#include "nixd/AST/ParseAST.h" | ||
#include "nixd/Expr/Expr.h" | ||
|
||
namespace nixd { | ||
|
||
AttrLocator::AttrLocator(const ParseAST &AST) { | ||
// Traverse the AST, record all attr locations | ||
// We can assume that Nix attrs are not interleaving | ||
// i.e. they are concrete tokens. | ||
struct VTy : RecursiveASTVisitor<VTy> { | ||
AttrLocator &This; | ||
const ParseAST &AST; | ||
bool visitExprAttrs(const nix::ExprAttrs *EA) { | ||
for (const auto &[Symbol, Attr] : EA->attrs) { | ||
lspserver::Range Range = AST.nPair(Attr.pos); | ||
// Note: "a.b.c" will be assigned more than once | ||
// Our visitor model ensures the order that descendant nodes will be | ||
// visited at last, for nesting attrs. | ||
This.Attrs[Range] = Attr.e; | ||
} | ||
return true; | ||
} | ||
} V{.This = *this, .AST = AST}; | ||
V.traverseExpr(AST.root()); | ||
} | ||
|
||
const nix::Expr *AttrLocator::locate(lspserver::Position Pos) const { | ||
auto It = Attrs.upper_bound({Pos, {INT_MAX, INT_MAX}}); | ||
if (It == Attrs.begin()) | ||
return nullptr; | ||
--It; | ||
const auto &[Range, Expr] = *It; | ||
|
||
// Check that the range actually contains the desired position. | ||
// Otherwise, we should return nullptr, denoting the position is not covered | ||
// by any knwon attrs. | ||
return Range.contains(Pos) ? Expr : nullptr; | ||
} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters