quarto-dev / quarto

Quarto open-source scientific and technical publishing system
https://quarto.org
GNU Affero General Public License v3.0
280 stars 19 forks source link

Add Copy Cell Command to VSCode #442

Open dmnkf opened 1 month ago

dmnkf commented 1 month ago

Currently .qmd in VSCode supports the run cell command, but no way of just copying or cutting a cell.

As I shift around cells and need copy snippets around regularly, the functionality of having a copy cell function is crucial for efficiency. As Quarto already supports the run cell command, it seems feasible to have a more broken down version of this which yanks the cell to the clipboard instead of in the interactive kernel.

Without knowing too much ts, the class could look something like this:

class CopyCurrentCellCommand extends RunCommand implements Command {
  constructor(host: ExtensionHost, engine: MarkdownEngine) {
    super(host, engine);
  }
  private static readonly id = "quarto.copyCurrentCell";
  public readonly id = CopyCurrentCellCommand.id;

  override async doExecute(
    editor: TextEditor,
    _tokens: Token[],
    _line: number,
    block?: Token
  ) {
    if (block && isExecutableLanguageBlock(block)) {
      const code = codeWithoutOptionsFromBlock(block);
      await this.copyToClipboard(code, editor);
    }
  }

  private async copyToClipboard(code: string, editor: TextEditor): Promise<void> {
    try {
      await navigator.clipboard.writeText(code);
      editor.showInformationMessage('Code copied to clipboard!');
    } catch (err) {
      editor.showErrorMessage('Failed to copy code to clipboard.');
    }
  }
}

this would be amazing. Thank you.