Skip to content

Commit

Permalink
Merge pull request serenadeai#7 from serenadeai/repeat-fixes
Browse files Browse the repository at this point in the history
Repeat fixes
  • Loading branch information
MattWiethoff authored Jun 29, 2022
2 parents 781967f + 102031c commit 96d1ae2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 54 deletions.
9 changes: 0 additions & 9 deletions client/src/main/execute/insert-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,4 @@ export default class InsertHistory {

return value.text;
}

normalize(text: string, app: string) {
const value = this.latest(app);
if (value && text.startsWith(value)) {
text = text.substring(value.length);
}

return text;
}
}
5 changes: 2 additions & 3 deletions client/src/main/execute/native-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ class TypeText implements Operation {
) {}

async execute() {
const text = this.insertHistory.normalize(this.text, this.active.app);
this.insertHistory.add(text, this.active.app);
await this.system.typeText(text, this.active.app);
this.insertHistory.add(this.text, this.active.app);
await this.system.typeText(this.text, this.active.app);
}

keystrokesCount(): number {
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/core/evaluator/CallbackEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ private CommandsResponse paste(EditorStateWithMetadata state, String directionNa
.build();
}

private void addToHistory(String token, String transcript) {
history.add(token, transcript);
}

public Optional<CommandsResponse> evaluate(
CallbackRequest request,
EditorStateWithMetadata state
Expand Down Expand Up @@ -210,7 +206,10 @@ public Optional<CommandsResponse> evaluate(
} else if (request.getType() == CallbackType.CALLBACK_TYPE_PASTE) {
return Optional.of(paste(state, request.getText()));
} else if (request.getType() == CallbackType.CALLBACK_TYPE_ADD_TO_HISTORY) {
addToHistory(state.getToken(), request.getText());
history.add(state.getToken(), request.getText());
return Optional.empty();
} else if (request.getType() == CallbackType.CALLBACK_TYPE_CLEAR_HISTORY) {
history.clear(state.getToken());
return Optional.empty();
}
} catch (Exception e) {
Expand Down
12 changes: 1 addition & 11 deletions core/src/test/java/core/BaseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,24 @@
import core.gen.rpc.Command;
import core.gen.rpc.CommandType;
import core.gen.rpc.CommandsResponse;
import core.gen.rpc.CommandsResponseAlternative;
import core.gen.rpc.EditorState;
import core.gen.rpc.EvaluateRequest;
import core.gen.rpc.EvaluateResponse;
import core.gen.rpc.EvaluateTextRequest;
import core.gen.rpc.InitializeRequest;
import core.gen.rpc.Language;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import toolbelt.client.CoreClient;

public class BaseServiceTest extends BaseTest {

private CoreClient client;
protected CoreClient client;

@BeforeEach
public void baseServiceBefore() {
Expand Down
65 changes: 39 additions & 26 deletions core/src/test/java/core/visitor/CommandsVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,53 @@

import com.google.protobuf.ByteString;
import core.BaseServiceTest;
import core.gen.rpc.CallbackRequest;
import core.gen.rpc.CallbackType;
import core.gen.rpc.Command;
import core.gen.rpc.CommandType;
import core.gen.rpc.CommandsResponse;
import core.gen.rpc.CommandsResponseAlternative;
import core.gen.rpc.EditorState;
import core.gen.rpc.EvaluateRequest;
import core.gen.rpc.EvaluateTextRequest;
import core.gen.rpc.Language;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import toolbelt.state.History;

// test commands that exist in every language but can't be captured in YAML tests
// we happen to use Python here, but none of this is Python-specific.
public class CommandsVisitorTest extends BaseServiceTest {

private Language language = Language.LANGUAGE_PYTHON;

private void clearHistory() {
client.send(
EvaluateRequest
.newBuilder()
.setCallbackRequest(
CallbackRequest.newBuilder().setType(CallbackType.CALLBACK_TYPE_CLEAR_HISTORY).build()
)
.build()
);
}

private void addToHistory(String command) {
client.send(
EvaluateRequest
.newBuilder()
.setCallbackRequest(
CallbackRequest
.newBuilder()
.setType(CallbackType.CALLBACK_TYPE_ADD_TO_HISTORY)
.setText(command)
.build()
)
.build()
);
}

@Test
public void testAlternatives() {
CommandsResponse response = makeRequest(
Expand Down Expand Up @@ -533,30 +560,22 @@ public void testQuantifier() {

@Test
public void testRepeat() {
// local environments use the in-memory data store, but the test process and the core
// process don't share memory, so this test won't work
if (System.getenv("IN_MEMORY") != null && System.getenv("IN_MEMORY").equals("1")) {
return;
}

History history = component.history();

history.clear(token);
history.add(token, "new tab");
clearHistory();
addToHistory("new tab");
assertCommandType("", 0, "repeat", language, CommandType.COMMAND_TYPE_CREATE_TAB);

history.clear(token);
history.add(token, "new tab");
history.add(token, "close tab");
clearHistory();
addToHistory("new tab");
addToHistory("close tab");
assertCommandType("", 0, "repeat", language, CommandType.COMMAND_TYPE_CLOSE_TAB);
assertCommandType("", 0, "repeat new", language, CommandType.COMMAND_TYPE_CREATE_TAB);
assertCommandType("", 0, "repeat close", language, CommandType.COMMAND_TYPE_CLOSE_TAB);
assertCommandType("", 0, "repeat", language, CommandType.COMMAND_TYPE_CLOSE_TAB);

// test we don't loop infinitely.
history.clear(token);
history.add(token, "change word to bar");
history.add(token, "end of line repeat change");
clearHistory();
addToHistory("change word to bar");
addToHistory("end of line repeat change");
assertCommandTypes(
"foo\n",
1,
Expand All @@ -568,14 +587,8 @@ public void testRepeat() {

@Test
public void testRepeatQuantifier() {
// see testRepeat
if (System.getenv("IN_MEMORY") != null && System.getenv("IN_MEMORY").equals("1")) {
return;
}

History history = component.history();
history.clear(token);
history.add(token, "delete character");
clearHistory();
addToHistory("delete character");

assertCommandTypes(
"abcdefghijklmn\n",
Expand Down
1 change: 1 addition & 0 deletions toolbelt/src/main/proto/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum CallbackType {
CALLBACK_TYPE_OPEN_FILE = 2;
CALLBACK_TYPE_PASTE = 3;
CALLBACK_TYPE_ADD_TO_HISTORY = 4;
CALLBACK_TYPE_CLEAR_HISTORY = 5;
}

enum CommandType {
Expand Down

0 comments on commit 96d1ae2

Please sign in to comment.