Path: blob/main/src/vs/workbench/api/test/browser/extHostTextEditor.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { Lazy } from '../../../../base/common/lazy.js';6import { URI } from '../../../../base/common/uri.js';7import { mock } from '../../../../base/test/common/mock.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';9import { RenderLineNumbersType, TextEditorCursorStyle } from '../../../../editor/common/config/editorOptions.js';10import { NullLogService } from '../../../../platform/log/common/log.js';11import { IResolvedTextEditorConfiguration, ITextEditorConfigurationUpdate, MainThreadTextEditorsShape } from '../../common/extHost.protocol.js';12import { ExtHostDocumentData } from '../../common/extHostDocumentData.js';13import { ExtHostTextEditor, ExtHostTextEditorOptions } from '../../common/extHostTextEditor.js';14import { Range, TextEditorLineNumbersStyle } from '../../common/extHostTypes.js';1516suite('ExtHostTextEditor', () => {1718let editor: ExtHostTextEditor;19const doc = new ExtHostDocumentData(undefined!, URI.file(''), [20'aaaa bbbb+cccc abc'21], '\n', 1, 'text', false, 'utf8');2223setup(() => {24editor = new ExtHostTextEditor('fake', null!, new NullLogService(), new Lazy(() => doc.document), [], { cursorStyle: TextEditorCursorStyle.Line, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4, originalIndentSize: 'tabSize' }, [], 1);25});2627test('disposed editor', () => {2829assert.ok(editor.value.document);30editor._acceptViewColumn(3);31assert.strictEqual(3, editor.value.viewColumn);3233editor.dispose();3435assert.throws(() => editor._acceptViewColumn(2));36assert.strictEqual(3, editor.value.viewColumn);3738assert.ok(editor.value.document);39assert.throws(() => editor._acceptOptions(null!));40assert.throws(() => editor._acceptSelections([]));41});4243test('API [bug]: registerTextEditorCommand clears redo stack even if no edits are made #55163', async function () {44let applyCount = 0;45const editor = new ExtHostTextEditor('edt1',46new class extends mock<MainThreadTextEditorsShape>() {47override $tryApplyEdits(): Promise<boolean> {48applyCount += 1;49return Promise.resolve(true);50}51}, new NullLogService(), new Lazy(() => doc.document), [], { cursorStyle: TextEditorCursorStyle.Line, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4, originalIndentSize: 'tabSize' }, [], 1);5253await editor.value.edit(edit => { });54assert.strictEqual(applyCount, 0);5556await editor.value.edit(edit => { edit.setEndOfLine(1); });57assert.strictEqual(applyCount, 1);5859await editor.value.edit(edit => { edit.delete(new Range(0, 0, 1, 1)); });60assert.strictEqual(applyCount, 2);61});6263ensureNoDisposablesAreLeakedInTestSuite();64});6566suite('ExtHostTextEditorOptions', () => {6768let opts: ExtHostTextEditorOptions;69let calls: ITextEditorConfigurationUpdate[] = [];7071setup(() => {72calls = [];73const mockProxy: MainThreadTextEditorsShape = {74dispose: undefined!,75$trySetOptions: (id: string, options: ITextEditorConfigurationUpdate) => {76assert.strictEqual(id, '1');77calls.push(options);78return Promise.resolve(undefined);79},80$tryShowTextDocument: undefined!,81$registerTextEditorDecorationType: undefined!,82$removeTextEditorDecorationType: undefined!,83$tryShowEditor: undefined!,84$tryHideEditor: undefined!,85$trySetDecorations: undefined!,86$trySetDecorationsFast: undefined!,87$tryRevealRange: undefined!,88$trySetSelections: undefined!,89$tryApplyEdits: undefined!,90$tryInsertSnippet: undefined!,91$getDiffInformation: undefined!92};93opts = new ExtHostTextEditorOptions(mockProxy, '1', {94tabSize: 4,95indentSize: 4,96originalIndentSize: 'tabSize',97insertSpaces: false,98cursorStyle: TextEditorCursorStyle.Line,99lineNumbers: RenderLineNumbersType.On100}, new NullLogService());101});102103teardown(() => {104opts = null!;105calls = null!;106});107108function assertState(opts: ExtHostTextEditorOptions, expected: Omit<IResolvedTextEditorConfiguration, 'originalIndentSize'>): void {109const actual = {110tabSize: opts.value.tabSize,111indentSize: opts.value.indentSize,112insertSpaces: opts.value.insertSpaces,113cursorStyle: opts.value.cursorStyle,114lineNumbers: opts.value.lineNumbers115};116assert.deepStrictEqual(actual, expected);117}118119test('can set tabSize to the same value', () => {120opts.value.tabSize = 4;121assertState(opts, {122tabSize: 4,123indentSize: 4,124insertSpaces: false,125cursorStyle: TextEditorCursorStyle.Line,126lineNumbers: RenderLineNumbersType.On127});128assert.deepStrictEqual(calls, []);129});130131test('can change tabSize to positive integer', () => {132opts.value.tabSize = 1;133assertState(opts, {134tabSize: 1,135indentSize: 4,136insertSpaces: false,137cursorStyle: TextEditorCursorStyle.Line,138lineNumbers: RenderLineNumbersType.On139});140assert.deepStrictEqual(calls, [{ tabSize: 1 }]);141});142143test('can change tabSize to positive float', () => {144opts.value.tabSize = 2.3;145assertState(opts, {146tabSize: 2,147indentSize: 4,148insertSpaces: false,149cursorStyle: TextEditorCursorStyle.Line,150lineNumbers: RenderLineNumbersType.On151});152assert.deepStrictEqual(calls, [{ tabSize: 2 }]);153});154155test('can change tabSize to a string number', () => {156opts.value.tabSize = '2';157assertState(opts, {158tabSize: 2,159indentSize: 4,160insertSpaces: false,161cursorStyle: TextEditorCursorStyle.Line,162lineNumbers: RenderLineNumbersType.On163});164assert.deepStrictEqual(calls, [{ tabSize: 2 }]);165});166167test('tabSize can request indentation detection', () => {168opts.value.tabSize = 'auto';169assertState(opts, {170tabSize: 4,171indentSize: 4,172insertSpaces: false,173cursorStyle: TextEditorCursorStyle.Line,174lineNumbers: RenderLineNumbersType.On175});176assert.deepStrictEqual(calls, [{ tabSize: 'auto' }]);177});178179test('ignores invalid tabSize 1', () => {180opts.value.tabSize = null!;181assertState(opts, {182tabSize: 4,183indentSize: 4,184insertSpaces: false,185cursorStyle: TextEditorCursorStyle.Line,186lineNumbers: RenderLineNumbersType.On187});188assert.deepStrictEqual(calls, []);189});190191test('ignores invalid tabSize 2', () => {192opts.value.tabSize = -5;193assertState(opts, {194tabSize: 4,195indentSize: 4,196insertSpaces: false,197cursorStyle: TextEditorCursorStyle.Line,198lineNumbers: RenderLineNumbersType.On199});200assert.deepStrictEqual(calls, []);201});202203test('ignores invalid tabSize 3', () => {204opts.value.tabSize = 'hello';205assertState(opts, {206tabSize: 4,207indentSize: 4,208insertSpaces: false,209cursorStyle: TextEditorCursorStyle.Line,210lineNumbers: RenderLineNumbersType.On211});212assert.deepStrictEqual(calls, []);213});214215test('ignores invalid tabSize 4', () => {216opts.value.tabSize = '-17';217assertState(opts, {218tabSize: 4,219indentSize: 4,220insertSpaces: false,221cursorStyle: TextEditorCursorStyle.Line,222lineNumbers: RenderLineNumbersType.On223});224assert.deepStrictEqual(calls, []);225});226227test('can set indentSize to the same value', () => {228opts.value.indentSize = 4;229assertState(opts, {230tabSize: 4,231indentSize: 4,232insertSpaces: false,233cursorStyle: TextEditorCursorStyle.Line,234lineNumbers: RenderLineNumbersType.On235});236assert.deepStrictEqual(calls, [{ indentSize: 4 }]);237});238239test('can change indentSize to positive integer', () => {240opts.value.indentSize = 1;241assertState(opts, {242tabSize: 4,243indentSize: 1,244insertSpaces: false,245cursorStyle: TextEditorCursorStyle.Line,246lineNumbers: RenderLineNumbersType.On247});248assert.deepStrictEqual(calls, [{ indentSize: 1 }]);249});250251test('can change indentSize to positive float', () => {252opts.value.indentSize = 2.3;253assertState(opts, {254tabSize: 4,255indentSize: 2,256insertSpaces: false,257cursorStyle: TextEditorCursorStyle.Line,258lineNumbers: RenderLineNumbersType.On259});260assert.deepStrictEqual(calls, [{ indentSize: 2 }]);261});262263test('can change indentSize to a string number', () => {264opts.value.indentSize = <any>'2';265assertState(opts, {266tabSize: 4,267indentSize: 2,268insertSpaces: false,269cursorStyle: TextEditorCursorStyle.Line,270lineNumbers: RenderLineNumbersType.On271});272assert.deepStrictEqual(calls, [{ indentSize: 2 }]);273});274275test('indentSize can request to use tabSize', () => {276opts.value.indentSize = 'tabSize';277assertState(opts, {278tabSize: 4,279indentSize: 4,280insertSpaces: false,281cursorStyle: TextEditorCursorStyle.Line,282lineNumbers: RenderLineNumbersType.On283});284assert.deepStrictEqual(calls, [{ indentSize: 'tabSize' }]);285});286287test('indentSize cannot request indentation detection', () => {288opts.value.indentSize = <any>'auto';289assertState(opts, {290tabSize: 4,291indentSize: 4,292insertSpaces: false,293cursorStyle: TextEditorCursorStyle.Line,294lineNumbers: RenderLineNumbersType.On295});296assert.deepStrictEqual(calls, []);297});298299test('ignores invalid indentSize 1', () => {300opts.value.indentSize = null!;301assertState(opts, {302tabSize: 4,303indentSize: 4,304insertSpaces: false,305cursorStyle: TextEditorCursorStyle.Line,306lineNumbers: RenderLineNumbersType.On307});308assert.deepStrictEqual(calls, []);309});310311test('ignores invalid indentSize 2', () => {312opts.value.indentSize = -5;313assertState(opts, {314tabSize: 4,315indentSize: 4,316insertSpaces: false,317cursorStyle: TextEditorCursorStyle.Line,318lineNumbers: RenderLineNumbersType.On319});320assert.deepStrictEqual(calls, []);321});322323test('ignores invalid indentSize 3', () => {324opts.value.indentSize = <any>'hello';325assertState(opts, {326tabSize: 4,327indentSize: 4,328insertSpaces: false,329cursorStyle: TextEditorCursorStyle.Line,330lineNumbers: RenderLineNumbersType.On331});332assert.deepStrictEqual(calls, []);333});334335test('ignores invalid indentSize 4', () => {336opts.value.indentSize = <any>'-17';337assertState(opts, {338tabSize: 4,339indentSize: 4,340insertSpaces: false,341cursorStyle: TextEditorCursorStyle.Line,342lineNumbers: RenderLineNumbersType.On343});344assert.deepStrictEqual(calls, []);345});346347test('can set insertSpaces to the same value', () => {348opts.value.insertSpaces = false;349assertState(opts, {350tabSize: 4,351indentSize: 4,352insertSpaces: false,353cursorStyle: TextEditorCursorStyle.Line,354lineNumbers: RenderLineNumbersType.On355});356assert.deepStrictEqual(calls, []);357});358359test('can set insertSpaces to boolean', () => {360opts.value.insertSpaces = true;361assertState(opts, {362tabSize: 4,363indentSize: 4,364insertSpaces: true,365cursorStyle: TextEditorCursorStyle.Line,366lineNumbers: RenderLineNumbersType.On367});368assert.deepStrictEqual(calls, [{ insertSpaces: true }]);369});370371test('can set insertSpaces to false string', () => {372opts.value.insertSpaces = 'false';373assertState(opts, {374tabSize: 4,375indentSize: 4,376insertSpaces: false,377cursorStyle: TextEditorCursorStyle.Line,378lineNumbers: RenderLineNumbersType.On379});380assert.deepStrictEqual(calls, []);381});382383test('can set insertSpaces to truey', () => {384opts.value.insertSpaces = 'hello';385assertState(opts, {386tabSize: 4,387indentSize: 4,388insertSpaces: true,389cursorStyle: TextEditorCursorStyle.Line,390lineNumbers: RenderLineNumbersType.On391});392assert.deepStrictEqual(calls, [{ insertSpaces: true }]);393});394395test('insertSpaces can request indentation detection', () => {396opts.value.insertSpaces = 'auto';397assertState(opts, {398tabSize: 4,399indentSize: 4,400insertSpaces: false,401cursorStyle: TextEditorCursorStyle.Line,402lineNumbers: RenderLineNumbersType.On403});404assert.deepStrictEqual(calls, [{ insertSpaces: 'auto' }]);405});406407test('can set cursorStyle to same value', () => {408opts.value.cursorStyle = TextEditorCursorStyle.Line;409assertState(opts, {410tabSize: 4,411indentSize: 4,412insertSpaces: false,413cursorStyle: TextEditorCursorStyle.Line,414lineNumbers: RenderLineNumbersType.On415});416assert.deepStrictEqual(calls, []);417});418419test('can change cursorStyle', () => {420opts.value.cursorStyle = TextEditorCursorStyle.Block;421assertState(opts, {422tabSize: 4,423indentSize: 4,424insertSpaces: false,425cursorStyle: TextEditorCursorStyle.Block,426lineNumbers: RenderLineNumbersType.On427});428assert.deepStrictEqual(calls, [{ cursorStyle: TextEditorCursorStyle.Block }]);429});430431test('can set lineNumbers to same value', () => {432opts.value.lineNumbers = TextEditorLineNumbersStyle.On;433assertState(opts, {434tabSize: 4,435indentSize: 4,436insertSpaces: false,437cursorStyle: TextEditorCursorStyle.Line,438lineNumbers: RenderLineNumbersType.On439});440assert.deepStrictEqual(calls, []);441});442443test('can change lineNumbers', () => {444opts.value.lineNumbers = TextEditorLineNumbersStyle.Off;445assertState(opts, {446tabSize: 4,447indentSize: 4,448insertSpaces: false,449cursorStyle: TextEditorCursorStyle.Line,450lineNumbers: RenderLineNumbersType.Off451});452assert.deepStrictEqual(calls, [{ lineNumbers: RenderLineNumbersType.Off }]);453});454455test('can do bulk updates 0', () => {456opts.assign({457tabSize: 4,458indentSize: 4,459insertSpaces: false,460cursorStyle: TextEditorCursorStyle.Line,461lineNumbers: TextEditorLineNumbersStyle.On462});463assertState(opts, {464tabSize: 4,465indentSize: 4,466insertSpaces: false,467cursorStyle: TextEditorCursorStyle.Line,468lineNumbers: RenderLineNumbersType.On469});470assert.deepStrictEqual(calls, [{ indentSize: 4 }]);471});472473test('can do bulk updates 1', () => {474opts.assign({475tabSize: 'auto',476insertSpaces: true477});478assertState(opts, {479tabSize: 4,480indentSize: 4,481insertSpaces: true,482cursorStyle: TextEditorCursorStyle.Line,483lineNumbers: RenderLineNumbersType.On484});485assert.deepStrictEqual(calls, [{ tabSize: 'auto', insertSpaces: true }]);486});487488test('can do bulk updates 2', () => {489opts.assign({490tabSize: 3,491insertSpaces: 'auto'492});493assertState(opts, {494tabSize: 3,495indentSize: 4,496insertSpaces: false,497cursorStyle: TextEditorCursorStyle.Line,498lineNumbers: RenderLineNumbersType.On499});500assert.deepStrictEqual(calls, [{ tabSize: 3, insertSpaces: 'auto' }]);501});502503test('can do bulk updates 3', () => {504opts.assign({505cursorStyle: TextEditorCursorStyle.Block,506lineNumbers: TextEditorLineNumbersStyle.Relative507});508assertState(opts, {509tabSize: 4,510indentSize: 4,511insertSpaces: false,512cursorStyle: TextEditorCursorStyle.Block,513lineNumbers: RenderLineNumbersType.Relative514});515assert.deepStrictEqual(calls, [{ cursorStyle: TextEditorCursorStyle.Block, lineNumbers: RenderLineNumbersType.Relative }]);516});517518ensureNoDisposablesAreLeakedInTestSuite();519});520521522