Skip to content

Commit

Permalink
Removed additional highlight before scroll (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
  • Loading branch information
AndreasArvidsson and pokey authored Nov 29, 2021
1 parent 5cdb954 commit a263fc5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 79 deletions.
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@
"default": 100,
"description": "How long in milliseconds to show a pending edit decoration"
},
"cursorless.showAdditionalHighlightBeforeScroll": {
"type": "boolean",
"default": false,
"description": "Whether to show a highlight before scrolling in addition to after"
},
"cursorless.hatSizeAdjustment": {
"type": "number",
"default": 0,
Expand Down
53 changes: 23 additions & 30 deletions src/actions/Scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import {
Graph,
TypedSelection,
} from "../typings/Types";
import { displayDecorationsWhileRunningFunc } from "../util/editDisplayUtils";
import { groupBy } from "../util/itertools";
import { commands, window, workspace } from "vscode";
import { commands, window } from "vscode";
import { focusEditor } from "../util/setSelectionsAndFocusEditor";
import {
displayPendingEditDecorationsForSelection,
} from "../util/editDisplayUtils";

class Scroll implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }];
getTargetPreferences: () => ActionPreferences[] = () => [
{ insideOutsideType: "inside" },
];

constructor(private graph: Graph, private at: string) {
this.run = this.run.bind(this);
Expand All @@ -27,38 +31,27 @@ class Scroll implements Action {
return { lineNumber: getLineNumber(targets, this.at), editor };
});

const scrollCallback = async () => {
const originalEditor = window.activeTextEditor;

for (const lineWithEditor of lines) {
// For reveal line to the work we have to have the correct editor focused
if (lineWithEditor.editor !== window.activeTextEditor) {
await focusEditor(lineWithEditor.editor);
}
await commands.executeCommand("revealLine", {
lineNumber: lineWithEditor.lineNumber,
at: this.at,
});
}
const originalEditor = window.activeTextEditor;

// If necessary focus back original editor
if (
originalEditor != null &&
originalEditor !== window.activeTextEditor
) {
await focusEditor(originalEditor);
for (const lineWithEditor of lines) {
// For reveal line to the work we have to have the correct editor focused
if (lineWithEditor.editor !== window.activeTextEditor) {
await focusEditor(lineWithEditor.editor);
}
};
await commands.executeCommand("revealLine", {
lineNumber: lineWithEditor.lineNumber,
at: this.at,
});
}

const showAdditionalHighlightBeforeScroll = workspace
.getConfiguration("cursorless")
.get<boolean>("showAdditionalHighlightBeforeScroll")!;
// If necessary focus back original editor
if (originalEditor != null && originalEditor !== window.activeTextEditor) {
await focusEditor(originalEditor);
}

await displayDecorationsWhileRunningFunc(
await displayPendingEditDecorationsForSelection(
targets.map((target) => target.selection),
this.graph.editStyles.referenced.line,
scrollCallback,
showAdditionalHighlightBeforeScroll
this.graph.editStyles.referenced.line
);

return {
Expand Down
44 changes: 0 additions & 44 deletions src/util/editDisplayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,50 +98,6 @@ export default async function displayPendingEditDecorations(
});
}

/**
1. Shows decorations.
2. Wait for pending edit decoration time while subtracting the time it takes to actually run the callback
3. Removes decorations
*/
export async function displayDecorationsWhileRunningFunc(
selections: SelectionWithEditor[],
decorationType: TextEditorDecorationType,
callback: () => Promise<void>,
showAdditionalHighlightBeforeCallback: boolean
) {
if (!showAdditionalHighlightBeforeCallback) {
await callback();
}

await runForEachEditor(
selections,
(s) => s.editor,
async (editor, selections) => {
editor.setDecorations(
decorationType,
selections.map((s) => s.selection)
);
}
);

const pendingEditDecorationTime = getPendingEditDecorationTime();

if (showAdditionalHighlightBeforeCallback) {
await sleep(pendingEditDecorationTime);
await callback();
}

await sleep(pendingEditDecorationTime);

await runForEachEditor(
selections,
(s) => s.editor,
async (editor) => {
editor.setDecorations(decorationType, []);
}
);
}

function useLineDecorations(selection: TypedSelection) {
return (
isLineSelectionType(selection.selectionType) &&
Expand Down

0 comments on commit a263fc5

Please sign in to comment.