Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reject whole batch of files because some of them failed to load… #66

Merged
merged 1 commit into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/classes/FileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class FileReader {
return [];
}

// TODO: Try using workspace.findFiles(...) instead of node fs methods
let files = fs.readdirSync(root);
let names = [];
for (let i = 0; i < files.length; i++) {
Expand All @@ -125,7 +126,12 @@ export class FileReader {
private static readFileFromNames(uris_or_strings): Promise<FileType[]> {
return new Promise(function (resolve, reject) {
let docs: FileType[] = [];
let count = 0;
// Count of successfully opened files
let openedCount = 0;
// Count of files which failed to load
let failedCount = 0;

function totalCount() { return openedCount + failedCount; }

for (let uri of uris_or_strings) {
let docPrm = workspace.openTextDocument(uri);
Expand All @@ -134,14 +140,23 @@ export class FileReader {
if(doc) // File maybe corrupted
docs.push(new FileType(doc));

count++;
openedCount++;
// Detect and end the function early
if (count == uris_or_strings.length) {
if (totalCount() == uris_or_strings.length) {
resolve(docs);
}
},
function (reason) {
reject(reason);
// Keep going, try other files.
failedCount++;
// Detect and end the function early
if (failedCount == uris_or_strings.length) {
// All files failed to open, so we reject
reject("No file has been read successfully.");
}
else if (totalCount() == uris_or_strings.length) {
resolve(docs);
}
});
}
if (uris_or_strings.length == 0)
Expand Down
6 changes: 4 additions & 2 deletions src/types/CommandType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ export class ParseAllFilesCommand implements CommandType {
let results = [], errors = [];
FileReader.readProjectFiles(
(files: FileType[], progress, error) => {
if (!error) {
if (files.length > 0) {
files = FileFilter.filter(files);
let todos = Parser.parse(files);
results = results.concat(todos);
OutputWriter.writeTodo(todos);
StatusBarManager.getInstance().setWorking(`${WORKING_ICON} ${progress}%`, "Click to cancel");
totalFiles += todos.length;
}
else {
// We could have files available, but still have an error
// because some of them failed to load.
if (error) {
errors.push(error);
}
},
Expand Down