Java Code Examples for org.eclipse.lsp4j.CompletionList#setIsIncomplete()

The following examples show how to use org.eclipse.lsp4j.CompletionList#setIsIncomplete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XContentAssistService.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create the completion proposals.
 */
public CompletionList createCompletionList(Document document, XtextResource resource,
		TextDocumentPositionParams params, CancelIndicator cancelIndicator) {
	CompletionList result = new CompletionList();
	result.setIsIncomplete(false);
	// we set isInComplete to true, so we get asked always, which is the best match to the expected behavior in
	// Xtext
	XIdeContentProposalAcceptor acceptor = new XIdeContentProposalAcceptor(cancelIndicator);
	int caretOffset = document.getOffSet(params.getPosition());
	Position caretPosition = params.getPosition();
	TextRegion position = new TextRegion(caretOffset, 0);
	try {
		createProposals(document.getContents(), position, caretOffset, resource, acceptor);
	} catch (Throwable t) {
		if (!operationCanceledManager.isOperationCanceledException(t)) {
			throw t;
		}
	}
	operationCanceledManager.checkCanceled(cancelIndicator);
	IterableExtensions.forEach(acceptor.getEntries(), (it, idx) -> {
		CompletionItem item = toCompletionItem(it, caretOffset, caretPosition, document);
		item.setSortText(Strings.padStart(Integer.toString(idx.intValue()), 5, '0'));
		result.getItems().add(item);
	});
	return result;
}
 
Example 2
Source File: LauncherTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRequest() throws Exception {
	CompletionParams p = new CompletionParams();
	p.setPosition(new Position(1,1));
	p.setTextDocument(new TextDocumentIdentifier("test/foo.txt"));
	
	CompletionList result = new CompletionList();
	result.setIsIncomplete(true);
	result.setItems(new ArrayList<>());
	
	CompletionItem item = new CompletionItem();
	item.setDetail("test");
	item.setDocumentation("doc");
	item.setFilterText("filter");
	item.setInsertText("insert");
	item.setKind(CompletionItemKind.Field);
	result.getItems().add(item);
	
	server.expectedRequests.put("textDocument/completion", new Pair<>(p, result));
	CompletableFuture<Either<List<CompletionItem>, CompletionList>> future = clientLauncher.getRemoteProxy().getTextDocumentService().completion(p);
	Assert.assertEquals(Either.forRight(result).toString(), future.get(TIMEOUT, TimeUnit.MILLISECONDS).toString());
	client.joinOnEmpty();
}
 
Example 3
Source File: ContentAssistService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public CompletionList createCompletionList(Document document, XtextResource resource, CompletionParams params,
		CancelIndicator cancelIndicator) {
	try {
		CompletionList result = new CompletionList();
		result.setIsIncomplete(true);
		IdeContentProposalAcceptor acceptor = proposalAcceptorProvider.get();
		int caretOffset = document.getOffSet(params.getPosition());
		Position caretPosition = params.getPosition();
		TextRegion position = new TextRegion(caretOffset, 0);
		try {
			createProposals(document.getContents(), position, caretOffset, resource, acceptor);
		} catch (Throwable t) {
			if (!operationCanceledManager.isOperationCanceledException(t)) {
				throw t;
			}
		}
		int idx = 0;
		for (ContentAssistEntry it : acceptor.getEntries()) {
			CompletionItem item = toCompletionItem(it, caretOffset, caretPosition, document);
			item.setSortText(Strings.padStart(Integer.toString(idx), 5, '0'));
			result.getItems().add(item);
			idx++;
		}
		return result;
	} catch (Throwable e) {
		throw Exceptions.sneakyThrow(e);
	}
}
 
Example 4
Source File: CompletionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private CompletionList computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException {
	CompletionResponses.clear();
	if (unit == null) {
		return null;
	}
	List<CompletionItem> proposals = new ArrayList<>();

	final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
	CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset, manager);
	// Allow completions for unresolved types - since 3.3
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);

	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);

	collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);

	collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);

	collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
	collector.setFavoriteReferences(getFavoriteStaticMembers());

	if (offset >-1 && !monitor.isCanceled()) {
		IBuffer buffer = unit.getBuffer();
		if (buffer != null && buffer.getLength() >= offset) {
			IProgressMonitor subMonitor = new ProgressMonitorWrapper(monitor) {
				private long timeLimit;
				private final long TIMEOUT = Long.getLong("completion.timeout", 5000);

				@Override
				public void beginTask(String name, int totalWork) {
					timeLimit = System.currentTimeMillis() + TIMEOUT;
				}

				@Override
				public boolean isCanceled() {
					return super.isCanceled() || timeLimit <= System.currentTimeMillis();
				}

			};
			try {
				if (isIndexEngineEnabled()) {
					unit.codeComplete(offset, collector, subMonitor);
				} else {
					ModelBasedCompletionEngine.codeComplete(unit, offset, collector, DefaultWorkingCopyOwner.PRIMARY, subMonitor);
				}
				proposals.addAll(collector.getCompletionItems());
				if (isSnippetStringSupported() && !UNSUPPORTED_RESOURCES.contains(unit.getResource().getName())) {
					proposals.addAll(SnippetCompletionProposal.getSnippets(unit, collector.getContext(), subMonitor));
				}
				proposals.addAll(new JavadocCompletionProposal().getProposals(unit, offset, collector, subMonitor));
			} catch (OperationCanceledException e) {
				monitor.setCanceled(true);
			}
		}
	}
	proposals.sort(PROPOSAL_COMPARATOR);
	CompletionList list = new CompletionList(proposals);
	list.setIsIncomplete(!collector.isComplete());
	return list;
}