提交 32465012 编写于 作者: A Alex Dima

Add fast path for vscode.TextEditor.setDecorations

上级 d4408e2b
......@@ -30,6 +30,7 @@ import { ICursorPositionChangedEvent, ICursorSelectionChangedEvent } from 'vs/ed
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { CommonEditorRegistry } from 'vs/editor/common/editorCommonExtensions';
import { VerticalRevealType } from 'vs/editor/common/view/viewEvents';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModelWithDecorations';
let EDITOR_ID = 0;
......@@ -856,6 +857,26 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo
this._decorationTypeKeysToIds[decorationTypeKey] = this.deltaDecorations(oldDecorationsIds, newModelDecorations);
}
public setDecorationsFast(decorationTypeKey: string, ranges: IRange[]): void {
// remove decoration sub types that are no longer used, deregister decoration type if necessary
let oldDecorationsSubTypes = this._decorationTypeSubtypes[decorationTypeKey] || {};
for (let subType in oldDecorationsSubTypes) {
this._removeDecorationType(decorationTypeKey + '-' + subType);
}
this._decorationTypeSubtypes[decorationTypeKey] = {};
const opts = ModelDecorationOptions.createDynamic(this._resolveDecorationOptions(decorationTypeKey, false));
let newModelDecorations: editorCommon.IModelDeltaDecoration[] = new Array<editorCommon.IModelDeltaDecoration>(ranges.length);
for (let i = 0, len = ranges.length; i < len; i++) {
newModelDecorations[i] = { range: ranges[i], options: opts };
}
// update all decorations
let oldDecorationsIds = this._decorationTypeKeysToIds[decorationTypeKey] || [];
this._decorationTypeKeysToIds[decorationTypeKey] = this.deltaDecorations(oldDecorationsIds, newModelDecorations);
}
public removeDecorations(decorationTypeKey: string): void {
// remove decorations for type and sub type
let oldDecorationsIds = this._decorationTypeKeysToIds[decorationTypeKey];
......
......@@ -1984,6 +1984,11 @@ export interface ICommonCodeEditor extends IEditor {
*/
setDecorations(decorationTypeKey: string, ranges: IDecorationOptions[]): void;
/**
* @internal
*/
setDecorationsFast(decorationTypeKey: string, ranges: IRange[]): void;
/**
* @internal
*/
......
......@@ -244,6 +244,17 @@ export class MainThreadTextEditor {
this._codeEditor.setDecorations(key, ranges);
}
public setDecorationsFast(key: string, _ranges: number[]): void {
if (!this._codeEditor) {
return;
}
let ranges: Range[] = [];
for (let i = 0, len = Math.floor(_ranges.length / 4); i < len; i++) {
ranges[i] = new Range(_ranges[4 * i], _ranges[4 * i + 1], _ranges[4 * i + 2], _ranges[4 * i + 3]);
}
this._codeEditor.setDecorationsFast(key, ranges);
}
public revealRange(range: IRange, revealType: TextEditorRevealType): void {
if (!this._codeEditor) {
return;
......
......@@ -195,6 +195,14 @@ export class MainThreadEditors implements MainThreadEditorsShape {
return TPromise.as(null);
}
$trySetDecorationsFast(id: string, key: string, ranges: string): TPromise<any> {
if (!this._documentsAndEditors.getEditor(id)) {
return TPromise.wrapError(disposed(`TextEditor(${id})`));
}
this._documentsAndEditors.getEditor(id).setDecorationsFast(key, /*TODO: marshaller is too slow*/JSON.parse(ranges));
return TPromise.as(null);
}
$tryRevealRange(id: string, range: IRange, revealType: TextEditorRevealType): TPromise<any> {
if (!this._documentsAndEditors.getEditor(id)) {
return TPromise.wrapError(disposed(`TextEditor(${id})`));
......
......@@ -210,6 +210,7 @@ export interface MainThreadEditorsShape extends IDisposable {
$tryHideEditor(id: string): TPromise<void>;
$trySetOptions(id: string, options: ITextEditorConfigurationUpdate): TPromise<any>;
$trySetDecorations(id: string, key: string, ranges: editorCommon.IDecorationOptions[]): TPromise<any>;
$trySetDecorationsFast(id: string, key: string, ranges: string): TPromise<any>;
$tryRevealRange(id: string, range: IRange, revealType: TextEditorRevealType): TPromise<any>;
$trySetSelections(id: string, selections: ISelection[]): TPromise<any>;
$tryApplyEdits(id: string, modelVersionId: number, edits: editorCommon.ISingleEditOperation[], opts: IApplyEditsOptions): TPromise<boolean>;
......
......@@ -416,11 +416,29 @@ export class ExtHostTextEditor implements vscode.TextEditor {
setDecorations(decorationType: vscode.TextEditorDecorationType, ranges: Range[] | vscode.DecorationOptions[]): void {
this._runOnProxy(
() => this._proxy.$trySetDecorations(
this._id,
decorationType.key,
TypeConverters.fromRangeOrRangeWithMessage(ranges)
)
() => {
if (TypeConverters.isDecorationOptionsArr(ranges)) {
return this._proxy.$trySetDecorations(
this._id,
decorationType.key,
TypeConverters.fromRangeOrRangeWithMessage(ranges)
);
} else {
let _ranges: number[] = new Array<number>(4 * ranges.length);
for (let i = 0, len = ranges.length; i < len; i++) {
const range = ranges[i];
_ranges[4 * i] = range.start.line + 1;
_ranges[4 * i + 1] = range.start.character + 1;
_ranges[4 * i + 2] = range.end.line + 1;
_ranges[4 * i + 3] = range.end.character + 1;
}
return this._proxy.$trySetDecorationsFast(
this._id,
decorationType.key,
/*TODO: marshaller is too slow*/JSON.stringify(_ranges)
);
}
}
);
}
......
......@@ -139,7 +139,7 @@ function isDecorationOptions(something: any): something is vscode.DecorationOpti
return (typeof something.range !== 'undefined');
}
function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
export function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
if (something.length === 0) {
return true;
}
......
......@@ -61,6 +61,7 @@ suite('ExtHostTextEditorOptions', () => {
$tryShowEditor: undefined,
$tryHideEditor: undefined,
$trySetDecorations: undefined,
$trySetDecorationsFast: undefined,
$tryRevealRange: undefined,
$trySetSelections: undefined,
$tryApplyEdits: undefined,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册