Search in sources :

Example 1 with CompletionItem

use of org.graalvm.tools.lsp.server.types.CompletionItem in project graal by oracle.

the class CompletionTest method objectPropertyCompletionViaCoverageData.

@Test
public void objectPropertyCompletionViaCoverageData() throws InterruptedException, ExecutionException {
    URI uri = createDummyFileUriForSL();
    Future<?> future = truffleAdapter.parse(PROG_OBJ_NOT_CALLED, "sl", uri);
    future.get();
    Future<Boolean> futureCoverage = truffleAdapter.runCoverageAnalysis(uri);
    futureCoverage.get();
    setTriggerCharacters();
    replace(uri, Range.create(Position.create(8, 12), Position.create(8, 12)), ".", "extraneous input '.'");
    Future<CompletionList> futureC = truffleAdapter.completion(uri, 8, 13, null);
    CompletionList completionList = futureC.get();
    assertEquals(1, completionList.getItems().size());
    CompletionItem item = completionList.getItems().get(0);
    assertEquals("p", item.getLabel());
    assertEquals("Number", item.getDetail());
    assertEquals(CompletionItemKind.Property, item.getKind());
}
Also used : CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) URI(java.net.URI) Test(org.junit.Test)

Example 2 with CompletionItem

use of org.graalvm.tools.lsp.server.types.CompletionItem in project graal by oracle.

the class CompletionTest method checkEmpty.

private void checkEmpty(URI uri, int line, int column) throws InterruptedException, ExecutionException {
    Future<CompletionList> futureCompletions = truffleAdapter.completion(uri, line, column, null);
    CompletionList completionList = futureCompletions.get();
    assertFalse(completionList.isIncomplete());
    List<CompletionItem> items = completionList.getItems();
    assertTrue(items.isEmpty());
}
Also used : CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem)

Example 3 with CompletionItem

use of org.graalvm.tools.lsp.server.types.CompletionItem in project graal by oracle.

the class CompletionTest method checkGlobalsAndLocals.

private int checkGlobalsAndLocals(URI uri, int line, int column, int numberOfGlobalsItems, Object... vars) throws InterruptedException, ExecutionException {
    Future<CompletionList> futureCompletions = truffleAdapter.completion(uri, line, column, null);
    CompletionList completionList = futureCompletions.get();
    assertFalse(completionList.isIncomplete());
    List<CompletionItem> items = completionList.getItems();
    assertFalse(items.isEmpty());
    NodeInfo nodeInfo = SLLanguage.lookupNodeInfo(SLHelloEqualsWorldBuiltin.class);
    assertNotNull(nodeInfo);
    String shortName = nodeInfo.shortName();
    assertTrue("Built-in function " + shortName + " not found.", items.stream().anyMatch(item -> item.getLabel().startsWith(shortName)));
    for (int i = 0; i < vars.length; i += 2) {
        String var = (String) vars[i];
        boolean present = (boolean) vars[i + 1];
        if (present) {
            assertTrue(var + " should be found in function scope", items.stream().anyMatch(item -> item.getLabel().startsWith(var)));
        } else {
            assertTrue(var + " should not be found in main-function scope", items.stream().noneMatch(item -> item.getLabel().startsWith(var)));
        }
    }
    if (numberOfGlobalsItems != -1) {
        assertEquals(numberOfGlobalsItems, items.size());
    }
    return items.size();
}
Also used : Arrays(java.util.Arrays) Position(org.graalvm.tools.lsp.server.types.Position) NodeInfo(com.oracle.truffle.api.nodes.NodeInfo) Future(java.util.concurrent.Future) CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) SLLanguage(com.oracle.truffle.sl.SLLanguage) CompletionItemKind(org.graalvm.tools.lsp.server.types.CompletionItemKind) PublishDiagnosticsParams(org.graalvm.tools.lsp.server.types.PublishDiagnosticsParams) DiagnosticsNotification(org.graalvm.tools.lsp.exceptions.DiagnosticsNotification) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ServerCapabilities(org.graalvm.tools.lsp.server.types.ServerCapabilities) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Range(org.graalvm.tools.lsp.server.types.Range) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) CompletionOptions(org.graalvm.tools.lsp.server.types.CompletionOptions) SLHelloEqualsWorldBuiltin(com.oracle.truffle.sl.builtins.SLHelloEqualsWorldBuiltin) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) NodeInfo(com.oracle.truffle.api.nodes.NodeInfo)

Example 4 with CompletionItem

use of org.graalvm.tools.lsp.server.types.CompletionItem in project graal by oracle.

the class CompletionRequestHandler method fillCompletionsFromTruffleObject.

protected boolean fillCompletionsFromTruffleObject(List<CompletionItem> completions, LanguageInfo langInfo, Object object) {
    if (object == null) {
        return false;
    }
    Object metaObject = getMetaObject(langInfo, object);
    if (metaObject == null) {
        return false;
    }
    Object languageView = env.getLanguageView(langInfo, object);
    Object members = null;
    if (INTEROP.hasMembers(languageView)) {
        try {
            members = INTEROP.getMembers(languageView);
        } catch (UnsupportedMessageException ex) {
        // No members
        }
    }
    if (members == null || !INTEROP.hasArrayElements(members)) {
        logger.fine("No completions found for object: " + languageView);
        return false;
    }
    int counter = 0;
    long size;
    try {
        size = INTEROP.getArraySize(members);
    } catch (UnsupportedMessageException ex) {
        size = 0;
    }
    for (long i = 0; i < size; i++) {
        String key;
        Object value;
        try {
            key = INTEROP.readArrayElement(members, i).toString();
            if (INTEROP.isMemberReadable(languageView, key)) {
                value = INTEROP.readMember(languageView, key);
            } else {
                value = null;
            }
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable t) {
            logger.log(Level.CONFIG, languageView.toString(), t);
            continue;
        }
        CompletionItem completion = CompletionItem.create(key);
        ++counter;
        // Keep the order in which the keys were provided
        completion.setSortText(String.format("%s%06d.%s", "+", counter, key));
        completion.setKind(CompletionItemKind.Property);
        completion.setDetail(createCompletionDetail(value, langInfo));
        try {
            completion.setDocumentation(createDocumentation(value, langInfo, "of " + INTEROP.getMetaQualifiedName(metaObject)));
        } catch (UnsupportedMessageException e) {
            throw new AssertionError(e);
        }
        completions.add(completion);
    }
    return counter > 0;
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 5 with CompletionItem

use of org.graalvm.tools.lsp.server.types.CompletionItem in project graal by oracle.

the class CompletionTest method objectPropertyCompletionLocalFile.

@Test
public void objectPropertyCompletionLocalFile() throws InterruptedException, ExecutionException {
    URI uri = createDummyFileUriForSL();
    Future<?> future = truffleAdapter.parse(PROG_OBJ_NOT_CALLED, "sl", uri);
    future.get();
    setTriggerCharacters();
    replace(uri, Range.create(Position.create(2, 12), Position.create(2, 12)), ".", "extraneous input '.'");
    Future<CompletionList> futureC = truffleAdapter.completion(uri, 2, 13, null);
    CompletionList completionList = futureC.get();
    assertEquals(1, completionList.getItems().size());
    CompletionItem item = completionList.getItems().get(0);
    assertEquals("p", item.getLabel());
    assertEquals("Number", item.getDetail());
    assertEquals(CompletionItemKind.Property, item.getKind());
    replace(uri, Range.create(Position.create(2, 12), Position.create(2, 13)), "", null);
    replace(uri, Range.create(Position.create(12, 7), Position.create(12, 7)), ".", "missing IDENTIFIER");
    futureC = truffleAdapter.completion(uri, 12, 8, null);
    try {
        futureC.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof DiagnosticsNotification);
    }
}
Also used : CompletionList(org.graalvm.tools.lsp.server.types.CompletionList) CompletionItem(org.graalvm.tools.lsp.server.types.CompletionItem) DiagnosticsNotification(org.graalvm.tools.lsp.exceptions.DiagnosticsNotification) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Test(org.junit.Test)

Aggregations

CompletionItem (org.graalvm.tools.lsp.server.types.CompletionItem)6 CompletionList (org.graalvm.tools.lsp.server.types.CompletionList)5 URI (java.net.URI)4 DiagnosticsNotification (org.graalvm.tools.lsp.exceptions.DiagnosticsNotification)3 Test (org.junit.Test)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 CompletionItemKind (org.graalvm.tools.lsp.server.types.CompletionItemKind)2 CallTarget (com.oracle.truffle.api.CallTarget)1 CompilerDirectives (com.oracle.truffle.api.CompilerDirectives)1 MaterializedFrame (com.oracle.truffle.api.frame.MaterializedFrame)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 NodeLibrary (com.oracle.truffle.api.interop.NodeLibrary)1