Skip to content

Commit

Permalink
Fix replace one occurence
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Jan 4, 2022
1 parent d325e50 commit e535731
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/cells/src/searchprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export class CellSearchProvider implements IDisposable, IBaseSearchProvider {
if (this.matchesSize === 0 || !this.isActive) {
this.currentIndex = null;
} else {
if (this._lastReplacementPosition) {
this.cell.editor.setCursorPosition(this._lastReplacementPosition);
this._lastReplacementPosition = null;
}

// This starts from the cursor position
let match = await this.cmHandler.highlightNext();
if (match) {
Expand Down Expand Up @@ -266,11 +271,6 @@ export class CellSearchProvider implements IDisposable, IBaseSearchProvider {
this.query,
content.text
);
if (this._lastReplacementPosition) {
this.cell.editor.setCursorPosition(this._lastReplacementPosition);
this._lastReplacementPosition = null;
this.highlightNext();
}
} else {
this.cmHandler.matches = [];
}
Expand Down
25 changes: 13 additions & 12 deletions packages/notebook/src/searchprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,31 +226,32 @@ export class NotebookSearchProvider extends SearchProvider<NotebookPanel> {
async replaceCurrentMatch(newText: string, loop = true): Promise<boolean> {
let replaceOccurred = false;

if (this._currentProviderIndex !== null) {
const unrenderMarkdownCell = async (
highlightNext = false
): Promise<void> => {
// Unrendered markdown cell
const activeCell = this.widget?.content.activeCell;
if (
activeCell?.model.type === 'markdown' &&
(activeCell as MarkdownCell).rendered
) {
(activeCell as MarkdownCell).rendered = false;
if (highlightNext) {
await this.highlightNext(loop);
}
}
};

if (this._currentProviderIndex !== null) {
await unrenderMarkdownCell();

const searchEngine = this._searchProviders[this._currentProviderIndex];
replaceOccurred = await searchEngine.replaceCurrentMatch(newText);
}
if (!replaceOccurred) {
await this.highlightNext(loop);
}

// Unrendered markdown cell
const activeCell = this.widget?.content.activeCell;
if (
activeCell?.model.type === 'markdown' &&
(activeCell as MarkdownCell).rendered
) {
(activeCell as MarkdownCell).rendered = false;
}
await this.highlightNext(loop);
// Force highlighting the first hit in the unrendered cell
await unrenderMarkdownCell(true);
return replaceOccurred;
}

Expand Down

0 comments on commit e535731

Please sign in to comment.