Last active
August 29, 2015 14:17
-
-
Save spoenemann/bd8d26e35d685d66cc64 to your computer and use it in GitHub Desktop.
DomainmodelProposalProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others. | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
*******************************************************************************/ | |
package org.eclipse.xtext.example.domainmodel.ui.contentassist | |
import com.google.inject.Inject | |
import de.cau.cs.kieler.core.properties.IProperty | |
import de.cau.cs.kieler.kiml.LayoutMetaDataService | |
import de.cau.cs.kieler.kiml.options.LayoutOptions | |
import java.util.HashSet | |
import org.eclipse.emf.ecore.EObject | |
import org.eclipse.jdt.core.Flags | |
import org.eclipse.xtext.Assignment | |
import org.eclipse.xtext.example.domainmodel.domainmodel.ConfigurationElement | |
import org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel | |
import org.eclipse.xtext.example.domainmodel.services.DomainmodelGrammarAccess | |
import org.eclipse.xtext.example.domainmodel.view.DomainModelDiagramSynthesis | |
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext | |
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor | |
class DomainmodelProposalProvider extends AbstractDomainmodelProposalProvider { | |
@Inject DomainmodelGrammarAccess grammmarAccess; | |
extension LayoutMetaDataService layoutMetaDataService = LayoutMetaDataService.instance | |
/** | |
* Provide completion proposals for the key of a configuration element. | |
*/ | |
def override completeConfigurationElement_Key(EObject model, Assignment assignment, | |
ContentAssistContext context, ICompletionProposalAcceptor acceptor) { | |
val domainModel = model as DomainModel | |
val declaredConfigKeys = new HashSet(domainModel.configElements.map[key]) | |
// Add proposals for synthesis options | |
for (field : typeof(DomainModelDiagramSynthesis).fields.filter[ | |
Flags.isStatic(modifiers) && typeof(IProperty).isAssignableFrom(type)]) { | |
val property = field.get(null) as IProperty<?> | |
if (!declaredConfigKeys.contains(property.id)) { | |
val proposal = valueConverter.toString(property.id, | |
grammmarAccess.qualifiedNameRule.name) | |
acceptor.accept(createCompletionProposal(proposal, context)) | |
} | |
} | |
// Add proposals for layout options | |
val selectedAlgo = domainModel.configElements.findFirst[ | |
!key.nullOrEmpty && LayoutOptions.ALGORITHM.id.endsWith(key) | |
]?.value?.algorithmDataBySuffix | |
val activeAlgo = selectedAlgo ?: "de.cau.cs.kieler.kiml.ogdf.planarization".algorithmData | |
optionData.filter[ layoutOption | | |
(activeAlgo == null || activeAlgo.knowsOption(layoutOption)) | |
&& !declaredConfigKeys.contains(layoutOption.id) | |
].forEach[ layoutOption | | |
val proposal = valueConverter.toString(layoutOption.id, grammmarAccess.qualifiedNameRule.name) | |
val displayString = layoutOption.id + " (" + layoutOption.name + ")" | |
acceptor.accept(createCompletionProposal(proposal, displayString, null, context)) | |
] | |
if (selectedAlgo == null) { | |
val displayString = LayoutOptions.ALGORITHM.id + " (Layout Algorithm)" | |
acceptor.accept(createCompletionProposal(LayoutOptions.ALGORITHM.id, displayString, null, context)) | |
} | |
} | |
/** | |
* Provide completion proposals for the value of a configuration element. | |
*/ | |
def override completeConfigurationElement_Value(EObject model, Assignment assignment, | |
ContentAssistContext context, ICompletionProposalAcceptor acceptor) { | |
val optionKey = (model as ConfigurationElement).key | |
val layoutOption = optionKey?.optionDataBySuffix | |
if (layoutOption != null) { | |
if (layoutOption == LayoutOptions.ALGORITHM) { | |
for (layoutAlgorithm : algorithmData) { | |
val proposal = valueConverter.toString(layoutAlgorithm.id, grammmarAccess.qualifiedNameRule.name) | |
val displayString = layoutAlgorithm.id + " (" + layoutAlgorithm.name + ")" | |
acceptor.accept(createCompletionProposal(proposal, displayString, null, context)) | |
} | |
} else if (layoutOption.choices.length == 0) { | |
val defValue = layoutOption.getDefault ?: layoutOption.defaultDefault | |
val proposal = if (defValue instanceof String) '\"' + defValue.toString + '\"' | |
else defValue.toString | |
acceptor.accept(createCompletionProposal(proposal, context)) | |
} else { | |
for (proposal : layoutOption.choices) { | |
acceptor.accept(createCompletionProposal(proposal, context)) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment