Skip to content

Commit 74f1f13

Browse files
authored
Reland folding range (#94)
1 parent ef4ce06 commit 74f1f13

File tree

11 files changed

+592
-609
lines changed

11 files changed

+592
-609
lines changed

include/Basic/SourceCode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace clice {
88

99
struct LocalSourceRange {
1010
/// The begin position offset to the source file.
11-
uint32_t begin;
11+
uint32_t begin = -1;
1212

1313
/// The end position offset to the source file.
14-
uint32_t end;
14+
uint32_t end = -1;
1515

1616
constexpr bool operator== (const LocalSourceRange& other) const = default;
1717

include/Compiler/Directive.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct Pragma {
119119
/// Kind of the pragma.
120120
Kind kind;
121121

122-
/// Location of the pragma token.
122+
/// Location of the `#` token.
123123
clang::SourceLocation loc;
124124
};
125125

include/Feature/FoldingRange.h

+27-58
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,59 @@
1-
#include "Basic/Document.h"
1+
#pragma once
2+
23
#include "Basic/SourceCode.h"
34
#include "Index/Shared.h"
4-
#include "Support/JSON.h"
5+
#include "Support/Enum.h"
56

67
namespace clice {
78

8-
namespace proto {
9-
10-
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#foldingRangeClientCapabilities
11-
struct FoldingRangeClientCapabilities {};
9+
class ASTInfo;
1210

13-
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#foldingRangeParams
14-
struct FoldingRangeParams {
15-
/// The text document.
16-
TextDocumentIdentifier textDocument;
17-
};
11+
namespace feature {
1812

1913
struct FoldingRangeKind : refl::Enum<FoldingRangeKind> {
2014
enum Kind : uint8_t {
2115
Invalid = 0,
2216
Comment,
2317
Imports,
2418
Region,
19+
Namespace,
20+
Class,
21+
Enum,
22+
Struct,
23+
Union,
24+
LambdaCapture,
25+
FunctionParams,
26+
FunctionBody,
27+
FunctionCall,
28+
CompoundStmt,
29+
AccessSpecifier,
30+
ConditionDirective,
31+
Initializer,
2532
};
2633

2734
using Enum::Enum;
2835

2936
constexpr static auto InvalidEnum = Invalid;
3037
};
3138

32-
/// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#foldingRange
39+
/// We don't record the coalesced text for a range, because it's rarely useful.
3340
struct FoldingRange {
34-
/// The zero-based start line of the range to fold. The folded area starts after the line's last
35-
/// character.
36-
uinteger startLine;
37-
38-
/// The zero-based end line of the range to fold. The folded area ends with the line's last
39-
/// character.
40-
uinteger endLine;
41-
42-
/// The zero-based character offset from where the folded range starts.
43-
uinteger startCharacter;
44-
45-
/// The zero-based character offset before the folded range ends.
46-
uinteger endCharacter;
41+
/// The range to fold.
42+
LocalSourceRange range;
4743

4844
/// Describes the kind of the folding range.
4945
FoldingRangeKind kind;
5046

51-
// The text that the client should show when the specified range is collapsed.
52-
string collapsedText;
47+
/// The text to display when the folding range is collapsed.
48+
std::string text;
5349
};
5450

55-
using FoldingRangeResult = std::vector<FoldingRange>;
56-
57-
} // namespace proto
58-
59-
class ASTInfo;
60-
class SourceConverter;
61-
62-
namespace feature::foldingrange {
63-
64-
json::Value capability(json::Value clientCapabilities);
65-
66-
/// We don't record the coalesced text for a range, because it's rarely useful.
67-
struct FoldingRange {
68-
LocalSourceRange range;
69-
proto::FoldingRangeKind kind;
70-
};
71-
72-
using Result = std::vector<FoldingRange>;
51+
/// Generate folding range for interested file only.
52+
std::vector<FoldingRange> foldingRange(ASTInfo& AST);
7353

7454
/// Generate folding range for all files.
75-
index::Shared<Result> foldingRange(ASTInfo& AST);
76-
77-
/// Return folding range in main file.
78-
Result foldingRange(proto::FoldingRangeParams param, ASTInfo& AST);
79-
80-
proto::FoldingRange toLspType(const FoldingRange& folding,
81-
const SourceConverter& SC,
82-
llvm::StringRef content);
83-
84-
proto::FoldingRangeResult toLspResult(llvm::ArrayRef<FoldingRange> foldings,
85-
const SourceConverter& SC,
86-
llvm::StringRef content);
55+
index::Shared<std::vector<FoldingRange>> indexFoldingRange(ASTInfo& AST);
8756

88-
} // namespace feature::foldingrange
57+
} // namespace feature
8958

9059
} // namespace clice

include/Index/FeatureIndex.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Shared.h"
66
#include "Feature/SemanticTokens.h"
7+
#include "Feature/FoldingRange.h"
78

89
#include "llvm/ADT/DenseMap.h"
910
#include "clang/Basic/SourceLocation.h"

include/Server/LSPConverter.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class LSPConverter {
1717

1818
Result convert(llvm::StringRef path, llvm::ArrayRef<feature::SemanticToken> tokens);
1919

20+
Result convert(llvm::StringRef path, llvm::ArrayRef<feature::FoldingRange> foldings);
21+
2022
Result convert(const feature::Hover& hover);
2123

2224
private:

include/Support/JSON.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ struct Serde<E> {
343343
}
344344
};
345345

346+
template <typename T>
347+
constexpr inline bool is_optional_v = false;
348+
349+
template <typename T>
350+
constexpr inline bool is_optional_v<std::optional<T>> = true;
351+
346352
template <refl::reflectable_struct T>
347353
struct Serde<T> {
348354
constexpr inline static bool stateful =
@@ -351,9 +357,16 @@ struct Serde<T> {
351357
template <typename... Serdes>
352358
static json::Value serialize(const T& t, Serdes&&... serdes) {
353359
json::Object object;
354-
refl::foreach(t, [&](std::string_view name, auto& member) {
355-
object.try_emplace(llvm::StringRef(name),
356-
json::serialize(member, std::forward<Serdes>(serdes)...));
360+
refl::foreach(t, [&]<typename Field>(std::string_view name, const Field& field) {
361+
if constexpr(is_optional_v<Field>) {
362+
if(field) {
363+
object.try_emplace(llvm::StringRef(name),
364+
json::serialize(*field, std::forward<Serdes>(serdes)...));
365+
}
366+
} else {
367+
object.try_emplace(llvm::StringRef(name),
368+
json::serialize(field, std::forward<Serdes>(serdes)...));
369+
}
357370
});
358371
return object;
359372
}

include/Support/Struct.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace clice::refl {
1212
namespace impl {
1313

1414
struct Any {
15-
consteval Any(std::size_t);
15+
constexpr Any(std::size_t);
1616

1717
template <typename T>
18-
consteval operator T () const;
18+
constexpr operator T () const;
1919
};
2020

2121
template <typename T, std::size_t N>

0 commit comments

Comments
 (0)