@@ -677,7 +677,8 @@ class SwiftDocumentSyntaxInfo {
677
677
std::string PrimaryFile;
678
678
// / Whether or not the AST stored in the source file is up-to-date or just an
679
679
// / artifact of incremental syntax parsing
680
- bool HasUpToDateAST;
680
+ bool IncrementalParsingEnabled;
681
+ bool IsParsed;
681
682
682
683
public:
683
684
SwiftDocumentSyntaxInfo (const CompilerInvocation &CompInv,
@@ -713,13 +714,16 @@ class SwiftDocumentSyntaxInfo {
713
714
Parser->getParser ().Context .evaluator );
714
715
Parser->getDiagnosticEngine ().addConsumer (DiagConsumer);
715
716
716
- // If there is a syntax parsing cache, incremental syntax parsing is
717
- // performed and thus the generated AST may not be up-to-date.
718
- HasUpToDateAST = CompInv. getMainFileSyntaxParsingCache () == nullptr ;
717
+ IncrementalParsingEnabled =
718
+ CompInv. getMainFileSyntaxParsingCache () != nullptr ;
719
+ IsParsed = false ;
719
720
}
720
721
721
- void parse () {
722
- Parser->parse ();
722
+ void parseIfNeeded () {
723
+ if (!IsParsed) {
724
+ Parser->parse ();
725
+ IsParsed = true ;
726
+ }
723
727
}
724
728
725
729
SourceFile &getSourceFile () {
@@ -738,7 +742,7 @@ class SwiftDocumentSyntaxInfo {
738
742
return SM;
739
743
}
740
744
741
- bool hasUpToDateAST () { return HasUpToDateAST ; }
745
+ bool isIncrementalParsingEnabled () { return IncrementalParsingEnabled ; }
742
746
743
747
ArrayRef<DiagnosticEntryInfo> getDiagnostics () {
744
748
return DiagConsumer.getDiagnosticsForBuffer (BufferID);
@@ -1074,6 +1078,7 @@ struct SwiftEditorDocument::Implementation {
1074
1078
1075
1079
std::shared_ptr<SwiftDocumentSyntaxInfo> getSyntaxInfo () {
1076
1080
llvm::sys::ScopedLock L (AccessMtx);
1081
+ SyntaxInfo->parseIfNeeded ();
1077
1082
return SyntaxInfo;
1078
1083
}
1079
1084
@@ -1949,9 +1954,10 @@ void SwiftEditorDocument::updateSemaInfo() {
1949
1954
::updateSemaInfo (SemanticInfo, EditableBuffer);
1950
1955
}
1951
1956
1952
- void SwiftEditorDocument::parse (ImmutableTextSnapshotRef Snapshot,
1953
- SwiftLangSupport &Lang, bool BuildSyntaxTree,
1954
- SyntaxParsingCache *SyntaxCache) {
1957
+ void SwiftEditorDocument::resetSyntaxInfo (ImmutableTextSnapshotRef Snapshot,
1958
+ SwiftLangSupport &Lang,
1959
+ bool BuildSyntaxTree,
1960
+ SyntaxParsingCache *SyntaxCache) {
1955
1961
llvm::sys::ScopedLock L (Impl.AccessMtx );
1956
1962
1957
1963
assert (Impl.SemanticInfo && " Impl.SemanticInfo must be set" );
@@ -1982,15 +1988,14 @@ void SwiftEditorDocument::parse(ImmutableTextSnapshotRef Snapshot,
1982
1988
// Access to Impl.SyntaxInfo is guarded by Impl.AccessMtx
1983
1989
Impl.SyntaxInfo .reset (
1984
1990
new SwiftDocumentSyntaxInfo (CompInv, Snapshot, Args, Impl.FilePath ));
1985
-
1986
- Impl.SyntaxInfo ->parse ();
1987
1991
}
1988
1992
1989
1993
static UIdent SemaDiagStage (" source.diagnostic.stage.swift.sema" );
1990
1994
static UIdent ParseDiagStage (" source.diagnostic.stage.swift.parse" );
1991
1995
1992
1996
void SwiftEditorDocument::readSyntaxInfo (EditorConsumer &Consumer, bool ReportDiags) {
1993
1997
llvm::sys::ScopedLock L (Impl.AccessMtx );
1998
+ Impl.SyntaxInfo ->parseIfNeeded ();
1994
1999
1995
2000
Impl.ParserDiagnostics = Impl.SyntaxInfo ->getDiagnostics ();
1996
2001
if (ReportDiags) {
@@ -2105,8 +2110,8 @@ SwiftEditorDocument::getSyntaxTree() const {
2105
2110
2106
2111
std::string SwiftEditorDocument::getFilePath () const { return Impl.FilePath ; }
2107
2112
2108
- bool SwiftEditorDocument::hasUpToDateAST () const {
2109
- return Impl.SyntaxInfo ->hasUpToDateAST ();
2113
+ bool SwiftEditorDocument::isIncrementalParsingEnabled () const {
2114
+ return Impl.SyntaxInfo ->isIncrementalParsingEnabled ();
2110
2115
}
2111
2116
2112
2117
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
@@ -2335,7 +2340,7 @@ void SwiftLangSupport::editorOpen(
2335
2340
EditorDoc = new SwiftEditorDocument (Name, *this , fileSystem);
2336
2341
Snapshot = EditorDoc->initializeText (
2337
2342
Buf, Args, Consumer.needsSemanticInfo (), fileSystem);
2338
- EditorDoc->parse (Snapshot, *this , Consumer.syntaxTreeEnabled ());
2343
+ EditorDoc->resetSyntaxInfo (Snapshot, *this , Consumer.syntaxTreeEnabled ());
2339
2344
if (EditorDocuments->getOrUpdate (Name, *this , EditorDoc)) {
2340
2345
// Document already exists, re-initialize it. This should only happen
2341
2346
// if we get OPEN request while the previous document is not closed.
@@ -2349,7 +2354,7 @@ void SwiftLangSupport::editorOpen(
2349
2354
if (!Snapshot) {
2350
2355
Snapshot = EditorDoc->initializeText (
2351
2356
Buf, Args, Consumer.needsSemanticInfo (), fileSystem);
2352
- EditorDoc->parse (Snapshot, *this , Consumer.syntaxTreeEnabled ());
2357
+ EditorDoc->resetSyntaxInfo (Snapshot, *this , Consumer.syntaxTreeEnabled ());
2353
2358
}
2354
2359
2355
2360
if (Consumer.needsSemanticInfo ()) {
@@ -2402,7 +2407,7 @@ void verifyIncrementalParse(SwiftEditorDocumentRef EditorDoc,
2402
2407
SwiftDocumentSyntaxInfo ScratchSyntaxInfo (Invocation,
2403
2408
EditorDoc->getLatestSnapshot (),
2404
2409
Args, EditorDoc->getFilePath ());
2405
- ScratchSyntaxInfo.parse ();
2410
+ ScratchSyntaxInfo.parseIfNeeded ();
2406
2411
2407
2412
// Dump the from-scratch syntax tree
2408
2413
std::string FromScratchTreeString;
@@ -2507,19 +2512,22 @@ void SwiftLangSupport::editorReplaceText(StringRef Name,
2507
2512
}
2508
2513
2509
2514
// If client doesn't need any information, we doen't need to parse it.
2515
+
2516
+
2517
+ SyntaxParsingCache *SyntaxCachePtr = nullptr ;
2518
+ if (SyntaxCache.hasValue ()) {
2519
+ SyntaxCachePtr = SyntaxCache.getPointer ();
2520
+ }
2521
+ EditorDoc->resetSyntaxInfo (Snapshot, *this , Consumer.syntaxTreeEnabled (),
2522
+ SyntaxCachePtr);
2523
+
2510
2524
if (!Consumer.documentStructureEnabled () &&
2511
2525
!Consumer.syntaxMapEnabled () &&
2512
2526
!Consumer.diagnosticsEnabled () &&
2513
2527
!Consumer.syntaxTreeEnabled ()) {
2514
2528
return ;
2515
2529
}
2516
2530
2517
- SyntaxParsingCache *SyntaxCachePtr = nullptr ;
2518
- if (SyntaxCache.hasValue ()) {
2519
- SyntaxCachePtr = SyntaxCache.getPointer ();
2520
- }
2521
- EditorDoc->parse (Snapshot, *this , Consumer.syntaxTreeEnabled (),
2522
- SyntaxCachePtr);
2523
2531
// Do not report syntactic diagnostics; will be handled in readSemanticInfo.
2524
2532
EditorDoc->readSyntaxInfo (Consumer, /* ReportDiags=*/ false );
2525
2533
@@ -2578,11 +2586,12 @@ void SwiftLangSupport::editorFormatText(StringRef Name, unsigned Line,
2578
2586
return ;
2579
2587
}
2580
2588
2581
- if (!EditorDoc->hasUpToDateAST ()) {
2582
- // An up-to-date AST is needed for formatting. If it does not exist, fall
2583
- // back to a full reparse of the file
2584
- EditorDoc->parse (EditorDoc->getLatestSnapshot (), *this ,
2585
- /* BuildSyntaxTree=*/ true );
2589
+ if (EditorDoc->isIncrementalParsingEnabled ()) {
2590
+ // If incremental parsing is enabled, AST is not updated properly. Fall
2591
+ // back to a full reparse of the file.
2592
+ EditorDoc->resetSyntaxInfo (EditorDoc->getLatestSnapshot (), *this ,
2593
+ /* BuildSyntaxTree=*/ true ,
2594
+ /* SyntaxCache=*/ nullptr );
2586
2595
}
2587
2596
2588
2597
EditorDoc->formatText (Line, Length, Consumer);
@@ -2616,5 +2625,13 @@ void SwiftLangSupport::editorExpandPlaceholder(StringRef Name, unsigned Offset,
2616
2625
return ;
2617
2626
}
2618
2627
2628
+ if (EditorDoc->isIncrementalParsingEnabled ()) {
2629
+ // If incremental parsing is enabled, AST is not updated properly. Fall
2630
+ // back to a full reparse of the file.
2631
+ EditorDoc->resetSyntaxInfo (EditorDoc->getLatestSnapshot (), *this ,
2632
+ /* BuildSyntaxTree=*/ true ,
2633
+ /* SyntaxCache=*/ nullptr );
2634
+ }
2635
+
2619
2636
EditorDoc->expandPlaceholder (Offset, Length, Consumer);
2620
2637
}
0 commit comments