Skip to content

Commit

Permalink
Merge branch 'main' into progress-bar
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Apr 7, 2024
2 parents 5d1e06f + 18a80e7 commit 10d31b5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ test bool selfApplyCurry() {
func = curry(curry, addition);
//assert int(int)(int) _ := func;
assert int(int)(int) _ := func;
func2 = func(1);
//assert int(int) _ := func2;
assert int(int) _ := func2;
return func2(1) == 2;
}
Expand Down
39 changes: 39 additions & 0 deletions src/org/rascalmpl/library/util/Clipboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.rascalmpl.library.util;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import org.rascalmpl.debug.IRascalMonitor;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValueFactory;

public class Clipboard {
private final IValueFactory vf;
private final java.awt.datatransfer.Clipboard cp;
private IRascalMonitor monitor;

public Clipboard(IValueFactory vf, IRascalMonitor monitor) {
this.vf = vf;
this.cp = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
this.monitor = monitor;
}

public void copy(IString arg) {
var selection = new StringSelection(arg.getValue());
cp.setContents(selection, selection);
}

public IString paste() {
try {
if (cp.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
return vf.string(cp.getData(DataFlavor.stringFlavor).toString());
}
}
catch (UnsupportedFlavorException | IOException e) {
monitor.warning("Clipboard::paste failed", null);
}

return vf.string("");
}
}
39 changes: 39 additions & 0 deletions src/org/rascalmpl/library/util/Clipboard.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@synopsis{Programmatic access to the native system clipboard (copy and paste), with some handy (de)-escaping features.}
module util::Clipboard

@javaClass{org.rascalmpl.library.util.Clipboard}
@synopsis{If there is textual content in the current clipboard, then return it as a string.}
@description{
This will transfer any plaintext content that is currently on the system clipboard, no matter
the source to a string value.

If the system's clipboard is not accessible (say we are running in a headless environment),
then paste always returns the empty string.
}
@pitfalls{
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds.
}
@benefits{
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL.
* paste encodes and escapes all kinds of wild characters automatically.
}
java str paste();

@javaClass{org.rascalmpl.library.util.Clipboard}
@synopsis{Load the contents of a string value into the system clipboard.}
@description{
This will put the contents of the string value in into the system clipboard.
So this is not the string value with quotes and escapes, but the pure string
data.

If the system's clipboard is not accessible (say we are running in a headless environment),
then copy always has no effect.
}
@pitfalls{
* the first time paste or copy are called the UI toolkit must boot up; it can take a few seconds.
}
@benefits{
* copy/paste allow for interesting and useful user interactions, especially while experimenting on the REPL.
* copy transfers the pure content of the string, no quotes or escapes.
}
java void copy(str content);

0 comments on commit 10d31b5

Please sign in to comment.