Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
Register formater for proto3 & allow linter to be disabled (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
0rax authored Nov 2, 2022
1 parent fc27179 commit faf8988
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@
"type": "string",
"default": "buf",
"description": "Path to the buf binary"
},
"buf.linting.enabled": {
"type": "boolean",
"default": true,
"description": "Lints code on file save using buf lint."
},
"buf.linting.lintOnSave": {
"type": "boolean",
"default": true,
"description": "Lints code on file save using buf lint."
}
}
},
"configurationDefaults": {
"[proto]": {
"[proto3]": {
"editor.formatOnSave": true
}
},
Expand All @@ -80,7 +90,7 @@
]
},
{
"id": "proto",
"id": "proto3",
"extensions": [
".proto"
],
Expand Down
25 changes: 18 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function activate(context: vscode.ExtensionContext) {
console.log("buf binary path was not set");
return;
}

const binaryVersion = version(binaryPath);
if (isError(binaryVersion)) {
vscode.window.showInformationMessage(
Expand Down Expand Up @@ -65,6 +64,10 @@ export function activate(context: vscode.ExtensionContext) {
if (!document.uri.path.endsWith(".proto")) {
return;
}
if (!vscode.workspace.getConfiguration("buf.linting")!.get<boolean>("enabled")) {
console.log("buf lint is disabled");
return;
}

if (vscode.workspace.workspaceFolders === undefined) {
console.log("workspace folders was undefined");
Expand All @@ -80,9 +83,7 @@ export function activate(context: vscode.ExtensionContext) {
return;
}

const binaryPath = vscode.workspace
.getConfiguration("buf")!
.get<string>("binaryPath");
const binaryPath = vscode.workspace.getConfiguration("buf")!.get<string>("binaryPath");
if (binaryPath === undefined) {
console.log("buf binary path was not set");
return;
Expand Down Expand Up @@ -125,9 +126,19 @@ export function activate(context: vscode.ExtensionContext) {
diagnosticCollection.set(document.uri, diagnostics);
};

context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('proto', new Formatter(binaryPath)));
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(doLint));
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(doLint));
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('proto3', new Formatter(binaryPath)));
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {
const settings = vscode.workspace.getConfiguration("buf.linting")!;
if (settings.get<boolean>("enabled") && settings.get<boolean>("lintOnSave")) {
doLint(e);
}
}));
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument((e: vscode.TextDocument) => {
const settings = vscode.workspace.getConfiguration("buf.linting")!;
if (settings.get<boolean>("enabled")) {
doLint(e);
}
}));
context.subscriptions.push(
vscode.commands.registerTextEditorCommand(
"vscode-buf.lint",
Expand Down

0 comments on commit faf8988

Please sign in to comment.