/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as assert from 'assert'; import { WorkspaceEdit, TextDocument, getLanguageModes, ClientCapabilities } from '../modes/languageModes'; import { getNodeFSRequestService } from '../node/nodeFs'; async function testRename(value: string, newName: string, expectedDocContent: string): Promise { const offset = value.indexOf('|'); value = value.substr(0, offset) + value.substr(offset + 1); const document = TextDocument.create('test://test/test.html', 'html', 0, value); const workspace = { settings: {}, folders: [{ name: 'foo', uri: 'test://foo' }] }; const languageModes = getLanguageModes({ css: true, javascript: true }, workspace, ClientCapabilities.LATEST, getNodeFSRequestService()); const javascriptMode = languageModes.getMode('javascript') const position = document.positionAt(offset); if (javascriptMode) { const workspaceEdit: WorkspaceEdit | null = await javascriptMode.doRename!(document, position, newName); if (!workspaceEdit || !workspaceEdit.changes) { assert.fail('No workspace edits'); } const edits = workspaceEdit.changes[document.uri.toString()]; if (!edits) { assert.fail(`No edits for file at ${document.uri.toString()}`); } const newDocContent = TextDocument.applyEdits(document, edits); assert.equal(newDocContent, expectedDocContent, `Expected: ${expectedDocContent}\nActual: ${newDocContent}`); } else { assert.fail('should have javascriptMode but no') } } async function testNoRename(value: string, newName: string): Promise { const offset = value.indexOf('|'); value = value.substr(0, offset) + value.substr(offset + 1); const document = TextDocument.create('test://test/test.html', 'html', 0, value); const workspace = { settings: {}, folders: [{ name: 'foo', uri: 'test://foo' }] }; const languageModes = getLanguageModes({ css: true, javascript: true }, workspace, ClientCapabilities.LATEST, getNodeFSRequestService()); const javascriptMode = languageModes.getMode('javascript') const position = document.positionAt(offset); if (javascriptMode) { const workspaceEdit: WorkspaceEdit | null = await javascriptMode.doRename!(document, position, newName); assert.ok(workspaceEdit?.changes === undefined, 'Should not rename but rename happened') } else { assert.fail('should have javascriptMode but no') } } suite('HTML Javascript Rename', () => { test('Rename Variable', async () => { const input = [ '', '', '', '', '' ] const output = [ '', '', '', '', '' ] await testRename(input.join('\n'), 'h', output.join('\n')) }) test('Rename Function', async () => { const input = [ '', '', '', '', '' ] const output = [ '', '', '', '', '' ] await testRename(input.join('\n'), 'sayName', output.join('\n')) }) test('Rename Function Params', async () => { const input = [ '', '', '', '', '' ] const output = [ '', '', '', '', '' ] await testRename(input.join('\n'), 'newName', output.join('\n')) }) test('Rename Class', async () => { const input = [ '', '', '', '', '' ] const output = [ '', '', '', '', '' ] await testRename(input.join('\n'), 'Bar', output.join('\n')) }) test('Cannot Rename literal', async () => { const stringLiteralInput = [ '', '', '', '', '' ] const numberLiteralInput = [ '', '', '', '', '' ] await testNoRename(stringLiteralInput.join('\n'), 'something') await testNoRename(numberLiteralInput.join('\n'), 'hhhh') }) });