Skip to content

Have protection against modifying things off screen#45

Description

@pokey

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions