diff --git a/statusbar-sample/.gitignore b/statusbar-sample/.gitignore new file mode 100644 index 00000000..8e5962ee --- /dev/null +++ b/statusbar-sample/.gitignore @@ -0,0 +1,2 @@ +out +node_modules \ No newline at end of file diff --git a/statusbar-sample/.vscode/launch.json b/statusbar-sample/.vscode/launch.json new file mode 100644 index 00000000..c776d21e --- /dev/null +++ b/statusbar-sample/.vscode/launch.json @@ -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" + } + ] +} diff --git a/statusbar-sample/.vscode/settings.json b/statusbar-sample/.vscode/settings.json new file mode 100644 index 00000000..3f5aa9cf --- /dev/null +++ b/statusbar-sample/.vscode/settings.json @@ -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 +} \ No newline at end of file diff --git a/statusbar-sample/.vscode/tasks.json b/statusbar-sample/.vscode/tasks.json new file mode 100644 index 00000000..1992757d --- /dev/null +++ b/statusbar-sample/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/statusbar-sample/.vscodeignore b/statusbar-sample/.vscodeignore new file mode 100644 index 00000000..795e7143 --- /dev/null +++ b/statusbar-sample/.vscodeignore @@ -0,0 +1,9 @@ +.vscode/** +typings/** +out/test/** +test/** +src/** +**/*.map +.gitignore +tsconfig.json +vsc-extension-quickstart.md diff --git a/statusbar-sample/CHANGELOG.md b/statusbar-sample/CHANGELOG.md new file mode 100644 index 00000000..37f799e8 --- /dev/null +++ b/statusbar-sample/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/statusbar-sample/README.md b/statusbar-sample/README.md new file mode 100644 index 00000000..750cd662 --- /dev/null +++ b/statusbar-sample/README.md @@ -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 + +![Print References](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 + +# How to run locally + +* `npm run compile` to start the compiler in watch mode +* open this folder in VS Code and press `F5` \ No newline at end of file diff --git a/statusbar-sample/package.json b/statusbar-sample/package.json new file mode 100644 index 00000000..442b81d8 --- /dev/null +++ b/statusbar-sample/package.json @@ -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" + } +} \ No newline at end of file diff --git a/statusbar-sample/preview.gif b/statusbar-sample/preview.gif new file mode 100644 index 00000000..9ffd2b81 Binary files /dev/null and b/statusbar-sample/preview.gif differ diff --git a/statusbar-sample/src/extension.ts b/statusbar-sample/src/extension.ts new file mode 100644 index 00000000..abca2ff3 --- /dev/null +++ b/statusbar-sample/src/extension.ts @@ -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() { +} \ No newline at end of file diff --git a/statusbar-sample/test/extension.test.ts b/statusbar-sample/test/extension.test.ts new file mode 100644 index 00000000..5c4a4da3 --- /dev/null +++ b/statusbar-sample/test/extension.test.ts @@ -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)); + }); +}); \ No newline at end of file diff --git a/statusbar-sample/test/index.ts b/statusbar-sample/test/index.ts new file mode 100644 index 00000000..50bae456 --- /dev/null +++ b/statusbar-sample/test/index.ts @@ -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; \ No newline at end of file diff --git a/statusbar-sample/tsconfig.json b/statusbar-sample/tsconfig.json new file mode 100644 index 00000000..11282c9a --- /dev/null +++ b/statusbar-sample/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "outDir": "out", + "lib": [ + "es6" + ], + "sourceMap": true, + "rootDir": "." + }, + "exclude": [ + "node_modules", + ".vscode-test" + ] +} \ No newline at end of file