intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.ui / src / net / sourceforge / phpeclipse / js / ui / internal / text / JSCompletionProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.js.ui.internal.text;
12
13 import net.sourceforge.phpeclipse.ui.WebUI;
14 import net.sourceforge.phpeclipse.ui.templates.template.BasicCompletionProcessor;
15 import net.sourceforge.phpeclipse.ui.templates.template.JSContextType;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.templates.TemplateContextType;
22
23
24 /**
25  * A completion processor for XML templates.
26  */
27 public class JSCompletionProcessor extends BasicCompletionProcessor {
28   /**
29          * We watch for angular brackets since those are often part of XML
30          * templates.
31          */
32         protected String extractPrefix(ITextViewer viewer, int offset) {
33                 IDocument document= viewer.getDocument();
34                 int i= offset;
35                 if (i > document.getLength())
36                         return ""; //$NON-NLS-1$
37                 
38                 try {
39                         while (i > 0) {
40                                 char ch= document.getChar(i - 1);
41                                 if (!Character.isJavaIdentifierPart(ch))
42                                         break;
43                                 i--;
44                         }
45         
46                         return document.get(i, offset - i);
47                 } catch (BadLocationException e) {
48                         return ""; //$NON-NLS-1$
49                 }
50         }
51         /**
52          * Return the XML context type that is supported by this plugin. 
53          */
54         protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
55                 return WebUI.getDefault().getContextTypeRegistry().getContextType(JSContextType.JS_CONTEXT_TYPE);
56         }
57
58         
59 }