org.eclipse.jface.text.templates.TemplateVariableResolver Java Examples

The following examples show how to use org.eclipse.jface.text.templates.TemplateVariableResolver. 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: CodetemplatesProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void completeVariable_Type(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if ((mode & NORMAL) != 0) {
		super.completeVariable_Name(model, assignment, context, acceptor);
		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()) {
						TemplateVariableResolver resolver = resolvers.next();
						String type = resolver.getType();
						StyledString displayString = new StyledString(type).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
						acceptor.accept(createCompletionProposal(type, displayString, null, context));
					}
				}
			}
		}
	}
}
 
Example #2
Source File: JavaLanguageServerPlugin.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the template context type registry for the java plug-in.
 *
 * @return the template context type registry for the java plug-in
 */
public synchronized ContextTypeRegistry getTemplateContextRegistry() {
	if (fContextTypeRegistry == null) {
		ContextTypeRegistry registry = new ContextTypeRegistry();

		JavaContextType statementContextType = new JavaContextType();
		statementContextType.setId(JavaContextType.ID_STATEMENTS);
		statementContextType.setName(JavaContextType.ID_STATEMENTS);
		statementContextType.initializeContextTypeResolvers();
		// Todo: Some of the resolvers is defined in the XML of the jdt.ui, now we have to add them manually.
		// See: https://github.com/eclipse/eclipse.jdt.ui/blob/cf6c42522ee5a5ea21a34fcfdecf3504d4750a04/org.eclipse.jdt.ui/plugin.xml#L5619-L5625
		TemplateVariableResolver resolver = new VarResolver();
		resolver.setType("var");
		statementContextType.addResolver(resolver);

		registry.addContextType(statementContextType);

		fContextTypeRegistry = registry;
	}

	return fContextTypeRegistry;
}
 
Example #3
Source File: CodeTemplateSourceViewerConfiguration.java    From typescript.java with MIT License 6 votes vote down vote up
public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
	try {
		IDocument doc= textViewer.getDocument();
		int offset= subject.getOffset();
		if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
			String varName= doc.get(offset, subject.getLength());
			TemplateContextType contextType= fProcessor.getContextType();
			if (contextType != null) {
				Iterator iter= contextType.resolvers();
				while (iter.hasNext()) {
					TemplateVariableResolver var= (TemplateVariableResolver) iter.next();
					if (varName.equals(var.getType())) {
						return var.getDescription();
					}
				}
			}
		}				
	} catch (BadLocationException e) {
	}
	return null;
}
 
Example #4
Source File: CodeTemplateContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
	// test that all variables are defined
	Iterator<TemplateVariableResolver> iterator= getContextType().resolvers();
	while (iterator.hasNext()) {
		TemplateVariableResolver var= iterator.next();
		if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
			Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	if (!canEvaluate(template))
		return null;

	String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);

	TemplateTranslator translator= new TemplateTranslator();
	TemplateBuffer buffer= translator.translate(pattern);
	getContextType().resolve(buffer, this);
	return buffer;
}
 
Example #5
Source File: CodeTemplateSourceViewerConfiguration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
	try {
		IDocument doc= textViewer.getDocument();
		int offset= subject.getOffset();
		if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
			String varName= doc.get(offset, subject.getLength());
			TemplateContextType contextType= fProcessor.getContextType();
			if (contextType != null) {
				Iterator<TemplateVariableResolver> iter= contextType.resolvers();
				while (iter.hasNext()) {
					TemplateVariableResolver var= iter.next();
					if (varName.equals(var.getType())) {
						return var.getDescription();
					}
				}
			}
		}
	} catch (BadLocationException e) {
	}
	return null;
}
 
Example #6
Source File: TemplateValidator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkParameters(Variable variable) {
	Codetemplate template = EcoreUtil2.getContainerOfType(variable, Codetemplate.class);
	Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
	if (templates != null && template != null) {
		Grammar language = templates.getLanguage();
		AbstractRule rule = template.getContext();
		ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(language);
		if (helper != null && rule != null && !rule.eIsProxy() && rule instanceof ParserRule) {
			String contextTypeId = helper.getId(rule);
			ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(language);
			TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
			if (contextType != null) {
				Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(),
						TemplateVariableResolver.class);
				String type = variable.getType();
				if (type == null)
					type = variable.getName();
				while (resolvers.hasNext()) {
					final TemplateVariableResolver resolver = resolvers.next();
					if (resolver.getType().equals(type)) {
						IInspectableTemplateVariableResolver inspectableResolver = registry
								.toInspectableResolver(resolver);
						if (inspectableResolver != null) {
							inspectableResolver.validateParameters(variable, this);
						}
					}
				}
			}
		}
	}
}
 
Example #7
Source File: CodetemplatesProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void completeNestedCrossReference(CrossReference crossReference, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor, TemplateData data) {
	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);
			TemplateVariableResolver crossRefResolver = getResolver(contextType, "CrossReference");
			if (crossRefResolver != null) {
				Assignment assignment = (Assignment) crossReference.eContainer();
				EReference reference = GrammarUtil.getReference(crossReference);
				if (reference != null) {
					String proposalText = "${" + assignment.getFeature() + ":CrossReference("
							+ reference.getEContainingClass().getName() + "." + reference.getName() + ")}";
					StyledString displayText = new StyledString("${", StyledString.DECORATIONS_STYLER)
							.append(assignment.getFeature())
							.append(":CrossReference(", StyledString.DECORATIONS_STYLER)
							.append(reference.getEContainingClass().getName() + "." + reference.getName(),
									StyledString.COUNTER_STYLER)
							.append(")}", StyledString.DECORATIONS_STYLER)
							.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() + 2);
						configurable.setSelectionLength(assignment.getFeature().length());
						configurable.setAutoInsertable(false);
						configurable.setSimpleLinkedMode(context.getViewer(), '\t');
						configurable.setPriority(configurable.getPriority() * 2);
					}
					acceptor.accept(proposal);
				}
			}
		}
	}
}
 
Example #8
Source File: CodetemplatesProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private TemplateVariableResolver getResolver(TemplateContextType contextType, String resolver) {
	if (contextType == null)
		return null;
	Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
	while(resolvers.hasNext()) {
		TemplateVariableResolver result = resolvers.next();
		if (resolver.equals(result.getType())) {
			return result;
		}
	}
	return null;
}
 
Example #9
Source File: InspectableTemplateVariableResolverRegistry.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IInspectableTemplateVariableResolver toInspectableResolver(TemplateVariableResolver resolver) {
	if (resolver instanceof IInspectableTemplateVariableResolver)
		return (IInspectableTemplateVariableResolver) resolver;
	if (resolver instanceof SimpleTemplateVariableResolver) {
		return new InspectableSimpleTemplateVariableResolver((SimpleTemplateVariableResolver) resolver);
	}
	if ("CrossReference".equals(resolver.getType())) {
		return new InspectableCrossReferenceVariableResolver((CrossReferenceTemplateVariableResolver) resolver);
	}
	if ("Enum".equals(resolver.getType())) {
		return new InspectableEnumVariableResolver((EnumTemplateVariableResolver) resolver);
	}
	return null;
}
 
Example #10
Source File: InspectableTemplateVariableResolverDecorator.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public InspectableTemplateVariableResolverDecorator(TemplateVariableResolver decorated) {
	this.decorated = decorated;
}
 
Example #11
Source File: TemplateVariableProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,	int documentOffset) {

		if (fContextType == null)
			return null;

		List<TemplateVariableProposal> proposals= new ArrayList<TemplateVariableProposal>();

		String text= viewer.getDocument().get();
		int start= getStart(text, documentOffset);
		int end= documentOffset;

		String string= text.substring(start, end);
		int colon= string.indexOf(':');
		boolean includeBrace= true;
		int offset= start;
		String prefix= string;
		if (colon != -1) {
			includeBrace= false;
			offset= start + colon + 1;
			prefix= string.substring(colon + 1);
		} else {
			int escape= string.indexOf("${"); //$NON-NLS-1$
			if (escape != -1) {
				offset= start + escape + 2;
				includeBrace= false;
				prefix= string.substring(escape + 2);
			}
		}
		if (prefix.equals("$")) //$NON-NLS-1$
			prefix= ""; //$NON-NLS-1$

		int length= end - offset;

		for (Iterator<TemplateVariableResolver> iterator= fContextType.resolvers(); iterator.hasNext(); ) {
			TemplateVariableResolver variable= iterator.next();

			if (variable.getType().startsWith(prefix))
				proposals.add(new TemplateVariableProposal(variable, offset, length, viewer, includeBrace));
		}

		Collections.sort(proposals, fgTemplateVariableProposalComparator);
		return proposals.toArray(new ICompletionProposal[proposals.size()]);
	}
 
Example #12
Source File: TemplateVariableResolverTestHelper.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Create a {@link TemplateVariable}.
 *
 * @param resolver
 *          {@link TemplateVariableResolver@} of the type which will resolve the {@link TemplateVariable} created, may not be {@code null}
 * @param name
 *          the name of the variable, may not be {@code null}
 * @param values
 *          the values available at this variable, non-empty, may not be {@code null}
 * @return a {@link TemplateVariable}
 * @throws {@link
 *           NullPointerException} if resolver.getType(), name or values is null
 * @throws {@link
 *           IllegalArgumentException} if resolver.getType() or name contains whitespace or values is empty
 * @throws {@link
 *           TemplateException}
 *           if translation failed
 */
public TemplateVariable createTemplateVariable(final TemplateVariableResolver resolver, final String name, final Object... values) throws NullPointerException, IllegalArgumentException, TemplateException {

  // Jump through hoops to create a real TemplateVariable because TemplateVariableType is final thus cannot be mocked,
  // and has protected constructors thus cannot be directly instantiated
  final String pattern = helper.createTemplateVariablePattern(resolver.getType(), name, values);
  final TemplateVariable[] variables = translator.translate(pattern).getVariables();
  assertEquals("Exactly one variable should be returned", 1, variables.length); //$NON-NLS-1$
  return variables[0];
}
 
Example #13
Source File: JavaPlugin.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Registers the given Java template context.
 *
 * @param registry the template context type registry
 * @param id the context type id
 * @param parent the parent context type
 * @since 3.4
 */
private static void registerJavaContext(ContributionContextTypeRegistry registry, String id, TemplateContextType parent) {
	TemplateContextType contextType= registry.getContextType(id);
	Iterator<TemplateVariableResolver> iter= parent.resolvers();
	while (iter.hasNext())
		contextType.addResolver(iter.next());
}
 
Example #14
Source File: TemplateVariableProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Creates a template variable proposal.
 *
 * @param variable the template variable
 * @param offset the offset to replace
 * @param length the length to replace
 * @param viewer the viewer
 * @param includeBrace whether to also replace the ${
 */
public TemplateVariableProposal(TemplateVariableResolver variable, int offset, int length, ITextViewer viewer, boolean includeBrace) {
	fResolver= variable;
	fOffset= offset;
	fLength= length;
	fViewer= viewer;
	fIncludeBrace= includeBrace;
}