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

The following examples show how to use org.eclipse.jface.text.templates.TemplateContextType. 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: GamlEditTemplateDialog.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public GamlEditTemplateDialog(final Shell parent, final TemplatePersistenceData data, final boolean edit,
		final ContextTypeRegistry registry, final TemplatesLanguageConfiguration configuration,
		final IEditedResourceProvider resourceProvider, final String languageName) {
	super(parent);
	this.data = data;
	this.configuration = configuration;
	this.resourceProvider = resourceProvider;
	this.languageName = languageName;

	final String title = edit ? TemplateDialogMessages.EditTemplateDialog_title_edit
			: TemplateDialogMessages.EditTemplateDialog_title_new;
	setTitle(title);

	// this.fTemplate = data.getTemplate();
	// fIsNameModifiable = isNameModifiable;

	final List<String[]> contexts = Lists.newArrayList();
	for (final Iterator<TemplateContextType> it =
			Iterators.filter(registry.contextTypes(), TemplateContextType.class); it.hasNext();) {
		final TemplateContextType type = it.next();
		contexts.add(new String[] { type.getId(), type.getName() });
	}
	// fContextTypes = contexts.toArray(new String[contexts.size()][]);
	// fContextTypeRegistry = registry;

}
 
Example #2
Source File: CheckCfgTemplateProposalProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void createTemplates(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
  if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck")) { //$NON-NLS-1$
    addConfiguredCheckTemplates(templateContext, context, acceptor);
    return;
  } else if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.kw_catalog")) { //$NON-NLS-1$
    addCatalogConfigurations(templateContext, context, acceptor);
  }
  TemplateContextType contextType = templateContext.getContextType();
  Template[] templates = templateStore.getTemplates(contextType.getId());
  for (Template template : templates) {

    if (!acceptor.canAcceptMoreTemplates()) {
      return;
    }
    if (validate(template, templateContext)) {
      acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template)));
    }
  }
}
 
Example #3
Source File: JavaContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates a 'java' template in the context of a compilation unit
 *
 * @param template the template to be evaluated
 * @param compilationUnit the compilation unit in which to evaluate the template
 * @param position the position inside the compilation unit for which to evaluate the template
 * @return the evaluated template
 * @throws CoreException in case the template is of an unknown context type
 * @throws BadLocationException in case the position is invalid in the compilation unit
 * @throws TemplateException in case the evaluation fails
 */
public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException {

	TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
	if (!(contextType instanceof CompilationUnitContextType))
		throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null));

	IDocument document= new Document();
	if (compilationUnit != null && compilationUnit.exists())
		document.set(compilationUnit.getSource());

	CompilationUnitContext context= ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit);
	context.setForceEvaluation(true);

	TemplateBuffer buffer= context.evaluate(template);
	if (buffer == null)
		return null;
	return buffer.getString();
}
 
Example #4
Source File: JavaPlugin.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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
 * @since 3.0
 */
public synchronized ContextTypeRegistry getTemplateContextRegistry() {
	if (fContextTypeRegistry == null) {
		ContributionContextTypeRegistry registry= new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR);

		TemplateContextType all_contextType= registry.getContextType(JavaContextType.ID_ALL);
		((AbstractJavaContextType) all_contextType).initializeContextTypeResolvers();

		registerJavaContext(registry, JavaContextType.ID_MEMBERS, all_contextType);
		registerJavaContext(registry, JavaContextType.ID_STATEMENTS, all_contextType);

		registerJavaContext(registry, SWTContextType.ID_ALL, all_contextType);
		all_contextType= registry.getContextType(SWTContextType.ID_ALL);

		registerJavaContext(registry, SWTContextType.ID_MEMBERS, all_contextType);
		registerJavaContext(registry, SWTContextType.ID_STATEMENTS, all_contextType);

		fContextTypeRegistry= registry;
	}

	return fContextTypeRegistry;
}
 
Example #5
Source File: JavaTemplatesPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void updatePatternViewer(Template template) {
	if (template == null) {
		getPatternViewer().getDocument().set(""); //$NON-NLS-1$
		return ;
	}
	String contextId= template.getContextTypeId();
	TemplateContextType type= getContextTypeRegistry().getContextType(contextId);
	fTemplateProcessor.setContextType(type);

	IDocument doc= getPatternViewer().getDocument();

	String start= null;
	if ("javadoc".equals(contextId)) { //$NON-NLS-1$
		start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
	} else
		start= ""; //$NON-NLS-1$

	doc.set(start + template.getPattern());
	int startLen= start.length();
	getPatternViewer().setDocument(doc, startLen, doc.getLength() - startLen);
}
 
Example #6
Source File: EditTemplateDialog.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public EditTemplateDialog(Shell parent, Template template, boolean edit, boolean isNameModifiable, 
		ContextTypeRegistry registry, TemplatesLanguageConfiguration configuration, 
		IEditedResourceProvider resourceProvider, String languageName) {
	super(parent);
	this.configuration = configuration;
	this.resourceProvider = resourceProvider;
	this.languageName = languageName;

	String title= edit
		? TemplateDialogMessages.EditTemplateDialog_title_edit
		: TemplateDialogMessages.EditTemplateDialog_title_new;
	setTitle(title);

	this.fTemplate= template;
	fIsNameModifiable= isNameModifiable;

	List<String[]> contexts= Lists.newArrayList();
	for (Iterator<TemplateContextType> it= Iterators.filter(registry.contextTypes(), TemplateContextType.class); it.hasNext();) {
		TemplateContextType type= it.next();
		contexts.add(new String[] { type.getId(), type.getName() });
	}
	fContextTypes= contexts.toArray(new String[contexts.size()][]);
	fContextTypeRegistry= registry;
}
 
Example #7
Source File: AdvancedTemplatesPreferencePage.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	if (selection.size() == 1) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String name = template.getName();
		TemplateContextType contextType = getContextTypeRegistry().getContextType(template.getContextTypeId());
		if (contextType != null) {
			String prefix = 
					"templates for " + grammarAccess.getGrammar().getName() +
					"'" + name + "'" + " for " + getContextTypeForGrammar(contextType) + ">>";
			String editablePart = template.getPattern();
			String suffix = "";
			partialEditor.updateModel(prefix, editablePart, suffix);
		} else {
			partialEditor.updateModel("", template.getPattern(), "");
		}
	} else {
		partialEditor.updateModel("", "", "");
	}
}
 
Example #8
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 #9
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 #10
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 #11
Source File: JSONProposalFactory.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private ICompletionProposal createProposal(ContentAssistContext context, String name, String value,
		String description, String rawTemplate, Image image, boolean isGenericProposal) {

	TemplateContextType contextType = getTemplateContextType();
	IXtextDocument document = context.getDocument();
	TemplateContext tContext = new DocumentTemplateContext(contextType, document, context.getOffset(), 0);
	Region replaceRegion = context.getReplaceRegion();

	// pre-populate ${name} and ${value} with given args
	if (isGenericProposal) {
		tContext.setVariable("name", name);
	}
	tContext.setVariable("value", value);

	return new StyledTemplateProposal(context, name, description, rawTemplate, isGenericProposal, tContext,
			replaceRegion, image);
}
 
Example #12
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 #13
Source File: OpenApi3ContextTypeProvider.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
public TemplateContextType getContextType(final Model model, final String path) {
    if (OpenApi3ContextTypeProvider.RootContextType.isRoot(path)) {
        return new RootContextType();
    }
    return allContextTypes().stream()//
            .filter(input -> input instanceof SchemaBasedTemplateContextType
                    && ((SchemaBasedTemplateContextType) input).matches(model, path))//
            .findFirst().orElse(null);
}
 
Example #14
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 #15
Source File: EditTemplateDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void doSourceChanged(IDocument document) {
	String text= document.get();
	fValidationStatus.setOK();
	TemplateContextType contextType= fContextTypeRegistry.getContextType(getContextId());
	if (contextType != null) {
		try {
			contextType.validate(text);
		} catch (TemplateException e) {
			fValidationStatus.setError(e.getLocalizedMessage());
		}
	}

	updateAction(ITextEditorActionConstants.UNDO);
	updateStatusAndButtons();
}
 
Example #16
Source File: CodeTemplateBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void updateSourceViewerInput(List<Object> selection) {
	if (fPatternViewer == null || fPatternViewer.getTextWidget().isDisposed()) {
		return;
	}
	if (selection.size() == 1 && selection.get(0) instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.get(0);
		Template template= data.getTemplate();
		TemplateContextType type= JavaPlugin.getDefault().getCodeTemplateContextRegistry().getContextType(template.getContextTypeId());
		fTemplateProcessor.setContextType(type);
		fPatternViewer.getDocument().set(template.getPattern());
	} else {
		fPatternViewer.getDocument().set(""); //$NON-NLS-1$
	}
}
 
Example #17
Source File: XtextTemplateContextType.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int compareTo(TemplateContextType templateContextType) {
	int result = getName().compareTo(templateContextType.getName());
	if (result == 0) {
		return getId().compareTo(templateContextType.getId());
	}
	return result;
}
 
Example #18
Source File: DefaultTemplateProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected TemplateContextType[] getContextTypes(final ContentAssistContext context) {
	final Set<TemplateContextType> result = Sets.newLinkedHashSet();
	IFollowElementAcceptor acceptor = createFollowElementAcceptor(result);
	List<AbstractElement> grammarElements = context.getFirstSetGrammarElements();
	for(AbstractElement element: grammarElements)
		acceptor.accept(element);
	return result.toArray(new TemplateContextType[result.size()]);
}
 
Example #19
Source File: JavaTemplatePreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();

	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);

		IDocument doc= viewer.getDocument();

		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$

		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}
}
 
Example #20
Source File: QuickTemplateProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean canEvaluate(TemplateContext context, Template template) {
	String contextId= context.getContextType().getId();
	if (JavaDocContextType.ID.equals(contextId)) {
		if (!template.matches("", contextId) || template.getPattern().indexOf($_LINE_SELECTION) == -1 && template.getPattern().indexOf($_WORD_SELECTION) == -1) //$NON-NLS-1$
			return false;
	} else {
		if (template.matches("", JavaDocContextType.ID) || template.getPattern().indexOf($_LINE_SELECTION) == -1) //$NON-NLS-1$
			return false;
	}
	TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
	return contextType instanceof CompilationUnitContextType;
}
 
Example #21
Source File: DefaultTemplateProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void createTemplates(TemplateContext templateContext, ContentAssistContext context, ITemplateAcceptor acceptor) {
	TemplateContextType contextType = templateContext.getContextType();
	Template[] templates = templateStore.getTemplates(contextType.getId());
	for (Template template : templates) {
		if (!acceptor.canAcceptMoreTemplates())
			return;
		if (validate(template, templateContext)) {
			acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template)));
		}
	}
}
 
Example #22
Source File: PyDocumentTemplateContext.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
        final ITextViewer viewer, final IRegion region) {
    if (contextType != null) {
        IDocument document = viewer.getDocument();
        PySelection selection = new PySelection(document,
                ((ITextSelection) viewer.getSelectionProvider().getSelection()).getOffset());
        String indent = selection.getIndentationFromLine();
        return PyDocumentTemplateContext.createContext(contextType, viewer, region, indent);
    }
    return null;
}
 
Example #23
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 #24
Source File: XbaseTemplateContext.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public XbaseTemplateContext create(TemplateContextType type, IDocument document, Position position,
		ContentAssistContext contentAssistContext, IScopeProvider scopeProvider) {
	XbaseTemplateContext result = new XbaseTemplateContext(type, document, position, contentAssistContext,
			scopeProvider);
	injector.injectMembers(result);
	return result;
}
 
Example #25
Source File: TemplateSet.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected String validateTemplate(Template template) {
	TemplateContextType type= fRegistry.getContextType(template.getContextTypeId());
	if (type == null) {
		return "Unknown context type: " + template.getContextTypeId(); //$NON-NLS-1$
	}
	try {
		type.validate(template.getPattern());
		return null;
	} catch (TemplateException e) {
		return e.getMessage();
	}
}
 
Example #26
Source File: SnippetsCompletionProcessor.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected TemplateContext createContext(ITextViewer viewer, IRegion region)
{
	TemplateContextType contextType = getContextType(viewer, region);
	if (contextType != null)
	{
		IDocument document = viewer.getDocument();
		return new DocumentSnippetTemplateContext(contextType, document, region.getOffset(), region.getLength());
	}
	return null;
}
 
Example #27
Source File: PyDocumentTemplateContext.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a concrete template context for the given region in the document. This involves finding out which
 * context type is valid at the given location, and then creating a context of this type. The default implementation
 * returns a <code>DocumentTemplateContext</code> for the context type at the given location.
 *
 * @param contextType the context type for the template.
 * @param viewer the viewer for which the context is created
 * @param region the region into <code>document</code> for which the context is created
 * @return a template context that can handle template insertion at the given location, or <code>null</code>
 */
public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
        final ITextViewer viewer, final IRegion region, String indent) {
    if (contextType != null) {
        IDocument document = viewer.getDocument();
        final String indentTo = indent;
        return new PyDocumentTemplateContext(contextType, document, region.getOffset(), region.getLength(),
                indentTo, viewer);
    }
    return null;
}
 
Example #28
Source File: JsonContentAssistProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
    String contextType = getContextTypeId(currentModel, currentPath.toString());
    ContextTypeRegistry registry = getContextTypeRegistry();
    if (registry != null) {
        return registry.getContextType(contextType);
    } else {
        return null;
    }
}
 
Example #29
Source File: TypeScriptTemplatePreferencePage.java    From typescript.java with MIT License 5 votes vote down vote up
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();
	
	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);
		
		IDocument doc= viewer.getDocument();
		
		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$
		
		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}		
}
 
Example #30
Source File: EditTemplateDialog.java    From typescript.java with MIT License 5 votes vote down vote up
protected void doSourceChanged(IDocument document) {
	String text = document.get();
	fValidationStatus.setOK();
	TemplateContextType contextType = fContextTypeRegistry.getContextType(getContextId());
	if (contextType != null) {
		try {
			contextType.validate(text);
		} catch (TemplateException e) {
			fValidationStatus.setError(e.getLocalizedMessage());
		}
	}

	updateUndoAction();
	updateStatusAndButtons();
}