towards code lens lifecycle, still #1698

This commit is contained in:
Johannes Rieken
2016-01-04 18:48:12 +01:00
parent de3e63c058
commit fa1c52de3b
7 changed files with 46 additions and 91 deletions

View File

@@ -23,7 +23,6 @@ import {ExtHostLanguages} from 'vs/workbench/api/common/extHostLanguages';
import {ExtHostLanguageFeatures} from 'vs/workbench/api/common/extHostLanguageFeatures';
import {registerApiCommands} from 'vs/workbench/api/common/extHostApiCommands';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import 'vs/workbench/api/common/extHostTypes.marshalling';
import Modes = require('vs/editor/common/modes');
import {IModeService} from 'vs/editor/common/services/modeService';
import {ICommentsSupportContribution, ITokenTypeClassificationSupportContribution} from 'vs/editor/common/modes/supports';

View File

@@ -175,7 +175,7 @@ class ExtHostApiCommands {
};
return this._commands.executeCommand<modes.IReference[]>('_executeDefinitionProvider', args).then(value => {
if (Array.isArray(value)) {
return value.map(typeConverters.toLocation);
return value.map(typeConverters.location.to);
}
});
}
@@ -211,7 +211,7 @@ class ExtHostApiCommands {
};
return this._commands.executeCommand<modes.IReference[]>('_executeDocumentHighlights', args).then(value => {
if (Array.isArray(value)) {
return value.map(typeConverters.toLocation);
return value.map(typeConverters.location.to);
}
});
}

View File

@@ -10,7 +10,9 @@ import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegi
import {IKeybindingService, ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService';
import {TPromise} from 'vs/base/common/winjs.base';
import {ExtHostEditors} from 'vs/workbench/api/common/extHostEditors';
import {Disposable} from 'vs/workbench/api/common/extHostTypes';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/common/extHostTypeConverters';
import {cloneAndChange} from 'vs/base/common/objects';
interface CommandHandler {
callback: Function;
@@ -30,7 +32,7 @@ export class ExtHostCommands {
this._proxy = threadService.getRemotable(MainThreadCommands);
}
registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): Disposable {
registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
if (!id.trim().length) {
throw new Error('invalid id');
@@ -43,7 +45,7 @@ export class ExtHostCommands {
this._commands[id] = { callback, thisArg, description };
this._proxy.$registerCommand(id);
return new Disposable(() => delete this._commands[id]);
return new extHostTypes.Disposable(() => delete this._commands[id]);
}
executeCommand<T>(id: string, ...args: any[]): Thenable<T> {
@@ -54,13 +56,22 @@ export class ExtHostCommands {
return this.$executeContributedCommand(id, ...args);
} else {
// // check that we can get all parameters over to
// // the other side
// for (let i = 0; i < args.length; i++) {
// if (args[i] !== null && typeof args[i] === 'object' && !canSerialize(args[i])) {
// throw new Error('illegal argument - can not serialize argument number: ' + i)
// }
// }
// automagically convert some argument types
args = cloneAndChange(args, function(value) {
if (value instanceof extHostTypes.Position) {
return extHostTypeConverter.fromPosition(value);
}
if (value instanceof extHostTypes.Range) {
return extHostTypeConverter.fromRange(value);
}
if (value instanceof extHostTypes.Location) {
return extHostTypeConverter.location.from(value);
}
if (!Array.isArray(value)) {
return value;
}
});
return this._proxy.$executeCommand(id, args);
}
@@ -70,7 +81,7 @@ export class ExtHostCommands {
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
let command = this._commands[id];
if (!command) {
return Promise.reject<T>(id);
return Promise.reject<T>(`Contributed command '${id}' does not exist.`);
}
try {
let {callback, thisArg, description} = command;

View File

@@ -78,8 +78,7 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
private _commands: ExtHostCommands;
private _provider: vscode.CodeLensProvider;
private _cachedLenses: { [uri: string]: vscode.CodeLens[] } = Object.create(null);
private _cachedCommands: IDisposable[] = [];
private _cache: { [uri: string]: [vscode.CodeLens[], IDisposable[]] } = Object.create(null);
constructor(documents: ExtHostModelService, commands: ExtHostCommands, provider: vscode.CodeLensProvider) {
this._documents = documents;
@@ -91,21 +90,25 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
let doc = this._documents.getDocument(resource);
let key = resource.toString();
this._cachedCommands = disposeAll(this._cachedCommands);
delete this._cachedLenses[key];
let entry = this._cache[key];
if (entry) {
// disposeAll(entry[1]);
delete this._cache[key];
}
return asWinJsPromise(token => this._provider.provideCodeLenses(doc, token)).then(value => {
if (!Array.isArray(value)) {
return;
}
this._cachedLenses[key] = value;
entry = [value, []];
this._cache[key] = entry;
return value.map((lens, i) => {
return <modes.ICodeLensSymbol>{
id: String(i),
range: TypeConverters.fromRange(lens.range),
command: TypeConverters.Command.from(lens.command, { commands: this._commands, disposables: this._cachedCommands })
command: TypeConverters.Command.from(lens.command, { commands: this._commands, disposables: entry[1] })
};
});
});
@@ -113,7 +116,7 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
resolveCodeLensSymbol(resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
let lenses = this._cachedLenses[resource.toString()];
let [lenses, disposables] = this._cache[resource.toString()];
if (!lenses) {
return;
}
@@ -140,7 +143,7 @@ class CodeLensAdapter implements modes.ICodeLensSupport {
};
}
symbol.command = TypeConverters.Command.from(command, { commands: this._commands, disposables: this._cachedCommands });
symbol.command = TypeConverters.Command.from(command, { commands: this._commands, disposables });
return symbol;
});
}

View File

@@ -298,11 +298,18 @@ export function toSymbolInformation(bearing: ITypeBearing): types.SymbolInformat
}
export function toLocation(reference: modes.IReference): types.Location {
return new types.Location(reference.resource, toRange(reference.range));
export const location = {
from(value: types.Location): modes.IReference {
return {
range: fromRange(value.range),
resource: value.uri
}
},
to(value: modes.IReference): types.Location {
return new types.Location(value.resource, toRange(value.range));
}
}
export function fromHover(hover: vscode.Hover): modes.IComputeExtraInfoResult {
return <modes.IComputeExtraInfoResult>{
range: fromRange(hover.range),

View File

@@ -1,65 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {registerMarshallingContribution, IMarshallingContribution} from 'vs/base/common/marshalling';
import * as types from './extHostTypes';
import {fromPosition, fromRange} from './extHostTypeConverters';
import {IReference} from 'vs/editor/common/modes';
abstract class OneWayMarshalling<T> implements IMarshallingContribution {
canDeserialize() {
return false;
}
deserialize() {
throw Error();
}
abstract canSerialize(obj: any): boolean;
abstract serialize(obj: T, serialize: (obj: any) => any): any;
}
class RangeMarshalling extends OneWayMarshalling<types.Range> {
canSerialize(obj: any): boolean {
return obj instanceof types.Range;
}
serialize(obj: types.Range, serialize: (obj: any) => any): any {
return fromRange(obj);
}
}
class PositionMarshalling extends OneWayMarshalling<types.Position> {
canSerialize(obj: any): boolean {
return obj instanceof types.Position;
}
serialize(obj: types.Position, serialize: (obj: any) => any): any {
return fromPosition(obj);
}
}
class LocationMarshalling extends OneWayMarshalling<types.Location> {
canSerialize(obj: any): boolean {
return obj instanceof types.Location;
}
serialize(obj: types.Location, serialize: (obj: any) => any): any {
return <IReference>{
resource: serialize(obj.uri),
range: serialize(obj.range)
};
}
}
registerMarshallingContribution(new RangeMarshalling());
registerMarshallingContribution(new PositionMarshalling());
registerMarshallingContribution(new LocationMarshalling());