瀏覽代碼

Backport PR #12310: Add argument `searchText` and `replaceText` to search and replace commands (#12418)

Co-authored-by: Alex Bozarth <ajbozart@us.ibm.com>
MeeseeksMachine 3 年之前
父節點
當前提交
59d097eab8

+ 63 - 0
galata/test/jupyterlab/search.test.ts

@@ -0,0 +1,63 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+import { test } from '@jupyterlab/galata';
+import { expect } from '@playwright/test';
+
+const DEFAULT_NAME = 'untitled.txt';
+
+const TEST_FILE_CONTENT = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam urna
+libero, dictum a egestas non, placerat vel neque. In imperdiet iaculis fermentum.
+Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia
+Curae; Cras augue tortor, tristique vitae varius nec, dictum eu lectus. Pellentesque
+id eleifend eros. In non odio in lorem iaculis sollicitudin. In faucibus ante ut
+arcu fringilla interdum. Maecenas elit nulla, imperdiet nec blandit et, consequat
+ut elit.`;
+
+test('Search with a text', async ({ page }) => {
+  const searchText = 'ipsum';
+  await page.menu.clickMenuItem('File>New>Text File');
+
+  await page.waitForSelector(`[role="main"] >> text=${DEFAULT_NAME}`);
+
+  await page.fill('[role="main"] >> textarea', TEST_FILE_CONTENT);
+
+  await page.evaluate(async searchText => {
+    await window.jupyterapp.commands.execute('documentsearch:start', {
+      searchText
+    });
+  }, searchText);
+
+  expect(
+    await page.locator('input.jp-DocumentSearch-input').inputValue()
+  ).toEqual(searchText);
+});
+
+test('Search with a text and replacement', async ({ page }) => {
+  const searchText = 'ipsum';
+  const replaceText = 'banana';
+  await page.menu.clickMenuItem('File>New>Text File');
+
+  await page.waitForSelector(`[role="main"] >> text=${DEFAULT_NAME}`);
+
+  await page.fill('[role="main"] >> textarea', TEST_FILE_CONTENT);
+
+  await page.evaluate(
+    async ([searchText, replaceText]) => {
+      await window.jupyterapp.commands.execute(
+        'documentsearch:startWithReplace',
+        {
+          searchText,
+          replaceText
+        }
+      );
+    },
+    [searchText, replaceText]
+  );
+
+  expect(
+    await page.locator('input.jp-DocumentSearch-input').inputValue()
+  ).toEqual(searchText);
+  expect(
+    await page.locator('input.jp-DocumentSearch-replace-entry').inputValue()
+  ).toEqual(replaceText);
+});

+ 14 - 2
packages/documentsearch-extension/src/index.ts

@@ -167,9 +167,13 @@ const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
     app.commands.addCommand(startCommand, {
       label: trans.__('Find…'),
       isEnabled: currentWidgetHasSearchProvider,
-      execute: () => {
+      execute: args => {
         const searchInstance = getCurrentWidgetSearchInstance();
         if (searchInstance) {
+          const searchText = args['searchText'] as string;
+          if (searchText) {
+            searchInstance.setSearchText(searchText);
+          }
           searchInstance.focusInput();
         }
       }
@@ -178,9 +182,17 @@ const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
     app.commands.addCommand(startReplaceCommand, {
       label: trans.__('Find and Replace…'),
       isEnabled: currentWidgetHasSearchProvider,
-      execute: () => {
+      execute: args => {
         const searchInstance = getCurrentWidgetSearchInstance();
         if (searchInstance) {
+          const searchText = args['searchText'] as string;
+          if (searchText) {
+            searchInstance.setSearchText(searchText);
+          }
+          const replaceText = args['replaceText'] as string;
+          if (replaceText) {
+            searchInstance.setReplaceText(replaceText);
+          }
           searchInstance.showReplace();
           searchInstance.focusInput();
         }

+ 18 - 0
packages/documentsearch/src/searchinstance.ts

@@ -102,6 +102,24 @@ export class SearchInstance implements IDisposable {
     this._displayState.forceFocus = false;
   }
 
+  /**
+   * Set the search text
+   *
+   * It does not trigger a view update.
+   */
+  setSearchText(search: string): void {
+    this._displayState.searchText = search;
+  }
+
+  /**
+   * Set the replace text
+   *
+   * It does not trigger a view update.
+   */
+  setReplaceText(replace: string): void {
+    this._displayState.replaceText = replace;
+  }
+
   /**
    * If there is a replace box, show it.
    */