Skip to content

Commit

Permalink
perf: handle extension registration efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Nov 23, 2018
1 parent 3be1f3d commit 9a6fd40
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
5 changes: 5 additions & 0 deletions src/commandManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { commands, Disposable, window } from 'vscode';

export const ACTIVATION_COMMAND: Disposable = commands.registerCommand('scss-formatter.activate', () => {
window.showInformationMessage('SCSS Formatter Activated');
});
47 changes: 8 additions & 39 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
'use strict';

import {
commands, Disposable, ExtensionContext,
languages, window, workspace
} from 'vscode';
import { ExtensionContext, languages } from 'vscode';

import { ACTIVATION_COMMAND } from './commandManager';
import { registerErrorHandlerDisposables, setupErrorHandler } from './errorHandler';
import { SCSSFormatter } from './ScssFormatProvider';
import { SCSSFormatter } from './scssFormatterProvider';
import { languageSelector } from './utils';

let formatterHandler: undefined | Disposable;

// dispose formatter
function disposeHandlers() {
if (formatterHandler) {
formatterHandler.dispose();
}
formatterHandler = undefined;
}

// register formatter
function registerFormatter() {
disposeHandlers();
const scssFormatter = new SCSSFormatter();
formatterHandler = languages.registerDocumentFormattingEditProvider(
languageSelector, scssFormatter
);
}

// method is called when extension is activated
export function activate(context: ExtensionContext) {
registerFormatter();

const disposable = commands.registerCommand('scss-formatter.activate', () => {
window.showInformationMessage('SCSS Formatter Activated');
});
const scssFormatter = new SCSSFormatter();

context.subscriptions.push(
workspace.onDidChangeWorkspaceFolders(registerFormatter),
{ dispose: disposeHandlers },
setupErrorHandler(),
disposable,
...registerErrorHandlerDisposables()
);
context.subscriptions.push(languages.registerDocumentFormattingEditProvider(languageSelector, scssFormatter));
context.subscriptions.push(ACTIVATION_COMMAND);
context.subscriptions.push(setupErrorHandler());
context.subscriptions.push(...registerErrorHandlerDisposables());
}

// method is called when extension is deactivated
export function deactivate() { } // tslint:disable-line:no-empty
File renamed without changes.

0 comments on commit 9a6fd40

Please sign in to comment.