Skip to content

Commit 0590230

Browse files
committedJun 8, 2017
Add CodeLens support for function and cmdlet references
This change adds support for CodeLenses that display the number of references of a given function or cmdlet defined in a script file. The actual implementation of this behavior is on the server side but we had to write a LanguageClient middleware to translate the arguments of the references command when it was sent from the server. Resolves #507 Resolves #827 (unrelated to CodeLenses but related to LanguageClient output)
1 parent fcf9be5 commit 0590230

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"onCommand:PowerShell.ShowSessionConsole"
3636
],
3737
"dependencies": {
38-
"vscode-languageclient": "^3.2.2"
38+
"vscode-languageclient": "3.3.0-alpha.6"
3939
},
4040
"devDependencies": {
4141
"@types/node": "^6.0.40",

‎src/session.ts

+56-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import { Logger } from './logging';
1515
import { IFeature } from './feature';
1616
import { Message } from 'vscode-jsonrpc';
1717
import { StringDecoder } from 'string_decoder';
18-
import { LanguageClient, LanguageClientOptions, Executable, RequestType, RequestType0, NotificationType, StreamInfo, ErrorAction, CloseAction } from 'vscode-languageclient';
18+
import {
19+
LanguageClient, LanguageClientOptions, Executable,
20+
RequestType, RequestType0, NotificationType,
21+
StreamInfo, ErrorAction, CloseAction, RevealOutputChannelOn,
22+
Middleware, ResolveCodeLensSignature } from 'vscode-languageclient';
1923

2024
export enum SessionStatus {
2125
NotStarted,
@@ -58,7 +62,7 @@ type SessionConfiguration =
5862
PathSessionConfiguration |
5963
BuiltInSessionConfiguration;
6064

61-
export class SessionManager {
65+
export class SessionManager implements Middleware {
6266

6367
private ShowSessionMenuCommandName = "PowerShell.ShowSessionMenu";
6468

@@ -471,7 +475,9 @@ export class SessionManager {
471475
// We have our own restart experience
472476
return CloseAction.DoNotRestart
473477
}
474-
}
478+
},
479+
revealOutputChannelOn: RevealOutputChannelOn.Never,
480+
middleware: this
475481
}
476482

477483
this.languageServerClient =
@@ -793,6 +799,53 @@ export class SessionManager {
793799
.showQuickPick<SessionMenuItem>(menuItems)
794800
.then((selectedItem) => { selectedItem.callback(); });
795801
}
802+
803+
// ----- LanguageClient middleware methods -----
804+
805+
resolveCodeLens(
806+
codeLens: vscode.CodeLens,
807+
token: vscode.CancellationToken,
808+
next: ResolveCodeLensSignature): vscode.ProviderResult<vscode.CodeLens> {
809+
var resolvedCodeLens = next(codeLens, token);
810+
811+
let resolveFunc =
812+
(codeLens: vscode.CodeLens): vscode.CodeLens => {
813+
if (codeLens.command.command === "editor.action.showReferences") {
814+
var oldArgs = codeLens.command.arguments;
815+
816+
// Our JSON objects don't get handled correctly by
817+
// VS Code's built in editor.action.showReferences
818+
// command so we need to convert them into the
819+
// appropriate types to send them as command
820+
// arguments.
821+
822+
codeLens.command.arguments = [
823+
vscode.Uri.parse(oldArgs[0]),
824+
new vscode.Position(oldArgs[1].line, oldArgs[1].character),
825+
oldArgs[2].map(position => {
826+
return new vscode.Location(
827+
vscode.Uri.parse(position.uri),
828+
new vscode.Range(
829+
position.range.start.line,
830+
position.range.start.character,
831+
position.range.end.line,
832+
position.range.end.character));
833+
})
834+
]
835+
}
836+
837+
return codeLens;
838+
}
839+
840+
if ((<Thenable<vscode.CodeLens>>resolvedCodeLens).then) {
841+
return (<Thenable<vscode.CodeLens>>resolvedCodeLens).then(resolveFunc);
842+
}
843+
else if (<vscode.CodeLens>resolvedCodeLens) {
844+
return resolveFunc(<vscode.CodeLens>resolvedCodeLens);
845+
}
846+
847+
return resolvedCodeLens;
848+
}
796849
}
797850

798851
class SessionMenuItem implements vscode.QuickPickItem {

0 commit comments

Comments
 (0)
Please sign in to comment.