Search in sources :

Example 1 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method completion.

/**
     * Returns a list of all items to display in the completion list at a
     * specific position in a document. Called automatically by VSCode as the
     * user types, and may not necessarily be triggered only on "." or ":".
     */
@Override
public CompletableFuture<CompletionList> completion(TextDocumentPositionParams position) {
    String textDocumentUri = position.getTextDocument().getUri();
    if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
        CompletionList result = new CompletionList();
        result.setIsIncomplete(false);
        result.setItems(new ArrayList<>());
        return CompletableFuture.completedFuture(result);
    }
    IMXMLTagData offsetTag = getOffsetMXMLTag(position);
    if (offsetTag != null) {
        IASNode embeddedNode = getEmbeddedActionScriptNodeInMXMLTag(offsetTag, currentOffset, position);
        if (embeddedNode != null) {
            return actionScriptCompletionWithNode(position, embeddedNode);
        }
        //so that's why we call isMXMLTagValidForCompletion()
        if (isMXMLTagValidForCompletion(offsetTag)) {
            return mxmlCompletion(position, offsetTag);
        }
    }
    if (offsetTag == null && position.getTextDocument().getUri().endsWith(MXML_EXTENSION)) {
        //it's possible for the offset tag to be null in an MXML file, but
        //we don't want to trigger ActionScript completion.
        //for some reason, the offset tag will be null if completion is
        //triggered at the asterisk:
        //<fx:Declarations>*
        CompletionList result = new CompletionList();
        result.setIsIncomplete(false);
        result.setItems(new ArrayList<>());
        return CompletableFuture.completedFuture(result);
    }
    return actionScriptCompletion(position);
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) IASNode(org.apache.flex.compiler.tree.as.IASNode) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData)

Example 2 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method mxmlCompletion.

private CompletableFuture<CompletionList> mxmlCompletion(TextDocumentPositionParams position, IMXMLTagData offsetTag) {
    CompletionList result = new CompletionList();
    result.setIsIncomplete(false);
    result.setItems(new ArrayList<>());
    if (isInXMLComment(position)) {
        //if we're inside a comment, no completion!
        return CompletableFuture.completedFuture(result);
    }
    IMXMLTagData parentTag = offsetTag.getParentTag();
    //for some reason, the attributes list includes the >, but that's not
    //what we want here, so check if currentOffset isn't the end of the tag!
    boolean isAttribute = offsetTag.isOffsetInAttributeList(currentOffset) && currentOffset < offsetTag.getAbsoluteEnd();
    if (isAttribute && offsetTag.isCloseTag()) {
        return CompletableFuture.completedFuture(result);
    }
    //inside <fx:Declarations>
    if (isDeclarationsTag(offsetTag)) {
        if (!isAttribute) {
            autoCompleteTypesForMXML(result);
        }
        return CompletableFuture.completedFuture(result);
    }
    IDefinition offsetDefinition = getDefinitionForMXMLTag(offsetTag);
    if (offsetDefinition == null) {
        IDefinition parentDefinition = null;
        if (parentTag != null) {
            parentDefinition = getDefinitionForMXMLTag(parentTag);
        }
        if (parentDefinition != null) {
            if (parentDefinition instanceof IClassDefinition) {
                IClassDefinition classDefinition = (IClassDefinition) parentDefinition;
                String offsetPrefix = offsetTag.getPrefix();
                if (offsetPrefix.length() == 0 || parentTag.getPrefix().equals(offsetPrefix)) {
                    //only add members if the prefix is the same as the
                    //parent tag. members can't have different prefixes.
                    //also allow members when we don't have a prefix.
                    addMembersForMXMLTypeToAutoComplete(classDefinition, parentTag, offsetPrefix.length() == 0, result);
                }
                if (!isAttribute) {
                    //tags can't appear in attributes, so skip types
                    String defaultPropertyName = classDefinition.getDefaultPropertyName(currentProject);
                    if (defaultPropertyName != null) {
                        //only add types if the class defines [DefaultProperty]
                        //metadata
                        autoCompleteTypesForMXMLFromExistingTag(result, offsetTag);
                    }
                }
            } else {
                //the parent is something like a property, so matching the
                //prefix is not required
                autoCompleteTypesForMXMLFromExistingTag(result, offsetTag);
            }
            return CompletableFuture.completedFuture(result);
        } else if (isDeclarationsTag(parentTag)) {
            autoCompleteTypesForMXMLFromExistingTag(result, offsetTag);
            return CompletableFuture.completedFuture(result);
        }
        return CompletableFuture.completedFuture(result);
    }
    if (offsetDefinition instanceof IClassDefinition) {
        IMXMLTagAttributeData attribute = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
        if (attribute != null) {
            return mxmlAttributeCompletion(offsetTag, result);
        }
        IClassDefinition classDefinition = (IClassDefinition) offsetDefinition;
        addMembersForMXMLTypeToAutoComplete(classDefinition, offsetTag, !isAttribute, result);
        String defaultPropertyName = classDefinition.getDefaultPropertyName(currentProject);
        if (defaultPropertyName != null && !isAttribute) {
            //if [DefaultProperty] is set, then we can instantiate
            //types as child elements
            //but we don't want to do that when in an attribute
            autoCompleteTypesForMXML(result);
        }
        return CompletableFuture.completedFuture(result);
    }
    if (offsetDefinition instanceof IVariableDefinition || offsetDefinition instanceof IEventDefinition || offsetDefinition instanceof IStyleDefinition) {
        if (!isAttribute) {
            autoCompleteTypesForMXML(result);
        }
        return CompletableFuture.completedFuture(result);
    }
    System.err.println("Unknown definition for MXML completion: " + offsetDefinition.getClass());
    return CompletableFuture.completedFuture(result);
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IVariableDefinition(org.apache.flex.compiler.definitions.IVariableDefinition) IStyleDefinition(org.apache.flex.compiler.definitions.IStyleDefinition) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) IEventDefinition(org.apache.flex.compiler.definitions.IEventDefinition) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Example 3 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project xtext-core by eclipse.

the class LanguageServerImpl method completion.

protected Either<List<CompletionItem>, CompletionList> completion(final CancelIndicator origialCancelIndicator, final TextDocumentPositionParams params) {
    final LanguageServerImpl.BufferedCancelIndicator cancelIndicator = new LanguageServerImpl.BufferedCancelIndicator(origialCancelIndicator);
    final URI uri = this._uriExtensions.toUri(params.getTextDocument().getUri());
    final IResourceServiceProvider resourceServiceProvider = this.languagesRegistry.getResourceServiceProvider(uri);
    ContentAssistService _get = null;
    if (resourceServiceProvider != null) {
        _get = resourceServiceProvider.<ContentAssistService>get(ContentAssistService.class);
    }
    final ContentAssistService contentAssistService = _get;
    if ((contentAssistService == null)) {
        CompletionList _completionList = new CompletionList();
        return Either.<List<CompletionItem>, CompletionList>forRight(_completionList);
    }
    final Function2<Document, XtextResource, CompletionList> _function = (Document document, XtextResource resource) -> {
        return contentAssistService.createCompletionList(document, resource, params, cancelIndicator);
    };
    final CompletionList completionList = this.workspaceManager.<CompletionList>doRead(uri, _function);
    return Either.<List<CompletionItem>, CompletionList>forRight(completionList);
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) ContentAssistService(org.eclipse.xtext.ide.server.contentassist.ContentAssistService) ArrayList(java.util.ArrayList) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) XtextResource(org.eclipse.xtext.resource.XtextResource) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI)

Example 4 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project xtext-core by eclipse.

the class ContentAssistService method createCompletionList.

public CompletionList createCompletionList(final Document document, final XtextResource resource, final TextDocumentPositionParams params, final CancelIndicator cancelIndicator) {
    try {
        final CompletionList result = new CompletionList();
        result.setIsIncomplete(true);
        final IdeContentProposalAcceptor acceptor = this.proposalAcceptorProvider.get();
        final int caretOffset = document.getOffSet(params.getPosition());
        final Position caretPosition = params.getPosition();
        final TextRegion position = new TextRegion(caretOffset, 0);
        try {
            this.createProposals(document.getContents(), position, caretOffset, resource, acceptor);
        } catch (final Throwable _t) {
            if (_t instanceof Throwable) {
                final Throwable t = (Throwable) _t;
                boolean _isOperationCanceledException = this.operationCanceledManager.isOperationCanceledException(t);
                boolean _not = (!_isOperationCanceledException);
                if (_not) {
                    throw t;
                }
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
        final Procedure2<ContentAssistEntry, Integer> _function = (ContentAssistEntry it, Integer idx) -> {
            final CompletionItem item = this.toCompletionItem(it, caretOffset, caretPosition, document);
            item.setSortText(Strings.padStart(Integer.toString((idx).intValue()), 5, '0'));
            List<CompletionItem> _items = result.getItems();
            _items.add(item);
        };
        IterableExtensions.<ContentAssistEntry>forEach(acceptor.getEntries(), _function);
        return result;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) TextRegion(org.eclipse.xtext.util.TextRegion) Position(org.eclipse.lsp4j.Position) CompletionItem(org.eclipse.lsp4j.CompletionItem) CompletionList(org.eclipse.lsp4j.CompletionList) List(java.util.List) IdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalAcceptor) IIdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor)

Example 5 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandler method completion.

Either<List<CompletionItem>, CompletionList> completion(TextDocumentPositionParams position, IProgressMonitor monitor) {
    List<CompletionItem> completionItems = null;
    try {
        ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
        completionItems = this.computeContentAssist(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor);
    } catch (OperationCanceledException ignorable) {
        // No need to pollute logs when query is cancelled
        monitor.setCanceled(true);
    } catch (Exception e) {
        JavaLanguageServerPlugin.logException("Problem with codeComplete for " + position.getTextDocument().getUri(), e);
        monitor.setCanceled(true);
    }
    CompletionList $ = new CompletionList();
    if (monitor.isCanceled()) {
        $.setIsIncomplete(true);
        completionItems = null;
        JavaLanguageServerPlugin.logInfo("Completion request cancelled");
    } else {
        JavaLanguageServerPlugin.logInfo("Completion request completed");
    }
    $.setItems(completionItems == null ? Collections.emptyList() : completionItems);
    return Either.forRight($);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

Aggregations

CompletionList (org.eclipse.lsp4j.CompletionList)44 CompletionItem (org.eclipse.lsp4j.CompletionItem)36 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)33 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)11 TextEdit (org.eclipse.lsp4j.TextEdit)11 List (java.util.List)10 Lsp4jAssertions.assertTextEdit (org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit)10 Range (org.eclipse.lsp4j.Range)9 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)7 Map (java.util.Map)6 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)6 Collections (java.util.Collections)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Path (org.eclipse.core.runtime.Path)5 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)5 DidChangeTextDocumentParams (org.eclipse.lsp4j.DidChangeTextDocumentParams)5 Comparator (java.util.Comparator)4