26
26
27
27
import java .io .IOException ;
28
28
import java .util .ArrayList ;
29
+ import java .util .Arrays ;
29
30
import java .util .HashMap ;
30
31
import java .util .List ;
31
32
import java .util .Map ;
33
+ import java .util .stream .Collectors ;
32
34
33
35
import javax .script .Bindings ;
34
36
import javax .script .ScriptContext ;
@@ -93,6 +95,10 @@ public void setShellOptions(KernelParameters kp) throws IOException {
93
95
@ Override
94
96
public AutocompleteResult autocomplete (String code , int index ) {
95
97
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
+
96
102
// TODO: we need to find a way the language related to the current cell.
97
103
// For now, we are just using the last used language.
98
104
AutoCompleter completer = this .completers .get (this .languageName );
@@ -101,17 +107,26 @@ public AutocompleteResult autocomplete(String code, int index) {
101
107
List <String > matches ;
102
108
int startIndex ;
103
109
if (completer != null ) {
104
- AutoCompletionResult result = completer .autocomplete (code , index , scriptEngine );
110
+ AutoCompletionResult result = completer .autocomplete (line , index , scriptEngine );
105
111
106
112
matches = (List <String >) result .getMatches ();
107
- startIndex = ( int ) result . getStartIndex () ;
113
+ startIndex = index ;
108
114
109
115
} else {
110
116
matches = new ArrayList <>();
111
117
startIndex = 0 ;
112
118
}
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
+ }
113
128
114
- return new AutocompleteResult (matches , startIndex );
129
+ return new AutocompleteResult (newMatches , startIndex );
115
130
}
116
131
117
132
@ Override
0 commit comments