Skip to content

Commit

Permalink
wip: mainly feature complete, needs testing, UI, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aschlaep committed Dec 22, 2018
1 parent 1439587 commit c84dafc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 37 deletions.
3 changes: 2 additions & 1 deletion packages/documentsearch-extension/package.json
Expand Up @@ -47,6 +47,7 @@
"access": "public"
},
"jupyterlab": {
"extension": true
"extension": true,
"schemaDir": "schema"
}
}
24 changes: 24 additions & 0 deletions packages/documentsearch-extension/schema/plugin.json
@@ -0,0 +1,24 @@
{
"title": "Document Search",
"description": "Document search plugin.",
"jupyter.lab.shortcuts": [
{
"command": "documentsearch:start",
"keys": ["Accel F"],
"selector": ".jp-Document"
},
{
"command": "documentsearch:highlightNext",
"keys": ["Accel G"],
"selector": ".jp-Document"
},
{
"command": "documentsearch:highlightPrevious",
"keys": ["Accel Shift G"],
"selector": ".jp-Document"
}
],
"properties": {},
"additionalProperties": false,
"type": "object"
}
1 change: 0 additions & 1 deletion packages/documentsearch-extension/src/executor.ts
Expand Up @@ -14,7 +14,6 @@ export class Executor {
// to know how to respond to an 'enter' keypress (new search or next search)
let cleanupPromise = Promise.resolve();
if (this._activeProvider) {
console.log('we have an active provider already, cleaning up with end');
cleanupPromise = this._activeProvider.endSearch();
}
this._currentWidget = this._shell.currentWidget;
Expand Down
53 changes: 34 additions & 19 deletions packages/documentsearch-extension/src/index.ts
Expand Up @@ -92,55 +92,60 @@ export interface ISearchProvider {
readonly currentMatchIndex: number;
}

console.log('documentsearch-extension loaded');

/**
* Initialization data for the document-search extension.
*/
const extension: JupyterLabPlugin<void> = {
id: 'document-search',
id: '@jupyterlab/documentsearch:plugin',
autoStart: true,
requires: [ICommandPalette],
activate: (app: JupyterLab, palette: ICommandPalette) => {
console.log('JupyterLab extension document-search is activated!');

// Create registry, retrieve all default providers
const registry: SearchProviderRegistry = new SearchProviderRegistry();
const executor: Executor = new Executor(registry, app.shell);

// Create widget, attach to signals
const widget: SearchBox = new SearchBox();

// Default to just searching on the current widget, could eventually
// read a flag provided by the search box widget if we want to search something else
widget.startSearch.connect((_, searchOptions) => {
const startSearchFn = (_: any, searchOptions: any) => {
executor.startSearch(searchOptions).then((matches: ISearchMatch[]) => {
console.log('matches from executor: ', matches);
widget.totalMatches = executor.matches.length;
widget.currentIndex = executor.currentMatchIndex;
});
});
widget.endSearch.connect(_ => {
};

const endSearchFn = () => {
executor.endSearch().then(() => {
widget.totalMatches = 0;
widget.currentIndex = 0;
});
});
widget.highlightNext.connect(_ => {
};

const highlightNextFn = () => {
executor.highlightNext().then(() => {
widget.totalMatches = executor.matches.length;
widget.currentIndex = executor.currentMatchIndex;
});
});
widget.highlightPrevious.connect(_ => {
};

const highlightPreviousFn = () => {
executor.highlightPrevious().then(() => {
widget.totalMatches = executor.matches.length;
widget.currentIndex = executor.currentMatchIndex;
});
});
};

const command: string = 'document:search';
app.commands.addCommand(command, {
// Default to just searching on the current widget, could eventually
// read a flag provided by the search box widget if we want to search something else
widget.startSearch.connect(startSearchFn);
widget.endSearch.connect(endSearchFn);
widget.highlightNext.connect(highlightNextFn);
widget.highlightPrevious.connect(highlightPreviousFn);

const startCommand: string = 'documentsearch:start';
const nextCommand: string = 'documentsearch:highlightNext';
const prevCommand: string = 'documentsearch:highlightPrevious';
app.commands.addCommand(startCommand, {
label: 'Search the open document',
execute: () => {
if (!widget.isAttached) {
Expand All @@ -151,8 +156,18 @@ const extension: JupyterLabPlugin<void> = {
}
});

app.commands.addCommand(nextCommand, {
label: 'Search the open document',
execute: highlightNextFn
});

app.commands.addCommand(prevCommand, {
label: 'Search the open document',
execute: highlightPreviousFn
});

// Add the command to the palette.
palette.addItem({ command, category: 'Tutorial' });
palette.addItem({ command: startCommand, category: 'Tutorial' });
}
};

Expand Down
Expand Up @@ -16,7 +16,6 @@ export class CodeMirrorSearchProvider implements ISearchProvider {
} else {
this._cm = domain.content.editor;
}
console.log('this._cm: ', this._cm);
Private.clearSearch(this._cm);

const state = Private.getSearchState(this._cm);
Expand Down Expand Up @@ -65,7 +64,6 @@ export class CodeMirrorSearchProvider implements ISearchProvider {
}
const match = this._matchState[cursorMatch.from.line][cursorMatch.from.ch];
this._matchIndex = match.index;
console.log('match: ', match);
return Promise.resolve(match);
}

Expand All @@ -85,10 +83,6 @@ export class CodeMirrorSearchProvider implements ISearchProvider {
}

canSearchOn(domain: any): boolean {
console.log('codemirror search provider: canSearchOn');
console.log('domain: ', domain);
console.log('domain.content: ', domain.content);
console.log('domain.content.editor: ', domain.content.editor);
return domain.content && domain.content.editor instanceof CodeMirrorEditor;
}

Expand Down
Expand Up @@ -34,26 +34,22 @@ export class NotebookSearchProvider implements ISearchProvider {
cmSearchProvider.shouldLoop = false;
let reRenderPlease = false;
if (cell instanceof MarkdownCell && cell.rendered) {
console.log('un-rendering markdown cell temporarily');
cell.rendered = false;
reRenderPlease = true;
}
if (cell.inputHidden) {
cell.inputHidden = false;
}
matchPromises.push(
cmSearchProvider
.startSearch(query, cmEditor)
.then((matchesFromCell: ISearchMatch[]) => {
// update the match indices to reflec the whole document index values
// update the match indices to reflect the whole document index values
if (cell instanceof MarkdownCell) {
if (matchesFromCell.length !== 0) {
console.log(
'found unrendered markdown cell with matches!: ',
cell
);
console.log('matches from the cell: ', matchesFromCell);
// un-render markdown cells with matches
this._unRenderedMarkdownCells.push(cell);
} else if (reRenderPlease) {
console.log('re-rendering markdown cell without matches');
cell.rendered = true;
}
}
Expand Down Expand Up @@ -85,7 +81,6 @@ export class NotebookSearchProvider implements ISearchProvider {

// Flatten matches into one array
return Promise.all(matchPromises).then(matchesFromCells => {
console.log('matches from cells: ', matchesFromCells);
let result: ISearchMatch[] = [];
matchesFromCells.forEach((cellMatches: ISearchMatch[]) => {
result.concat(cellMatches);
Expand Down Expand Up @@ -134,7 +129,6 @@ export class NotebookSearchProvider implements ISearchProvider {
}

canSearchOn(domain: any): boolean {
console.log('notebook provider: canSearchOn');
return domain instanceof NotebookPanel;
}

Expand Down

0 comments on commit c84dafc

Please sign in to comment.