Skip to content

Commit

Permalink
Adds an isReadonly field to TextDocument
Browse files Browse the repository at this point in the history
For microsoft#91697

This new field tracks if the file is on a readonly file system or not. This can be helpful for text based custom editors
  • Loading branch information
mjbvz committed Aug 27, 2020
1 parent 1c4f6ac commit d9dcad2
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2119,4 +2119,19 @@ declare module 'vscode' {
}): Disposable;
}
//#endregion

//#region https://github.com/microsoft/vscode/issues/91697

export interface TextDocument {

/**
* True if the document is on a readonly file system.
*
* `isReadonly === false` does not guarantee that can write the file. There still may be permissions
* issues or other problems that prevent the file from actually being written.
*/
readonly isReadonly: boolean;
}

//#endregion
}
15 changes: 13 additions & 2 deletions src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IEditor } from 'vs/editor/common/editorCommon';
import { ITextModel } from 'vs/editor/common/model';
import { IModelService, shouldSynchronizeModel } from 'vs/editor/common/services/modelService';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IFileService } from 'vs/platform/files/common/files';
import { FileSystemProviderCapabilities, IFileService } from 'vs/platform/files/common/files';
import { extHostCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { MainThreadDocuments } from 'vs/workbench/api/browser/mainThreadDocuments';
import { MainThreadTextEditor } from 'vs/workbench/api/browser/mainThreadEditor';
Expand All @@ -30,6 +30,7 @@ import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/
import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { Schemas } from 'vs/base/common/network';

namespace delta {

Expand Down Expand Up @@ -334,6 +335,7 @@ export class MainThreadDocumentsAndEditors {
@IWorkingCopyFileService workingCopyFileService: IWorkingCopyFileService,
@IUriIdentityService uriIdentityService: IUriIdentityService,
@IClipboardService private readonly _clipboardService: IClipboardService,
@IFileService private readonly _fileService: IFileService,
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostDocumentsAndEditors);

Expand Down Expand Up @@ -425,10 +427,19 @@ export class MainThreadDocumentsAndEditors {
lines: model.getLinesContent(),
EOL: model.getEOL(),
modeId: model.getLanguageIdentifier().language,
isDirty: this._textFileService.isDirty(model.uri)
isDirty: this._textFileService.isDirty(model.uri),
isReadonly: this.isReadonly(model),
};
}

private isReadonly(model: ITextModel): boolean {
if (model.uri.scheme === Schemas.untitled) {
// untitled is never readonly
return false;
}
return this._fileService.hasCapability(model.uri, FileSystemProviderCapabilities.Readonly);
}

private _toTextEditorAddData(textEditor: MainThreadTextEditor): ITextEditorAddData {
const props = textEditor.getProperties();
return {
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ export interface IModelAddedData {
EOL: string;
modeId: string;
isDirty: boolean;
isReadonly: boolean;
}
export interface ExtHostDocumentsShape {
$acceptModelModeChanged(strURL: UriComponents, oldModeId: string, newModeId: string): void;
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/api/common/extHostDocumentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class ExtHostDocumentData extends MirrorTextModel {
uri: URI, lines: string[], eol: string, versionId: number,
private _languageId: string,
private _isDirty: boolean,
private readonly _isReadonly: boolean,
private readonly _notebook?: vscode.NotebookDocument | undefined
) {
super(uri, lines, eol, versionId);
Expand Down Expand Up @@ -66,6 +67,7 @@ export class ExtHostDocumentData extends MirrorTextModel {
get version() { return that._versionId; },
get isClosed() { return that._isDisposed; },
get isDirty() { return that._isDirty; },
get isReadonly() { return that._isReadonly; },
get notebook() { return that._notebook; },
save() { return that._save(); },
getText(range?) { return range ? that._getTextInRange(range) : that.getText(); },
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHostDocumentsAndEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsSha
data.versionId,
data.modeId,
data.isDirty,
data.isReadonly,
data.notebook
));
this._documents.set(resource, ref);
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHostNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class ExtHostCell extends Disposable {
modeId: cell.language,
uri: cell.uri,
isDirty: false,
isReadonly: false,
versionId: 1,
notebook
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ suite('ExtHostLanguageFeatureCommands', function () {
extHostDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
isReadonly: false,
versionId: model.getVersionId(),
modeId: model.getLanguageIdentifier().language,
uri: model.uri,
Expand Down
14 changes: 7 additions & 7 deletions src/vs/workbench/test/browser/api/extHostDocumentData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ suite('ExtHostDocumentData', () => {
'and this is line number two', //27
'it is followed by #3', //20
'and finished with the fourth.', //29
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);
});

test('readonly-ness', () => {
Expand All @@ -55,7 +55,7 @@ suite('ExtHostDocumentData', () => {
saved = uri;
return Promise.resolve(true);
}
}, URI.parse('foo:bar'), [], '\n', 1, 'text', true);
}, URI.parse('foo:bar'), [], '\n', 1, 'text', true, false);

return data.document.save().then(() => {
assert.equal(saved.toString(), 'foo:bar');
Expand Down Expand Up @@ -242,7 +242,7 @@ suite('ExtHostDocumentData', () => {
test('getWordRangeAtPosition', () => {
data = new ExtHostDocumentData(undefined!, URI.file(''), [
'aaaa bbbb+cccc abc'
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);

let range = data.document.getWordRangeAtPosition(new Position(0, 2))!;
assert.equal(range.start.line, 0);
Expand Down Expand Up @@ -276,7 +276,7 @@ suite('ExtHostDocumentData', () => {
'function() {',
' "far boo"',
'}'
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);

let range = data.document.getWordRangeAtPosition(new Position(0, 0), /\/\*.+\*\//);
assert.equal(range, undefined);
Expand Down Expand Up @@ -304,7 +304,7 @@ suite('ExtHostDocumentData', () => {

data = new ExtHostDocumentData(undefined!, URI.file(''), [
perfData._$_$_expensive
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);

let range = data.document.getWordRangeAtPosition(new Position(0, 1_177_170), regex)!;
assert.equal(range, undefined);
Expand All @@ -323,7 +323,7 @@ suite('ExtHostDocumentData', () => {

data = new ExtHostDocumentData(undefined!, URI.file(''), [
line
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);

let range = data.document.getWordRangeAtPosition(new Position(0, 27), regex)!;
assert.equal(range.start.line, 0);
Expand Down Expand Up @@ -387,7 +387,7 @@ suite('ExtHostDocumentData updates line mapping', () => {
}

function testLineMappingDirectionAfterEvents(lines: string[], eol: string, direction: AssertDocumentLineMappingDirection, e: IModelChangedEvent): void {
let myDocument = new ExtHostDocumentData(undefined!, URI.file(''), lines.slice(0), eol, 1, 'text', false);
let myDocument = new ExtHostDocumentData(undefined!, URI.file(''), lines.slice(0), eol, 1, 'text', false, false);
assertDocumentLineMapping(myDocument, direction);

myDocument.onEvents(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ suite('ExtHostDocumentSaveParticipant', () => {
documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
isReadonly: false,
modeId: 'foo',
uri: resource,
versionId: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ suite('ExtHostLanguageFeatures', function () {
extHostDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
isReadonly: false,
versionId: model.getVersionId(),
modeId: model.getLanguageIdentifier().language,
uri: model.uri,
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/test/browser/api/extHostNotebook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ suite('NotebookCell#Document', function () {
addData.push({
EOL: '\n',
isDirty: doc.isDirty,
isReadonly: false,
lines: doc.getText().split('\n'),
modeId: doc.languageId,
uri: doc.uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite('ExtHostTextEditor', () => {
let editor: ExtHostTextEditor;
let doc = new ExtHostDocumentData(undefined!, URI.file(''), [
'aaaa bbbb+cccc abc'
], '\n', 1, 'text', false);
], '\n', 1, 'text', false, false);

setup(() => {
editor = new ExtHostTextEditor('fake', null!, new NullLogService(), doc, [], { cursorStyle: 0, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4 }, [], 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite('ExtHostTextEditors.applyWorkspaceEdit', () => {
documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
isReadonly: false,
modeId: 'foo',
uri: resource,
versionId: 1337,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ suite('MainThreadDocumentsAndEditors', () => {
readText() {
return Promise.resolve('clipboard_contents');
}
},
new class extends mock<IFileService>() {

}
);
});
Expand Down

0 comments on commit d9dcad2

Please sign in to comment.