Skip to content

Commit

Permalink
Add new activation logic for lightning and lwc extensions (#1253) (#1279
Browse files Browse the repository at this point in the history
)

@W-6075727@
  • Loading branch information
lcampos authored Apr 23, 2019
1 parent 78b8578 commit 75cf332
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 115 deletions.
33 changes: 24 additions & 9 deletions packages/salesforcedx-vscode-lightning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"aura-language-server": "2.1.0",
"change-case": "^3.1.0",
"lightning-lsp-common": "2.1.0",
"open": "^6.0.0",
"open": "6.0.0",
"vscode-extension-telemetry": "0.0.17",
"vscode-languageclient": "^3.5.1"
},
Expand All @@ -37,7 +37,7 @@
"@types/chai": "^4.0.0",
"@types/mocha": "2.2.38",
"@types/node": "8.9.3",
"@types/open": "^6.0.0",
"@types/open": "6.0.0",
"@types/sinon": "^2.3.2",
"chai": "^4.0.2",
"cross-env": "5.2.0",
Expand All @@ -46,9 +46,6 @@
"typescript": "3.1.6",
"vscode": "1.1.17"
},
"extensionDependencies": [
"salesforce.salesforcedx-vscode-core"
],
"scripts": {
"vscode:prepublish": "npm prune --production",
"vscode:package": "vsce package",
Expand All @@ -64,8 +61,11 @@
"test:vscode-insiders-integration": "cross-env CODE_VERSION=insiders npm run test:vscode-integration"
},
"activationEvents": [
"onLanguage:html",
"onLanguage:javascript",
"workspaceContains:sfdx-project.json",
"workspaceContains:**/core/workspace-user.xml"
"workspaceContains:**/workspace-user.xml",
"onView:salesforce-lightning-explorer"
],
"main": "./out/src",
"contributes": {
Expand Down Expand Up @@ -104,7 +104,7 @@
{
"id": "salesforce-lightning-components",
"name": "%lightning_explorer_name%",
"when": "config.salesforcedx-vscode-lightning.show-lightning-explorer"
"when": "config.salesforcedx-vscode-lightning.showLightningExplorer"
}
]
},
Expand All @@ -126,13 +126,28 @@
],
"configuration": {
"type": "object",
"title": "%feature_previews_title%",
"title": "%lightning_preferences%",
"properties": {
"salesforcedx-vscode-lightning.show-lightning-explorer": {
"salesforcedx-vscode-lightning.showLightningExplorer": {
"type": "boolean",
"scope": "window",
"default": false,
"description": "%show_lightning_explorer_description%"
},
"salesforcedx-vscode-lightning.activationMode": {
"type": "string",
"description": "%activation_mode_description%",
"enum": [
"always",
"autodetect",
"off"
],
"enumDescriptions": [
"%activation_mode_always_on%",
"%activation_mode_autodetect%",
"%activation_mode_off%"
],
"default": "autodetect"
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/salesforcedx-vscode-lightning/package.nls.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"lightning_open_component_title": "SFDX: Open Lightning Component",
"lightning_explorer_title": "Lightning Explorer",
"feature_previews_title": "Salesforce Feature Previews",
"lightning_preferences": "Salesforce Lightning",
"lightning_explorer_name": "Lightning Components",
"show_lightning_explorer_description": "Show the lightning explorer view"
"show_lightning_explorer_description": "Preview Feature: Show the Lightning Explorer View",
"activation_mode_description": "Configures when the Aura and LWC language servers (LSPs) should be active (requires restart)",
"activation_mode_always_on": "Lighning LSPs will always be activate, regardless of vscode workspace structure",
"activation_mode_autodetect": "Lighning LSPs will only become active if a valid vscode workspace is detected",
"activation_mode_off": "Lighning LSPs will never be activated"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { paramCase } from 'change-case';
import opn = require('open');
import { stringify } from 'querystring';
import { Uri, window, workspace } from 'vscode';
import { LanguageClient } from 'vscode-languageclient';
import { COMPONENT_LIBRARY_BUNDLE_LINK } from '../../constants';
Expand All @@ -16,7 +17,7 @@ export function createQuickOpenCommand(client: LanguageClient) {
return (arg: any) => {
if (arg instanceof LwcNode) {
const node = arg as LwcNode;
const tag: string = node.label;
const tag: string = node.label || '';
const url: string = COMPONENT_LIBRARY_BUNDLE_LINK + tag;
opn(url).catch();
return;
Expand All @@ -26,7 +27,7 @@ export function createQuickOpenCommand(client: LanguageClient) {
.onReady()
.then(() => {
console.log('Client ready');
client.sendRequest('salesforce/listComponents', {}).then(
client.sendRequest<string>('salesforce/listComponents', {}).then(
(jsonData: string) => {
const tags: Map<string, any> = new Map(JSON.parse(jsonData));
const items: TagItem[] = [];
Expand All @@ -49,11 +50,15 @@ export function createQuickOpenCommand(client: LanguageClient) {
}
}
const sorted = items.sort((a, b) => (a.label < b.label ? -1 : 1));
window.showQuickPick(sorted, {}).then((data: TagItem) => {
workspace
.openTextDocument(data.uri)
.then(doc => window.showTextDocument(doc));
});
window
.showQuickPick(sorted, {})
.then((data: TagItem | undefined) => {
if (data) {
workspace
.openTextDocument(data.uri)
.then(doc => window.showTextDocument(doc));
}
});
},
err => {
console.error(
Expand Down
53 changes: 53 additions & 0 deletions packages/salesforcedx-vscode-lightning/src/dxsupport/waitForDX.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as vscode from 'vscode';

let wait: Promise<vscode.Extension<any>>;

export async function waitForDX(activate: boolean = false) {
if (!wait) {
wait = new Promise((resolve, reject) => {
// 120 seconds from now
const expires = new Date().getTime() + 1000 * 120;
const dosetup = () => {
let success = false;
try {
const coreDependency = vscode.extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
);
if (coreDependency && !coreDependency.isActive && activate) {
return coreDependency.activate().then(api => {
resolve(
vscode.extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
)
);
});
}
if (coreDependency && coreDependency.exports) {
success = true;
resolve(coreDependency);
}
} catch (ignore) {
// ignore
}
if (!success) {
if (new Date().getTime() > expires) {
const msg =
'salesforce.salesforcedx-vscode-core not installed or activated, some features unavailable';
console.log(msg);
reject(msg);
} else {
setTimeout(dosetup, 100);
}
}
};
setTimeout(dosetup, 100);
});
}
return wait;
}
90 changes: 62 additions & 28 deletions packages/salesforcedx-vscode-lightning/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { shared as lspCommon } from 'lightning-lsp-common';
import { WorkspaceType } from 'lightning-lsp-common/lib/shared';
import * as path from 'path';
import {
commands,
ExtensionContext,
extensions,
ProgressLocation,
Uri,
window,
Expand All @@ -26,8 +27,6 @@ import { nls } from './messages';
import { telemetryService } from './telemetry';
import { ComponentTreeProvider } from './views/component-tree-provider';

let client: LanguageClient;

// See https://github.com/Microsoft/vscode-languageserver-node/issues/105
export function code2ProtocolConverter(value: Uri): string {
if (/^win32/.test(process.platform)) {
Expand All @@ -43,17 +42,58 @@ function protocol2CodeConverter(value: string): Uri {
return Uri.parse(value);
}

function getActivationMode(): string {
const config = workspace.getConfiguration('salesforcedx-vscode-lightning');
return config.get('activationMode') || 'autodetect'; // default to autodetect
}

export async function activate(context: ExtensionContext) {
console.log('Aura Components Extension Activated');
const extensionHRStart = process.hrtime();
console.log('Activation Mode: ' + getActivationMode());
// Run our auto detection routine before we activate
// 1) If activationMode is off, don't startup no matter what
if (getActivationMode() === 'off') {
console.log('Aura Language Server activationMode set to off, exiting...');
return;
}

// 2) if we have no workspace folders, exit
if (!workspace.workspaceFolders) {
console.log('No workspace, exiting extension');
return;
}

// 3) If activationMode is autodetect or always, check workspaceType before startup
const workspaceType = lspCommon.detectWorkspaceType(
workspace.workspaceFolders[0].uri.fsPath
);

// Check if we have a valid project structure
if (getActivationMode() === 'autodetect' && !lspCommon.isLWC(workspaceType)) {
// If activationMode === autodetect and we don't have a valid workspace type, exit
console.log(
'Aura LSP - autodetect did not find a valid project structure, exiting....'
);
console.log('WorkspaceType detected: ' + workspaceType);
return;
}
// If activationMode === always, ignore workspace type and continue activating

// 4) If we get here, we either passed autodetect validation or activationMode == always
console.log('Aura Components Extension Activated');
console.log('WorkspaceType detected: ' + workspaceType);

// Start the Aura Language Server

// Setup the language server
const serverModule = context.asAbsolutePath(
path.join('node_modules', 'aura-language-server', 'lib', 'server.js')
);

// The debug options for the server
const debugOptions = { execArgv: ['--nolazy', '--inspect=6020'] };
// let debugOptions = { };
const debugOptions = {
execArgv: ['--nolazy', '--inspect-brk=6020']
};

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
Expand All @@ -66,6 +106,7 @@ export async function activate(context: ExtensionContext) {
}
};

// Setup our fileSystemWatchers
const clientOptions: LanguageClientOptions = {
outputChannelName: nls.localize('channel_name'),
documentSelector: [
Expand Down Expand Up @@ -109,19 +150,22 @@ export async function activate(context: ExtensionContext) {
};

// Create the language client and start the client.
this.client = client = new LanguageClient(
const client = new LanguageClient(
'auraLanguageServer',
nls.localize('client_name'),
serverOptions,
clientOptions
);
// UI customizations

// Add Quick Open command
context.subscriptions.push(
commands.registerCommand(
'salesforce-lightning-quickopen',
createQuickOpenCommand(client)
)
);

// Add Lightning Explorer data provider
const componentProvider = new ComponentTreeProvider(client, context);
window.registerTreeDataProvider(
'salesforce-lightning-components',
Expand All @@ -131,30 +175,20 @@ export async function activate(context: ExtensionContext) {
client
.onReady()
.then(() => {
this.client.onNotification('salesforce/indexingStarted', startIndexing);
this.client.onNotification('salesforce/indexingEnded', endIndexing);
client.onNotification('salesforce/indexingStarted', startIndexing);
client.onNotification('salesforce/indexingEnded', endIndexing);
})
.catch();

// do this last
client.start();
context.subscriptions.push(this.client);
// Start the language server
const disp = client.start();

const sfdxCoreExtension = extensions.getExtension(
'salesforce.salesforcedx-vscode-core'
);

// Telemetry
if (sfdxCoreExtension && sfdxCoreExtension.exports) {
sfdxCoreExtension.exports.telemetryService.showTelemetryMessage();

telemetryService.initializeService(
sfdxCoreExtension.exports.telemetryService.getReporter(),
sfdxCoreExtension.exports.telemetryService.isTelemetryEnabled()
);
}
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disp);

telemetryService.sendExtensionActivationEvent(extensionHRStart);
// Notify telemetry that our extension is now active
telemetryService.sendExtensionActivationEvent(extensionHRStart).catch();
}

let indexingResolve: any;
Expand Down Expand Up @@ -185,5 +219,5 @@ function reportIndexing(indexingPromise: Promise<void>) {

export function deactivate() {
console.log('Aura Components Extension Deactivated');
telemetryService.sendExtensionDeactivationEvent();
telemetryService.sendExtensionDeactivationEvent().catch();
}
Loading

0 comments on commit 75cf332

Please sign in to comment.