Skip to content
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
11 changes: 5 additions & 6 deletions src/buf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as child_process from "child_process";

Check warning on line 1 in src/buf.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 1 in src/buf.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 1 in src/buf.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase
import { Error } from "./errors";
import { parse, Version } from "./version";

Expand Down Expand Up @@ -37,13 +37,12 @@
shell: process.platform === "win32",
}
);
if (output.error !== undefined) {
return { errorMessage: output.error.message };
}
if (output.status !== null && output.status === 0) {
return [];
// If the command fails to run, such as a failed module/workspace build, return the error.
if (output.status === 1) {
return output.stderr.trim().split("\n");
}
return output.stdout.trim().split("\n");
// If the command succeeds with no lint failures and an empty output will be returned.
return output.stdout.trim().split("\n").filter((s) => s.trim().length > 0);
};

export const version = (binaryPath: string): Version | Error => {
Expand Down
11 changes: 7 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { format, less } from "./version";
import { getBinaryPath } from "./get-binary-path";

export function activate(context: vscode.ExtensionContext) {
const { binaryPath } = getBinaryPath();
const outputChannel = vscode.window.createOutputChannel("Buf", "console");
const { binaryPath } = getBinaryPath(outputChannel);
if (binaryPath === undefined) {
return;
}
Expand Down Expand Up @@ -57,12 +58,13 @@ export function activate(context: vscode.ExtensionContext) {

const diagnosticCollection =
vscode.languages.createDiagnosticCollection("vscode-buf.lint");

const doLint = (document: vscode.TextDocument) => {
if (!document.uri.path.endsWith(".proto")) {
return;
}

const { binaryPath, cwd } = getBinaryPath();
const { binaryPath, cwd } = getBinaryPath(outputChannel);
if (binaryPath === undefined) {
return;
}
Expand All @@ -82,7 +84,8 @@ export function activate(context: vscode.ExtensionContext) {
}
const warnings = parseLines(lines);
if (isError(warnings)) {
console.log(warnings);
outputChannel.appendLine(warnings.errorMessage);
outputChannel.show();
return;
}
const warningsForThisDocument = warnings.filter(
Expand All @@ -109,7 +112,7 @@ export function activate(context: vscode.ExtensionContext) {
diagnosticCollection.set(document.uri, diagnostics);
};

const formatter = new Formatter(binaryPath);
const formatter = new Formatter(binaryPath, outputChannel);
context.subscriptions.push(
vscode.languages.registerDocumentFormattingEditProvider("proto", formatter)
);
Expand Down
9 changes: 6 additions & 3 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as vscode from "vscode";
import { TextEncoder } from "util";

export class Formatter implements vscode.DocumentFormattingEditProvider {
outputChannel: vscode.OutputChannel | undefined;
readonly binaryPath: string = '';

constructor(binaryPath: string) {
constructor(binaryPath: string, outputChannel: vscode.OutputChannel) {
if (!binaryPath || binaryPath.length === 0) {
throw new Error('binaryPath is required to construct a formatter');
}
this.binaryPath = binaryPath;
this.outputChannel = outputChannel;
}

public provideDocumentFormattingEdits(
Expand All @@ -26,8 +28,9 @@ export class Formatter implements vscode.DocumentFormattingEditProvider {
return this.runFormatter(document, token).then(
(edits) => edits,
(err) => {
console.log(err);
return Promise.reject('Check the console in dev tools to find errors when formatting.');
this.outputChannel?.appendLine(err);
this.outputChannel?.show();
return Promise.reject('Check the console to find errors when formatting.');
}
);
}
Expand Down
21 changes: 13 additions & 8 deletions src/get-binary-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,37 @@ import pkg from "../package.json";
const defaultBinaryPath =
pkg.contributes.configuration.properties["buf.binaryPath"].default;

const getWorkspaceFolderFsPath = () => {
const getWorkspaceFolderFsPath = (outputChannel: vscode.OutputChannel) => {
if (vscode.workspace.workspaceFolders === undefined) {
console.log("workspace folders was undefined");
outputChannel.appendLine("workspace folders was undefined");
outputChannel.show();
return;
}
if (vscode.workspace.workspaceFolders.length === 0) {
console.log("workspace folders was not set");
outputChannel.appendLine("workspace folders was not set");
outputChannel.show();
return;
}
const uri = vscode.workspace.workspaceFolders[0].uri;
if (uri.scheme !== "file") {
console.log("uri was not file: ", uri.scheme);
outputChannel.appendLine(`uri was not file: ${uri.scheme}`);
outputChannel.show();
return;
}
return uri.fsPath;
};

export const getBinaryPath = () => {
const workspaceFolderFsPath = getWorkspaceFolderFsPath();
export const getBinaryPath = (outputChannel: vscode.OutputChannel) => {
const workspaceFolderFsPath = getWorkspaceFolderFsPath(outputChannel);
if (workspaceFolderFsPath === undefined) {
return {};
}
let binaryPath = vscode.workspace
.getConfiguration("buf")!
.get<string>("binaryPath");
if (binaryPath === undefined) {
console.log("buf binary path was not set");
outputChannel.appendLine("buf binary path was not set");
outputChannel.show();
return {};
}

Expand All @@ -41,7 +45,8 @@ export const getBinaryPath = () => {
binaryPath = path.join(workspaceFolderFsPath, binaryPath);

if (!existsSync(binaryPath)) {
console.log("buf binary path does not exist: ", binaryPath);
outputChannel.appendLine(`buf binary path does not exist: ${binaryPath}`);
outputChannel.show();
return {};
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ export const parseLines = (errorLines: string[]): Warning[] | Error => {
for (let index = 0; index < errorLines.length; index++) {
try {
const warning = JSON.parse(errorLines[index]);
if (!isWarning(warning)) {
return {
errorMessage: `failed to parse "${errorLines[index]}" as warning`,
};
if (isWarning(warning)) {
warnings.push(warning);
}
warnings.push(warning);
} catch (error) {
return {
errorMessage: `failed to parse "${errorLines[index]}" as warning: ${error}`,
errorMessage: `${errorLines.join(",")}`,
};
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/test/buf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import path from "path";

suite("Buf CLI tests", () => {
vscode.window.showInformationMessage("Start all tests.");
const outputChannel = vscode.window.createOutputChannel("Buf-test", "console");

test("Default path succeeds when buf is installed", async () => {
const version = buf.version("buf");
Expand All @@ -19,7 +20,7 @@ suite("Buf CLI tests", () => {
});

test("Relative path loaded from config", async () => {
const { binaryPath } = getBinaryPath();
const { binaryPath } = getBinaryPath(outputChannel);
assert.ok(binaryPath);
const version = buf.version(binaryPath);
if ("errorMessage" in version) {
Expand All @@ -33,7 +34,7 @@ suite("Buf CLI tests", () => {
let storedPath: string | undefined;

setup(() => {
const { cwd } = getBinaryPath();
const { cwd } = getBinaryPath(outputChannel);
storedPath = vscode.workspace
.getConfiguration("buf")
.get<string>("binaryPath");
Expand All @@ -50,7 +51,7 @@ suite("Buf CLI tests", () => {
});

test("version", async () => {
const { binaryPath } = getBinaryPath();
const { binaryPath } = getBinaryPath(outputChannel);
assert.ok(binaryPath);
const version = buf.version(binaryPath);
if ("errorMessage" in version) {
Expand Down
Loading