use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.
the class CodetemplatesProposalProvider method completeVariable_Name.
@Override
public void completeVariable_Name(EObject model, Assignment assignment, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
if ((mode & NORMAL) != 0) {
{
String proposalText = "variable";
StyledString displayText = new StyledString(proposalText).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setSelectionStart(configurable.getReplacementOffset());
configurable.setSelectionLength(proposalText.length());
configurable.setAutoInsertable(false);
configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
}
acceptor.accept(proposal);
}
TemplateData data = new TemplateData(model);
if (data.doCreateProposals()) {
ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
if (helper != null) {
String contextTypeId = helper.getId(data.rule);
ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
if (contextType != null) {
Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
while (resolvers.hasNext()) {
final TemplateVariableResolver resolver = resolvers.next();
IInspectableTemplateVariableResolver inspectableResolver = templateVariableResolverRegistry.toInspectableResolver(resolver);
if (inspectableResolver != null) {
if (!inspectableResolver.hasMandatoryParameters()) {
String type = resolver.getType();
StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
acceptor.accept(createCompletionProposal(type, displayString, null, context));
}
String proposalText = "variable:" + resolver.getType();
StyledString displayText = new StyledString("variable").append(":" + resolver.getType(), StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
if (inspectableResolver.hasMandatoryParameters())
proposalText = proposalText + "()";
Builder builder = context.copy();
PrefixMatcher newMatcher = new PrefixMatcher() {
@Override
public boolean isCandidateMatchingPrefix(String name, String prefix) {
return context.getMatcher().isCandidateMatchingPrefix(name, prefix) || context.getMatcher().isCandidateMatchingPrefix("variable:" + resolver.getType(), prefix) || context.getMatcher().isCandidateMatchingPrefix(resolver.getType(), prefix);
}
};
builder.setMatcher(newMatcher);
ContentAssistContext myContext = builder.toContext();
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, myContext);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setSelectionStart(configurable.getReplacementOffset());
configurable.setSelectionLength("variable".length());
configurable.setAutoInsertable(false);
if (inspectableResolver.hasMandatoryParameters()) {
configurable.setCursorPosition(proposalText.length() - 1);
}
configurable.setSimpleLinkedMode(myContext.getViewer(), '\t');
}
acceptor.accept(proposal);
} else {
String type = resolver.getType();
StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
acceptor.accept(createCompletionProposal(type, displayString, null, context));
}
}
}
}
if (data.template.getBody() != null) {
for (Variable variable : Iterables.filter(data.template.getBody().getParts(), Variable.class)) {
if (variable != model && variable.getName() != null) {
String proposalText = variable.getName();
StyledString displayText = new StyledString(proposalText).append(" - existing variable", StyledString.QUALIFIER_STYLER);
if (variable.getType() != null)
displayText = displayText.append(" of type " + variable.getType(), StyledString.QUALIFIER_STYLER);
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
acceptor.accept(proposal);
}
}
}
}
}
}
use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.
the class SemanticHighlighter method provideHighlightingFor.
@Override
public void provideHighlightingFor(XtextResource resource, final IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
if (resource == null || resource.getContents().isEmpty())
return;
Codetemplates templates = (Codetemplates) resource.getContents().get(0);
Grammar grammar = templates.getLanguage();
if (grammar != null && !grammar.eIsProxy()) {
TemplateBodyHighlighter highlighter = getHighlighter(grammar);
if (highlighter != null) {
ContextTypeIdHelper helper = registry.getContextTypeIdHelper(grammar);
ContextTypeRegistry contextTypeRegistry = registry.getContextTypeRegistry(grammar);
for (Codetemplate template : templates.getTemplates()) {
operationCanceledManager.checkCanceled(cancelIndicator);
if (template.getBody() != null) {
final EvaluatedTemplate evaluatedTemplate = new EvaluatedTemplate(template);
highlighter.provideHighlightingFor(evaluatedTemplate.getMappedString(), new IHighlightedPositionAcceptor() {
@Override
public void addPosition(int offset, int length, String... id) {
int beginOffset = evaluatedTemplate.getOriginalOffset(offset);
int endOffset = evaluatedTemplate.getOriginalOffset(offset + length);
int fixedLength = endOffset - beginOffset;
acceptor.addPosition(beginOffset, fixedLength, id);
}
});
String id = null;
TemplateContextType contextType = null;
if (template.getContext() != null) {
id = helper.getId(template.getContext());
if (id != null)
contextType = contextTypeRegistry.getContextType(id);
}
Set<String> defaultResolvers = Sets.newHashSet();
if (contextType != null) {
Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
while (resolvers.hasNext()) {
TemplateVariableResolver resolver = resolvers.next();
defaultResolvers.add(resolver.getType());
}
}
for (TemplatePart part : template.getBody().getParts()) {
if (part instanceof Variable) {
Variable variable = (Variable) part;
ICompositeNode node = NodeModelUtils.findActualNodeFor(variable);
if (node != null) {
for (ILeafNode leafNode : node.getLeafNodes()) {
if (leafNode.getGrammarElement() instanceof Keyword) {
acceptor.addPosition(leafNode.getTotalOffset(), leafNode.getTotalLength(), TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE);
}
}
List<INode> typeNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__TYPE);
if (typeNodes.isEmpty()) {
if (defaultResolvers.contains(variable.getName())) {
List<INode> nameNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__NAME);
for (INode nameNode : nameNodes) {
highlightNode(nameNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE, acceptor);
}
}
} else {
for (INode typeNode : typeNodes) {
highlightNode(typeNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE, acceptor);
}
}
List<INode> parameterNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__PARAMETERS);
for (INode parameterNode : parameterNodes) {
highlightNode(parameterNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE_ARGUMENT, acceptor);
}
}
}
}
}
}
}
}
}
use of org.eclipse.jface.text.templates.TemplateContextType in project titan.EclipsePlug-ins by eclipse.
the class ContentAssistProcessor method computeCompletionProposals.
// FIXME add semantic check guard on project level.
@Override
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
if (editor == null) {
return new ICompletionProposal[] {};
}
IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
if (file == null) {
return new ICompletionProposal[] {};
}
IDocument doc = viewer.getDocument();
TTCN3ReferenceParser refParser = new TTCN3ReferenceParser(true);
Reference ref = refParser.findReferenceForCompletion(file, offset, doc);
if (ref == null || ref.getSubreferences().isEmpty()) {
return new ICompletionProposal[] {};
}
Scope scope = null;
ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
Module tempModule = projectSourceParser.containedModule(file);
String moduleName = null;
if (tempModule != null) {
moduleName = tempModule.getName();
scope = tempModule.getSmallestEnclosingScope(refParser.getReplacementOffset());
ref.setMyScope(scope);
ref.detectModid();
}
IPreferencesService prefs = Platform.getPreferencesService();
if (prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null)) {
TITANDebugConsole.println("parsed the reference: " + ref);
}
TemplateContextType contextType = new TemplateContextType(TTCN3CodeSkeletons.CONTEXT_IDENTIFIER, TTCN3CodeSkeletons.CONTEXT_NAME);
ProposalCollector propCollector = new ProposalCollector(Identifier_type.ID_TTCN, TTCN3CodeSkeletons.CONTEXT_IDENTIFIER, contextType, doc, ref, refParser.getReplacementOffset());
propCollector.setProjectParser(projectSourceParser);
if (moduleName == null) {
// rootless behavior
if (ref.getModuleIdentifier() == null) {
Set<String> moduleNames = projectSourceParser.getKnownModuleNames();
Module module;
for (String name : moduleNames) {
module = projectSourceParser.getModuleByName(name);
if (module != null) {
propCollector.addProposal(name, name, ImageCache.getImage("ttcn.gif"), TTCN3Module.MODULE);
module.getAssignments().addProposal(propCollector);
}
}
} else {
Module module = projectSourceParser.getModuleByName(ref.getModuleIdentifier().getName());
if (module != null) {
module.getAssignments().addProposal(propCollector);
}
}
} else {
/*
* search for the best scope in the module's scope
* hierarchy and call proposal adding function on the
* found scope instead of what can be found here
*/
if (scope != null) {
scope.addProposal(propCollector);
}
}
propCollector.sortTillMarked();
propCollector.markPosition();
if (ref.getSubreferences().size() != 1) {
if (PreferenceConstantValues.SORT_ALPHABETICALLY.equals(sortingpolicy)) {
propCollector.sortAll();
}
return propCollector.getCompletitions();
}
Set<String> knownModuleNames = projectSourceParser.getKnownModuleNames();
for (String knownModuleName : knownModuleNames) {
Identifier tempIdentifier = new Identifier(Identifier_type.ID_NAME, knownModuleName);
Module tempModule2 = projectSourceParser.getModuleByName(knownModuleName);
propCollector.addProposal(tempIdentifier, ImageCache.getImage(tempModule2.getOutlineIcon()), "module");
}
propCollector.sortTillMarked();
propCollector.markPosition();
if (ref.getModuleIdentifier() == null) {
if (scope == null) {
TTCN3CodeSkeletons.addSkeletonProposals(doc, refParser.getReplacementOffset(), propCollector);
} else {
scope.addSkeletonProposal(propCollector);
}
propCollector.addTemplateProposal("refers", new Template("refers( function/altstep/testcase name )", "", propCollector.getContextIdentifier(), "refers( ${fatName} );", false), TTCN3CodeSkeletons.SKELETON_IMAGE);
propCollector.addTemplateProposal("derefers", new Template("derefers( function/altstep/testcase name )(parameters)", "", propCollector.getContextIdentifier(), "derefers( ${fatName} ) ( ${parameters} );", false), TTCN3CodeSkeletons.SKELETON_IMAGE);
propCollector.sortTillMarked();
propCollector.markPosition();
TTCN3CodeSkeletons.addPredefinedSkeletonProposals(doc, refParser.getReplacementOffset(), propCollector);
if (scope == null) {
TTCN3Keywords.addKeywordProposals(propCollector);
} else {
scope.addKeywordProposal(propCollector);
}
propCollector.sortTillMarked();
propCollector.markPosition();
} else {
if (scope == null || !(scope instanceof StatementBlock)) {
if (PreferenceConstantValues.SORT_ALPHABETICALLY.equals(sortingpolicy)) {
propCollector.sortAll();
}
return propCollector.getCompletitions();
}
String fakeModule = ref.getModuleIdentifier().getName();
if ("any component".equals(fakeModule) || "all component".equals(fakeModule)) {
Component_Type.addAnyorAllProposal(propCollector, 0);
} else if ("any port".equals(fakeModule) || "all port".equals(fakeModule)) {
PortTypeBody.addAnyorAllProposal(propCollector, 0);
} else if ("any timer".equals(fakeModule) || "all timer".equals(fakeModule)) {
Timer.addAnyorAllProposal(propCollector, 0);
}
}
if (PreferenceConstantValues.SORT_ALPHABETICALLY.equals(sortingpolicy)) {
propCollector.sortAll();
}
return propCollector.getCompletitions();
}
use of org.eclipse.jface.text.templates.TemplateContextType in project titan.EclipsePlug-ins by eclipse.
the class ContentAssistProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
IDocument doc = viewer.getDocument();
IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
int ofs = findWordStart(offset, doc);
String incompleteString = "";
try {
if (doc != null && offset >= ofs) {
incompleteString = doc.get(ofs, offset - ofs);
}
} catch (BadLocationException e) {
ErrorReporter.logExceptionStackTrace(e);
}
String[] reference = incompleteString.trim().split(REFERENCE_SPLITTER, -1);
Reference ref = new Reference(null);
ref.setLocation(new Location(file, 0, 0, offset - ofs));
FieldSubReference subref = new FieldSubReference(new Identifier(Identifier_type.ID_TTCN, reference[0]));
subref.setLocation(new Location(file, 0, 0, reference[0].length()));
ref.addSubReference(subref);
if (reference.length > 1) {
subref = new FieldSubReference(new Identifier(Identifier_type.ID_TTCN, reference[1]));
subref.setLocation(new Location(file, 0, reference[0].length() + 1, offset - ofs));
ref.addSubReference(subref);
}
TemplateContextType contextType = new TemplateContextType(TTCN3CodeSkeletons.CONTEXT_IDENTIFIER, TTCN3CodeSkeletons.CONTEXT_NAME);
ProposalCollector propCollector = new ProposalCollector(Identifier_type.ID_TTCN, TTCN3CodeSkeletons.CONTEXT_IDENTIFIER, contextType, doc, ref, ofs);
TTCN3CodeSkeletons.addSkeletonProposals(doc, offset, propCollector);
TTCN3Keywords.addKeywordProposals(propCollector);
String sortingpolicy = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.CONTENTASSISTANT_PROPOSAL_SORTING);
if (PreferenceConstantValues.SORT_ALPHABETICALLY.equals(sortingpolicy)) {
propCollector.sortAll();
}
return propCollector.getCompletitions();
}
use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.jdt.ls by eclipse.
the class PreferenceManager method initialize.
/**
* Initialize default preference values of used bundles to match server
* functionality.
*/
public static void initialize() {
// Update JavaCore options
initializeJavaCoreOptions();
// Initialize default preferences
IEclipsePreferences defEclipsePrefs = DefaultScope.INSTANCE.getNode(IConstants.PLUGIN_ID);
defEclipsePrefs.put("org.eclipse.jdt.ui.typefilter.enabled", "");
defEclipsePrefs.put(CodeStyleConfiguration.ORGIMPORTS_IMPORTORDER, String.join(";", Preferences.JAVA_IMPORT_ORDER_DEFAULT));
// $NON-NLS-1$
defEclipsePrefs.put(MembersOrderPreferenceCacheCommon.APPEARANCE_MEMBER_SORT_ORDER, "T,SF,SI,SM,F,I,C,M");
defEclipsePrefs.put(CodeGenerationSettingsConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, Boolean.TRUE.toString());
defEclipsePrefs.put(StubUtility.CODEGEN_KEYWORD_THIS, Boolean.FALSE.toString());
defEclipsePrefs.put(StubUtility.CODEGEN_IS_FOR_GETTERS, Boolean.TRUE.toString());
// $NON-NLS-1$
defEclipsePrefs.put(StubUtility.CODEGEN_EXCEPTION_VAR_NAME, "e");
defEclipsePrefs.put(StubUtility.CODEGEN_ADD_COMMENTS, Boolean.FALSE.toString());
ContextTypeRegistry registry = new ContextTypeRegistry();
// Register standard context types from JDT
CodeTemplateContextType.registerContextTypes(registry);
// Register additional context types
registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.CLASSSNIPPET_CONTEXTTYPE));
registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.INTERFACESNIPPET_CONTEXTTYPE));
registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.RECORDSNIPPET_CONTEXTTYPE));
// These should be upstreamed into CodeTemplateContextType & GlobalVariables
TemplateContextType tmp = registry.getContextType(CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE);
tmp.addResolver(new CodeTemplatePreferences.Month());
tmp.addResolver(new CodeTemplatePreferences.ShortMonth());
tmp.addResolver(new CodeTemplatePreferences.Day());
tmp.addResolver(new CodeTemplatePreferences.Hour());
tmp.addResolver(new CodeTemplatePreferences.Minute());
JavaManipulation.setCodeTemplateContextRegistry(registry);
// Initialize templates
templates.put(CodeTemplatePreferences.CODETEMPLATE_FIELDCOMMENT, CodeGenerationTemplate.FIELDCOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_METHODCOMMENT, CodeGenerationTemplate.METHODCOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_CONSTRUCTORCOMMENT, CodeGenerationTemplate.CONSTRUCTORCOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_CONSTRUCTORBODY, CodeGenerationTemplate.CONSTRUCTORBODY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_DELEGATECOMMENT, CodeGenerationTemplate.DELEGATECOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_OVERRIDECOMMENT, CodeGenerationTemplate.OVERRIDECOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_TYPECOMMENT, CodeGenerationTemplate.TYPECOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_GETTERCOMMENT, CodeGenerationTemplate.GETTERCOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_SETTERCOMMENT, CodeGenerationTemplate.SETTERCOMMENT.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_GETTERBODY, CodeGenerationTemplate.GETTERBODY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_SETTERBODY, CodeGenerationTemplate.SETTERBOY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_CATCHBODY, CodeGenerationTemplate.CATCHBODY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_METHODBODY, CodeGenerationTemplate.METHODBODY.createTemplate());
reloadTemplateStore();
}
Aggregations