Skip to content

Commit

Permalink
Adapt statusbar-sample to demonstrate microsoft/vscode#160694
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed Sep 12, 2022
1 parent 133fa26 commit 65f033c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
18 changes: 16 additions & 2 deletions statusbar-sample/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Extension",
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/src/**/*.js"
],
"preLaunchTask": "npm: watch"
},
{
"name": "ONLY Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--disableExtensions"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/src/**/*.js"
Expand Down
4 changes: 2 additions & 2 deletions statusbar-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "https://github.com/Microsoft/vscode-extension-samples/issues"
},
"engines": {
"vscode": "^1.32.0"
"vscode": "^1.71.0"
},
"categories": [
"Other"
Expand All @@ -30,7 +30,7 @@
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/vscode": "^1.32.0",
"@types/vscode": "^1.71.0",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"eslint": "^8.13.0",
Expand Down
39 changes: 17 additions & 22 deletions statusbar-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,36 @@ let myStatusBarItem: vscode.StatusBarItem;
export function activate({ subscriptions }: vscode.ExtensionContext) {

// register a command that is invoked when the status bar
// item is selected
const myCommandId = 'sample.showSelectionCount';
// item is clicked
const myCommandId = 'sample.updateItem';
subscriptions.push(vscode.commands.registerCommand(myCommandId, () => {
const n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
vscode.window.showInformationMessage(`Yeah, ${n} line(s) selected... Keep going!`);
updateStatusBarItem();
}));

// create a new status bar item that we can now manage
myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
myStatusBarItem.command = myCommandId;
subscriptions.push(myStatusBarItem);

// register some listener that make sure the status bar
// item always up-to-date
subscriptions.push(vscode.window.onDidChangeActiveTextEditor(updateStatusBarItem));
subscriptions.push(vscode.window.onDidChangeTextEditorSelection(updateStatusBarItem));

// update status bar item once at start
// update status bar item immediately from activate()
updateStatusBarItem();
}

function updateStatusBarItem(): void {
const n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
if (n > 0) {
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;
let shell;
let attempt;

// Try multiple times in case it's a timing issue.
// TODO add a small delay between attempts to see if it succeeds after a bit.
for (attempt = 1; attempt <= 10; attempt++) {
shell = vscode.env.shell;
if (shell !== '') break;
}
if (vscode.env.shell === '') {
myStatusBarItem.text = `vscode.env.shell is EMPTY after ${attempt - 1} tries - CLICK HERE to check again`;
myStatusBarItem.show();
} else {
myStatusBarItem.hide();
}
}

function getNumberOfSelectedLines(editor: vscode.TextEditor | undefined): number {
let lines = 0;
if (editor) {
lines = editor.selections.reduce((prev, curr) => prev + (curr.end.line - curr.start.line), 0);
myStatusBarItem.text = `After ${attempt} tries shell is ${shell}`;
myStatusBarItem.show();
}
return lines;
}

0 comments on commit 65f033c

Please sign in to comment.