Skip to content

Commit 0b6d536

Browse files
committed
[clang-doc] make loading of json side bar async
1 parent 2287f8d commit 0b6d536

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

clang-tools-extra/clang-doc/HTMLGenerator.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) {
964964
std::error_code FileErr;
965965
llvm::SmallString<128> FilePath;
966966
llvm::sys::path::native(CDCtx.OutDirectory, FilePath);
967-
llvm::sys::path::append(FilePath, "index_json.js");
967+
llvm::sys::path::append(FilePath, "index.json");
968968
llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::OF_None);
969969
if (FileErr != OK) {
970970
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -985,9 +985,7 @@ static llvm::Error SerializeIndex(ClangDocContext &CDCtx) {
985985
});
986986
});
987987
};
988-
OS << "var JsonIndex = `\n";
989988
IndexToJSON(CDCtx.Idx);
990-
OS << "`;\n";
991989
return llvm::Error::success();
992990
}
993991

@@ -1049,31 +1047,33 @@ static llvm::Error CopyFile(StringRef FilePath, StringRef OutDirectory) {
10491047
std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
10501048
if (FileErr != OK) {
10511049
return llvm::createStringError(llvm::inconvertibleErrorCode(),
1052-
"error creating file " +
1053-
llvm::sys::path::filename(FilePath) +
1054-
": " + FileErr.message() + "\n");
1050+
"error creating file " + FilePath + ": " +
1051+
FileErr.message() + "\n");
10551052
}
10561053
return llvm::Error::success();
10571054
}
10581055

10591056
llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) {
1060-
auto Err = SerializeIndex(CDCtx);
1061-
if (Err)
1057+
if (auto Err = SerializeIndex(CDCtx)) {
10621058
return Err;
1063-
Err = GenIndex(CDCtx);
1064-
if (Err)
1059+
}
1060+
1061+
if (auto Err = GenIndex(CDCtx)) {
10651062
return Err;
1063+
}
10661064

10671065
for (const auto &FilePath : CDCtx.UserStylesheets) {
1068-
Err = CopyFile(FilePath, CDCtx.OutDirectory);
1069-
if (Err)
1066+
if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) {
10701067
return Err;
1068+
}
10711069
}
1070+
10721071
for (const auto &FilePath : CDCtx.FilesToCopy) {
1073-
Err = CopyFile(FilePath, CDCtx.OutDirectory);
1074-
if (Err)
1072+
if (auto Err = CopyFile(FilePath, CDCtx.OutDirectory)) {
10751073
return Err;
1074+
}
10761075
}
1076+
10771077
return llvm::Error::success();
10781078
}
10791079

clang-tools-extra/clang-doc/Representation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ struct ClangDocContext {
491491
std::string SourceRoot; // Directory where processed files are stored. Links
492492
// to definition locations will only be generated if
493493
// the file is in this dir.
494+
494495
// URL of repository that hosts code used for links to definition locations.
495496
std::optional<std::string> RepositoryUrl;
496497
// Path of CSS stylesheets that will be copied to OutDirectory and used to

clang-tools-extra/clang-doc/assets/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function createIndex(Index) {
8282
document.addEventListener("DOMContentLoaded", function() {
8383
// JsonIndex is a variable from another file that contains the index
8484
// in JSON format
85-
var Index = JSON.parse(JsonIndex);
86-
createIndex(Index);
85+
fetch("/index.json")
86+
.then((response) => response.json())
87+
.then((Index) => { createIndex(Index); });
8788
});

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ Example usage for a project using a compile commands database:
155155
return 1;
156156
}
157157

158+
// add option to customize url fragment
159+
// such as
160+
// https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-doc/ClangDocMain.cpp#L1
161+
158162
// Fail early if an invalid format was provided.
159163
std::string Format = getFormatString();
160164
llvm::outs() << "Emiting docs in " << Format << " format.\n";
@@ -179,7 +183,7 @@ Example usage for a project using a compile commands database:
179183
SourceRoot,
180184
RepositoryUrl,
181185
{UserStylesheets.begin(), UserStylesheets.end()},
182-
{"index.js", "index_json.js"}};
186+
{"index.js"}};
183187

184188
if (Format == "html") {
185189
void *MainAddr = (void *)(intptr_t)GetExecutablePath;

0 commit comments

Comments
 (0)