mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
Add a sample for status bar contribution (show number of selected lines)
This commit is contained in:
2
statusbar-sample/.gitignore
vendored
Normal file
2
statusbar-sample/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
out
|
||||
node_modules
|
||||
28
statusbar-sample/.vscode/launch.json
vendored
Normal file
28
statusbar-sample/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
// A launch configuration that compiles the extension and then opens it inside a new window
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Extension",
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/out/src/**/*.js"],
|
||||
"preLaunchTask": "npm"
|
||||
},
|
||||
{
|
||||
"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": "npm"
|
||||
}
|
||||
]
|
||||
}
|
||||
10
statusbar-sample/.vscode/settings.json
vendored
Normal file
10
statusbar-sample/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.exclude": {
|
||||
"out": false // set this to true to hide the "out" folder with the compiled JS files
|
||||
},
|
||||
"search.exclude": {
|
||||
"out": true // set this to false to include "out" folder in search results
|
||||
},
|
||||
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
|
||||
}
|
||||
30
statusbar-sample/.vscode/tasks.json
vendored
Normal file
30
statusbar-sample/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
// Available variables which can be used inside of strings.
|
||||
// ${workspaceRoot}: the root folder of the team
|
||||
// ${file}: the current opened file
|
||||
// ${fileBasename}: the current opened file's basename
|
||||
// ${fileDirname}: the current opened file's dirname
|
||||
// ${fileExtname}: the current opened file's extension
|
||||
// ${cwd}: the current working directory of the spawned process
|
||||
|
||||
// A task runner that calls a custom npm script that compiles the extension.
|
||||
{
|
||||
"version": "0.1.0",
|
||||
|
||||
// we want to run npm
|
||||
"command": "npm",
|
||||
|
||||
// the command is a shell script
|
||||
"isShellCommand": true,
|
||||
|
||||
// show the output window only if unrecognized errors occur.
|
||||
"showOutput": "silent",
|
||||
|
||||
// we run the custom script "compile" as defined in package.json
|
||||
"args": ["run", "compile", "--loglevel", "silent"],
|
||||
|
||||
// The tsc compiler is started in watching mode
|
||||
"isWatching": true,
|
||||
|
||||
// use the standard tsc in watch mode problem matcher to find compile problems in the output.
|
||||
"problemMatcher": "$tsc-watch"
|
||||
}
|
||||
9
statusbar-sample/.vscodeignore
Normal file
9
statusbar-sample/.vscodeignore
Normal file
@ -0,0 +1,9 @@
|
||||
.vscode/**
|
||||
typings/**
|
||||
out/test/**
|
||||
test/**
|
||||
src/**
|
||||
**/*.map
|
||||
.gitignore
|
||||
tsconfig.json
|
||||
vsc-extension-quickstart.md
|
||||
7
statusbar-sample/CHANGELOG.md
Normal file
7
statusbar-sample/CHANGELOG.md
Normal file
@ -0,0 +1,7 @@
|
||||
# Change Log
|
||||
All notable changes to the "status-ts" extension will be documented in this file.
|
||||
|
||||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
||||
|
||||
## [Unreleased]
|
||||
- Initial release
|
||||
22
statusbar-sample/README.md
Normal file
22
statusbar-sample/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# References Editor Sample
|
||||
|
||||
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
|
||||
|
||||
# How to run locally
|
||||
|
||||
* `npm run compile` to start the compiler in watch mode
|
||||
* open this folder in VS Code and press `F5`
|
||||
45
statusbar-sample/package.json
Normal file
45
statusbar-sample/package.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "status-ts",
|
||||
"displayName": "Selected Line Numbers",
|
||||
"description": "Shows the number of selected lines in the status bar",
|
||||
"version": "0.0.1",
|
||||
"publisher": "bpasero",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Microsoft/vscode-extension-samples"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/Microsoft/vscode-extension-samples/issues"
|
||||
},
|
||||
"engines": {
|
||||
"vscode": "^1.5.0"
|
||||
},
|
||||
"categories": [
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"*"
|
||||
],
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "extension.selectedLines",
|
||||
"title": "Show Selected Lines"
|
||||
}
|
||||
]
|
||||
},
|
||||
"main": "./out/src/extension",
|
||||
"scripts": {
|
||||
"vscode:prepublish": "tsc -p ./",
|
||||
"compile": "tsc -watch -p ./",
|
||||
"postinstall": "node ./node_modules/vscode/bin/install",
|
||||
"test": "node ./node_modules/vscode/bin/test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^2.0.3",
|
||||
"vscode": "^1.0.0",
|
||||
"mocha": "^2.3.3",
|
||||
"@types/node": "^6.0.40",
|
||||
"@types/mocha": "^2.2.32"
|
||||
}
|
||||
}
|
||||
BIN
statusbar-sample/preview.gif
Normal file
BIN
statusbar-sample/preview.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 MiB |
59
statusbar-sample/src/extension.ts
Normal file
59
statusbar-sample/src/extension.ts
Normal file
@ -0,0 +1,59 @@
|
||||
/*---------------------------------------------------------
|
||||
* Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
*--------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ExtensionContext, StatusBarAlignment, window, StatusBarItem, Selection, workspace, TextEditor, commands } from 'vscode';
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
const status = window.createStatusBarItem(StatusBarAlignment.Right, 100);
|
||||
status.command = 'extension.selectedLines';
|
||||
context.subscriptions.push(status);
|
||||
|
||||
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)));
|
||||
|
||||
context.subscriptions.push(commands.registerCommand('extension.selectedLines', () => {
|
||||
window.showInformationMessage(getSelectedLines());
|
||||
}));
|
||||
|
||||
updateStatus(status);
|
||||
}
|
||||
|
||||
function updateStatus(status: StatusBarItem): void {
|
||||
let text = getSelectedLines();
|
||||
if (text) {
|
||||
status.text = '$(megaphone) ' + text;
|
||||
}
|
||||
|
||||
if (text) {
|
||||
status.show();
|
||||
} else {
|
||||
status.hide();
|
||||
}
|
||||
}
|
||||
|
||||
function getSelectedLines(): string {
|
||||
const editor = window.activeTextEditor;
|
||||
let text: string;
|
||||
|
||||
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`;
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
export function deactivate() {
|
||||
}
|
||||
22
statusbar-sample/test/extension.test.ts
Normal file
22
statusbar-sample/test/extension.test.ts
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Note: This example test is leveraging the Mocha test framework.
|
||||
// Please refer to their documentation on https://mochajs.org/ for help.
|
||||
//
|
||||
|
||||
// The module 'assert' provides assertion methods from node
|
||||
import * as assert from 'assert';
|
||||
|
||||
// You can import and use all API from the 'vscode' module
|
||||
// as well as import your extension to test it
|
||||
import * as vscode from 'vscode';
|
||||
import * as myExtension from '../src/extension';
|
||||
|
||||
// Defines a Mocha test suite to group tests of similar kind together
|
||||
suite("Extension Tests", () => {
|
||||
|
||||
// Defines a Mocha unit test
|
||||
test("Something 1", () => {
|
||||
assert.equal(-1, [1, 2, 3].indexOf(5));
|
||||
assert.equal(-1, [1, 2, 3].indexOf(0));
|
||||
});
|
||||
});
|
||||
22
statusbar-sample/test/index.ts
Normal file
22
statusbar-sample/test/index.ts
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING
|
||||
//
|
||||
// This file is providing the test runner to use when running extension tests.
|
||||
// By default the test runner in use is Mocha based.
|
||||
//
|
||||
// You can provide your own test runner if you want to override it by exporting
|
||||
// a function run(testRoot: string, clb: (error:Error) => void) that the extension
|
||||
// host can call to run the tests. The test runner is expected to use console.log
|
||||
// to report the results back to the caller. When the tests are finished, return
|
||||
// a possible error to the callback or null if none.
|
||||
|
||||
var testRunner = require('vscode/lib/testrunner');
|
||||
|
||||
// You can directly control Mocha options by uncommenting the following lines
|
||||
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
|
||||
testRunner.configure({
|
||||
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
|
||||
useColors: true // colored output from test results
|
||||
});
|
||||
|
||||
module.exports = testRunner;
|
||||
16
statusbar-sample/tsconfig.json
Normal file
16
statusbar-sample/tsconfig.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"outDir": "out",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"sourceMap": true,
|
||||
"rootDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
".vscode-test"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user