Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit d0335e3

Browse files
committed
Fix autocompletion by correctly using index and startIndex
1 parent 16336bd commit d0335e3

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/main/java/org/scijava/jupyter/kernel/evaluator/ScijavaEvaluator.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
import java.io.IOException;
2828
import java.util.ArrayList;
29+
import java.util.Arrays;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
33+
import java.util.stream.Collectors;
3234

3335
import javax.script.Bindings;
3436
import javax.script.ScriptContext;
@@ -93,6 +95,10 @@ public void setShellOptions(KernelParameters kp) throws IOException {
9395
@Override
9496
public AutocompleteResult autocomplete(String code, int index) {
9597

98+
// Get only the line corresponding to the index.
99+
List<String> lines = Arrays.asList(code.substring(0, index).split("\n"));
100+
String line = lines.get(lines.size() - 1);
101+
96102
// TODO: we need to find a way the language related to the current cell.
97103
// For now, we are just using the last used language.
98104
AutoCompleter completer = this.completers.get(this.languageName);
@@ -101,17 +107,26 @@ public AutocompleteResult autocomplete(String code, int index) {
101107
List<String> matches;
102108
int startIndex;
103109
if (completer != null) {
104-
AutoCompletionResult result = completer.autocomplete(code, index, scriptEngine);
110+
AutoCompletionResult result = completer.autocomplete(line, index, scriptEngine);
105111

106112
matches = (List<String>) result.getMatches();
107-
startIndex = (int) result.getStartIndex();
113+
startIndex = index;
108114

109115
} else {
110116
matches = new ArrayList<>();
111117
startIndex = 0;
112118
}
119+
120+
// Reconstruct each matches with the correct index
121+
List<String> newMatches = new ArrayList<>();
122+
String newLine;
123+
for (String match: matches) {
124+
lines.set(lines.size() - 1, match);
125+
newLine = lines.stream().collect(Collectors.joining("\n"));
126+
newMatches.add(newLine.substring(startIndex, newLine.length()));
127+
}
113128

114-
return new AutocompleteResult(matches, startIndex);
129+
return new AutocompleteResult(newMatches, startIndex);
115130
}
116131

117132
@Override

0 commit comments

Comments
 (0)