Java 类org.eclipse.xtext.ui.editor.templates.XtextTemplateContext 实例源码

项目:dsl-devkit    文件:ResourceNameTemplateVariableResolver.java   
@Override
public List<String> resolveValues(final TemplateVariable variable, final XtextTemplateContext templateContext) {
  final List<String> result = Lists.newArrayList();
  IDocument document = templateContext.getDocument();
  final Object obj = variable.getVariableType().getParams().iterator().next();
  if (obj instanceof String) {
    final String variableName = (String) obj;
    final IXtextDocument xtextDocument = (IXtextDocument) document;
    IFile file = xtextDocument.getAdapter(IFile.class);

    if ("package".equals(variableName) && document instanceof IXtextDocument) {
      if (file != null && file.getParent() instanceof IFolder) {
        IJavaProject javaProject = JavaCore.create(file.getProject());
        try {
          IPackageFragment packageFragment = javaProject.findPackageFragment(file.getParent().getFullPath());
          result.add(packageFragment.getElementName());
        } catch (JavaModelException e) {
          LOGGER.error("Could not determine package for file of given document");
        }
      }
    } else if ("catalog".equals(variableName)) {
      final String fileName = file.getName();
      result.add(fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName);
    }
  }
  return Lists.newArrayList(Iterables.filter(result, Predicates.notNull()));
}
项目:sadlos2    文件:TemplateQuickAssistProcessor.java   
protected ICompletionProposal createTemplateProposal(
        TemplateIssueResolution res) {
    try {
        TemplateContextSupplier fac = res.getContextFactory();
        IDocument doc = XtextDocumentUtil.get(qaCtx.getSourceViewer());
        Position pos = fac.getPosition(doc, qaCtx);
        String prefix = fac.getPrefix(doc, qaCtx);
        String postfix = fac.getPostfix(doc, qaCtx);
        Map<String, String> vars = fac.getVariables(doc, qaCtx);
        Region r = new Region(pos.getOffset(), pos.getLength());
        ContentAssistContext cactx = builderProvider.get()
                .setOffset(pos.getOffset()).setPrefix(prefix)
                .setViewer(qaCtx.getSourceViewer()).toContext();

        // create a TemplateProposalContext
        Image i = ((DefaultTemplateProposalProvider) templatePP)
                .getImage(res.getTemplate());
        XtextTemplateContextType type = templateCtxTypeProvider.get();
        XtextTemplateContext tplctx = new QuickfixTemplateContext(type,
                doc, pos, cactx, scopeProvider, prefix, postfix);
        for (Map.Entry<String, String> e : vars.entrySet())
            tplctx.setVariable(e.getKey(), e.getValue());

        // create a TemplateProposal
        XtextTemplateProposal proposal = new XtextTemplateProposal(
                res.getTemplate(), tplctx, r, i);
        return proposal;
    } catch (RuntimeException e1) {
        throw e1;
    } catch (Exception e2) {
        throw new WrappedException(e2);
    }
}