Skip to content

Commit 49af4a9

Browse files
authored
Clean Server (#93)
1 parent 74f1f13 commit 49af4a9

38 files changed

+1406
-1303
lines changed

docs/configuration.md

+38
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,41 @@ If `index.instantiation` is `true`, clice will traverse declarations in template
177177
178178
If `index.instantiation` is `false`, clice will not index entities inside template instantiations, and `go-to-definition` will return no results.
179179
<br>
180+
181+
## Feature
182+
183+
### Semantic Tokens
184+
185+
| Name | Type | Default |
186+
| ------------------------------------ | ------------------ | ------- |
187+
| `feature.semanticTokens.typeMap` | `array` of `table` | `[]` |
188+
| `feature.semanticTokens.modifierMap` | `array` of `table` | `[]` |
189+
190+
Maps the customized semantic symbol kinds or modifier to LSP semantic token types or modifiers.
191+
192+
Example:
193+
194+
```toml
195+
[feature.semanticTokens]
196+
typeMap = [
197+
{"from": "header", "to": "string"},
198+
{"from": "attribute", "to": "decorator"},
199+
]
200+
201+
modifierMap = [
202+
{"from": "const", "to": "readonly"},
203+
{"from": "pureVirtual", "to": "abstract"},
204+
]
205+
```
206+
207+
For all clice symbol kinds, please refer to [SymbolKind](https://github.com/clice-project/clice/blob/main/include/AST/SymbolKind.h). The first letter of the name should be translated to lowercase. For all LSP semantic token types, refer to [SemanticTokenKind](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens).
208+
209+
210+
| Name | Type | Default |
211+
| --------------------------------- | --------- | ------- |
212+
| `feature.semanticTokens.standard` | `boolean` | `false` |
213+
214+
If `true`, clice will map the semantic token types and modifiers to the standard ones automatically.
215+
216+
### Folding Range
217+

include/Basic/Lifecycle.h

+56
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,59 @@ struct SemanticTokensOptions {
7878
bool full = true;
7979
};
8080

81+
struct SemanticTokens {
82+
/// The actual tokens.
83+
std::vector<std::uint32_t> data;
84+
};
85+
86+
/// A set of predefined range kinds.
87+
enum class FoldingRangeKind {
88+
/// Folding range for a comment.
89+
Comment,
90+
91+
/// Folding range for imports or includes.
92+
Imports,
93+
94+
/// Folding range for a region.
95+
Region,
96+
};
97+
98+
/// Represents a folding range. To be valid, start and end line must be bigger
99+
/// than zero and smaller than the number of lines in the document. Clients
100+
/// are free to ignore invalid ranges.
101+
struct FoldingRange {
102+
/// The zero-based start line of the range to fold. The folded area starts
103+
/// after the line's last character. To be valid, the end must be zero or
104+
/// larger and smaller than the number of lines in the document.
105+
uint32_t startLine;
106+
107+
/// The zero-based character offset from where the folded range starts. If
108+
/// not defined, defaults to the length of the start line.
109+
std::optional<uint32_t> startCharacter;
110+
111+
/// The zero-based end line of the range to fold. The folded area ends with
112+
/// the line's last character. To be valid, the end must be zero or larger
113+
/// and smaller than the number of lines in the document.
114+
uint32_t endLine;
115+
116+
/// The zero-based character offset before the folded range ends. If not
117+
/// defined, defaults to the length of the end line.
118+
std::optional<uint32_t> endCharacter;
119+
120+
/// Describes the kind of the folding range such as `comment` or `region`.
121+
/// The kind is used to categorize folding ranges and used by commands like
122+
/// 'Fold all comments'. See [FoldingRangeKind](#FoldingRangeKind) for an
123+
/// enumeration of standardized kinds.
124+
FoldingRangeKind kind;
125+
126+
/// The text that the client should show when the specified range is
127+
/// collapsed. If not defined or not supported by the client, a default
128+
/// will be chosen by the client.
129+
///
130+
/// @since 3.17.0 - proposed
131+
std::optional<std::string> collapsedText;
132+
};
133+
81134
/// Server Capability.
82135
struct ServerCapabilities {
83136
/// The position encoding the server picked from the encodings offered
@@ -118,6 +171,9 @@ struct ServerCapabilities {
118171

119172
/// The server provides semantic tokens support.
120173
SemanticTokensOptions semanticTokensProvider;
174+
175+
/// The server provides folding provider support.
176+
bool foldingRangeProvider = true;
121177
};
122178

123179
struct InitializeResult {

include/Compiler/Command.h

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include <expected>
4-
#include "llvm/ADT/StringRef.h"
5-
#include "llvm/ADT/SmallVector.h"
4+
#include "llvm/ADT/StringMap.h"
65

76
namespace clice {
87

@@ -18,4 +17,50 @@ std::expected<void, std::string> mangleCommand(llvm::StringRef command,
1817
llvm::SmallVectorImpl<const char*>& out,
1918
llvm::SmallVectorImpl<char>& buffer);
2019

20+
/// `CompilationDatabase` is responsible for managing the compile commands.
21+
///
22+
/// FIXME: currently we assume that a file only occurs once in the CDB.
23+
/// This is not always correct, but it is enough for now.
24+
class CompilationDatabase {
25+
public:
26+
/// Update the compile commands with the given file.
27+
void updateCommands(llvm::StringRef file);
28+
29+
/// Update the compile commands with the given file and compile command.
30+
void updateCommand(llvm::StringRef file, llvm::StringRef command);
31+
32+
/// Update the module map with the given file and module name.
33+
void updateModule(llvm::StringRef file, llvm::StringRef name);
34+
35+
/// Lookup the compile commands of the given file.
36+
llvm::StringRef getCommand(llvm::StringRef file);
37+
38+
/// Lookup the module interface unit file path of the given module name.
39+
llvm::StringRef getModuleFile(llvm::StringRef name);
40+
41+
auto size() const {
42+
return commands.size();
43+
}
44+
45+
auto begin() {
46+
return commands.begin();
47+
}
48+
49+
auto end() {
50+
return commands.end();
51+
}
52+
53+
private:
54+
/// A map between file path and compile commands.
55+
llvm::StringMap<std::string> commands;
56+
57+
/// For C++20 module, we only can got dependent module name
58+
/// in source context. But we need dependent module file path
59+
/// to build PCM. So we will scan(preprocess) all project files
60+
/// to build a module map between module name and module file path.
61+
/// **Note that** this only includes module interface unit, for module
62+
/// implementation unit, the scan could be delayed until compiling it.
63+
llvm::StringMap<std::string> moduleMap;
64+
};
65+
2166
} // namespace clice

include/Feature/DocumentSymbol.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "Basic/Document.h"
24
#include "Basic/SourceCode.h"
35
#include "Index/Shared.h"

include/Index/FeatureIndex.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class FeatureIndex {
3131
}
3232
}
3333

34-
llvm::ArrayRef<feature::SemanticToken> semanticTokens() const;
34+
std::vector<feature::SemanticToken> semanticTokens() const;
35+
36+
std::vector<feature::FoldingRange> foldingRanges() const;
3537

3638
public:
3739
char* base;

include/Index/Shared2.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "SymbolIndex.h"
4+
#include "FeatureIndex.h"
5+
#include "Async/FileSystem.h"
6+
7+
namespace clice::index {
8+
9+
struct Index2 {
10+
std::optional<SymbolIndex> symbol;
11+
llvm::XXH128_hash_t symbolHash = {0, 0};
12+
13+
std::optional<FeatureIndex> feature;
14+
llvm::XXH128_hash_t featureHash = {0, 0};
15+
16+
static Shared<Index2> build(ASTInfo& AST);
17+
18+
async::Task<> write(std::string path) {
19+
if(symbol) {
20+
co_await async::fs::write(path + ".sidx", symbol->base, symbol->size);
21+
}
22+
23+
if(feature) {
24+
co_await async::fs::write(path + ".fidx", feature->base, feature->size);
25+
}
26+
}
27+
};
28+
29+
} // namespace clice::index

include/Server/Cache.h

-60
This file was deleted.

include/Server/Config.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#pragma once
22

33
#include <vector>
4+
#include <expected>
45

56
#include "llvm/ADT/ArrayRef.h"
67
#include "llvm/ADT/StringRef.h"
78

89
namespace clice::config {
910

1011
/// Read the config file, call when the program starts.
11-
void load(llvm::StringRef execute, llvm::StringRef filename);
12+
std::expected<void, std::string> load(llvm::StringRef execute, llvm::StringRef filename);
1213

1314
/// Initialize the config, replace all predefined variables in the config file.
1415
/// called in `Server::initialize`.

include/Server/Database.h

-53
This file was deleted.

0 commit comments

Comments
 (0)