Skip to content

Commit

Permalink
De-duplicate queries when navigating repl history with ctrl+up/down (F…
Browse files Browse the repository at this point in the history
…ixes #216)
  • Loading branch information
jaclarke committed Feb 26, 2024
1 parent 3dd75b8 commit a8f76e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions shared/studio/tabs/repl/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ const ReplInput = observer(function ReplInput() {
{
key: "Mod-ArrowUp",
run: () => {
replState.navigateHistory(-1);
replState.navigateHistory(1);
return true;
},
},
{
key: "Mod-ArrowDown",
run: () => {
replState.navigateHistory(1);
replState.navigateHistory(-1);
return true;
},
},
Expand Down
49 changes: 37 additions & 12 deletions shared/studio/tabs/repl/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,52 @@ export class Repl extends Model({

initialScrollPos = 0;

// -1 is draft, then counts how many history items back from most recent
historyCursor = -1;
draftQuery = Text.empty;

dedupedQueryHistory: ReplHistoryItem[] = [];

_addDedupedHistoryQueries(history: ReplHistoryItem[]) {
let lastQuery =
this.dedupedQueryHistory[this.dedupedQueryHistory.length - 1]?.query;
for (let i = history.length - 1; i >= 0; i--) {
const item = history[i];
if (item.query !== lastQuery) {
this.dedupedQueryHistory.push(item);
lastQuery = item.query;
}
}
}

@action
navigateHistory(direction: 1 | -1) {
let cursor =
(this.historyCursor === -1
? this.queryHistory.length
: this.historyCursor) + direction;
// 1 => backwards, -1 => forwards
if (this._fetchingHistory) {
return;
}
let cursor = this.historyCursor + direction;

if (cursor === this.queryHistory.length) {
if (
cursor < -1 ||
(cursor >= this.dedupedQueryHistory.length && !this._hasUnfetchedHistory)
) {
return;
}
if (cursor === -1) {
this.currentQuery = this.draftQuery;
this.historyCursor = -1;
} else {
const historyItem = this.queryHistory[cursor];
if (historyItem) {
if (this.historyCursor === -1) {
this.draftQuery = this.currentQuery;
}
this.currentQuery = Text.of(historyItem.query.split("\n"));
this.historyCursor = cursor;
if (cursor >= this.dedupedQueryHistory.length) {
this.fetchReplHistory().then(() => this.navigateHistory(1));
return;
}
const historyItem = this.dedupedQueryHistory[cursor];
if (this.historyCursor === -1) {
this.draftQuery = this.currentQuery;
}
this.currentQuery = Text.of(historyItem.query.split("\n"));
this.historyCursor = cursor;
}
}

Expand Down Expand Up @@ -293,6 +317,7 @@ export class Repl extends Model({
this.itemHeights.addHistoryItems(
Array(history.length).fill(defaultItemHeight)
);
this._addDedupedHistoryQueries(historyItems);

this._fetchingHistory = false;
});
Expand Down

0 comments on commit a8f76e2

Please sign in to comment.