Skip to content

Commit

Permalink
Add root path configuration (#54)
Browse files Browse the repository at this point in the history
Allow users to customize the root path to be used by the Smithy VSCode extension. A new test was added to test this behavior. Some cleanup was also done.
  • Loading branch information
eduardomourar authored Sep 22, 2022
1 parent 48c5f16 commit d11e728
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ repositories where they can be located.
{
{
"maven": {
"dependencies": ["software.amazon.smithy:smithy-aws-traits:1.23.1"],
"dependencies": ["software.amazon.smithy:smithy-aws-traits:1.25.0"],
"repositories": [{ "url": "https://repo1.maven.org/maven2/" }]
}
}
Expand Down
70 changes: 35 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
"type": "string",
"default": "0.2.0",
"description": "Version of the Smithy LSP (see https://github.com/smithy/smithy-language-server)"
},
"smithyLsp.rootPath": {
"scope": "resource",
"type": "string"
}
}
}
Expand Down Expand Up @@ -142,7 +146,7 @@
},
"dependencies": {
"follow-redirects": "^1.14.9",
"vscode-languageclient": "^7.0.0",
"vscode-languageclient": "^8.0.2",
"vscode-nls": "^5.2.0",
"vscode-tmgrammar-test": "^0.1.1"
}
Expand Down
82 changes: 60 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as net from "net";
import * as fs from "fs";
import * as child_process from "child_process";
import { workspace, ExtensionContext } from "vscode";
import * as vscode from "vscode";
import { SelectorDecorator } from "./selector/selector-decorator";
import { selectorRunCommandHandler, selectorClearCommandHandler } from "./selector/selector-command-handlers";
Expand All @@ -19,7 +18,7 @@ import { getCoursierExecutable } from "./coursier/coursier";

let client: LanguageClient;

export function activate(context: ExtensionContext) {
export function activate(context: vscode.ExtensionContext) {
async function createServer(): Promise<StreamInfo> {
function startServer(executable: string): Promise<StreamInfo> {
console.log(`Executable located at ${executable}.`);
Expand Down Expand Up @@ -108,32 +107,14 @@ export function activate(context: ExtensionContext) {
return await startServer(binaryPath);
}

// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
documentSelector: [
{ scheme: "file", language: "smithy" },
{ scheme: "smithyjar", language: "smithy" },
],
synchronize: {
// Notify the server about file changes to 'smithy-build.json' files contained in the workspace
fileEvents: workspace.createFileSystemWatcher("**/{smithy-build}.json"),
},
outputChannelName: "Smithy Language Server",

// Don't switch to output window when the server returns output.
revealOutputChannelOn: RevealOutputChannelOn.Never,
};

// Create the language client and start the client.

client = new LanguageClient("smithyLsp", "Smithy LSP", createServer, clientOptions);
client = new LanguageClient("smithyLsp", "Smithy LSP", createServer, getClientOptions());

// Set client on `this` context to use with command handlers.
this.client = client;

const smithyContentProvider = createSmithyContentProvider(client);
context.subscriptions.push(workspace.registerTextDocumentContentProvider("smithyjar", smithyContentProvider));
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("smithyjar", smithyContentProvider));

// Set default expression input, and use context to hold state between command invocations.
this.expression = "Enter Selector Expression";
Expand All @@ -149,13 +130,70 @@ export function activate(context: ExtensionContext) {
client.start();
}

function getClientOptions(): LanguageClientOptions {
let workspaceFolder: vscode.WorkspaceFolder;

let rootPath: string = vscode.workspace.getConfiguration("smithyLsp").get("rootPath");

if (rootPath) {
const workspaceRoot = getWorkspaceRoot();
if (rootPath.startsWith("${workspaceRoot}") && workspaceRoot === "") {
console.warn(`Unable to retrieve the workspace root.`);
}
workspaceFolder = {
uri: vscode.Uri.file(rootPath.replace("${workspaceRoot}", workspaceRoot)),
name: "smithy-lsp-root-path",
index: 1,
};
}

// Configure file patterns relative to the workspace folder.
let filePattern: vscode.GlobPattern = "**/{smithy-build}.json";
let selectorPattern: string = null;
if (workspaceFolder) {
filePattern = new vscode.RelativePattern(workspaceFolder, filePattern);
selectorPattern = `${workspaceFolder.uri.fsPath}/**/*`;
}

// Options to control the language client
return {
// Register the server for plain text documents
documentSelector: [
{ scheme: "file", language: "smithy", pattern: selectorPattern },
{ scheme: "smithyjar", language: "smithy", pattern: selectorPattern },
],
synchronize: {
// Notify the server about file changes to 'smithy-build.json' files contained in the workspace
fileEvents: vscode.workspace.createFileSystemWatcher(filePattern),
},
outputChannelName: "Smithy Language Server",

workspaceFolder,

// Don't switch to output window when the server returns output.
revealOutputChannelOn: RevealOutputChannelOn.Never,
};
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}

function getWorkspaceRoot(): string | undefined {
let folders = vscode.workspace.workspaceFolders;
if (!folders || folders.length === 0) {
return "";
}
let folder = folders[0];
if (folder.uri.scheme === "file") {
return folder.uri.fsPath;
}
return "";
}

function createSmithyContentProvider(languageClient: LanguageClient): vscode.TextDocumentContentProvider {
return <vscode.TextDocumentContentProvider>{
provideTextDocumentContent: async (uri: vscode.Uri, token: CancellationToken): Promise<string> => {
Expand Down
7 changes: 7 additions & 0 deletions test-fixtures/suite1/main.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ $version: "2.0"

namespace example.weather

use aws.api#dataPlane
use aws.api#service

/// Provides weather forecasts.
@service(
sdkId: "Weather",
)
service Weather {
version: "2006-03-01"
operations: [GetCurrentTime]
}

@readonly
@dataPlane
operation GetCurrentTime {
input := {}
output := {
Expand Down
3 changes: 3 additions & 0 deletions test-fixtures/suite4/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"smithyLsp.rootPath": "${workspaceRoot}/smithy"
}
59 changes: 59 additions & 0 deletions test-fixtures/suite4/smithy/main.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$version: "2.0"

namespace example.weather

use aws.api#dataPlane
use smithy.waiters#waitable

/// Provides weather forecasts.
service Weather {
version: "2006-03-01"
operations: [GetCurrentTime, GetWeatherReport]
}

@readonly
@dataPlane
operation GetCurrentTime {
input := {}
output := {
@required
time: Timestamp
}
}

@waitable(
ReportGenerated: {
documentation: "Wait until the weather report is generated"
acceptors: [
{
state: "success"
matcher: {
success: true
}
}
{
state: "retry"
matcher: {
errorType: "NotFound"
}
}
]
}
)
operation GetWeatherReport {
input := {
@required
cityId: String
}
output := {
@required
temperature: String
}
errors: [NotFound]
}

@error("client")
structure NotFound {
@required
message: String
}
6 changes: 6 additions & 0 deletions test-fixtures/suite4/smithy/smithy-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"maven": {
"dependencies": ["software.amazon.smithy:smithy-aws-traits:1.25.0", "software.amazon.smithy:smithy-waiters:1.25.0"],
"repositories": [{ "url": "https://repo1.maven.org/maven2/" }]
}
}
7 changes: 7 additions & 0 deletions tests/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ async function go() {
launchArgs: [resolve(__dirname, "../../test-fixtures/suite3")],
});

// Suite 4 - User-specific root
await runTests({
extensionDevelopmentPath,
extensionTestsPath: resolve(__dirname, "./suite4"),
launchArgs: [resolve(__dirname, "../../test-fixtures/suite4")],
});

// Confirm that webpacked and vsce packaged extension can be installed.
const vscodeExecutablePath = await downloadAndUnzipVSCode();
const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
Expand Down
Loading

0 comments on commit d11e728

Please sign in to comment.