@@ -113,10 +113,11 @@ const Definition &findVarDefinition(const ExprVar &Var,
113113}
114114
115115// / \brief Convert nixf::Definition to lspserver::Location
116- Location convertToLocation (const Definition &Def, URIForFile URI) {
116+ Location convertToLocation (llvm::StringRef Src, const Definition &Def,
117+ URIForFile URI) {
117118 return Location{
118119 .uri = std::move (URI),
119- .range = toLSPRange (Def.syntax ()->range ()),
120+ .range = toLSPRange (Src, Def.syntax ()->range ()),
120121 };
121122}
122123
@@ -280,9 +281,9 @@ Locations defineSelect(const ExprSelect &Sel, const VariableLookupAnalysis &VLA,
280281}
281282
282283Locations defineVarStatic (const ExprVar &Var, const VariableLookupAnalysis &VLA,
283- const URIForFile &URI) {
284+ const URIForFile &URI, llvm::StringRef Src ) {
284285 const Definition &Def = findVarDefinition (Var, VLA);
285- return {convertToLocation (Def, URI)};
286+ return {convertToLocation (Src, Def, URI)};
286287}
287288
288289template <class T >
@@ -293,9 +294,9 @@ std::vector<T> mergeVec(std::vector<T> A, const std::vector<T> &B) {
293294
294295Locations defineVar (const ExprVar &Var, const VariableLookupAnalysis &VLA,
295296 const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient,
296- const URIForFile &URI) {
297+ const URIForFile &URI, llvm::StringRef Src ) {
297298 try {
298- Locations StaticLocs = defineVarStatic (Var, VLA, URI);
299+ Locations StaticLocs = defineVarStatic (Var, VLA, URI, Src );
299300
300301 // Nixpkgs locations.
301302 try {
@@ -353,6 +354,8 @@ void Controller::onDefinition(const TextDocumentPositionParams &Params,
353354 auto Action = [Reply = std::move (Reply), URI = Params.textDocument .uri ,
354355 Pos = toNixfPosition (Params.position ), this ]() mutable {
355356 std::string File (URI.file ());
357+ auto Draft = Store.getDraft (File)->Contents ;
358+ auto Src = llvm::StringRef (*Draft);
356359 if (std::shared_ptr<NixTU> TU = getTU (File, Reply)) [[likely]] {
357360 if (std::shared_ptr<Node> AST = getAST (*TU, Reply)) [[likely]] {
358361 const VariableLookupAnalysis &VLA = *TU->variableLookup ();
@@ -374,12 +377,12 @@ void Controller::onDefinition(const TextDocumentPositionParams &Params,
374377 return Reply (squash ([&]() -> llvm::Expected<Locations> {
375378 // Special case for inherited names.
376379 if (const ExprVar *Var = findInheritVar (N, PM, VLA))
377- return defineVar (*Var, VLA, PM, *nixpkgsClient (), URI);
380+ return defineVar (*Var, VLA, PM, *nixpkgsClient (), URI, Src );
378381
379382 switch (UpExpr.kind ()) {
380383 case Node::NK_ExprVar: {
381384 const auto &Var = static_cast <const ExprVar &>(UpExpr);
382- return defineVar (Var, VLA, PM, *nixpkgsClient (), URI);
385+ return defineVar (Var, VLA, PM, *nixpkgsClient (), URI, Src );
383386 }
384387 case Node::NK_ExprSelect: {
385388 const auto &Sel = static_cast <const ExprSelect &>(UpExpr);
0 commit comments