Skip to content

Commit 1df1dc7

Browse files
authored
Merge pull request #18016 from ahoppen/003-incremental-syntax-coloring
[libSyntax] Incremental syntax colouring
2 parents f0cb64d + 21dc447 commit 1df1dc7

File tree

18 files changed

+373
-34
lines changed

18 files changed

+373
-34
lines changed

include/swift/Basic/JSONSerialization.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/Regex.h"
2828
#include "llvm/Support/raw_ostream.h"
2929

30+
#include <map>
3031
#include <vector>
3132

3233
namespace swift {
@@ -341,6 +342,10 @@ struct unvalidatedObjectTraits : public std::integral_constant<bool,
341342
&& !has_ObjectValidateTraits<T>::value> {};
342343

343344
class Output {
345+
public:
346+
using UserInfoMap = std::map<void *, void *>;
347+
348+
private:
344349
enum State {
345350
ArrayFirstValue,
346351
ArrayOtherValue,
@@ -353,13 +358,19 @@ class Output {
353358
bool PrettyPrint;
354359
bool NeedBitValueComma;
355360
bool EnumerationMatchFound;
361+
UserInfoMap UserInfo;
356362

357363
public:
358-
Output(llvm::raw_ostream &os, bool PrettyPrint = true) : Stream(os),
359-
PrettyPrint(PrettyPrint), NeedBitValueComma(false),
360-
EnumerationMatchFound(false) {}
364+
Output(llvm::raw_ostream &os, UserInfoMap UserInfo = {},
365+
bool PrettyPrint = true)
366+
: Stream(os), PrettyPrint(PrettyPrint), NeedBitValueComma(false),
367+
EnumerationMatchFound(false), UserInfo(UserInfo) {}
361368
virtual ~Output() = default;
362369

370+
UserInfoMap &getUserInfo() {
371+
return UserInfo;
372+
}
373+
363374
unsigned beginArray();
364375
bool preflightElement(unsigned, void *&);
365376
void postflightElement(void*);

include/swift/Basic/OwnedString.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class OwnedString {
7171
OwnedString(): OwnedString(nullptr, 0, StringOwnership::Unowned) {}
7272

7373
OwnedString(const char *Data, size_t Length):
74-
OwnedString(Data, Length, StringOwnership::Unowned) {}
74+
OwnedString(Data, Length, StringOwnership::Copied) {}
7575

7676
OwnedString(StringRef Str) : OwnedString(Str.data(), Str.size()) {}
7777

@@ -106,7 +106,7 @@ class OwnedString {
106106
return *this;
107107
}
108108

109-
OwnedString copy() {
109+
OwnedString copy() const {
110110
return OwnedString(Data, Length, StringOwnership::Copied);
111111
}
112112

include/swift/Parse/SyntaxParsingCache.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ namespace swift {
4747

4848
using namespace swift::syntax;
4949

50+
struct SyntaxReuseRegion {
51+
/// The byte offset at which the range begins
52+
uintptr_t Start;
53+
/// The byte offset at which the end ends
54+
uintptr_t End;
55+
};
56+
5057
class SyntaxParsingCache {
5158
/// The syntax tree prior to the edit
5259
SourceFileSyntax OldSyntaxTree;
@@ -61,7 +68,7 @@ class SyntaxParsingCache {
6168

6269
/// If \c RecordReuseInformation buffer offsets of ranges that have been
6370
/// successfully looked up in this cache are stored.
64-
std::vector<std::pair<unsigned, unsigned>> ReusedRanges;
71+
std::vector<SyntaxReuseRegion> ReusedRanges;
6572

6673
public:
6774
SyntaxParsingCache(SourceFileSyntax OldSyntaxTree)
@@ -85,7 +92,7 @@ class SyntaxParsingCache {
8592
/// Return the ranges of the new source file that have been successfully
8693
/// looked up in this cache as a (start, end) pair of byte offsets in the
8794
/// post-edit file.
88-
std::vector<std::pair<unsigned, unsigned>> getReusedRanges() const {
95+
std::vector<SyntaxReuseRegion> getReusedRanges() const {
8996
return ReusedRanges;
9097
}
9198

include/swift/Subsystems.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace swift {
6161
class SILParserTUState;
6262
class SourceFile;
6363
class SourceManager;
64+
class SyntaxParsingCache;
6465
class Token;
6566
class TopLevelContext;
6667
struct TypeLoc;
@@ -327,7 +328,8 @@ namespace swift {
327328
class ParserUnit {
328329
public:
329330
ParserUnit(SourceManager &SM, unsigned BufferID,
330-
const LangOptions &LangOpts, StringRef ModuleName);
331+
const LangOptions &LangOpts, StringRef ModuleName,
332+
SyntaxParsingCache *SyntaxCache = nullptr);
331333
ParserUnit(SourceManager &SM, unsigned BufferID);
332334
ParserUnit(SourceManager &SM, unsigned BufferID,
333335
unsigned Offset, unsigned EndOffset);

include/swift/Syntax/Serialization/SyntaxSerialization.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
namespace swift {
2828
namespace json {
2929

30+
/// The associated value will be interpreted as \c bool. If \c true the node IDs
31+
/// will not be included in the serialized JSON.
32+
static void *DontSerializeNodeIdsUserInfoKey = &DontSerializeNodeIdsUserInfoKey;
33+
3034
/// Serialization traits for SourcePresence.
3135
template <>
3236
struct ScalarEnumerationTraits<syntax::SourcePresence> {
@@ -141,8 +145,12 @@ struct ObjectTraits<syntax::RawSyntax> {
141145
}
142146
auto presence = value.getPresence();
143147
out.mapRequired("presence", presence);
144-
auto nodeId = value.getId();
145-
out.mapRequired("id", nodeId);
148+
149+
bool omitNodeId = (bool)out.getUserInfo()[DontSerializeNodeIdsUserInfoKey];
150+
if (!omitNodeId) {
151+
auto nodeId = value.getId();
152+
out.mapRequired("id", nodeId);
153+
}
146154
}
147155
};
148156

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ static bool emitLoadedModuleTraceIfNeeded(ASTContext &ctxt,
242242
std::string stringBuffer;
243243
{
244244
llvm::raw_string_ostream memoryBuffer(stringBuffer);
245-
json::Output jsonOutput(memoryBuffer, /*PrettyPrint=*/false);
245+
json::Output jsonOutput(memoryBuffer, /*UserInfo=*/{},
246+
/*PrettyPrint=*/false);
246247
json::jsonize(jsonOutput, trace, /*Required=*/true);
247248
}
248249
stringBuffer += "\n";
@@ -289,7 +290,7 @@ static bool emitSyntax(SourceFile *SF, LangOptions &LangOpts,
289290
auto os = getFileOutputStream(OutputFilename, SF->getASTContext());
290291
if (!os) return true;
291292

292-
json::Output jsonOut(*os, /*PrettyPrint=*/false);
293+
json::Output jsonOut(*os, /*UserInfo=*/{}, /*PrettyPrint=*/false);
293294
auto Root = SF->getSyntaxRoot().getRaw();
294295
jsonOut << *Root;
295296
*os << "\n";

lib/Parse/Parser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,9 +1018,11 @@ ParserUnit::ParserUnit(SourceManager &SM, unsigned BufferID)
10181018
}
10191019

10201020
ParserUnit::ParserUnit(SourceManager &SM, unsigned BufferID,
1021-
const LangOptions &LangOpts, StringRef ModuleName)
1022-
: Impl(*new Implementation(SM, BufferID, LangOpts, ModuleName)) {
1021+
const LangOptions &LangOpts, StringRef ModuleName,
1022+
SyntaxParsingCache *SyntaxCache)
1023+
: Impl(*new Implementation(SM, BufferID, LangOpts, ModuleName)) {
10231024

1025+
Impl.SF->SyntaxParsingCache = SyntaxCache;
10241026
Impl.TheParser.reset(new Parser(BufferID, *Impl.SF, nullptr));
10251027
}
10261028

test/incrParse/invalid.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %incparse-test %s --test-case NO_CHANGES
3+
// RUN: %incparse-test %s --test-case NESTED_INITIALIZERS
34

45
func start() {}
56

67
class Bar
78

89
let y = 1
10+
11+
class NestedInitializers {
12+
<<NESTED_INITIALIZERS<|||init() {>>>
13+
init() {
14+
15+
}
16+
<<NESTED_INITIALIZERS<|||}>>>
17+
}

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ struct DiagnosticEntryInfo : DiagnosticEntryInfoBase {
188188
SmallVector<DiagnosticEntryInfoBase, 1> Notes;
189189
};
190190

191+
struct SourceFileRange {
192+
/// The byte offset at which the range begins
193+
uintptr_t Start;
194+
/// The byte offset at which the end ends
195+
uintptr_t End;
196+
};
197+
191198
class EditorConsumer {
192199
virtual void anchor();
193200
public:
@@ -240,6 +247,10 @@ class EditorConsumer {
240247
virtual bool handleSerializedSyntaxTree(StringRef Text) = 0;
241248
virtual bool syntaxTreeEnabled() = 0;
242249

250+
virtual bool syntaxReuseInfoEnabled() = 0;
251+
virtual bool handleSyntaxReuseRegions(
252+
std::vector<SourceFileRange> ReuseRegions) = 0;
253+
243254
virtual void finished() {}
244255

245256
// FIXME: This is just for bootstrapping incremental syntax tree parsing.

0 commit comments

Comments
 (0)