mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
more updates #110
This commit is contained in:
@ -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
|
||||
|
||||

|
||||
|
||||
# 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)
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user