Skip to content

Commit

Permalink
add command to open config file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoieni committed May 29, 2023
1 parent cb2f04b commit 6fea8e1
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module.exports = {
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
project: './tsconfig.json',
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
//"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
],
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
// "@typescript-eslint/no-floating-promises": ["warn"],
curly: "warn",
eqeqeq: "warn",
"no-throw-literal": "warn",
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module.exports = {
tabWidth: 4,
useTabs: false,
singleQuote: false,
max_line_length: 100,
printWidth: 100,
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
"commands": [
{
"command": "glimpse.run",
"title": "Glimpse"
"title": "Glimpse: Run"
},
{
"command": "glimpse.configure",
"title": "Glimpse: Configure"
}
]
},
Expand Down
49 changes: 49 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as vscode from "vscode";

const defaultConfig = `// Edit the default glimpse menu using javascript.
module.exports = function editConfig(menu) {
return menu;
}`;

export async function glimpseConfigure(context: vscode.ExtensionContext) {
try {
const storageUri = context.globalStorageUri;
await createDirIfDoesntExist(storageUri);
const configPath = vscode.Uri.joinPath(storageUri, "config.js");
await openConfig(configPath);
} catch (err) {
console.error("Failed to run Glimpse configure", err);
}
}

async function createDirIfDoesntExist(dir: vscode.Uri) {
try {
// if dir doesn't exist, this will throw an error
await vscode.workspace.fs.stat(dir);
} catch (err) {
await vscode.workspace.fs.createDirectory(dir);
}
}

async function createFileIfDoesntExist(file: vscode.Uri, content: string) {
try {
// if file doesn't exist, this will throw an error
await vscode.workspace.fs.stat(file);
console.log("config file already exists");
} catch (err) {
console.log("creating file", file);
await vscode.workspace.fs.writeFile(file, Buffer.from(content));
// wait a while so that we avoid the error "file not found" in vscode
await sleep(1000);
}
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function openConfig(configUri: vscode.Uri) {
await createFileIfDoesntExist(configUri, defaultConfig);
// open the file in the editor
await vscode.commands.executeCommand("vscode.open", configUri);
}
16 changes: 14 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from "vscode";
import { glimpseRun } from "./glimpse";
import { glimpseConfigure } from "./config";

// This method is called when the extension is activated.
// The extension is activated the very first time the command is executed.
Expand All @@ -8,9 +9,20 @@ export function activate(context: vscode.ExtensionContext) {

// The commandId parameter must match the command field in package.json
context.subscriptions.push(
vscode.commands.registerCommand("glimpse.run", glimpseRun)
vscode.commands.registerCommand("glimpse.run", () => {
glimpseRun();
})
);
context.subscriptions.push(
vscode.commands.registerCommand("glimpse.configure", () => {
glimpseConfigure(context).catch((err) => {
console.error("Failed to run async Glimpse configure", err);
});
})
);
}

// This method is called when the extension is deactivated
export function deactivate() {}
export function deactivate() {
console.log("deactivate Glimpse");
}
25 changes: 11 additions & 14 deletions src/glimpse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as vscode from "vscode";
import { Menu, menu } from "./keys";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function glimpseRun(context: vscode.ExtensionContext) {
export function glimpseRun() {
try {
const glimpses = menu();
pick(glimpses);
Expand All @@ -26,37 +25,35 @@ function pick(glimpses: Menu) {
quickPick.items = options;
// Allow to select action by key press.
quickPick.onDidChangeValue(() => {
onValueChange(quickPick, glimpses);
onValueChange(quickPick, glimpses).catch((err) => {
console.error("onDidChangeValue failure", err);
});
});
// Allow to select action with enter key.
quickPick.onDidAccept(() => {
executeGlimpse(quickPick, glimpses);
executeGlimpse(quickPick, glimpses).catch((err) => {
console.error("onDidAccept failure", err);
});
});
quickPick.onDidHide(() => quickPick.dispose());
quickPick.show();
}

function onValueChange(
quickPick: vscode.QuickPick<vscode.QuickPickItem>,
glimpses: Menu
) {
async function onValueChange(quickPick: vscode.QuickPick<vscode.QuickPickItem>, glimpses: Menu) {
console.log("user typed ", quickPick.value);
if (quickPick.value.length !== 0) {
executeGlimpse(quickPick, glimpses);
await executeGlimpse(quickPick, glimpses);
}
}

function executeGlimpse(
quickPick: vscode.QuickPick<vscode.QuickPickItem>,
glimpses: Menu
) {
async function executeGlimpse(quickPick: vscode.QuickPick<vscode.QuickPickItem>, glimpses: Menu) {
const activeItem = quickPick.activeItems[0];
if (activeItem) {
const key = activeItem.label;
const command = glimpses.items.get(key)?.command;
if (command) {
if (typeof command === "string") {
vscode.commands.executeCommand(command);
await vscode.commands.executeCommand(command);
if (glimpses.transient) {
pick(glimpses);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ async function main() {
}
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
main();
1 change: 1 addition & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as vscode from "vscode";
// import * as myExtension from '../../extension';

suite("Extension Test Suite", () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showInformationMessage("Start all tests.");

test("Sample test", () => {
Expand Down

0 comments on commit 6fea8e1

Please sign in to comment.