Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ public List<InterpreterCompletion> completion(final String buf, final int cursor
final InterpreterContext interpreterContext)
throws InterpreterException {
if (!isOpened) {
LOGGER.warn("completion is called when RemoterInterpreter is not opened for " + className);
return new ArrayList<>();
open();
}
RemoteInterpreterProcess interpreterProcess = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void setText(String newText) {
this.scriptText = this.text.substring(headingSpace.length() + intpText.length() + 1).trim();
} else {
this.intpText = "";
this.scriptText = this.text;
this.scriptText = this.text.trim();
}
}
}
Expand Down Expand Up @@ -250,14 +250,17 @@ public List<InterpreterCompletion> completion(String buffer, int cursor) {
return note.getInterpreterCompletion();
}
}
String trimmedBuffer = buffer != null ? buffer.trim() : null;
cursor = calculateCursorPosition(buffer, trimmedBuffer, cursor);
this.interpreter = getBindedInterpreter();

setText(buffer);

cursor = calculateCursorPosition(buffer, cursor);

InterpreterContext interpreterContext = getInterpreterContextWithoutRunner(null);

try {
if (this.interpreter != null) {
return this.interpreter.completion(scriptText, cursor, interpreterContext);
return this.interpreter.completion(this.scriptText, cursor, interpreterContext);
} else {
return null;
}
Expand All @@ -266,24 +269,15 @@ public List<InterpreterCompletion> completion(String buffer, int cursor) {
}
}

public int calculateCursorPosition(String buffer, String trimmedBuffer, int cursor) {
int countWhitespacesAtStart = buffer.indexOf(trimmedBuffer);
if (countWhitespacesAtStart > 0) {
cursor -= countWhitespacesAtStart;
}
public int calculateCursorPosition(String buffer, int cursor) {
// scriptText trimmed

// parse text to get interpreter component
String repl = null;
if (trimmedBuffer != null) {
Matcher matcher = REPL_PATTERN.matcher(trimmedBuffer);
if (matcher.matches()) {
repl = matcher.group(2);
}
if (this.scriptText.isEmpty()) {
return 0;
}

if (repl != null && cursor > repl.length()) {
String body = trimmedBuffer.substring(repl.length() + 1);
cursor -= repl.length() + 1 + body.indexOf(body.trim());
int countCharactersBeforeScript = buffer.indexOf(this.scriptText);
if (countCharactersBeforeScript > 0) {
cursor -= countCharactersBeforeScript;
}

return cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ public void returnUnchangedResultsWithDifferentUser() throws Throwable {
@Test
public void testCursorPosition() {
Paragraph paragraph = spy(new Paragraph());
doReturn(null).when(paragraph).getIntpText();
// left = buffer, middle = cursor position into source code, right = cursor position after parse
List<Triple<String, Integer, Integer>> dataSet = Arrays.asList(
Triple.of("%jdbc schema.", 13, 7),
Expand All @@ -294,7 +293,8 @@ public void testCursorPosition() {
);

for (Triple<String, Integer, Integer> data : dataSet) {
Integer actual = paragraph.calculateCursorPosition(data.getLeft(), data.getLeft().trim(), data.getMiddle());
paragraph.setText(data.getLeft());
Integer actual = paragraph.calculateCursorPosition(data.getLeft(), data.getMiddle());
assertEquals(data.getRight(), actual);
}
}
Expand Down