Skip to content

Commit

Permalink
Formatter update (#7)
Browse files Browse the repository at this point in the history
Adds a new command to format an entire workspace. Resolves #6.

---------

Co-authored-by: Eugene “Aeron” Glybin <aeron@aeron.cc>
  • Loading branch information
dcwatson and Aeron committed Nov 17, 2023
1 parent 93ab418 commit e5b3183
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

These versions follow [Semantic Versioning 2.0](https://semver.org).

## 1.0.4 (2023-11-17)

### Added

- The `Format Workspace with Black` command, to run Black on the entire workspace
(by @dcwatson in #6)

## 1.0.3 (2023-08-27)

### Fixed
Expand Down
27 changes: 26 additions & 1 deletion Scripts/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class Formatter {
const executablePath = nova.path.expanduser(this.config.get("executablePath"));
const commandArguments = this.config.get("commandArguments");
const defaultOptions = (filename)
? ["--quiet", `--stdin-filename=${filename}`, "-"]
? (filename !== ".")
? ["--quiet", `--stdin-filename=${filename}`, "-"]
: ["--quiet", filename]
: ["--quiet", "-"];

if (!nova.fs.stat(executablePath)) {
Expand Down Expand Up @@ -100,6 +102,29 @@ class Formatter {
writer.close();
});
}

async formatWorkspace(workspace) {
let process = await this.getProcess(".");

if (!process) {
return;
}

let errBuffer = [];

process.onStderr((error) => errBuffer.push(error));
process.onDidExit((status) => {
if (status === 0) {
console.log("Formatting the workspace");
} else {
console.error(errBuffer.join(""));
}
});

console.log("Running " + process.command + " " + process.args.join(" "));

process.start();
}
}

module.exports = Formatter;
3 changes: 3 additions & 0 deletions Scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ exports.activate = function() {
});

nova.commands.register("formatWithBlack", formatter.format, formatter);
nova.commands.register(
"formatWorkspaceWithBlack", formatter.formatWorkspace, formatter
);
};
9 changes: 8 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Black",
"organization": "Aeron",
"description": "Black, the uncompromising Python code formatter, for Nova.",
"version": "1.0.3",
"version": "1.0.4",
"categories": ["formatters", "commands"],
"repository": "https://github.com/Aeron/Black.novaextension",
"bugs": "https://github.com/Aeron/Black.novaextension/issues",
Expand All @@ -26,6 +26,13 @@
"syntaxes": ["python"]
}
}
],
"extensions": [
{
"title": "Format Workspace with Black",
"command": "formatWorkspaceWithBlack",
"shortcut": "cmd-shift-opt-B"
}
]
},
"config": [
Expand Down

0 comments on commit e5b3183

Please sign in to comment.