Skip to content

Commit

Permalink
Don't generate empty json variables
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 12, 2019
1 parent 2002b4b commit 796e6e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
19 changes: 15 additions & 4 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,11 +1060,22 @@ themePicker.onblur = handleThemeButtonsBlur;
.expect("invalid osstring conversion")))
.collect::<Vec<_>>();
files.sort_unstable_by(|a, b| a.cmp(b));
// FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty.
format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}",
let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
let dirs = if subs.is_empty() {
String::new()
} else {
format!(",\"dirs\":[{}]", subs)
};
let files = files.join(",");
let files = if files.is_empty() {
String::new()
} else {
format!(",\"files\":[{}]", files)
};
format!("{{\"name\":\"{name}\"{dirs}{files}}}",
name=self.elem.to_str().expect("invalid osstring conversion"),
subs=subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","),
files=files.join(","))
dirs=dirs,
files=files)
}
}

Expand Down
34 changes: 19 additions & 15 deletions src/librustdoc/html/static/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,32 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
children.className = "children";
var folders = document.createElement("div");
folders.className = "folders";
for (var i = 0; i < elem.dirs.length; ++i) {
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
hasFoundFile) === true) {
addClass(name, "expand");
hasFoundFile = true;
if (elem.dirs) {
for (var i = 0; i < elem.dirs.length; ++i) {
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
hasFoundFile) === true) {
addClass(name, "expand");
hasFoundFile = true;
}
}
}
children.appendChild(folders);

var files = document.createElement("div");
files.className = "files";
for (i = 0; i < elem.files.length; ++i) {
var file = document.createElement("a");
file.innerText = elem.files[i];
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
if (hasFoundFile === false &&
currentFile === fullPath + elem.files[i]) {
file.className = "selected";
addClass(name, "expand");
hasFoundFile = true;
if (elem.files) {
for (i = 0; i < elem.files.length; ++i) {
var file = document.createElement("a");
file.innerText = elem.files[i];
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
if (hasFoundFile === false &&
currentFile === fullPath + elem.files[i]) {
file.className = "selected";
addClass(name, "expand");
hasFoundFile = true;
}
files.appendChild(file);
}
files.appendChild(file);
}
search.fullPath = fullPath;
children.appendChild(files);
Expand Down

0 comments on commit 796e6e3

Please sign in to comment.