Package org.eclipse.jface.text.templates

Examples of org.eclipse.jface.text.templates.TemplateBuffer


    if (template != null) {
      TemplateContextType contextType = XMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsXML.NEW);
      IDocument document = new Document();
      TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
      try {
        TemplateBuffer buffer = context.evaluate(template);
        templateString = buffer.getString();
      }
      catch (Exception e) {
        Logger.log(Logger.WARNING_DEBUG, "Could not create template for new xml", e); //$NON-NLS-1$
      }
    }
View Full Code Here


            template = createNewTemplate(template, buffer.toString());
        }

        try {
            TemplateTranslator translator = new TemplateTranslator();
            TemplateBuffer templateBuffer = translator.translate(template);

            getContextType().resolve(templateBuffer, this);

            return templateBuffer;
        } catch (Exception e) {
View Full Code Here

    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE,
        Signature.getQualifier(typeQualifiedName));
    context.setVariable(CodeTemplateContextType.TYPENAME,
        Signature.getSimpleName(typeQualifiedName));

    TemplateBuffer buffer;
    try {
      buffer = context.evaluate(template);
    } catch (BadLocationException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    }
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
      return null;
    }

    TemplateVariable position = findVariable(buffer,
View Full Code Here

        context.setVariable(
            CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG,
            getSeeTag(targetTypeName, methodName,
                targetParamTypeNames));
    }
    TemplateBuffer buffer;
    try {
      buffer = context.evaluate(template);
    } catch (BadLocationException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null) {
      return null;
    }

    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
      return null;
    }
    TemplateVariable position = findVariable(buffer,
        CodeTemplateContextType.TAGS); // look if PHPDoc tags have to
View Full Code Here

    return evaluateTemplate(context, template);
  }

  private static String evaluateTemplate(CodeTemplateContext context,
      Template template) throws CoreException {
    TemplateBuffer buffer;
    try {
      buffer = context.evaluate(template);
    } catch (BadLocationException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
      throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null)
      return null;
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
      return null;
    }
    return str;
  }
View Full Code Here

    return str;
  }

  private static String evaluateTemplate(CodeTemplateContext context,
      Template template, String[] fullLineVariables) throws CoreException {
    TemplateBuffer buffer;
    try {
      buffer = context.evaluate(template);
      if (buffer == null)
        return null;
      String str = fixEmptyVariables(buffer, fullLineVariables);
View Full Code Here

  /* (non-Javadoc)
   * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
   */
  public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {

    TemplateBuffer templateBuffer= super.evaluate(template);
    IDocument doc = fViewer.getDocument();
    ITextSelection selection = (ITextSelection) fViewer.getSelectionProvider().getSelection();
    if (templateBuffer == null) {
      return null;
    }
    //TODO Not enabled see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=55356
//    if (false && AntUIPlugin.getDefault().getPreferenceStore().getBoolean(AntEditorPreferenceConstants.TEMPLATES_USE_CODEFORMATTER)) {
//      FormattingPreferences prefs = new FormattingPreferences();
//      CFMLFormatter.format(templateBuffer, this, prefs);
//    }

      StringBuffer indented = new StringBuffer();
    String pattern = templateBuffer.getString();
    String[] lines = pattern.replaceAll("\\r?(\\n)","$1").split("\\n");
    int lineOffset = doc.getLineInformationOfOffset(selection.getOffset()).getOffset();
    String soFar = doc.get(lineOffset, selection.getOffset() - lineOffset);
    soFar = soFar.replaceAll("\\S", "");
    int indentLength = XmlDocumentFormatter.computeIndent(soFar, 4);
    int[] list = new int[lines.length];
    indented.append(lines[0]).append(fLineDelimiter);
    for(int x = 1; x < lines.length; x++ ){
      indented.append(soFar);
      indented.append(lines[x]);
      indented.append(fLineDelimiter);
      list[x]= (new Integer(soFar.length()*x));
    }
    indented.setLength(indented.lastIndexOf(fLineDelimiter));
      TemplateVariable[] variables= templateBuffer.getVariables();
    for (int i= 0; i != variables.length; i++) {
        TemplateVariable variable= variables[i];
      int[] offsets= new int[variable.getOffsets().length];
      for (int j= 0; j != offsets.length; j++) {
        int offset = variable.getOffsets()[j];
        int varLine = pattern.substring(0,offset).split(fLineDelimiter).length;
        offsets[j]= offset + list[varLine-1];
      }     
       variable.setOffsets(offsets);  
    }
    templateBuffer.setContent(indented.toString(), variables);

   
    return templateBuffer;
  }
View Full Code Here

        Document doc = new Document("" + "\n" + "");
        PyDocumentTemplateContext context = new PyDocumentTemplateContext(new TemplateContextType(), doc, 0, 0, null,
                new TestIndentPrefs(true, 4));

        Template template = new Template("", "", "", "if a:\n\tpass", true);
        TemplateBuffer buffer = context.evaluate(template);
        assertEquals("if a:\n    pass", buffer.getString());

        context = new PyDocumentTemplateContext(new TemplateContextType(), doc, 0, 0, null, new TestIndentPrefs(false,
                4));
        template = new Template("", "", "", "if a\n    print 'a:    '", true);
        buffer = context.evaluate(template);
        assertEquals("if a\n\tprint 'a:    '", buffer.getString());

        doc = new Document("" + "\n\t" + "");
        context = new PyDocumentTemplateContext(new TemplateContextType(), doc, doc.getLength(), 0, "\t",
                new TestIndentPrefs(false, 4));
        template = new Template("", "", "", "if a\n    print 'a:    '", true);
        buffer = context.evaluate(template);
        assertEquals("if a\n\t\tprint 'a:    '", buffer.getString());

        doc = new Document("" + "\n    " + "");
        context = new PyDocumentTemplateContext(new TemplateContextType(), doc, doc.getLength(), 0, "    ",
                new TestIndentPrefs(true, 4));
        template = new Template("", "", "", "if a\n\tprint 'a:    '", true);
        buffer = context.evaluate(template);
        assertEquals("if a\n        print 'a:    '", buffer.getString());

        //let's check if we have a template with \n and a document with \r\n (it should be applied with \r\n)
        doc = new Document("" + "\r\n    " + "");
        context = new PyDocumentTemplateContext(new TemplateContextType(), doc, doc.getLength(), 0, "    ",
                new TestIndentPrefs(true, 4));
        template = new Template("", "", "", "if a\n\tprint 'a:    '", true);
        buffer = context.evaluate(template);
        assertEquals("if a\r\n        print 'a:    '", buffer.getString());

        //let's check if we have a template with \r\n and a document with \r (it should be applied with \r)
        doc = new Document("" + "\r    " + "");
        context = new PyDocumentTemplateContext(new TemplateContextType(), doc, doc.getLength(), 0, "    ",
                new TestIndentPrefs(true, 4));
        template = new Template("", "", "", "if a\r\n\tprint 'a:    '", true);
        buffer = context.evaluate(template);
        assertEquals("if a\r        print 'a:    '", buffer.getString());

    }
View Full Code Here

    String string = null;
    int offset = 0;
    if (template != null) {
      IDocument document = new Document();
      DocumentTemplateContext context = getContext(contextTypeRegistry, template, containerName, fileName, document, varHolder);
      TemplateBuffer buffer = null;
      try {
        buffer = context.evaluate(template);
      } catch (BadLocationException e) {
        PHPUiPlugin.log(e);
      } catch (TemplateException e) {
        PHPUiPlugin.log(e);
      }
      if (buffer != null) {
        string = buffer.getString();
        TemplateVariable[] variables = buffer.getVariables();
        for (int i = 0; i != variables.length; i++) {
          TemplateVariable variable = variables[i];
          if ("cursor".equals(variable.getName())) {//$NON-NLS-1$
            offset = variable.getOffsets()[0];
          }
View Full Code Here

    IDocument document= viewer.getDocument();
    try {
      fContext.setReadOnly(false);
      int start;
      TemplateBuffer templateBuffer;
      {
        int oldReplaceOffset= getReplaceOffset();
        try {
          // this may already modify the document (e.g. add imports)
          templateBuffer= fContext.evaluate(fTemplate);
        } catch (TemplateException e1) {
          fSelectedRegion= fRegion;
          return;
        }

        start= getReplaceOffset();
        int shift= start - oldReplaceOffset;
        int end= Math.max(getReplaceEndOffset(), offset + shift);

        // insert template string
        String templateString= templateBuffer.getString();
        document.replace(start, end - start, templateString);
      }

      // translate positions
      LinkedModeModel model= new LinkedModeModel();
      TemplateVariable[] variables= templateBuffer.getVariables();
      boolean hasPositions= false;
      for (int i= 0; i != variables.length; i++) {
        TemplateVariable variable= variables[i];

        if (variable.isUnambiguous())
View Full Code Here

TOP

Related Classes of org.eclipse.jface.text.templates.TemplateBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.