more updates #110

This commit is contained in:
Johannes Rieken
2018-11-15 10:41:25 +01:00
parent 3fedfb19e9
commit cf2e614357
2 changed files with 21 additions and 20 deletions

View File

@ -2,21 +2,11 @@
This is a sample extension that adds a status bar entry showing the current number of selected lines.
It is not intended as a product quality extension.
- Open a text editor
- Select some lines
- See the number of selected lines in the status bar
![Show number of selected lines](https://raw.githubusercontent.com/Microsoft/vscode-extension-samples/master/statusbar-sample/preview.gif)
# How it works, what it shows?
- The extension implements and registers a [`StatusBarItem`](http://code.visualstudio.com/docs/extensionAPI/vscode-api#StatusBarItem) to show the number of selected lines.
- The extension listens to [`events`](http://code.visualstudio.com/docs/extensionAPI/vscode-api#_window) when the active text editor changes to keep the number of selected lines up to date.
- Registers a command via `package.json` that will show the number of selected lines as message
## VS Code API
# How to run locally
### `vscode` module
* `npm run compile` to start the compiler in watch mode
* open this folder in VS Code and press `F5`
- [`languages.registerCompletionItemProvider`](https://code.visualstudio.com/docs/extensionAPI/vscode-api#window.createStatusBarItem)

View File

@ -8,23 +8,34 @@ import * as vscode from 'vscode';
let myStatusBarItem: vscode.StatusBarItem;
export function activate(context: vscode.ExtensionContext) {
export function activate({ subscriptions }: vscode.ExtensionContext) {
// register a command that is invoked when the status bar
// item is selected
const myCommandId = 'sample.showSelectionCount';
subscriptions.push(vscode.commands.registerCommand(myCommandId, () => {
let n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
vscode.window.showInformationMessage(`Yeah, ${n} line(s) selected... Keep going!`);
}));
// 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);
context.subscriptions.push(myStatusBarItem);
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(updateStatusBarItem));
context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(updateStatusBarItem));
context.subscriptions.push(vscode.window.onDidChangeTextEditorViewColumn(updateStatusBarItem));
// 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
updateStatusBarItem();
}
function updateStatusBarItem(): void {
let n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
if (n > 0) {
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;;
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;
myStatusBarItem.show();
} else {
myStatusBarItem.hide();