dark icon indicating copy to clipboard operation
dark copied to clipboard

Pasting Patterns from clipboard does not work

Open StachuDotNet opened this issue 3 years ago • 3 comments
trafficstars

We have a function in Fluid, pasteOverSelection, which handles pasting Exprs from a clipboard into an existing Expr.

It currently fails when trying to paste into a Pattern - it looks for an expr at the current caretTarget, doesn't find one, and fails with pasting over non-existant handler.

Currently the algorithm of this fn is roughly:

  • delete whatever's currently selected in the editor
  • find the expr now under cursor, where the selection was
  • from the clipboard, try to create an Expr to inject, with Clipboard.clipboardContentsToExpr
  • inject it (which seems to be a rather complicated process)

I suspect we should change this to, roughly,

  • delete whatever's currently selected in the editor
  • find the expr or pattern now under cursor, where the selection was
  • if it's an expr, use existing functionality with Clipboard.clipboardContentsToExpr and the complicated Expr injection
  • if it's otherwise a pattern, use a new Clipboard.clipboardContentsToPat and new relevant 'injection' code

StachuDotNet avatar Sep 06 '22 14:09 StachuDotNet

in the meantime, we'd benefit from improving the error message. "Cannot find Expr at target to paste into" feels slightly better to me.

StachuDotNet avatar Sep 06 '22 14:09 StachuDotNet

While we can't currently reconstruct a pattern at the root of an AST, we can reconstruct a match expression containing Patterns.

One optional/hacky way to handle this is to (and this idea is hand-wavey), wrap a copied pattern in a match, reconstruct the match expr, then extract the first (only) pattern

StachuDotNet avatar Sep 13 '22 16:09 StachuDotNet

A relevant conversation is in this PR recently: https://github.com/darklang/dark/pull/4469 Worth reading through. that mainly discusses patterns within a match expr.

StachuDotNet avatar Sep 15 '22 15:09 StachuDotNet

Is this fixed?

pbiggar avatar Jan 02 '23 16:01 pbiggar

No. You can copy patterns now, but you have to be selecting at least part of a surrounding match statement. This limitation is caused by Fluid.res's getCopySelection directly calling reconstructExprFromRange - only Exprs can be reconstructed.

Those recent-ish efforts paved the way to fix this, but we still face the issue: Peek 2023-01-02 11-19

The next step here would probably be to update the return type of reconstructExprFromRange from option<FluidExpression.t> to option<FluidExpression.fluidMatchPatOrExpr> (along with the name), and go from there. (OP lists some of the related bits of effort)

StachuDotNet avatar Jan 02 '23 16:01 StachuDotNet

Closing as it doesn't apply to darklang-next

pbiggar avatar Mar 01 '23 04:03 pbiggar