Skip to content

Commit

Permalink
Clean up event handler logic
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed May 4, 2023
1 parent e13cf17 commit 7f5851c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
9 changes: 5 additions & 4 deletions packages/completer/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export class CompleterModel implements Completer.IModel {
}

let { start, end } = cursor;
console.log('start', start, 'end', end);
// Also include any filtering/additional-typing that has occurred
// since the completion request in the patched length.
end = end + (current.text.length - original.text.length);
Expand Down Expand Up @@ -368,13 +369,13 @@ export class CompleterModel implements Completer.IModel {
let results: Private.ICompletionMatch[] = [];
for (let item of items) {
// See if label matches query string
// With ICompletionItems, the label may include parameters, so we exclude them from the matcher.
// With ICompletionItems, the label may include parameters,
// so we exclude them from the matcher.
// e.g. Given label `foo(b, a, r)` and query `bar`,
// don't count parameters, `b`, `a`, and `r` as matches.
const index = item.label.indexOf('(');
let prefix = index > -1 ? item.label.substring(0, index) : item.label;
prefix = escapeHTML(prefix);
let match = StringExt.matchSumOfSquares(prefix, query);
const text = index > -1 ? item.label.substring(0, index) : item.label;
const match = text !== query && StringExt.matchSumOfSquares(text, query);
// Filter non-matching items.
if (match) {
// Highlight label text if there's a match
Expand Down
41 changes: 19 additions & 22 deletions packages/completer/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,30 +522,26 @@ export class Completer extends Widget {
private _cycle(direction: Private.scrollType): void {
const items = this.node.querySelectorAll(`.${ITEM_CLASS}`);
const index = this._activeIndex;
const last = items.length - 1;
let active = this.node.querySelector(`.${ACTIVE_CLASS}`) as HTMLElement;
active.classList.remove(ACTIVE_CLASS);

if (direction === 'up') {
this._activeIndex = index === 0 ? items.length - 1 : index - 1;
} else if (direction === 'down') {
this._activeIndex = index < items.length - 1 ? index + 1 : 0;
} else {
// Measure the number of items on a page.
const boxHeight = this.node.getBoundingClientRect().height;
const itemHeight = active.getBoundingClientRect().height;
const pageLength = Math.floor(boxHeight / itemHeight);

// Update the index
if (direction === 'pageUp') {
this._activeIndex = index - pageLength;
} else {
this._activeIndex = index + pageLength;
}
// Clamp to the length of the list.
this._activeIndex = Math.min(
Math.max(0, this._activeIndex),
items.length - 1
);
switch (direction) {
case 'up':
this._activeIndex = index === 0 ? last : index - 1;
break;
case 'down':
this._activeIndex = index < last ? index + 1 : 0;
break;
case 'pageUp':
case 'pageDown':
// Measure the number of items on a page and clamp to the list length.
const container = this.node.getBoundingClientRect();
const current = active.getBoundingClientRect();
const page = Math.floor(container.height / current.height);
const sign = direction === 'pageUp' ? -1 : 1;
this._activeIndex = Math.min(Math.max(0, index + sign * page), last);
break;
}

active = items[this._activeIndex] as HTMLElement;
Expand Down Expand Up @@ -593,12 +589,13 @@ export class Completer extends Widget {
// then emit a completion signal with that `query` (=subset match),
// but only if the query has actually changed.
// See: https://github.com/jupyterlab/jupyterlab/issues/10439#issuecomment-875189540
if (model.query && model.query != this._lastSubsetMatch) {
if (model.query && model.query !== this._lastSubsetMatch) {
model.subsetMatch = true;
this._selected.emit(model.query);
model.subsetMatch = false;
this._lastSubsetMatch = model.query;
}

// If the query changed, update rendering of the options.
if (populated) {
this.update();
Expand Down

0 comments on commit 7f5851c

Please sign in to comment.