Path: blob/main/src/vs/workbench/test/browser/codeeditor.test.ts
4778 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*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { TestInstantiationService } from '../../../platform/instantiation/test/common/instantiationServiceMock.js';7import { URI } from '../../../base/common/uri.js';8import { workbenchInstantiationService, TestEditorService } from './workbenchTestServices.js';9import { IModelService } from '../../../editor/common/services/model.js';10import { ILanguageService } from '../../../editor/common/languages/language.js';11import { LanguageService } from '../../../editor/common/services/languageService.js';12import { RangeHighlightDecorations } from '../../browser/codeeditor.js';13import { TextModel } from '../../../editor/common/model/textModel.js';14import { createTestCodeEditor } from '../../../editor/test/browser/testCodeEditor.js';15import { Range, IRange } from '../../../editor/common/core/range.js';16import { Position } from '../../../editor/common/core/position.js';17import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';18import { TestConfigurationService } from '../../../platform/configuration/test/common/testConfigurationService.js';19import { ModelService } from '../../../editor/common/services/modelService.js';20import { CoreNavigationCommands } from '../../../editor/browser/coreCommands.js';21import { ICodeEditor } from '../../../editor/browser/editorBrowser.js';22import { IEditorService } from '../../services/editor/common/editorService.js';23import { createTextModel } from '../../../editor/test/common/testTextModel.js';24import { IThemeService } from '../../../platform/theme/common/themeService.js';25import { TestThemeService } from '../../../platform/theme/test/common/testThemeService.js';26import { DisposableStore } from '../../../base/common/lifecycle.js';27import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../base/test/common/utils.js';2829suite('Editor - Range decorations', () => {3031let disposables: DisposableStore;32let instantiationService: TestInstantiationService;33let codeEditor: ICodeEditor;34let model: TextModel;35let text: string;36let testObject: RangeHighlightDecorations;37const modelsToDispose: TextModel[] = [];3839setup(() => {40disposables = new DisposableStore();41instantiationService = workbenchInstantiationService(undefined, disposables);42instantiationService.stub(IEditorService, new TestEditorService());43instantiationService.stub(ILanguageService, LanguageService);44instantiationService.stub(IModelService, stubModelService(instantiationService));45text = 'LINE1' + '\n' + 'LINE2' + '\n' + 'LINE3' + '\n' + 'LINE4' + '\r\n' + 'LINE5';46model = disposables.add(aModel(URI.file('some_file')));47codeEditor = disposables.add(createTestCodeEditor(model));4849instantiationService.stub(IEditorService, 'activeEditor', { get resource() { return codeEditor.getModel()!.uri; } });50instantiationService.stub(IEditorService, 'activeTextEditorControl', codeEditor);5152testObject = disposables.add(instantiationService.createInstance(RangeHighlightDecorations));53});5455teardown(() => {56codeEditor.dispose();57modelsToDispose.forEach(model => model.dispose());58disposables.dispose();59});6061ensureNoDisposablesAreLeakedInTestSuite();6263test('highlight range for the resource if it is an active editor', function () {64const range: IRange = new Range(1, 1, 1, 1);65testObject.highlightRange({ resource: model.uri, range });6667const actuals = rangeHighlightDecorations(model);6869assert.deepStrictEqual(actuals, [range]);70});7172test('remove highlight range', function () {73testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });74testObject.removeHighlightRange();7576const actuals = rangeHighlightDecorations(model);7778assert.deepStrictEqual(actuals, []);79});8081test('highlight range for the resource removes previous highlight', function () {82testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });83const range: IRange = new Range(2, 2, 4, 3);84testObject.highlightRange({ resource: model.uri, range });8586const actuals = rangeHighlightDecorations(model);8788assert.deepStrictEqual(actuals, [range]);89});9091test('highlight range for a new resource removes highlight of previous resource', function () {92testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });9394const anotherModel = prepareActiveEditor('anotherModel');95const range: IRange = new Range(2, 2, 4, 3);96testObject.highlightRange({ resource: anotherModel.uri, range });9798let actuals = rangeHighlightDecorations(model);99assert.deepStrictEqual(actuals, []);100actuals = rangeHighlightDecorations(anotherModel);101assert.deepStrictEqual(actuals, [range]);102});103104test('highlight is removed on model change', function () {105testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });106prepareActiveEditor('anotherModel');107108const actuals = rangeHighlightDecorations(model);109assert.deepStrictEqual(actuals, []);110});111112test('highlight is removed on cursor position change', function () {113testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });114codeEditor.trigger('mouse', CoreNavigationCommands.MoveTo.id, {115position: new Position(2, 1)116});117118const actuals = rangeHighlightDecorations(model);119assert.deepStrictEqual(actuals, []);120});121122test('range is not highlight if not active editor', function () {123const model = aModel(URI.file('some model'));124testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });125126const actuals = rangeHighlightDecorations(model);127assert.deepStrictEqual(actuals, []);128});129130test('previous highlight is not removed if not active editor', function () {131const range = new Range(1, 1, 1, 1);132testObject.highlightRange({ resource: model.uri, range });133134const model1 = aModel(URI.file('some model'));135testObject.highlightRange({ resource: model1.uri, range: { startLineNumber: 2, startColumn: 1, endLineNumber: 2, endColumn: 1 } });136137const actuals = rangeHighlightDecorations(model);138assert.deepStrictEqual(actuals, [range]);139});140141function prepareActiveEditor(resource: string): TextModel {142const model = aModel(URI.file(resource));143codeEditor.setModel(model);144return model;145}146147function aModel(resource: URI, content: string = text): TextModel {148const model = createTextModel(content, undefined, undefined, resource);149modelsToDispose.push(model);150return model;151}152153function rangeHighlightDecorations(m: TextModel): IRange[] {154const rangeHighlights: IRange[] = [];155156for (const dec of m.getAllDecorations()) {157if (dec.options.className === 'rangeHighlight') {158rangeHighlights.push(dec.range);159}160}161162rangeHighlights.sort(Range.compareRangesUsingStarts);163return rangeHighlights;164}165166function stubModelService(instantiationService: TestInstantiationService): IModelService {167instantiationService.stub(IConfigurationService, new TestConfigurationService());168instantiationService.stub(IThemeService, new TestThemeService());169return instantiationService.createInstance(ModelService);170}171});172173174