mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-31 04:27:45 +00:00
feat: finish extension
This commit is contained in:
parent
63b45b881e
commit
6bee230d57
4 changed files with 142 additions and 47 deletions
|
@ -4,18 +4,52 @@
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
This extension adds support
|
This extension adds built-in editor support
|
||||||
for formatting Nix files
|
for formatting Nix files automatically
|
||||||
with [Alejandra](https://github.com/kamadorueda/alejandra).
|
with [Alejandra](https://github.com/kamadorueda/alejandra).
|
||||||
|
|
||||||
## Requirements
|
## Getting started
|
||||||
|
|
||||||
Make sure to install [Alejandra](https://github.com/kamadorueda/alejandra) in your system first.
|
1. Make sure to install
|
||||||
|
[Alejandra](https://github.com/kamadorueda/alejandra)
|
||||||
|
in your system first
|
||||||
|
as explained [here](https://github.com/kamadorueda/alejandra).
|
||||||
|
|
||||||
## Settings
|
1. Install the vscode extension and reload the window (just close and open again).
|
||||||
|
|
||||||
This extension contributes the following settings:
|
1. Open a Nix file,
|
||||||
|
do a right click
|
||||||
|
and you should be able to see "Format Document" in the menu.
|
||||||
|
|
||||||
- `alejandra.path`: Specifies an alternative full path to the Alejandra executable.
|
Alternatively, it will be formatted automatically when you save the file.
|
||||||
|
|
||||||
By default we will use the Alejandra installed in your system.
|
Enjoy!
|
||||||
|
|
||||||
|
# Troubleshooting
|
||||||
|
|
||||||
|
If you encounter a problem
|
||||||
|
please let us know in the
|
||||||
|
[issues section](https://github.com/kamadorueda/alejandra/issues).
|
||||||
|
|
||||||
|
The most probable causes of failure are:
|
||||||
|
|
||||||
|
- Not having Alejandra installed in your system.
|
||||||
|
|
||||||
|
In this case please follow the instructions
|
||||||
|
[here](https://github.com/kamadorueda/alejandra).
|
||||||
|
|
||||||
|
- A misconfiguration.
|
||||||
|
|
||||||
|
In this case please make sure that your config contains the following values:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"[nix]": {
|
||||||
|
"editor.defaultFormatter": "kamadorueda.alejandra",
|
||||||
|
"editor.formatOnPaste": true,
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.formatOnType": false
|
||||||
|
},
|
||||||
|
"alejandra.program": "alejandra"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -1,31 +1,72 @@
|
||||||
const vscode = require('vscode');
|
const { execFile } = require("child_process");
|
||||||
const { execFileSync } = require("child_process");
|
const vscode = require("vscode");
|
||||||
|
|
||||||
function activate(context) {
|
const activate = (_) => {
|
||||||
const config = vscode.workspace.getConfiguration("alejandra");
|
const outputChannel = vscode.window.createOutputChannel("Alejandra");
|
||||||
|
|
||||||
context.subscriptions.push(
|
vscode.languages.registerDocumentFormattingEditProvider("nix", {
|
||||||
vscode.languages.registerDocumentFormattingEditProvider("nix", {
|
provideDocumentFormattingEdits(document, _) {
|
||||||
provideDocumentFormattingEdits: (document, _, _) => {
|
const config = {
|
||||||
const range = new vscode.Range(0, 0, document.lineCount, 0);
|
alejandra: vscode.workspace.getConfiguration("alejandra"),
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const formattedText = execFileSync(config.path, { input: document.getText() });
|
outputChannel.appendLine(
|
||||||
return [vscode.TextEdit.replace(range, formattedText.toString())];
|
`Running Alejandra with settings: ${JSON.stringify(config)}`
|
||||||
}
|
);
|
||||||
catch (e) {
|
|
||||||
vscode.window.showErrorMessage(`alejandra failed: ${e}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
const process = execFile(
|
||||||
}
|
config.alejandra.program,
|
||||||
})
|
[],
|
||||||
);
|
{},
|
||||||
}
|
(error, stdout, stderr) => {
|
||||||
|
if (error) {
|
||||||
|
outputChannel.appendLine(`error: ${error}`);
|
||||||
|
outputChannel.appendLine(`stderr: ${stderr}`);
|
||||||
|
vscode.window.showErrorMessage(
|
||||||
|
`While executing Alejandra with settings: ` +
|
||||||
|
`${JSON.stringify(config)}, ` +
|
||||||
|
`${error}`
|
||||||
|
);
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
function deactivate() {}
|
const documentRange = new vscode.Range(
|
||||||
|
document.lineAt(0).range.start,
|
||||||
|
document.lineAt(
|
||||||
|
document.lineCount - 1
|
||||||
|
).rangeIncludingLineBreak.end
|
||||||
|
);
|
||||||
|
|
||||||
|
resolve([new vscode.TextEdit(documentRange, stdout)]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const documentText = document.getText();
|
||||||
|
|
||||||
|
outputChannel.appendLine(
|
||||||
|
`Feeding ${documentText.length} of input to stdin`
|
||||||
|
);
|
||||||
|
|
||||||
|
process.stdin.write(documentText);
|
||||||
|
process.stdin.end();
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showErrorMessage(
|
||||||
|
`While executing Alejandra with settings: ` +
|
||||||
|
`${JSON.stringify(config)} ` +
|
||||||
|
`${error}`
|
||||||
|
);
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const deactivate = () => {};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
activate,
|
activate,
|
||||||
deactivate
|
deactivate,
|
||||||
}
|
};
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"checkJs": false,
|
"checkJs": true,
|
||||||
"lib": ["ES2020"],
|
"lib": [
|
||||||
"module": "commonjs",
|
"ES2020"
|
||||||
|
],
|
||||||
|
"module": "CommonJS",
|
||||||
"target": "ES2020"
|
"target": "ES2020"
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules"]
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,25 +10,40 @@
|
||||||
"categories": [
|
"categories": [
|
||||||
"Formatters"
|
"Formatters"
|
||||||
],
|
],
|
||||||
"configuration": {
|
"contributes": {
|
||||||
|
"configuration": {
|
||||||
|
"properties": {
|
||||||
|
"alejandra.program": {
|
||||||
|
"default": "alejandra",
|
||||||
|
"description": "Specifies an alternative full path to the Alejandra executable.",
|
||||||
|
"type": [
|
||||||
|
"string"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"title": "Alejandra"
|
||||||
|
},
|
||||||
|
"configurationDefaults": {
|
||||||
|
"[nix]": {
|
||||||
|
"editor.defaultFormatter": "kamadorueda.alejandra",
|
||||||
|
"editor.formatOnPaste": true,
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.formatOnType": false
|
||||||
|
},
|
||||||
|
"alejandra.program": "alejandra"
|
||||||
|
},
|
||||||
"languages": [
|
"languages": [
|
||||||
{
|
{
|
||||||
|
"aliases": [
|
||||||
|
"nix",
|
||||||
|
"Nix"
|
||||||
|
],
|
||||||
"extensions": [
|
"extensions": [
|
||||||
".nix"
|
".nix"
|
||||||
],
|
],
|
||||||
"id": "nix"
|
"id": "nix"
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"properties": {
|
|
||||||
"alejandra.program": {
|
|
||||||
"default": "alejandra",
|
|
||||||
"description": "Specifies an alternative full path to the Alejandra executable.",
|
|
||||||
"type": [
|
|
||||||
"string"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"title": "Alejandra"
|
|
||||||
},
|
},
|
||||||
"description": "The Uncompromising Nix Code Formatter",
|
"description": "The Uncompromising Nix Code Formatter",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -45,6 +60,7 @@
|
||||||
"license": "Unlicense",
|
"license": "Unlicense",
|
||||||
"main": "./extension.js",
|
"main": "./extension.js",
|
||||||
"name": "alejandra",
|
"name": "alejandra",
|
||||||
|
"publisher": "kamadorueda",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/kamadorueda/alejandra"
|
"url": "https://github.com/kamadorueda/alejandra"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue