Skip to content

Have protection against modifying things off screen #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pokey opened this issue Jun 30, 2021 · 0 comments · Fixed by #165
Open

Have protection against modifying things off screen #45

pokey opened this issue Jun 30, 2021 · 0 comments · Fixed by #165
Assignees
Labels
enhancement New feature or request

Comments

@pokey
Copy link
Member

pokey commented Jun 30, 2021

If a command, eg "chuck" is about to delete something you can't see, it should either fail or warn. Could have a setting to turn this one off if you like to live dangerously 😈

This protection would be important to have before we support #44

Fwiw this support could prob be implemented by a separate extension that Cursorless could then be a client of if it wanted to do something special

Here's some code to get us started. Was originally in extension.ts, but removed as part of #1035 as it is still just unused draft code:

  function checkForEditsOutsideViewport(
    event: vscode.TextDocumentChangeEvent
  ) {
    // TODO: Only activate this code during the course of a cursorless action
    // Can register pre/post command hooks the way we do with test case recorder
    // TODO: Move this thing to a graph component
    // TODO: Need to move command executor and test case recorder to graph
    // component while we're doing this stuff so it's easier to register the
    // hooks
    // TODO: Should run this code even if document is not in a visible editor
    // as long as we are during the course of a cursorless command.
    const editor = vscode.window.activeTextEditor;

    if (
      editor == null ||
      editor.document !== event.document ||
      event.reason === vscode.TextDocumentChangeReason.Undo ||
      event.reason === vscode.TextDocumentChangeReason.Redo
    ) {
      return;
    }

    const ranges = event.contentChanges
      .filter(
        (contentChange) =>
          !editor.visibleRanges.some(
            (visibleRange) =>
              contentChange.range.intersection(visibleRange) != null
          )
      )
      .map(({ range }) => range);

    if (ranges.length > 0) {
      ranges.sort((a, b) => a.start.line - b.start.line);
      const linesText = ranges
        .map((range) => `${range.start.line + 1}-${range.end.line + 1}`)
        .join(", ");
      vscode.window.showWarningMessage(
        `Modification outside of viewport at lines: ${linesText}`
      );
    }
  }

  vscode.workspace.onDidChangeTextDocument(checkForEditsOutsideViewport)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants