mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
update status bar sample, #110
This commit is contained in:
21
statusbar-sample/.vscode/launch.json
vendored
21
statusbar-sample/.vscode/launch.json
vendored
@ -7,22 +7,15 @@
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/out/src/**/*.js"],
|
||||
"preLaunchTask": "tsc: watch - tsconfig.json"
|
||||
},
|
||||
{
|
||||
"name": "Launch Tests",
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/out/test/**/*.js"],
|
||||
"preLaunchTask": "tsc: watch - tsconfig.json"
|
||||
"outFiles": [
|
||||
"${workspaceRoot}/out/src/**/*.js"
|
||||
],
|
||||
"preLaunchTask": "npm: watch"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
2413
statusbar-sample/package-lock.json
generated
2413
statusbar-sample/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -20,26 +20,14 @@
|
||||
"activationEvents": [
|
||||
"*"
|
||||
],
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "extension.selectedLines",
|
||||
"title": "Show Selected Lines"
|
||||
}
|
||||
]
|
||||
},
|
||||
"main": "./out/extension",
|
||||
"scripts": {
|
||||
"vscode:prepublish": "tsc -p ./",
|
||||
"compile": "tsc -watch -p ./",
|
||||
"postinstall": "node ./node_modules/vscode/bin/install",
|
||||
"test": "node ./node_modules/vscode/bin/test"
|
||||
"watch": "tsc -watch -p ./",
|
||||
"postinstall": "node ./node_modules/vscode/bin/install"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^6.0.40",
|
||||
"mocha": "^2.3.3",
|
||||
"tslint": "^5.11.0",
|
||||
"typescript": "^2.1.4",
|
||||
"vscode": "^1.1.17"
|
||||
"typescript": "^3.1.6",
|
||||
"vscode": "^1.1.21"
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,53 +4,37 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ExtensionContext, StatusBarAlignment, window, StatusBarItem, Selection, workspace, TextEditor, commands } from 'vscode';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
const status = window.createStatusBarItem(StatusBarAlignment.Right, 100);
|
||||
status.command = 'extension.selectedLines';
|
||||
context.subscriptions.push(status);
|
||||
let myStatusBarItem: vscode.StatusBarItem;
|
||||
|
||||
context.subscriptions.push(window.onDidChangeActiveTextEditor(e => updateStatus(status)));
|
||||
context.subscriptions.push(window.onDidChangeTextEditorSelection(e => updateStatus(status)));
|
||||
context.subscriptions.push(window.onDidChangeTextEditorViewColumn(e => updateStatus(status)));
|
||||
context.subscriptions.push(workspace.onDidOpenTextDocument(e => updateStatus(status)));
|
||||
context.subscriptions.push(workspace.onDidCloseTextDocument(e => updateStatus(status)));
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
context.subscriptions.push(commands.registerCommand('extension.selectedLines', () => {
|
||||
window.showInformationMessage(getSelectedLines());
|
||||
}));
|
||||
myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
|
||||
|
||||
updateStatus(status);
|
||||
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));
|
||||
|
||||
updateStatusBarItem();
|
||||
}
|
||||
|
||||
function updateStatus(status: StatusBarItem): void {
|
||||
let text = getSelectedLines();
|
||||
if (text) {
|
||||
status.text = '$(megaphone) ' + text;
|
||||
}
|
||||
|
||||
if (text) {
|
||||
status.show();
|
||||
function updateStatusBarItem(): void {
|
||||
let n = getNumberOfSelectedLines(vscode.window.activeTextEditor);
|
||||
if (n > 0) {
|
||||
myStatusBarItem.text = `$(megaphone) ${n} line(s) selected`;;
|
||||
myStatusBarItem.show();
|
||||
} else {
|
||||
status.hide();
|
||||
myStatusBarItem.hide();
|
||||
}
|
||||
}
|
||||
|
||||
function getSelectedLines(): string {
|
||||
const editor = window.activeTextEditor;
|
||||
let text: string;
|
||||
|
||||
function getNumberOfSelectedLines(editor: vscode.TextEditor | undefined): number {
|
||||
let lines = 0;
|
||||
if (editor) {
|
||||
let lines = 0;
|
||||
editor.selections.forEach(selection => {
|
||||
lines += (selection.end.line - selection.start.line + 1);
|
||||
});
|
||||
|
||||
if (lines > 0) {
|
||||
text = `${lines} line(s) selected`;
|
||||
}
|
||||
lines = editor.selections.reduce((prev, curr) => prev + (curr.end.line - curr.start.line), 0);
|
||||
}
|
||||
|
||||
return text;
|
||||
return lines;
|
||||
}
|
||||
|
||||
@ -1,11 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"outDir": "out",
|
||||
"lib": ["es6"],
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"strict": true,
|
||||
"sourceMap": true,
|
||||
"rootDir": "src"
|
||||
},
|
||||
"exclude": ["node_modules"]
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
".vscode-test"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user