Skip to content

Commit

Permalink
adding TextDocument#isVisible and internals for a visbility change ev…
Browse files Browse the repository at this point in the history
…ent, #15723
  • Loading branch information
jrieken committed Mar 23, 2017
1 parent c01a616 commit a6a41fd
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 9 deletions.
12 changes: 12 additions & 0 deletions extensions/vscode-api-tests/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ suite('workspace-namespace', () => {
});
});

test('openTextDocument, visible/invisible', function () {
return createRandomFile().then(file => {
return workspace.openTextDocument(file);
}).then(doc => {
assert.equal(doc.isVisible, false);
return window.showTextDocument(doc).then(editor => {
assert.ok(editor.document === doc);
assert.equal(doc.isVisible, true);
});
});
});

test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', () => {
return createRandomFile().then(file => {
let disposables: Disposable[] = [];
Expand Down
7 changes: 6 additions & 1 deletion src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ declare module 'vscode' {
readonly version: number;

/**
* true if there are unpersisted changes.
* `true` if this document is showing in one or more [editors](#TextEditor).
*/
readonly isVisible: boolean;

/**
* `true` if there are unpersisted changes.
*/
readonly isDirty: boolean;

Expand Down
13 changes: 13 additions & 0 deletions src/vs/workbench/api/node/extHostDocumentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class ExtHostDocumentData extends MirrorModel2 {
private _proxy: MainThreadDocumentsShape;
private _languageId: string;
private _isDirty: boolean;
private _isVisible: boolean = false;
private _document: vscode.TextDocument;
private _textLines: vscode.TextLine[] = [];
private _isDisposed: boolean = false;
Expand All @@ -48,6 +49,7 @@ export class ExtHostDocumentData extends MirrorModel2 {
ok(!this._isDisposed);
this._isDisposed = true;
this._isDirty = false;
this._isVisible = false;
}

equalLines({ lines }: ITextSource): boolean {
Expand All @@ -72,6 +74,7 @@ export class ExtHostDocumentData extends MirrorModel2 {
get isUntitled() { return data._uri.scheme !== 'file'; },
get languageId() { return data._languageId; },
get version() { return data._versionId; },
get isVisible() { return data._isVisible; },
get isDirty() { return data._isDirty; },
save() { return data._save(); },
getText(range?) { return range ? data._getTextInRange(range) : data.getText(); },
Expand All @@ -97,6 +100,16 @@ export class ExtHostDocumentData extends MirrorModel2 {
this._isDirty = isDirty;
}

_acceptIsVisible(value: boolean): boolean {
ok(!this._isDisposed);
if (this._isVisible === value) {
return false;
} else {
this._isVisible = value;
return true;
}
}

private _save(): TPromise<boolean> {
if (this._isDisposed) {
return TPromise.wrapError<boolean>('Document has been closed');
Expand Down
34 changes: 26 additions & 8 deletions src/vs/workbench/api/node/extHostDocumentsAndEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export class ExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditorsShape
private readonly _onDidRemoveDocuments = new Emitter<ExtHostDocumentData[]>();
private readonly _onDidChangeVisibleTextEditors = new Emitter<ExtHostTextEditor[]>();
private readonly _onDidChangeActiveTextEditor = new Emitter<ExtHostTextEditor>();
private readonly _onDidChangeVisibleDocuments = new Emitter<ExtHostDocumentData[]>();

readonly onDidAddDocuments: Event<ExtHostDocumentData[]> = this._onDidAddDocuments.event;
readonly onDidRemoveDocuments: Event<ExtHostDocumentData[]> = this._onDidRemoveDocuments.event;
readonly onDidChangeVisibleTextEditors: Event<ExtHostTextEditor[]> = this._onDidChangeVisibleTextEditors.event;
readonly onDidChangeActiveTextEditor: Event<ExtHostTextEditor> = this._onDidChangeActiveTextEditor.event;
readonly onDidChangeVisibleTextDocuments: Event<ExtHostDocumentData[]> = this._onDidChangeVisibleDocuments.event;

constructor(
@IThreadService private _threadService: IThreadService
Expand All @@ -40,6 +42,21 @@ export class ExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditorsShape
const removedDocuments: ExtHostDocumentData[] = [];
const addedDocuments: ExtHostDocumentData[] = [];
const removedEditors: ExtHostTextEditor[] = [];
const visibilityChangedDocuments = new Set<ExtHostDocumentData>();

if (delta.removedEditors) {
for (const id of delta.removedEditors) {
const editor = this._editors.get(id);
this._editors.delete(id);
removedEditors.push(editor);

const uri = editor.document.uri.toString();
const data = this._documents.get(uri);
if (data._acceptIsVisible(false)) {
visibilityChangedDocuments.add(data);
}
}
}

if (delta.removedDocuments) {
for (const id of delta.removedDocuments) {
Expand Down Expand Up @@ -67,14 +84,6 @@ export class ExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditorsShape
}
}

if (delta.removedEditors) {
for (const id of delta.removedEditors) {
const editor = this._editors.get(id);
this._editors.delete(id);
removedEditors.push(editor);
}
}

if (delta.addedEditors) {
for (const data of delta.addedEditors) {
assert.ok(this._documents.has(data.document.toString()), `document '${data.document}' does not exist`);
Expand All @@ -90,6 +99,9 @@ export class ExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditorsShape
typeConverters.toViewColumn(data.editorPosition)
);
this._editors.set(data.id, editor);
if (documentData._acceptIsVisible(true)) {
visibilityChangedDocuments.add(documentData);
}
}
}

Expand All @@ -113,6 +125,12 @@ export class ExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditorsShape
this._onDidChangeActiveTextEditor.fire(this.activeEditor());
}

if (visibilityChangedDocuments.size > 0) {
const documents: ExtHostDocumentData[] = [];
visibilityChangedDocuments.forEach(data => documents.push(data));
this._onDidChangeVisibleDocuments.fire(documents);
}

// now that the events are out, dispose removed documents and editors
dispose(removedDocuments);
dispose(removedEditors);
Expand Down
145 changes: 145 additions & 0 deletions src/vs/workbench/test/node/api/extHostDocumentsAndEditors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*---------------------------------------------------------------------------------------------
* 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 * as assert from 'assert';
import URI from 'vs/base/common/uri';
import { ExtHostDocumentData } from 'vs/workbench/api/node/extHostDocumentData';
import { ITextEditorAddData } from 'vs/workbench/api/node/extHost.protocol';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/extHostDocumentsAndEditors';
import { OneGetThreadService } from './testThreadService';
import { Model } from 'vs/editor/common/model/model';

suite('ExtHostDocumentsAndEditors', () => {

function modelAddData(model: Model) {
return {
isDirty: false,
versionId: model.getVersionId(),
modeId: model.getLanguageIdentifier().language,
url: model.uri,
lines: model.getValue().split(model.getEOL()),
EOL: model.getEOL(),
};
}

function editorAddData(model: Model, id = model.id): ITextEditorAddData {
return {
id,
document: model.uri,
options: { tabSize: 4, insertSpaces: true, cursorStyle: 0, lineNumbers: 0 },
selections: [],
editorPosition: 1
};
}

let documentsAndEditors: ExtHostDocumentsAndEditors;

setup(() => {
documentsAndEditors = new ExtHostDocumentsAndEditors(OneGetThreadService(null));
});

test('onDidChangeVisibleTextDocuments, new document => no event', () => {

const model = Model.createFromString('foo', undefined, undefined, URI.parse('foo:bar'));

let docs: ExtHostDocumentData[] = [];
let sub = documentsAndEditors.onDidChangeVisibleTextDocuments(e => docs.push(null));

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [modelAddData(model)]
});

assert.equal(docs.length, 0);
sub.dispose();
});

test('onDidChangeVisibleTextDocuments, new editor => event', () => {

const model = Model.createFromString('foo', undefined, undefined, URI.parse('foo:bar'));

let docs: ExtHostDocumentData[] = [];
let sub = documentsAndEditors.onDidChangeVisibleTextDocuments(e => docs.push(...e));

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [modelAddData(model)]
});

assert.equal(docs.length, 0);

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedEditors: [editorAddData(model)]
});

assert.equal(docs.length, 1);
assert.equal(docs[0].document.uri.toString(), model.uri.toString());

sub.dispose();
});

test('onDidChangeVisibleTextDocuments, removed editor => event', () => {

const model = Model.createFromString('foo', undefined, undefined, URI.parse('foo:bar'));

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [modelAddData(model)],
addedEditors: [editorAddData(model)]
});

const all = documentsAndEditors.allDocuments();
assert.equal(1, all.length);
assert.equal(all[0].document.isVisible, true);

let docs: ExtHostDocumentData[] = [];
let sub = documentsAndEditors.onDidChangeVisibleTextDocuments(e => docs.push(...e));

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
removedEditors: [model.id]
});

assert.equal(docs.length, 1);
assert.equal(docs[0].document.uri.toString(), model.uri.toString());
assert.equal(docs[0].document.isVisible, false);

sub.dispose();
});


test('onDidChangeVisibleTextDocuments, removed/added editor => event', () => {

const model1 = Model.createFromString('foo', undefined, undefined, URI.parse('foo:bar'));
const model2 = Model.createFromString('foo', undefined, undefined, URI.parse('foo:bar2'));

documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [modelAddData(model1)],
addedEditors: [editorAddData(model1, 'left')]
});

const all = documentsAndEditors.allDocuments();
assert.equal(1, all.length);
assert.equal(all[0].document.isVisible, true);

let docs: ExtHostDocumentData[] = [];
let sub = documentsAndEditors.onDidChangeVisibleTextDocuments(e => docs.push(...e));


// left gets a new editor and right get the document from left
documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [modelAddData(model2)],
addedEditors: [editorAddData(model2, 'newLeft'), editorAddData(model1, 'right')],
removedEditors: ['left']
});

assert.equal(docs.length, 2);

let [left, right] = docs;
assert.equal(left.document.isVisible, true);
assert.equal(right.document.isVisible, true);

sub.dispose();
});

});

0 comments on commit a6a41fd

Please sign in to comment.