Path: blob/main/extensions/copilot/src/platform/parser/test/node/getNodeMatchingSelection.spec.ts
13405 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 { outdent } from 'outdent';6import { afterAll, expect, suite, test } from 'vitest';7import {8_dispose,9} from '../../node/parserImpl';10import { _parse } from '../../node/parserWithCaching';11import { _getNodeMatchingSelection } from '../../node/selectionParsing';12import { WASMLanguage } from '../../node/treeSitterLanguages';1314suite('getNodeMatchingSelection', () => {15function deannotateSrc(annotatedSrc: string) {16const startIndex = annotatedSrc.indexOf('<<');17const endIndex = annotatedSrc.indexOf('>>') - 2;18return {19deannotatedSrc: annotatedSrc.replace('<<', '').replace('>>', ''),20annotatedRange: {21startIndex,22endIndex,23},24};25}2627async function getNode(28annotatedSrc: string,29languageId = WASMLanguage.TypeScript30) {31const { deannotatedSrc, annotatedRange } = deannotateSrc(annotatedSrc);3233const parseTreeRef = await _parse(languageId, deannotatedSrc);3435try {36const r = _getNodeMatchingSelection(37parseTreeRef.tree,38annotatedRange,39languageId40);41return r ? r.text : 'undefined';42} finally {43parseTreeRef.dispose();44}45}4647afterAll(() => _dispose());4849suite('with function', () => {50test('within identifier', async () => {51const source = outdent`52class Foo {5354ba<<>>r() {5556}57}58`;59expect(await getNode(source)).toMatchInlineSnapshot(`"undefined"`);60});6162test('whitespace before', async () => {63const source = outdent`64class Foo {6566<< bar() {6768}>>69}70`;71expect(await getNode(source)).toMatchInlineSnapshot(`72"bar() {7374}"75`);76});7778test('whitespace before & after', async () => {79const source = outdent`80class Foo {8182<< bar() {8384}85>>86}87`;88expect(await getNode(source)).toMatchInlineSnapshot(`89"bar() {9091}"92`);93});9495test('range misses closes }', async () => {96const source = outdent`97class Foo {9899<<bar() {100101>>}102}103`;104expect(await getNode(source)).toMatchInlineSnapshot(`105"bar() {106107}"108`);109});110111test('imprecise selection', async () => {112const source = outdent`113class Foo {114115b<<ar() {116117>>}118}119`;120expect(await getNode(source)).toMatchInlineSnapshot(`121"bar() {122123}"124`);125});126});127128suite('with class', () => {129test('precise selection', async () => {130const source = outdent`131<<class Foo {132133bar() {134135}136}>>137`;138expect(await getNode(source)).toMatchInlineSnapshot(`139"class Foo {140141bar() {142143}144}"145`);146});147148test('range misses closing }', async () => {149const source = outdent`150<<class Foo {151152bar() {153154}155>>}156`;157expect(await getNode(source)).toMatchInlineSnapshot(`158"class Foo {159160bar() {161162}163}"164`);165});166167test('imprecise selection', async () => {168const source = outdent`169class Foo << {170171bar() {172173}>>174}175`;176expect(await getNode(source)).toMatchInlineSnapshot(`177"class Foo {178179bar() {180181}182}"183`);184});185});186187suite('with interface', () => {188test('precise selection', async () => {189const source = outdent`190<<interface Foo {191192bar(): void;193194}>>`;195expect(await getNode(source)).toMatchInlineSnapshot(`196"interface Foo {197198bar(): void;199200}"201`);202});203204test('whitespace before & after', async () => {205const source = outdent`206<<207interface Foo {208209bar(): void;210211}212>>`;213expect(await getNode(source)).toMatchInlineSnapshot(`214"interface Foo {215216bar(): void;217218}"219`);220});221222test('within a function', async () => {223const source = outdent`224<<225function bar() {226interface Foo {227228bar(): void;229230}231232return 42;233}234>>`;235expect(await getNode(source)).toMatchInlineSnapshot(`236"function bar() {237interface Foo {238239bar(): void;240241}242243return 42;244}"245`);246});247248test('most of function is selected', async () => {249const source = outdent`250<<251function bar() {252interface Foo {253254bar(): void;255256}257258return 42;>>259}260`;261expect(await getNode(source)).toMatchInlineSnapshot(`262"function bar() {263interface Foo {264265bar(): void;266267}268269return 42;270}"271`);272});273});274});275276277278