Java Code Examples for org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry#getProposal()

The following examples show how to use org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry#getProposal() . 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: XIdeContentProposalAcceptor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void accept(ContentAssistEntry entry, int priority) {
	if (entry != null) {
		if (entry.getProposal() == null) {
			throw new IllegalArgumentException("proposal must not be null.");
		}
		String entryString = entry.toString();
		if (entries.containsKey(entryString)) {
			Pair<Integer, ContentAssistEntry> existingProposal = entries.get(entryString);
			priority = existingProposal.getKey();
		}
		entries.put(entryString, Pair.of(priority, entry));
	}
}
 
Example 2
Source File: XContentAssistService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Convert to a completion item.
 */
protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset,
		Position caretPosition, Document document) {
	CompletionItem result = new CompletionItem();
	String label = null;
	if (entry.getLabel() != null) {
		label = entry.getLabel();
	} else {
		label = entry.getProposal();
	}
	result.setLabel(label);
	result.setDetail(entry.getDescription());
	result.setDocumentation(entry.getDocumentation());
	String prefix = null;
	if (entry.getPrefix() != null) {
		prefix = entry.getPrefix();
	} else {
		prefix = "";
	}
	Position prefixPosition = document.getPosition(caretOffset - prefix.length());
	result.setTextEdit(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal()));
	if (!entry.getTextReplacements().isEmpty()) {
		if (result.getAdditionalTextEdits() == null) {
			result.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size()));
		}
		entry.getTextReplacements().forEach(it -> {
			result.getAdditionalTextEdits().add(toTextEdit(it, document));
		});
	}
	if (entry.getKind().startsWith(ContentAssistEntry.KIND_SNIPPET + ":")) {
		result.setInsertTextFormat(InsertTextFormat.Snippet);
		entry.setKind(entry.getKind().substring(ContentAssistEntry.KIND_SNIPPET.length() + 1));
	} else if (Objects.equal(entry.getKind(), ContentAssistEntry.KIND_SNIPPET)) {
		result.setInsertTextFormat(InsertTextFormat.Snippet);
	}
	result.setKind(translateKind(entry));
	return result;
}
 
Example 3
Source File: UiToIdeContentProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected StyledString getDisplayString(ContentAssistEntry entry) {
	StyledString result = new StyledString(entry.getLabel() != null ? entry.getLabel() : entry.getProposal());
	if (!Strings.isNullOrEmpty(entry.getDescription())) {
		result.append(new StyledString(" \u2013 " + entry.getDescription(), StyledString.QUALIFIER_STYLER));
	}
	return result;
}
 
Example 4
Source File: ContentAssistService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition,
		Document document) {
	CompletionItem completionItem = new CompletionItem();
	String label = entry.getLabel();
	if (label == null) {
		label = entry.getProposal();
	}
	completionItem.setLabel(label);
	completionItem.setDetail(entry.getDescription());
	completionItem.setDocumentation(entry.getDocumentation());
	String prefix = entry.getPrefix();
	if (prefix == null) {
		prefix = "";
	}
	int prefixOffset = caretOffset - prefix.length();
	Position prefixPosition = document.getPosition(prefixOffset);
	completionItem.setTextEdit(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal()));
	completionItem.setKind(translateKind(entry));
	if (!entry.getTextReplacements().isEmpty()) {
		if (completionItem.getAdditionalTextEdits() == null) {
			completionItem.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size()));
		}
		entry.getTextReplacements().forEach(
				(ReplaceRegion it) -> completionItem.getAdditionalTextEdits().add(toTextEdit(it, document)));
	}
	if (ContentAssistEntry.KIND_SNIPPET.equals(entry.getKind())) {
		completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
	}
	return completionItem;
}
 
Example 5
Source File: ContentAssistService.java    From xtext-web with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Invoke the proposal provider and put the results into a
 * {@link ContentAssistResult} object.
 */
protected ContentAssistResult createProposals(List<ContentAssistContext> contexts, String stateId,
		int proposalsLimit) {
	ContentAssistResult result = new ContentAssistResult(stateId);
	if (!contexts.isEmpty()) {
		Set<Pair<Integer, ContentAssistEntry>> proposals = new HashSet<Pair<Integer, ContentAssistEntry>>();
		IIdeContentProposalAcceptor acceptor = new IIdeContentProposalAcceptor() {
			@Override
			public void accept(ContentAssistEntry entry, int priority) {
				if (entry != null) {
					if (entry.getProposal() == null) {
						throw new IllegalArgumentException("proposal must not be null.");
					}
					proposals.add(Pair.of(priority, entry));
				}
			}

			@Override
			public boolean canAcceptMoreProposals() {
				return proposals.size() < proposalsLimit;
			}
		};
		proposalProvider.createProposals(contexts, acceptor);
		List<Pair<Integer, ContentAssistEntry>> sorted = IterableExtensions.sortWith(proposals,
				(Pair<Integer, ContentAssistEntry> p1, Pair<Integer, ContentAssistEntry> p2) -> {
					int prioResult = p2.getKey().compareTo(p1.getKey());
					if (prioResult != 0) {
						return prioResult;
					}
					String s1 = p1.getValue().getLabel();
					if (s1 == null) {
						s1 = p1.getValue().getProposal();
					}
					String s2 = p2.getValue().getLabel();
					if (s2 == null) {
						s2 = p2.getValue().getProposal();
					}
					return s1.compareTo(s2);
				});
		result.getEntries()
				.addAll(Lists.transform(sorted, (Pair<Integer, ContentAssistEntry> it) -> it.getValue()));
	}
	return result;
}