Skip to content
Snippets Groups Projects
Commit 674577df authored by Tomas Stupka's avatar Tomas Stupka
Browse files

in-quote completion token has to start at last quote index + 1

parent 5a95d188
No related branches found
No related tags found
No related merge requests found
......@@ -102,7 +102,7 @@ public class JLineConsoleCompleter implements Completer {
// are we in quotes?
int lastQuoteIdx = isInQuotes(buffer, cursor);
if (lastQuoteIdx != -1) {
return lastQuoteIdx;
return lastQuoteIdx + 1;
}
Value opt = env.getMember("options");
......@@ -144,7 +144,7 @@ public class JLineConsoleCompleter implements Completer {
int idx = -1;
int qidx = -1;
int c = 0;
while (++idx <= cursor && idx < buffer.length()) {
while (++idx < cursor && idx < buffer.length()) {
if (buffer.charAt(idx) == '\'' || buffer.charAt(idx) == '\"') {
qidx = idx;
c++;
......
......@@ -35,6 +35,9 @@ import org.junit.Before;
import org.junit.Test;
import com.oracle.truffle.r.launcher.JLineConsoleCompleter;
import java.io.IOException;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.junit.Ignore;
public class TestJLineConsoleCompleter {
......@@ -42,6 +45,11 @@ public class TestJLineConsoleCompleter {
private Context context;
private JLineConsoleCompleter consoleCompleter;
private static final String TEST_PATH = "_TestJLineConsoleCompleter";
private static final Source GET_COMPLETION_ENV = Source.newBuilder("R", "utils:::.CompletionEnv", "<completion>").internal(true).buildLiteral();
private static final Source GET_FUNCTION = Source.newBuilder("R", "`$`", "<completion>").internal(true).buildLiteral();
@Before
public void before() {
JLineConsoleCompleter.testingMode();
......@@ -54,6 +62,13 @@ public class TestJLineConsoleCompleter {
if (context != null) {
context.close();
}
String testPath = getTestPath();
if (testPath != null) {
File f = new File(testPath);
if (f.exists()) {
delete(f);
}
}
}
// disabled because it uses Engine, which clashes with other tests that use PolyglotEngine
......@@ -141,6 +156,61 @@ public class TestJLineConsoleCompleter {
assertCompl(noName + "\"." + File.separator, 8, NOT_EMPTY);
}
@Ignore
@Test
public void testPathCompl() throws IOException {
testPathCompl('"');
testPathCompl('\'');
}
private void testPathCompl(char quote) throws IOException {
File testDir = getTestDir();
String testDirPath = testDir.getAbsolutePath() + File.separator;
// no files in {testDirPath}
// >source("{testDirPath}/<TAB> => no compl
String cli = "source(" + quote + testDirPath;
assertCompl(cli, cli.length());
// >source("{testDirPath}/pa<TAB> => no compl
cli = "source(" + quote + testDirPath + "pa";
assertCompl(cli, cli.length());
// one file in {testDirPath}
File path = new File(testDir, "path1");
path.mkdirs();
// >source("{testDirPath}/pa<TAB> => testDirPath}/path1
cli = "source(" + quote + testDirPath + "pa";
assertCompl(cli, cli.length(), testDir.getAbsolutePath() + File.separator + "path1");
// >source("{testDirPath}/pa<TAB>" (in closed quotes) => testDirPath}/path1
cli = "source(" + quote + testDirPath + "pa" + quote;
assertCompl(cli, cli.length() - 1, testDir.getAbsolutePath() + File.separator + "path1");
Value completionEnv = context.eval(GET_COMPLETION_ENV);
Value getFunction = context.eval(GET_FUNCTION);
Value token = getFunction.execute(completionEnv, "token");
Assert.assertEquals(testDirPath + "pa", token.asString());
// three files in {testDirPath}
new File(testDir, "path2").mkdirs();
new File(testDir, "path3").mkdirs();
// >source("{testDirPath}/pa<TAB> => testDirPath}/path1-3
cli = "source(" + quote + testDirPath + "pa";
assertCompl(cli, cli.length(), testDirPath + "path1", testDirPath + "path2", testDirPath + "path3");
token = getFunction.execute(completionEnv, "token");
Assert.assertEquals(testDirPath + "pa", token.asString());
// >source("{testDirPath}/pa<TAB>" (in closed quotes) => testDirPath}/path1-3
cli = "source(" + quote + testDirPath + "pa" + quote;
assertCompl(cli, cli.length() - 1, testDirPath + "path1", testDirPath + "path2", testDirPath + "path3");
// >source("{testDirPath}/papa<TAB> => no compl
cli = "source(" + quote + testDirPath + "papa";
assertCompl(cli, cli.length());
}
// e.g. check if the file path completion returned at least something
private static final String NOT_EMPTY = "_@_Only.Check.If.Result.Not.Empty_@_";
......@@ -156,4 +226,40 @@ public class TestJLineConsoleCompleter {
Assert.assertArrayEquals(expected, l.toArray(new CharSequence[l.size()]));
}
}
private File getTestDir() throws IOException {
String testPath = getTestPath();
if (testPath != null) {
File f = new File(testPath);
if (f.exists()) {
delete(f);
}
f.mkdirs();
return f;
}
return null;
}
private String getTestPath() {
String tmpDir = System.getProperty("java.io.tmpdir");
if (tmpDir != null) {
return tmpDir + File.separator + TEST_PATH;
}
return null;
}
private void delete(File f) {
if (f.exists()) {
if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (File child : files) {
delete(child);
}
}
}
f.delete();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment