Skip to content

Commit a7f719e

Browse files
committed
command: executeCodeLensProvider
1 parent 4dad67b commit a7f719e

7 files changed

Lines changed: 78 additions & 33 deletions

File tree

src/vs/editor/common/modes.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,16 +808,15 @@ export interface ICommand {
808808
export interface ICodeLensSymbol {
809809
range: EditorCommon.IRange;
810810
id?: string;
811-
kind?: string;
812-
name?: string;
811+
command?: ICommand;
813812
}
814813

815814
/**
816815
* Interface used for the code lense support
817816
*/
818817
export interface ICodeLensSupport {
819818
findCodeLensSymbols(resource: URI): TPromise<ICodeLensSymbol[]>;
820-
resolveCodeLensSymbol(resource: URI, symbol: ICodeLensSymbol): TPromise<ICommand>;
819+
resolveCodeLensSymbol(resource: URI, symbol: ICodeLensSymbol): TPromise<ICodeLensSymbol>;
821820
}
822821

823822
export interface ITaskSummary {

src/vs/editor/contrib/codelens/browser/codelens.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ class CodeLensContentWidget implements EditorBrowser.IContentWidget {
103103
}
104104
}
105105

106-
public withCommands(commands: Modes.ICommand[]): void {
106+
public withCommands(symbols: Modes.ICodeLensSymbol[]): void {
107107
this._commands = Object.create(null);
108-
if (!commands || !commands.length) {
108+
if (!symbols || !symbols.length) {
109109
this._domNode.innerHTML = 'no commands';
110110
return;
111111
}
112112

113113
let html: string[] = [];
114-
for (let i = 0; i < commands.length; i++) {
115-
let command = commands[i];
114+
for (let i = 0; i < symbols.length; i++) {
115+
let command = symbols[i].command;
116116
let part: string;
117117
if (command.id) {
118118
part = format('<a id={0}>{1}</a>', i, command.title);
@@ -303,8 +303,8 @@ class CodeLens {
303303
return this._data;
304304
}
305305

306-
public updateCommands(commands: Modes.ICommand[], currentModelsVersionId: number): void {
307-
this._contentWidget.withCommands(commands);
306+
public updateCommands(symbols: Modes.ICodeLensSymbol[], currentModelsVersionId: number): void {
307+
this._contentWidget.withCommands(symbols);
308308
this._lastUpdateModelsVersionId = currentModelsVersionId;
309309
}
310310

@@ -616,17 +616,15 @@ export class CodeLensContribution implements EditorCommon.IEditorContribution {
616616
var resource = model.getAssociatedResource();
617617
var promises = toResolve.map((request, i) => {
618618

619-
let commands = new Array<Modes.ICommand>(request.length);
619+
let resolvedSymbols = new Array<Modes.ICodeLensSymbol>(request.length);
620620
let promises = request.map((request, i) => {
621-
return request.support.resolveCodeLensSymbol(resource, request.symbol).then(command => {
622-
if (command) {
623-
commands[i] = command;
624-
}
621+
return request.support.resolveCodeLensSymbol(resource, request.symbol).then(symbol => {
622+
resolvedSymbols[i] = symbol;
625623
});
626624
});
627625

628626
return TPromise.join(promises).then(() => {
629-
lenses[i].updateCommands(commands, currentModelsVersionId);
627+
lenses[i].updateCommands(resolvedSymbols, currentModelsVersionId);
630628
})
631629
});
632630

src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,13 @@ export class ExtHostLanguageFeatureCommands {
220220
const args = {
221221
resource
222222
};
223-
return this._commands._executeContributedCommand<ICodeLensData[]>('_executeCodeLensProvider', args).then(value => {
223+
224+
return this._commands.executeCommand<ICodeLensData[]>('_executeCodeLensProvider', args).then(value => {
224225
if (Array.isArray(value)) {
225-
return value.map(item => new types.CodeLens(typeConverters.toRange(item.symbol.range)));
226+
return value.map(item => {
227+
return new types.CodeLens(typeConverters.toRange(item.symbol.range),
228+
typeConverters.Command.to(item.symbol.command));
229+
});
226230
}
227231
});
228232
}

src/vs/workbench/api/common/extHostLanguageFeatures.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
106106
return value.map((lens, i) => {
107107
return <modes.ICodeLensSymbol>{
108108
id: String(i),
109-
range: TypeConverters.fromRange(lens.range)
109+
range: TypeConverters.fromRange(lens.range),
110+
command: TypeConverters.Command.from(lens.command)
110111
}
111112
});
112113
});
113114
}
114115

115-
resolveCodeLensSymbol(resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICommand> {
116+
resolveCodeLensSymbol(resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
116117

117118
let lenses = this._cache[resource.toString()];
118119
if (!lenses) {
@@ -140,11 +141,9 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
140141
command: 'missing',
141142
}
142143
}
143-
return {
144-
id: command.command,
145-
title: command.title,
146-
arguments: command.arguments
147-
}
144+
145+
symbol.command = TypeConverters.Command.from(command);
146+
return symbol;
148147
});
149148
}
150149
}
@@ -687,7 +686,7 @@ export class ExtHostLanguageFeatures {
687686
return this._withAdapter(handle, CodeLensAdapter, adapter => adapter.findCodeLensSymbols(resource));
688687
}
689688

690-
$resolveCodeLensSymbol(handle:number, resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICommand> {
689+
$resolveCodeLensSymbol(handle:number, resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
691690
return this._withAdapter(handle, CodeLensAdapter, adapter => adapter.resolveCodeLensSymbol(resource, symbol));
692691
}
693692

@@ -891,7 +890,7 @@ export class MainThreadLanguageFeatures {
891890
findCodeLensSymbols: (resource: URI): TPromise<modes.ICodeLensSymbol[]> => {
892891
return this._proxy.$findCodeLensSymbols(handle, resource);
893892
},
894-
resolveCodeLensSymbol: (resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICommand> => {
893+
resolveCodeLensSymbol: (resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> => {
895894
return this._proxy.$resolveCodeLensSymbol(handle, resource, symbol);
896895
}
897896
});

src/vs/workbench/api/common/pluginHostTypeConverters.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import * as types from './pluginHostTypes';
1111
import {Position as EditorPosition} from 'vs/platform/editor/common/editor';
1212
import {IPosition, ISelection, IRange, IRangeWithMessage, ISingleEditOperation} from 'vs/editor/common/editorCommon';
1313
import {IHTMLContentElement} from 'vs/base/common/htmlContent';
14-
import {ITypeBearing} from 'vs/workbench/parts/search/common/search'
14+
import {ITypeBearing} from 'vs/workbench/parts/search/common/search';
15+
import * as vscode from 'vscode';
1516

1617
export interface PositionLike {
1718
line: number;
@@ -407,4 +408,26 @@ export namespace SignatureHelp {
407408

408409
return result;
409410
}
411+
}
412+
413+
export const Command = {
414+
from(command: vscode.Command): modes.ICommand {
415+
if (command) {
416+
return <modes.ICommand>{
417+
id: command.command,
418+
title: command.title,
419+
arguments: command.arguments
420+
};
421+
}
422+
},
423+
424+
to(command: modes.ICommand): vscode.Command {
425+
if (command) {
426+
return <vscode.Command>{
427+
command: command.id,
428+
title: command.title,
429+
arguments: command.arguments
430+
};
431+
}
432+
}
410433
}

src/vs/workbench/test/common/api/extHostLanguageFeatureCommands.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,26 @@ suite('ExtHostLanguageFeatureCommands', function() {
276276
});
277277
});
278278
});
279+
280+
// --- code lens
281+
282+
test('CodeLens, back and forth', function(done) {
283+
disposables.push(extHost.registerCodeLensProvider(defaultSelector, <vscode.CodeLensProvider>{
284+
provideCodeLenses(): any {
285+
return [new types.CodeLens(new types.Range(0, 0, 1, 1), { title: 'Title', command: 'cmd', arguments: [1, 2, true] })];
286+
}
287+
}));
288+
289+
threadService.sync().then(() => {
290+
commands.executeCommand<vscode.CodeLens[]>('vscode.executeCodeLensProvider', model.getAssociatedResource()).then(value => {
291+
assert.equal(value.length, 1);
292+
let [first] = value;
293+
294+
assert.equal(first.command.title, 'Title');
295+
assert.equal(first.command.command, 'cmd');
296+
assert.deepEqual(first.command.arguments, [1, 2, true]);
297+
done();
298+
});
299+
});
300+
});
279301
});

src/vs/workbench/test/common/api/extHostLanguageFeatures.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ suite('ExtHostLanguageFeatures', function() {
205205
assert.equal(value.length, 1);
206206
let data = value[0];
207207

208-
data.support.resolveCodeLensSymbol(model.getAssociatedResource(), data.symbol).then(command => {
209-
assert.equal(command.id, 'id');
210-
assert.equal(command.title, 'Title');
208+
data.support.resolveCodeLensSymbol(model.getAssociatedResource(), data.symbol).then(symbol => {
209+
assert.equal(symbol.command.id, 'id');
210+
assert.equal(symbol.command.title, 'Title');
211211
done();
212212
});
213213
});
@@ -228,10 +228,10 @@ suite('ExtHostLanguageFeatures', function() {
228228
assert.equal(value.length, 1);
229229

230230
let data = value[0];
231-
data.support.resolveCodeLensSymbol(model.getAssociatedResource(), data.symbol).then(command => {
231+
data.support.resolveCodeLensSymbol(model.getAssociatedResource(), data.symbol).then(symbol => {
232232

233-
assert.equal(command.id, 'missing');
234-
assert.equal(command.title, '<<MISSING COMMAND>>');
233+
assert.equal(symbol.command.id, 'missing');
234+
assert.equal(symbol.command.title, '<<MISSING COMMAND>>');
235235
done();
236236
});
237237
});

0 commit comments

Comments
 (0)