Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to open find with replace #7725

Merged
merged 5 commits into from Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 50 additions & 28 deletions packages/documentsearch-extension/src/index.ts
Expand Up @@ -89,42 +89,64 @@ const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
const activeSearches = new Map<string, SearchInstance>();

const startCommand: string = 'documentsearch:start';
const startReplaceCommand: string = 'documentsearch:startWithReplace';
const nextCommand: string = 'documentsearch:highlightNext';
const prevCommand: string = 'documentsearch:highlightPrevious';

const currentWidgetHasSearchProvider = () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return false;
}
return registry.getProviderForWidget(currentWidget) !== undefined;
};
const getCurrentWidgetSearchInstance = () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
const widgetId = currentWidget.id;
let searchInstance = activeSearches.get(widgetId);
if (!searchInstance) {
const searchProvider = registry.getProviderForWidget(currentWidget);
if (!searchProvider) {
return;
}
searchInstance = new SearchInstance(currentWidget, searchProvider);

activeSearches.set(widgetId, searchInstance);
// find next and previous are now enabled
app.commands.notifyCommandChanged();

searchInstance.disposed.connect(() => {
activeSearches.delete(widgetId);
// find next and previous are now not enabled
app.commands.notifyCommandChanged();
});
}
return searchInstance;
};

app.commands.addCommand(startCommand, {
label: 'Find…',
isEnabled: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return false;
}
return registry.getProviderForWidget(currentWidget) !== undefined;
},
isEnabled: currentWidgetHasSearchProvider,
execute: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
const searchInstance = getCurrentWidgetSearchInstance();
if (searchInstance) {
searchInstance.focusInput();
}
const widgetId = currentWidget.id;
let searchInstance = activeSearches.get(widgetId);
if (!searchInstance) {
const searchProvider = registry.getProviderForWidget(currentWidget);
if (!searchProvider) {
return;
}
searchInstance = new SearchInstance(currentWidget, searchProvider);

activeSearches.set(widgetId, searchInstance);
// find next and previous are now enabled
app.commands.notifyCommandChanged();
}
});

searchInstance.disposed.connect(() => {
activeSearches.delete(widgetId);
// find next and previous are now not enabled
app.commands.notifyCommandChanged();
});
app.commands.addCommand(startReplaceCommand, {
label: 'Find and Replace…',
isEnabled: currentWidgetHasSearchProvider,
execute: () => {
const searchInstance = getCurrentWidgetSearchInstance();
if (searchInstance) {
searchInstance.showReplace();
searchInstance.focusInput();
}
searchInstance.focusInput();
}
});

Expand Down
6 changes: 6 additions & 0 deletions packages/documentsearch/src/searchinstance.ts
Expand Up @@ -77,6 +77,12 @@ export class SearchInstance implements IDisposable {
this._displayState.forceFocus = false;
}

/**
* If there is a replace box, show it.
*/
showReplace(): void {
this._displayState.replaceEntryShown = true;
}
/**
* Updates the match index and total display in the search widget.
*/
Expand Down