Skip to content

Commit 73fccb4

Browse files
committed
YQL-20112: Support experimental documentation
LSP supports the following propoerty of the completion item, that supports Markdown format: ``` /** * A human-readable string that represents a doc-comment. */ documentation?: string | MarkupContent; ``` I added docs only for a few items just to check a look in the UI. Also some docs are shortened. Documentation is opt-in decorator for a NameService. Because it is not needed in the YDB CLI, for example. commit_hash:b400ed1224be2906b7fb1da29e9c97aa7578d710
1 parent 6eff12e commit 73fccb4

File tree

9 files changed

+147
-3
lines changed

9 files changed

+147
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "documentation.h"
2+
3+
#include <yql/essentials/sql/v1/complete/name/service/name_service.h>
4+
5+
namespace NSQLComplete {
6+
7+
namespace {
8+
9+
class TDocumentation: public IDocumentation {
10+
public:
11+
explicit TDocumentation(TDocByNormalizedNameMap docs)
12+
: Docs_(std::move(docs))
13+
{
14+
}
15+
16+
TMaybe<TString> Lookup(TStringBuf name) const override {
17+
if (auto* ptr = Docs_.FindPtr(Normalized(name))) {
18+
return *ptr;
19+
}
20+
return Nothing();
21+
}
22+
23+
private:
24+
TString Normalized(TStringBuf name) const {
25+
return NormalizeName(name);
26+
}
27+
28+
TDocByNormalizedNameMap Docs_;
29+
};
30+
31+
} // namespace
32+
33+
IDocumentation::TPtr MakeStaticDocumentation(TDocByNormalizedNameMap docs) {
34+
return new TDocumentation(std::move(docs));
35+
}
36+
37+
} // namespace NSQLComplete
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <util/generic/ptr.h>
4+
#include <util/generic/maybe.h>
5+
6+
namespace NSQLComplete {
7+
8+
class IDocumentation: public TThrRefBase {
9+
public:
10+
using TPtr = TIntrusivePtr<IDocumentation>;
11+
12+
virtual TMaybe<TString> Lookup(TStringBuf name) const = 0;
13+
};
14+
15+
using TDocByNormalizedNameMap = THashMap<TString, TString>;
16+
17+
IDocumentation::TPtr MakeStaticDocumentation(TDocByNormalizedNameMap docs);
18+
19+
} // namespace NSQLComplete
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "name_service.h"
2+
3+
namespace NSQLComplete {
4+
5+
namespace {
6+
7+
class TNameService: public INameService {
8+
public:
9+
TNameService(IDocumentation::TPtr docs, INameService::TPtr origin)
10+
: Docs_(std::move(docs))
11+
, Origin_(std::move(origin))
12+
{
13+
}
14+
15+
NThreading::TFuture<TNameResponse> Lookup(const TNameRequest& request) const override {
16+
return Origin_->Lookup(request).Apply([docs = Docs_](auto f) {
17+
TNameResponse response = f.ExtractValue();
18+
for (TGenericName& name : response.RankedNames) {
19+
name = std::visit([&](auto&& name) -> TGenericName {
20+
using T = std::decay_t<decltype(name)>;
21+
22+
if constexpr (std::is_base_of_v<TDescribed, T> &&
23+
std::is_base_of_v<TIdentifier, T>) {
24+
name.Description = docs->Lookup(name.Identifier);
25+
}
26+
27+
return std::move(name);
28+
}, std::move(name));
29+
}
30+
return response;
31+
});
32+
}
33+
34+
private:
35+
IDocumentation::TPtr Docs_;
36+
INameService::TPtr Origin_;
37+
};
38+
39+
} // namespace
40+
41+
INameService::TPtr MakeDocumentingNameService(IDocumentation::TPtr docs, INameService::TPtr origin) {
42+
return new TNameService(std::move(docs), std::move(origin));
43+
}
44+
45+
} // namespace NSQLComplete
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "documentation.h"
4+
5+
#include <yql/essentials/sql/v1/complete/name/service/name_service.h>
6+
7+
namespace NSQLComplete {
8+
9+
INameService::TPtr MakeDocumentingNameService(IDocumentation::TPtr docs, INameService::TPtr origin);
10+
11+
} // namespace NSQLComplete
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
LIBRARY()
2+
3+
SRCS(
4+
documentation.cpp
5+
name_service.cpp
6+
)
7+
8+
PEERDIR(
9+
yql/essentials/sql/v1/complete/name/service
10+
)
11+
12+
END()

yql/essentials/sql/v1/complete/name/service/name_service.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace NSQLComplete {
2020
TString Namespace;
2121
};
2222

23+
struct TDescribed {
24+
TMaybe<TString> Description;
25+
};
26+
2327
struct TKeyword {
2428
TString Content;
2529
};
@@ -40,7 +44,7 @@ namespace NSQLComplete {
4044
EKind Kind = EKind::Simple;
4145
};
4246

43-
struct TFunctionName: TIdentifier {
47+
struct TFunctionName: TIdentifier, TDescribed {
4448
struct TConstraints: TNamespaced {};
4549
};
4650

yql/essentials/sql/v1/complete/name/service/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ RECURSE(
1515
binding
1616
cluster
1717
column
18+
docs
1819
impatient
1920
ranking
2021
schema

yql/essentials/sql/v1/complete/name_mapping.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ namespace NSQLComplete {
143143
TCandidate ToCandidate(TGenericName generic, TLocalSyntaxContext& local) {
144144
return std::visit([&](auto&& name) -> TCandidate {
145145
using T = std::decay_t<decltype(name)>;
146+
146147
constexpr bool IsContextSensitive =
147148
std::is_same_v<T, TKeyword> ||
148149
std::is_same_v<T, TFolderName> ||
@@ -151,11 +152,24 @@ namespace NSQLComplete {
151152
std::is_same_v<T, TBindingName> ||
152153
std::is_same_v<T, TUnknownName>;
153154

155+
constexpr bool IsDocumented =
156+
std::is_base_of_v<TDescribed, T>;
157+
158+
TMaybe<TString> documentation;
159+
if constexpr (IsDocumented) {
160+
documentation = std::move(name.Description);
161+
}
162+
163+
TCandidate candidate;
154164
if constexpr (IsContextSensitive) {
155-
return ToCandidate(std::move(name), local);
165+
candidate = ToCandidate(std::move(name), local);
156166
} else {
157-
return ToCandidate(std::move(name));
167+
candidate = ToCandidate(std::move(name));
158168
}
169+
170+
candidate.Documentation = std::move(documentation);
171+
172+
return candidate;
159173
}, std::move(generic));
160174
}
161175

yql/essentials/sql/v1/complete/sql_complete.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace NSQLComplete {
3939
ECandidateKind Kind;
4040
TString Content;
4141
size_t CursorShift = 0;
42+
TMaybe<TString> Documentation = Nothing();
4243

4344
friend bool operator==(const TCandidate& lhs, const TCandidate& rhs) = default;
4445

0 commit comments

Comments
 (0)