Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #279 from prabushi/autoclose
Browse files Browse the repository at this point in the history
Add usability improvements
  • Loading branch information
prabushi authored Mar 12, 2021
2 parents 451d018 + 424e444 commit 05444e2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 257 deletions.
180 changes: 90 additions & 90 deletions src/api-editor/activator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* under the License.
*
*/
import { workspace, commands, window, ViewColumn, ExtensionContext, TextEditor, WebviewPanel, TextDocumentChangeEvent, Uri, Position } from 'vscode';
import { workspace, commands, window, ViewColumn, ExtensionContext, TextEditor, WebviewPanel, TextDocumentChangeEvent, Uri } from 'vscode';
import { ExtendedLangClient } from '../core/extended-language-client';
import * as _ from 'lodash';
import { apiEditorRender } from './renderer';
import { BallerinaExtension } from '../core';
import { API_DESIGNER_NO_SERVICE } from '../core/messages';
// import { API_DESIGNER_NO_SERVICE } from '../core/messages';
import { WebViewRPCHandler, WebViewMethod, getCommonWebViewOptions } from '../utils';
import { join } from "path";
import { readFileSync } from "fs";
// import { readFileSync } from "fs";
import { TM_EVENT_OPEN_API_DESIGNER, CMP_API_DESIGNER, sendTelemetryEvent, sendTelemetryException } from '../telemetry';

const DEBOUNCE_WAIT = 500;
Expand All @@ -34,16 +34,16 @@ let oasEditorPanel: WebviewPanel | undefined;
let activeEditor: TextEditor | undefined;
let preventAPIDesignerUpdate = false;

function updateOASWebView(docUri: Uri, resp: string, stale: boolean): void {
if (oasEditorPanel) {
oasEditorPanel.webview.postMessage({
command: 'update',
docUri: docUri.toString(),
json: resp,
stale
});
}
}
// function updateOASWebView(docUri: Uri, resp: string, stale: boolean): void {
// if (oasEditorPanel) {
// oasEditorPanel.webview.postMessage({
// command: 'update',
// docUri: docUri.toString(),
// json: resp,
// stale
// });
// }
// }

function showAPIEditorPanel(context: ExtensionContext, langClient: ExtendedLangClient, serviceName: string): any {

Expand All @@ -56,15 +56,15 @@ function showAPIEditorPanel(context: ExtensionContext, langClient: ExtendedLangC
return;
}

const docUri = e.document.uri;
if (oasEditorPanel) {
langClient.getBallerinaOASDef(docUri, oasEditorPanel.title.split('-')[1].trim()).then((resp) => {
if (resp.ballerinaOASJson !== undefined) {
updateOASWebView(docUri, JSON.stringify(resp.ballerinaOASJson), false);
preventAPIDesignerUpdate = true;
}
});
}
// const docUri = e.document.uri;
// if (oasEditorPanel) {
// langClient.getBallerinaOASDef(docUri, oasEditorPanel.title.split('-')[1].trim()).then((resp) => {
// if (resp.ballerinaOASJson !== undefined) {
// updateOASWebView(docUri, JSON.stringify(resp.ballerinaOASJson), false);
// preventAPIDesignerUpdate = true;
// }
// });
// }
}
}, DEBOUNCE_WAIT));

Expand All @@ -77,27 +77,27 @@ function showAPIEditorPanel(context: ExtensionContext, langClient: ExtendedLangC
activeEditor = window.activeTextEditor;

if (oasEditorPanel) {
langClient.getServiceListForActiveFile(activeEditor.document.uri).then((resp) => {
if (resp.services && resp.services.length > 1) {
window.showQuickPick(resp.services).then((selected) => {
if (selected && activeEditor) {
const html = apiEditorRender(context, langClient, activeEditor.document.uri, selected);
if (oasEditorPanel && html) {
oasEditorPanel.webview.html = html;
oasEditorPanel.title = "Ballerina API Designer - " + selected;
}
}
});
} else {
if (activeEditor) {
const html = apiEditorRender(context, langClient, activeEditor.document.uri, resp.services[0]);
if (oasEditorPanel && html) {
oasEditorPanel.webview.html = html;
oasEditorPanel.title = "Ballerina API Designer - " + resp.services[0];
}
}
}
});
// langClient.getServiceListForActiveFile(activeEditor.document.uri).then((resp) => {
// if (resp.services && resp.services.length > 1) {
// window.showQuickPick(resp.services).then((selected) => {
// if (selected && activeEditor) {
// const html = apiEditorRender(context, langClient, activeEditor.document.uri, selected);
// if (oasEditorPanel && html) {
// oasEditorPanel.webview.html = html;
// oasEditorPanel.title = "Ballerina API Designer - " + selected;
// }
// }
// });
// } else {
// if (activeEditor) {
// const html = apiEditorRender(context, langClient, activeEditor.document.uri, resp.services[0]);
// if (oasEditorPanel && html) {
// oasEditorPanel.webview.html = html;
// oasEditorPanel.title = "Ballerina API Designer - " + resp.services[0];
// }
// }
// }
// });
}
}
});
Expand All @@ -118,42 +118,42 @@ function showAPIEditorPanel(context: ExtensionContext, langClient: ExtendedLangC

if (serviceName) {
executeCreateAPIEditor(serviceName);
} else {
langClient.getServiceListForActiveFile(activeEditor.document.uri).then(resp => {
if (resp.services.length === 0) {
const actions: string[] = [];
// Provide an action to fill up empty bal files with a default service
const actionAddService = "Add HTTP Service";
if (activeEditor && activeEditor.document.getText().trim().length === 0) {
actions.push(actionAddService);
}
window.showInformationMessage(API_DESIGNER_NO_SERVICE, ...actions)
.then((selection) => {
if (selection === actionAddService) {
const svcTemplatePath = join(context.extensionPath, "resources",
"templates", "http-service.bal");
const svcTemplate = readFileSync(svcTemplatePath).toString();
if (activeEditor) {
activeEditor.edit((editBuilder) => {
editBuilder.insert(new Position(0, 0), svcTemplate);
}).then((insertSuccess) => {
if (insertSuccess) {
commands.executeCommand(CMD_SHOW_API_EDITOR);
}
});
}
}
});
} else if (resp.services && resp.services.length > 1) {
window.showQuickPick(resp.services).then(service => {
if (service && activeEditor) {
executeCreateAPIEditor(service);
}
});
} else {
executeCreateAPIEditor(resp.services[0]);
}
});
// } else {
// langClient.getServiceListForActiveFile(activeEditor.document.uri).then(resp => {
// if (resp.services.length === 0) {
// const actions: string[] = [];
// // Provide an action to fill up empty bal files with a default service
// const actionAddService = "Add HTTP Service";
// if (activeEditor && activeEditor.document.getText().trim().length === 0) {
// actions.push(actionAddService);
// }
// window.showInformationMessage(API_DESIGNER_NO_SERVICE, ...actions)
// .then((selection) => {
// if (selection === actionAddService) {
// const svcTemplatePath = join(context.extensionPath, "resources",
// "templates", "http-service.bal");
// const svcTemplate = readFileSync(svcTemplatePath).toString();
// if (activeEditor) {
// activeEditor.edit((editBuilder) => {
// editBuilder.insert(new Position(0, 0), svcTemplate);
// }).then((insertSuccess) => {
// if (insertSuccess) {
// commands.executeCommand(CMD_SHOW_API_EDITOR);
// }
// });
// }
// }
// });
// } else if (resp.services && resp.services.length > 1) {
// window.showQuickPick(resp.services).then(service => {
// if (service && activeEditor) {
// executeCreateAPIEditor(service);
// }
// });
// } else {
// executeCreateAPIEditor(resp.services[0]);
// }
// });
}
}

Expand All @@ -172,17 +172,17 @@ function createAPIEditorPanel(selectedService: string, renderHtml: string,
oasEditorPanel.webview.html = renderHtml;

const remoteMethods: WebViewMethod[] = [
{
methodName: "getOpenApiDef",
handler: (args: any[]): Thenable<any> => {
return langClient.getBallerinaOASDef(args[0], args[1]);
}
}, {
methodName: 'triggerOpenApiDefChange',
handler: (args: any[]) => {
return langClient.triggerOpenApiDefChange(args[0], args[1]);
}
}
// {
// methodName: "getOpenApiDef",
// handler: (args: any[]): Thenable<any> => {
// return langClient.getBallerinaOASDef(args[0], args[1]);
// }
// }, {
// methodName: 'triggerOpenApiDefChange',
// handler: (args: any[]) => {
// return langClient.triggerOpenApiDefChange(args[0], args[1]);
// }
// }
];
WebViewRPCHandler.create(oasEditorPanel, langClient, remoteMethods);

Expand Down
56 changes: 2 additions & 54 deletions src/core/extended-language-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*
*/

import { LanguageClient, TextDocumentPositionParams } from "vscode-languageclient";
import { Uri, Location } from "vscode";
import { LanguageClient } from "vscode-languageclient";
import { Uri } from "vscode";

export const BALLERINA_LANG_ID = "ballerina";

Expand Down Expand Up @@ -112,13 +112,6 @@ export interface GetSynRequest {
}

export class ExtendedLangClient extends LanguageClient {
getSyntaxHighlighter(params: string): Thenable<BallerinaSynResponse> {
const req: GetSynRequest = {
Params: params
};
return this.sendRequest("ballerinaSyntaxHighlighter/list", req);
}

getSyntaxTree(uri: Uri): Thenable<BallerinaSyntaxTreeResponse> {
const req: GetSyntaxTreeRequest = {
documentIdentifier: {
Expand All @@ -132,52 +125,7 @@ export class ExtendedLangClient extends LanguageClient {
return this.sendRequest("ballerinaExample/list", args);
}

getEndpoints(): Thenable<Array<any>> {
return this.sendRequest("ballerinaSymbol/endpoints", {})
.then((resp: any) => resp.endpoints);
}

getBallerinaOASDef(uri: Uri, oasService: string): Thenable<BallerinaOASResponse> {
const req: BallerinaOASRequest = {
ballerinaDocument: {
uri: uri.toString()
},
ballerinaService: oasService
};
return this.sendRequest("ballerinaDocument/openApiDefinition", req);
}

triggerOpenApiDefChange(oasJson: string, uri: Uri): void {
const req: BallerinaAstOasChangeRequest = {
oasDefinition: oasJson,
documentIdentifier: {
uri: uri.toString()
},
};
return this.sendNotification("ballerinaDocument/apiDesignDidChange", req);
}

getServiceListForActiveFile(uri: Uri): Thenable<BallerinaServiceListResponse> {
const req: BallerinaServiceListRequest = {
documentIdentifier: {
uri: uri.toString()
},
};
return this.sendRequest("ballerinaDocument/serviceList", req);
}

getBallerinaProject(params: GetBallerinaProjectParams): Thenable<BallerinaProject> {
return this.sendRequest("ballerinaDocument/project", params);
}

getDefinitionPosition(params: TextDocumentPositionParams): Thenable<Location> {
return this.sendRequest("textDocument/definition", params)
.then((res) => {
const definitions = res as any;
if (!(definitions.length > 0)) {
return Promise.reject();
}
return definitions[0];
});
}
}
2 changes: 1 addition & 1 deletion src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BALLERINA_HOME } from "./preferences";
*
*/
export const INVALID_HOME_MSG: string = "Ballerina Home is invalid, please check `" + BALLERINA_HOME + "` in settings";
export const INSTALL_BALLERINA: string = "Unable to auto detect ballerina in your environment. If you just installed Ballerina, you may need to restart VSCode." +
export const INSTALL_BALLERINA: string = "Unable to detect Ballerina in your environment. If you just installed Ballerina, you may need to restart VSCode." +
" If not, please install Ballerina or configure `" + BALLERINA_HOME + "` in settings.";
export const INSTALL_NEW_BALLERINA: string = " version of Ballerina VSCode extension only supports Ballerina v1.0.0-beta or later. If you just installed a new Ballerina version, you may need to restart VSCode. If not, please download and install the latest version or point `" + BALLERINA_HOME + "` in settings to a latest Ballerina distribution.";
export const DOWNLOAD_BALLERINA: string = "https://ballerina.io/downloads/";
Expand Down
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function activate(context: ExtensionContext): Promise<any> {
// Enable Ballerina API Designer
// activateAPIEditor(ballerinaExtInstance);
// Enable Ballerina Project related features
activateProjectFeatures(ballerinaExtInstance);
activateProjectFeatures();
// Enable Ballerina Syntax Highlighter
activateSyntaxHighlighter(ballerinaExtInstance);
// Enable Ballerina Telemetry listener
Expand All @@ -98,6 +98,9 @@ export function activate(context: ExtensionContext): Promise<any> {
});
}).catch((e) => {
log("Failed to activate Ballerina extension. " + (e.message ? e.message : e));
if (e.message && e.message.includes('Error when checking ballerina version.')) {
ballerinaExtInstance.showMessageInstallBallerina();
}
// When plugins fails to start, provide a warning upon each command execution
if (!ballerinaExtInstance.langClient) {
const cmds: any[] = ballerinaExtInstance.extension.packageJSON.contributes.commands;
Expand Down
41 changes: 1 addition & 40 deletions src/project/activator.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
import { BallerinaExtension, ExtendedLangClient, BALLERINA_LANG_ID } from "../core";
import { workspace, window, Uri } from "vscode";
import { activateTestRunner } from "./cli-cmds/test";
import { activateBuildCommand } from "./cli-cmds/build";
import { activateCloudCommand } from "./cli-cmds/cloud";
import { activateRunCommand } from "./cli-cmds/run";
import { activateDocCommand } from "./cli-cmds/doc";
import { activateAddCommand } from "./cli-cmds/add";
import { PROJECT_TYPE } from "./cli-cmds/cmd-runner";

function promptOpenFolder(path: string) {
if (workspace.workspaceFolders) {
const folder = workspace.workspaceFolders.find((folder) => {
return folder.uri.fsPath === path;
});
if (!folder) {
return;
}
}
const action = "Refresh Workspace";
window.showInformationMessage("File resides within a Ballerina project at " +
path, action)
.then((selection) => {
if (selection === action) {
workspace.updateWorkspaceFolders(0, 0, { uri: Uri.file(path) });
}
});
}

export function activate(ballerinaExtInstance: BallerinaExtension) {
let langClient = <ExtendedLangClient>ballerinaExtInstance.langClient;
// when a new file is opened, detect if it resides inside a project
// and notify user
workspace.onDidOpenTextDocument((document) => {
if (document.languageId === BALLERINA_LANG_ID) {
langClient.getBallerinaProject({
documentIdentifier: {
uri: document.uri.toString()
}
}).then((project) => {
if (project.kind !== PROJECT_TYPE.SINGLE_FILE && project.path) {
promptOpenFolder(project.path!);
}
});
}
});

export function activate() {
// activate ballerina test command
activateTestRunner();

Expand Down
Loading

0 comments on commit 05444e2

Please sign in to comment.