Package tool.editors.contentassist

Source Code of tool.editors.contentassist.ToolTemplateAssistProcessor

package tool.editors.contentassist;


import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jface.text.templates.TemplateException;
import org.eclipse.swt.graphics.Image;

import tool.ToolEditorActivator;

public class ToolTemplateAssistProcessor extends TemplateCompletionProcessor{
 
 
  @Override
  protected String extractPrefix(ITextViewer viewer, int offset) {
    int i= offset;
    IDocument document= viewer.getDocument();
    if (i > document.getLength())
      return "";
    try {
      while (i > 0) {
        char ch= document.getChar(i - 1);
        if (!Character.isJavaIdentifierPart(ch))
          break;
        i--;
      }
      if(i>0){
        int j=i;
        if(document.getChar(j-1)=='<')
          i--;
      }
      return document.get(i, offset - i);
    } catch (BadLocationException e) {
      return "";
    }
  }

  protected Template[] getTemplates(String contextTypeId) {
    ToolTemplateManager manager = ToolTemplateManager.getInstance();
    Template[] templates = manager.getTemplateStore().getTemplates();
    Template[] scopeTemplates = getScopeTemplates();
    Template[] allTemplates = new Template[templates.length + scopeTemplates.length];
    if (templates.length > 0)
      System.arraycopy(templates, 0, allTemplates, 0, templates.length);
    if (scopeTemplates.length > 0)
      System.arraycopy(scopeTemplates, 0, allTemplates, templates.length, allTemplates.length);
    return allTemplates;
  }

  private Template[] getScopeTemplates() {
    // TODO Auto-generated method stub
    return new Template[0];
  }

  protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
    ToolTemplateManager manager = ToolTemplateManager.getInstance();
    ToolTemplateContextType ct = (ToolTemplateContextType) manager.getContextTypeRegistry().getContextType(ToolTemplateContextType.CONTEXT_TYPE);
    return ct;
  }

  protected Image getImage(Template template) {
    return ToolEditorActivator.getDefault().getImageRegistry().get("notuds.gif");
  }

  public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();
    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset)
      offset= selection.getOffset() + selection.getLength();
    String prefix = extractPrefix(viewer, offset);
   
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region);
    if (context == null)
      return new ICompletionProposal[0];
    context.setVariable("selection", selection.getText()); // name of the selection variables {line, word_selection //$NON-NLS-1$
   
    Template[] templates = getTemplates(context.getContextType().getId());
    List<ICompletionProposal> matches = new ArrayList<ICompletionProposal>();
    for (int i= 0; i < templates.length; i++) {
      Template template= templates[i];
      try {
        context.getContextType().validate(template.getPattern());
      } catch (TemplateException e) {
        continue;
      }
      if(!prefix.equals("") &&prefix.charAt(0)=='<')
        prefix = prefix.substring(1);
      if (!prefix.equals("")&&(template.getName().startsWith(prefix) &&
          template.matches(prefix, context.getContextType().getId())))
        matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
    }
    return matches.toArray(new ICompletionProposal[matches.size()]);
  }
  protected boolean isLastCharDot(IDocument doc, int offset){
      try {
      return doc.getChar(offset-1) == '.';
    } catch (BadLocationException e) {
    ToolEditorActivator.showError("Error in ToolCompletionProcessor", e);
    }
    return false;
  }
  protected String lastIndent(ITextViewer viewer, int offset) {
    try {
      IDocument document = viewer.getDocument();
      int start = offset-1;
      while (start >= 0 && document.getChar(start)!= '\n') start--;
      int end = start;
      while (end < offset && Character.isWhitespace(document.getChar(end))) end++;
      String indent = document.get(start+1, end-start-1);
      return indent;
    } catch (BadLocationException e) {
      ToolEditorActivator.showError("Error in ToolCompletionProcessor", e);
    }
    return "";
  }
  @Override
  protected ICompletionProposal createProposal(Template template,
      TemplateContext context, IRegion region, int relevance) {
    ToolTemplateCompletionProposal prop = new ToolTemplateCompletionProposal(template, context, region, getImage(template), relevance);
    return prop;
  }

 
 
}
TOP

Related Classes of tool.editors.contentassist.ToolTemplateAssistProcessor

TOP
Copyright © 2018 www.massapi.com. 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.