diff --git a/vscode-extensions/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/testharness/Editor.java b/vscode-extensions/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/testharness/Editor.java index 3ca3dc00c..cc615f898 100644 --- a/vscode-extensions/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/testharness/Editor.java +++ b/vscode-extensions/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/testharness/Editor.java @@ -10,13 +10,42 @@ import java.util.List; import javax.swing.text.BadLocationException; +import io.typefox.lsapi.CompletionItem; +import io.typefox.lsapi.CompletionList; import io.typefox.lsapi.Diagnostic; import io.typefox.lsapi.Position; import io.typefox.lsapi.PublishDiagnosticsParams; import io.typefox.lsapi.Range; +import io.typefox.lsapi.TextEdit; public class Editor { + static class EditorState { + String documentContents; + int selectionStart; + int selectionEnd; + + public EditorState(String text) { + selectionStart = text.indexOf(CURSOR); + if (selectionStart>=0) { + text = text.substring(0,selectionStart) + text.substring(selectionStart+CURSOR.length()); + selectionEnd = text.indexOf(CURSOR, selectionStart); + if (selectionEnd>=0) { + text = text.substring(0, selectionEnd) + text.substring(selectionEnd+CURSOR.length()); + } else { + selectionEnd = selectionStart; + } + } else { + //No CURSOR markers found + selectionStart = text.length(); + selectionEnd = text.length(); + } + this.documentContents = text; + } + } + + private static final String CURSOR = "<*>"; + private static final Comparator PROBLEM_COMPARATOR = new Comparator() { @Override public int compare(Diagnostic o1, Diagnostic o2) { @@ -35,11 +64,18 @@ public class Editor { private LanguageServerHarness harness; private TextDocumentInfo document; + private int selectionEnd; + + private int selectionStart; + public Editor(LanguageServerHarness harness, String contents) throws Exception { this.harness = harness; - this.document = harness.openDocument(harness.createWorkingCopy(contents)); + EditorState state = new EditorState(contents); + this.document = harness.openDocument(harness.createWorkingCopy(state.documentContents)); + this.selectionStart = state.selectionStart; + this.selectionEnd = state.selectionEnd; } - + /** * Check that a 'expectedProblems' are found by the reconciler. Expected problems are * specified by string of the form "${badSnippet}|${messageSnippet}". The badSnippet @@ -84,15 +120,40 @@ public class Editor { } return buf.toString(); } + + /** + * Get the editor text, with cursor markers inserted (for easy textual comparison + * after applying a proposal) + */ + public String getText() { + String text = document.getText(); + text = text.substring(0, selectionEnd) + CURSOR + text.substring(selectionEnd); + if (selectionStart getCompletions() throws Exception { + CompletionList cl = harness.getCompletions(this.document, this.getCursor()); + return cl.getItems(); + } + + private Position getCursor() { + return document.toPosition(selectionStart); } public void assertIsHoverRegion(String string) {