Skip to content

Add CodeLens support for function/cmdlet references and Pester tests #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"onCommand:PowerShell.ShowSessionConsole"
],
"dependencies": {
"vscode-languageclient": "^3.2.2"
"vscode-languageclient": "3.3.0-alpha.6"
},
"devDependencies": {
"@types/node": "^6.0.40",
Expand Down
66 changes: 66 additions & 0 deletions src/features/PesterTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import vscode = require('vscode');
import utils = require('../utils');
import Window = vscode.window;
import ChildProcess = require('child_process');
import { SessionManager } from '../session';
import { IFeature, LanguageClient } from '../feature';

export class PesterTestsFeature implements IFeature {

private command: vscode.Disposable;
private languageClient: LanguageClient;

constructor(private sessionManager: SessionManager) {
this.command = vscode.commands.registerCommand(
'PowerShell.RunPesterTests',
(uriString, runInDebugger, describeBlockName?) => {
this.launchTests(uriString, runInDebugger, describeBlockName);
});
}

public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
}

public dispose() {
this.command.dispose();
}

private launchTests(uriString, runInDebugger, describeBlockName?) {
var uri = vscode.Uri.parse(uriString);
let currentDocument = vscode.window.activeTextEditor.document;

let launchConfig = {
request: "launch",
type: "PowerShell",
script: "Invoke-Pester",
args: [
`-Script "${uri.fsPath}"`,
describeBlockName
? `-TestName "${describeBlockName}"`
: ""
],
internalConsoleOptions: "neverOpen",
noDebug: !runInDebugger,
cwd:
currentDocument.isUntitled
? vscode.workspace.rootPath
: currentDocument.fileName
}

// Create or show the interactive console
// TODO #367: Check if "newSession" mode is configured
vscode.commands.executeCommand('PowerShell.ShowSessionConsole', true);

// Write out temporary debug session file
utils.writeSessionFile(
utils.getDebugSessionFilePath(),
this.sessionManager.getSessionDetails());

vscode.commands.executeCommand('vscode.startDebug', launchConfig);
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ExpandAliasFeature } from './features/ExpandAlias';
import { ShowHelpFeature } from './features/ShowOnlineHelp';
import { CodeActionsFeature } from './features/CodeActions';
import { RemoteFilesFeature } from './features/RemoteFiles';
import { PesterTestsFeature } from './features/PesterTests';
import { DebugSessionFeature } from './features/DebugSession';
import { PickPSHostProcessFeature } from './features/DebugSession';
import { SpecifyScriptArgsFeature } from './features/DebugSession';
Expand Down Expand Up @@ -110,6 +111,7 @@ export function activate(context: vscode.ExtensionContext): void {
new ExpandAliasFeature(),
new ShowHelpFeature(),
new FindModuleFeature(),
new PesterTestsFeature(sessionManager),
new ExtensionCommandsFeature(),
new SelectPSSARulesFeature(),
new CodeActionsFeature(),
Expand Down
59 changes: 56 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { Logger } from './logging';
import { IFeature } from './feature';
import { Message } from 'vscode-jsonrpc';
import { StringDecoder } from 'string_decoder';
import { LanguageClient, LanguageClientOptions, Executable, RequestType, RequestType0, NotificationType, StreamInfo, ErrorAction, CloseAction } from 'vscode-languageclient';
import {
LanguageClient, LanguageClientOptions, Executable,
RequestType, RequestType0, NotificationType,
StreamInfo, ErrorAction, CloseAction, RevealOutputChannelOn,
Middleware, ResolveCodeLensSignature } from 'vscode-languageclient';

export enum SessionStatus {
NotStarted,
Expand Down Expand Up @@ -58,7 +62,7 @@ type SessionConfiguration =
PathSessionConfiguration |
BuiltInSessionConfiguration;

export class SessionManager {
export class SessionManager implements Middleware {

private ShowSessionMenuCommandName = "PowerShell.ShowSessionMenu";

Expand Down Expand Up @@ -471,7 +475,9 @@ export class SessionManager {
// We have our own restart experience
return CloseAction.DoNotRestart
}
}
},
revealOutputChannelOn: RevealOutputChannelOn.Never,
middleware: this
}

this.languageServerClient =
Expand Down Expand Up @@ -793,6 +799,53 @@ export class SessionManager {
.showQuickPick<SessionMenuItem>(menuItems)
.then((selectedItem) => { selectedItem.callback(); });
}

// ----- LanguageClient middleware methods -----

resolveCodeLens(
codeLens: vscode.CodeLens,
token: vscode.CancellationToken,
next: ResolveCodeLensSignature): vscode.ProviderResult<vscode.CodeLens> {
var resolvedCodeLens = next(codeLens, token);

let resolveFunc =
(codeLens: vscode.CodeLens): vscode.CodeLens => {
if (codeLens.command.command === "editor.action.showReferences") {
var oldArgs = codeLens.command.arguments;

// Our JSON objects don't get handled correctly by
// VS Code's built in editor.action.showReferences
// command so we need to convert them into the
// appropriate types to send them as command
// arguments.

codeLens.command.arguments = [
vscode.Uri.parse(oldArgs[0]),
new vscode.Position(oldArgs[1].line, oldArgs[1].character),
oldArgs[2].map(position => {
return new vscode.Location(
vscode.Uri.parse(position.uri),
new vscode.Range(
position.range.start.line,
position.range.start.character,
position.range.end.line,
position.range.end.character));
})
]
}

return codeLens;
}

if ((<Thenable<vscode.CodeLens>>resolvedCodeLens).then) {
return (<Thenable<vscode.CodeLens>>resolvedCodeLens).then(resolveFunc);
}
else if (<vscode.CodeLens>resolvedCodeLens) {
return resolveFunc(<vscode.CodeLens>resolvedCodeLens);
}

return resolvedCodeLens;
}
}

class SessionMenuItem implements vscode.QuickPickItem {
Expand Down