mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[incrParse] Perform a full reparse of the file if needed for formatting
This commit is contained in:
@@ -774,6 +774,9 @@ class SwiftDocumentSyntaxInfo {
|
||||
unsigned BufferID;
|
||||
std::vector<std::string> Args;
|
||||
std::string PrimaryFile;
|
||||
/// Whether or not the AST stored in the source file is up-to-date or just an
|
||||
/// artifact of incremental syntax parsing
|
||||
bool HasUpToDateAST;
|
||||
|
||||
public:
|
||||
SwiftDocumentSyntaxInfo(const CompilerInvocation &CompInv,
|
||||
@@ -798,6 +801,10 @@ public:
|
||||
);
|
||||
|
||||
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
|
||||
|
||||
// If there is a syntax parsing cache, incremental syntax parsing is
|
||||
// performed and thus the generated AST may not be up-to-date.
|
||||
HasUpToDateAST = CompInv.getMainFileSyntaxParsingCache() == nullptr;
|
||||
}
|
||||
|
||||
void parse() {
|
||||
@@ -826,6 +833,8 @@ public:
|
||||
return SM;
|
||||
}
|
||||
|
||||
bool hasUpToDateAST() { return HasUpToDateAST; }
|
||||
|
||||
ArrayRef<DiagnosticEntryInfo> getDiagnostics() {
|
||||
return DiagConsumer.getDiagnosticsForBuffer(BufferID);
|
||||
}
|
||||
@@ -2018,6 +2027,10 @@ unsigned SwiftEditorDocument::getBufferID() const {
|
||||
|
||||
std::string SwiftEditorDocument::getFilePath() const { return Impl.FilePath; }
|
||||
|
||||
bool SwiftEditorDocument::hasUpToDateAST() const {
|
||||
return Impl.SyntaxInfo->hasUpToDateAST();
|
||||
}
|
||||
|
||||
void SwiftEditorDocument::formatText(unsigned Line, unsigned Length,
|
||||
EditorConsumer &Consumer) {
|
||||
auto SyntaxInfo = Impl.getSyntaxInfo();
|
||||
@@ -2442,6 +2455,13 @@ void SwiftLangSupport::editorFormatText(StringRef Name, unsigned Line,
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EditorDoc->hasUpToDateAST()) {
|
||||
// An up-to-date AST is needed for formatting. If it does not exist, fall
|
||||
// back to a full reparse of the file
|
||||
EditorDoc->parse(EditorDoc->getLatestSnapshot(), *this,
|
||||
/*BuildSyntaxTree=*/true);
|
||||
}
|
||||
|
||||
EditorDoc->formatText(Line, Length, Consumer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user