EclipsePlug-in使用TextEditor开发自己的编辑器,实现关键字高亮和代码提示.

来源:互联网 发布:macbook卸载软件 编辑:程序博客网 时间:2024/05/18 03:32

原文地址:http://blog.csdn.net/lyq19870515/article/details/19904913  

最近在开发EclipsePlug, 开发一个SQL代码编辑器, 所以就写一篇文章. 希望对大家有帮助. 让大家少走弯路. (代码可能不能运行, 但关键部分都有).  因为代码比较多. 所以可能不能一次性上传完成. 毕竟我还要修改, 空话不多说. 直接上代码.

首先是Editor类, 我取名为SQLEditor, 继承TextEditor:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.test.editors;  
  2. import org.eclipse.core.runtime.IProgressMonitor;  
  3. import org.eclipse.jface.text.BadLocationException;  
  4. import org.eclipse.jface.text.IDocument;  
  5. import org.eclipse.jface.text.IRegion;  
  6. import org.eclipse.jface.text.formatter.IContentFormatter;  
  7. import org.eclipse.swt.SWT;  
  8. import org.eclipse.swt.graphics.Font;  
  9. import org.eclipse.swt.graphics.FontData;  
  10. import org.eclipse.swt.graphics.Point;  
  11. import org.eclipse.swt.widgets.Composite;  
  12. import org.eclipse.ui.IViewPart;  
  13. import org.eclipse.ui.PartInitException;  
  14. import org.eclipse.ui.PlatformUI;  
  15. import org.eclipse.ui.editors.text.TextEditor;  
  16. import com.zdk.platform.studio.dbassistant.codeassistent.ColorManager;  
  17. import com.zdk.platform.studio.dbassistant.codeassistent.SQLConfiguration;  
  18. import com.zdk.platform.studio.dbassistant.codeassistent.SQLDocumentProvider;  
  19.   
  20. /** 
  21.  * 类说明: SQL语句编辑器. 
  22.  * @author xiao天__ 
  23.  * 
  24.  */  
  25. public class SQLEditor extends TextEditor {  
  26.       
  27.     /**editorID**/  
  28.     public static String ID = "com.test.SQLEditor";  
  29.       
  30.     private ColorManager colorManager;  
  31.     private SQLConfiguration configuration;  
  32.       
  33.     public SQLEditor() {  
  34.         super();  
  35.         configuration = new SQLConfiguration();  
  36.         setSourceViewerConfiguration(configuration);  
  37.         setDocumentProvider(new SQLDocumentProvider());  
  38.     }  
  39.       
  40.     /** 
  41.      * 方法说明: 设置字体. 
  42.      */  
  43.     public void initFont() {  
  44.         FontData fontData = new FontData("Consolas"11, SWT.NORMAL);  
  45.         Font font = new Font(getEditorSite().getShell().getDisplay(), fontData);  
  46.         this.getSourceViewer().getTextWidget().setFont(font);  
  47.     }  
  48.       
  49.     public void dispose() {  
  50.         if(colorManager != null) {  
  51.             colorManager.dispose();  
  52.         }  
  53.         super.dispose();  
  54.     }  
  55.       
  56.     @Override  
  57.     public void createPartControl(Composite parent) {  
  58.         super.createPartControl(parent);  
  59.         initFont();  
  60.     }  
  61. }  


如果大家使用的是有文件的方式. 就可以直接配置Plug-in.xml文件方式来打开SQLEditor, 而小天__使用的是不需要文件的方式来打开SQLEditor, 主要是项目需要, 所以这里给大家写两种方式:

方式一:

Plug-in.xml:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <plugin>  
  2.    <extension  
  3.          point="org.eclipse.ui.editors">  
  4.       <editor  
  5.             name="SQL编辑器"  
  6.             extensions="xml"  
  7.             icon="icons/sample.gif"  
  8.             contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"  
  9.             class="com.test.SQLEditor"  
  10.             id="com.test.SQLEditor">  
  11.       </editor>  
  12.    </extension>  
  13. </plugin>  

方式二:

Plug-in.xml的配置都是一样的, 因为没有文件. 所以我们必须要构建一个Input对象:

SQLEditorInput:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.test.editors;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.InputStream;  
  5. import org.eclipse.core.resources.IStorage;  
  6. import org.eclipse.core.runtime.CoreException;  
  7. import org.eclipse.core.runtime.IPath;  
  8. import org.eclipse.jface.resource.ImageDescriptor;  
  9. import org.eclipse.ui.IPersistableElement;  
  10. import org.eclipse.ui.IStorageEditorInput;  
  11. import org.eclipse.ui.IViewPart;  
  12. import org.eclipse.ui.PartInitException;  
  13. import org.eclipse.ui.PlatformUI;  
  14.   
  15. public class SQLEditorInput implements IStorageEditorInput {  
  16.     /**显示名称**/  
  17.     private String name;  
  18.     /**正文**/  
  19.     private String content = "";  
  20.     /**存储器**/  
  21.     public IStorage storage;  
  22.       
  23.     public SQLEditorInput(String name) {  
  24.         this.name = name;  
  25.         storage = new IStorage() {  
  26.             @Override  
  27.             public Object getAdapter(Class adapter) {  
  28.                 return null;  
  29.             }  
  30.             @Override  
  31.             public boolean isReadOnly() {  
  32.                 return isReadOnly;  
  33.             }  
  34.             @Override  
  35.             public String getName() {  
  36.                 return SQLEditorInput.this.name;  
  37.             }  
  38.             @Override  
  39.             public IPath getFullPath() {  
  40.                 return null;  
  41.             }  
  42.             @Override  
  43.             public InputStream getContents() throws CoreException {  
  44.                 return new ByteArrayInputStream(content.getBytes());  
  45.             }  
  46.         };  
  47.     }  
  48.       
  49.     public SQLEditorInput(String name, IStorage is) {  
  50.         this.name = name;  
  51.         this.storage = is;  
  52.     }  
  53.       
  54.     @Override  
  55.     public boolean exists() {  
  56.         return true;  
  57.     }  
  58.   
  59.     @Override  
  60.     public ImageDescriptor getImageDescriptor() {  
  61.         return null;  
  62.     }  
  63.   
  64.     @Override  
  65.     public String getName() {  
  66.         return name;  
  67.     }  
  68.   
  69.     @Override  
  70.     public IPersistableElement getPersistable() {  
  71.         return null;  
  72.     }  
  73.   
  74.     @Override  
  75.     public String getToolTipText() {  
  76.         return "SQL编辑器";  
  77.     }  
  78.   
  79.     @Override  
  80.     public Object getAdapter(Class adapter) {  
  81.         return null;  
  82.     }  
  83.   
  84.     @Override  
  85.     public IStorage getStorage() throws CoreException {  
  86.         return storage;  
  87.     }  
  88.   
  89.     @Override  
  90.     public boolean equals(Object obj) {  
  91.         if(obj instanceof SQLEditorInput){  
  92.             SQLEditorInput newDbEditorInput = (SQLEditorInput)obj;  
  93.             if(this.name.equals(newDbEditorInput.getName())) {  
  94.                 return true;  
  95.             }  
  96.         }  
  97.         return false;  
  98.     }  
  99. }  

打开编辑器方法:

这个方法可以写在工具栏的事件里. 右键菜单里. 这个就看需求了.

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public void openView() {  
  2.     SQLEditorInput input = new SQLEditorInput("新建查询");  
  3.     try {  
  4.         page.openEditor(input, "com.test.SQLEditor");  
  5.     } catch (PartInitException e) {  
  6.         e.printStackTrace();  
  7.     }  
  8. }  

SQLConfiguration类:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.zdk.platform.studio.dbassistant.codeassistent;  
  2. import org.eclipse.jface.text.IDocument;  
  3. import org.eclipse.jface.text.contentassist.ContentAssistant;  
  4. import org.eclipse.jface.text.contentassist.IContentAssistant;  
  5. import org.eclipse.jface.text.formatter.ContentFormatter;  
  6. import org.eclipse.jface.text.formatter.IContentFormatter;  
  7. import org.eclipse.jface.text.presentation.IPresentationReconciler;  
  8. import org.eclipse.jface.text.presentation.PresentationReconciler;  
  9. import org.eclipse.jface.text.rules.DefaultDamagerRepairer;  
  10. import org.eclipse.jface.text.source.ISourceViewer;  
  11. import org.eclipse.jface.text.source.SourceViewerConfiguration;  
  12. import com.zdk.platform.studio.dbassistant.codeassistent.assistent.SQLContentAssistent;  
  13.   
  14. public class SQLConfiguration extends SourceViewerConfiguration {  
  15.       
  16.     private SQLContentAssistent assistent;  
  17.       
  18.     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {  
  19.         PresentationReconciler reconciler = new PresentationReconciler();  
  20.         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new SQLPartitionScanner());  
  21.         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);  
  22.         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);  
  23.         return reconciler;  
  24.     }  
  25.       
  26.     @Override  
  27.     public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {  
  28.         ContentFormatter formatter = new ContentFormatter();  
  29.         formatter.setFormattingStrategy(new SQLFormattingStrategy(), IDocument.DEFAULT_CONTENT_TYPE);  
  30.         formatter.enablePartitionAwareFormatting(true);  
  31.         return formatter;  
  32.     }  
  33.   
  34.     /** 
  35.      * 设置自动提示. 
  36.      */  
  37.     @Override  
  38.     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {  
  39.         ContentAssistant contentAssistent = new ContentAssistant();  
  40.         assistent = new SQLContentAssistent();  
  41.         contentAssistent.setContentAssistProcessor(assistent, IDocument.DEFAULT_CONTENT_TYPE);  
  42.         contentAssistent.enableAutoActivation(true);  
  43.         contentAssistent.setAutoActivationDelay(200);  
  44.         return contentAssistent;  
  45.     }  
  46.   
  47.     public DBConfig getDbConfig() {  
  48.         return dbConfig;  
  49.     }  
  50. }  

SQLDocumentProvider类(说实话这个类.小天__都不清楚有什么用):

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.zdk.platform.studio.dbassistant.codeassistent;  
  2.   
  3. import org.eclipse.core.runtime.CoreException;  
  4. import org.eclipse.jface.text.IDocument;  
  5. import org.eclipse.jface.text.IDocumentPartitioner;  
  6. import org.eclipse.jface.text.rules.FastPartitioner;  
  7. import org.eclipse.ui.editors.text.FileDocumentProvider;  
  8.   
  9. public class SQLDocumentProvider extends FileDocumentProvider {  
  10.     protected IDocument createDocument(Object element) throws CoreException {  
  11.         IDocument document = super.createDocument(element);  
  12.         if (document != null) {  
  13.             IDocumentPartitioner partitioner =  
  14.                 new FastPartitioner(  
  15.                     new SQLPartitionScanner(),  
  16.                     new String[] { });  
  17.             partitioner.connect(document);  
  18.             document.setDocumentPartitioner(partitioner);  
  19.         }  
  20.         return document;  
  21.     }  
  22. }  


代码高亮部分:

ColorManager类:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.zdk.platform.studio.dbassistant.codeassistent;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Iterator;  
  5. import java.util.Map;  
  6. import org.eclipse.swt.graphics.Color;  
  7. import org.eclipse.swt.graphics.RGB;  
  8. import org.eclipse.swt.widgets.Display;  
  9.   
  10. /** 
  11.  * 类说明: 颜色管理类. 
  12.  * @author 小天__ 
  13.  * 
  14.  */  
  15. public class ColorManager {  
  16.       
  17.     /**注释颜色**/  
  18.     public static final RGB COLOR_COMMENT = new RGB(01280);  
  19.     /**关键字颜色**/  
  20.     public static final RGB COLOR_KEYWORD = new RGB(12800);  
  21.     /**普通文本颜色**/  
  22.     public static final RGB COLOR_TEXT = new RGB(000);  
  23.   
  24.     protected static Map fColorTable = new HashMap(10);  
  25.       
  26.     public void dispose() {  
  27.         Iterator e = fColorTable.values().iterator();  
  28.         while (e.hasNext())  
  29.              ((Color) e.next()).dispose();  
  30.     }  
  31.       
  32.     public static Color getColor(RGB rgb) {  
  33.         Color color = (Color) fColorTable.get(rgb);  
  34.         if (color == null) {  
  35.             color = new Color(Display.getCurrent(), rgb);  
  36.             fColorTable.put(rgb, color);  
  37.         }  
  38.         return color;  
  39.     }  
  40. }  

SQLPartitionScanner:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.test.codeassistent;  
  2.   
  3. import org.eclipse.jface.text.TextAttribute;  
  4. import org.eclipse.jface.text.rules.EndOfLineRule;  
  5. import org.eclipse.jface.text.rules.IPredicateRule;  
  6. import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;  
  7. import org.eclipse.jface.text.rules.SingleLineRule;  
  8. import org.eclipse.jface.text.rules.Token;  
  9.   
  10. import com.test.rule.KeyWordDetector;  
  11. import com.test.rule.KeyWordRule;  
  12.   
  13. public class SQLPartitionScanner extends RuleBasedPartitionScanner {  
  14.   
  15.     private TextAttribute keywordAttr = new TextAttribute(ColorManager.getColor(ColorManager.COLOR_KEYWORD));  
  16.   
  17.     public SQLPartitionScanner() {  
  18.         IPredicateRule[] rules = new IPredicateRule[1];  
  19.         rules[0] = new KeyWordRule(new KeyWordDetector(), new Token(keywordAttr));  
  20.         setPredicateRules(rules);  
  21.     }  
  22. }  

KeyWordRule:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.zdk.platform.studio.dbassistant.codeassistent.rule;  
  2.   
  3. import org.eclipse.jface.text.rules.ICharacterScanner;  
  4. import org.eclipse.jface.text.rules.IPredicateRule;  
  5. import org.eclipse.jface.text.rules.IToken;  
  6. import org.eclipse.jface.text.rules.IWordDetector;  
  7. import org.eclipse.jface.text.rules.Token;  
  8. import org.eclipse.jface.text.rules.WordRule;  
  9.   
  10. import com.test.SQLPartitionScanner;  
  11.   
  12. public class KeyWordRule extends WordRule implements IPredicateRule {  
  13.       
  14.     private StringBuffer fBuffer= new StringBuffer();  
  15.     private boolean fIgnoreCase= false;  
  16.     /**关键字**/  
  17.     public String keywords = "insert,update,delete,select";  
  18.       
  19.     public KeyWordRule(IWordDetector detector, IToken defaultToken) {  
  20.         super(detector, new Token(SQLPartitionScanner.textAttr));  
  21.         //增加关键字  
  22.         String[] keywordArray = keywords.split(",");  
  23.         for (int i = 0; i < keywordArray.length; i++) {  
  24.             String keywrod = keywordArray[i];  
  25.             addWord(keywrod, defaultToken);  
  26.         }  
  27.     }  
  28.       
  29.     public IToken evaluate(ICharacterScanner scanner) {  
  30.         int c= scanner.read();  
  31.         if (c != ICharacterScanner.EOF && fDetector.isWordStart((char) c)) {  
  32.             if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {  
  33.   
  34.                 fBuffer.setLength(0);  
  35.                 do {  
  36.                     fBuffer.append((char) c);  
  37.                     c= scanner.read();  
  38.                 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));  
  39.                 scanner.unread();  
  40.   
  41.                 String buffer= fBuffer.toString();  
  42.   
  43.                 if (fIgnoreCase) {  
  44.                     buffer = buffer.toLowerCase();  
  45.                 }  
  46.                 IToken token= (IToken)fWords.get(buffer);  
  47.                   
  48.                 if (token != null) {  
  49.                     return token;  
  50.                 }  
  51.                   
  52.                 if (fDefaultToken.isUndefined()) {  
  53.                     unreadBuffer(scanner);  
  54.                 }  
  55.                 return fDefaultToken;  
  56.             }  
  57.         }  
  58.         scanner.unread();  
  59.         return Token.UNDEFINED;  
  60.     }  
  61.   
  62.     @Override  
  63.     public IToken getSuccessToken() {  
  64.         return Token.UNDEFINED;  
  65.     }  
  66.   
  67.     @Override  
  68.     public IToken evaluate(ICharacterScanner scanner, boolean resume) {  
  69.         return this.fDefaultToken;  
  70.     }  
  71.       
  72. }  

KeyWordDetector:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.zdk.platform.studio.dbassistant.codeassistent.rule;  
  2.   
  3. /******************************************************************************* 
  4.  * Copyright (c) 2000 2005 IBM Corporation and others. 
  5.  * All rights reserved. This program and the accompanying materials 
  6.  * are made available under the terms of the Eclipse Public License v1.0 
  7.  * which accompanies this distribution, and is available at 
  8.  * http://www.eclipse.org/legal/epl-v10.html 
  9.  * 
  10.  * Contributors: 
  11.  *     IBM Corporation - initial API and implementation 
  12.  *     QNX Software System 
  13.  *******************************************************************************/  
  14.   
  15. import org.eclipse.jface.text.rules.IWordDetector;  
  16.   
  17. /** 
  18.  * A C aware word detector. 
  19.  */  
  20. public class KeyWordDetector implements IWordDetector {  
  21.   
  22.     /** 
  23.      * @see IWordDetector#isWordIdentifierStart 
  24.      */  
  25.     public boolean isWordStart(char c) {  
  26.         if(64 < c && c < 123 || c == '=') {   //大写字母A-Z, 小写字母a-z;  
  27.             return true;  
  28.         }  
  29.         return false;  
  30.     }  
  31.   
  32.     /** 
  33.      * @see IWordDetector#isWordIdentifierPart 
  34.      */  
  35.     public boolean isWordPart(char c) {  
  36.         if(64 < c && c < 123 || c == '=') {   //大写字母A-Z, 小写字母a-z;  
  37.             return true;  
  38.         }  
  39.         return false;  
  40.     }  
  41. }  

代码提示部分:

SQLContentAssistent:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.test.assistent;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.eclipse.jface.text.BadLocationException;  
  7. import org.eclipse.jface.text.IDocument;  
  8. import org.eclipse.jface.text.ITextViewer;  
  9. import org.eclipse.jface.text.contentassist.CompletionProposal;  
  10. import org.eclipse.jface.text.contentassist.ICompletionProposal;  
  11. import org.eclipse.jface.text.contentassist.IContentAssistProcessor;  
  12. import org.eclipse.jface.text.contentassist.IContextInformation;  
  13. import org.eclipse.jface.text.contentassist.IContextInformationValidator;  
  14.   
  15. import com.zdk.platform.studio.pojo.DBConfig;  
  16.   
  17. public class SQLContentAssistent implements IContentAssistProcessor {  
  18.        
  19.     /**提示集合**/  
  20.     public List<IAssistentContent> assistentContentList = new ArrayList<IAssistentContent>();  
  21.       
  22.     /**开始位置**/  
  23.     public int startIndex = 0;  
  24.     /**当前文档**/  
  25.     public IDocument doc;  
  26.     /**数据连接对象**/  
  27.     private DBConfig dbConfig;  
  28.       
  29.     /** 
  30.      * 构造方法: 添加提示种类. 
  31.      */  
  32.     public SQLContentAssistent() {  
  33.         super();  
  34.         assistentContentList.add(new KeywordAssistentData());  
  35.     }  
  36.   
  37.     @Override  
  38.     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {  
  39.         this.doc = viewer.getDocument();  
  40.         List list = new KeywordAssistentData().getAssistentData(this.doc, offset);  
  41.         return (CompletionProposal[]) list.toArray(new CompletionProposal[list.size()]);  
  42.     }  
  43.   
  44.     @Override  
  45.     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {  
  46.         // TODO 自动生成的方法存根  
  47.         return null;  
  48.     }  
  49.   
  50.     /** 
  51.      * 设置何时激活 
  52.      */  
  53.     @Override  
  54.     public char[] getCompletionProposalAutoActivationCharacters() {  
  55.         return new char[] {'.'};  
  56.     }  
  57.   
  58.     @Override  
  59.     public char[] getContextInformationAutoActivationCharacters() {  
  60.         return null;  
  61.     }  
  62.   
  63.     @Override  
  64.     public String getErrorMessage() {  
  65.         // TODO 自动生成的方法存根  
  66.         return null;  
  67.     }  
  68.   
  69.     @Override  
  70.     public IContextInformationValidator getContextInformationValidator() {  
  71.         // TODO 自动生成的方法存根  
  72.         return null;  
  73.     }  
  74.       
  75.     /** 
  76.      * 方法说明: 获取提示前面的字符串. 
  77.      * @param doc 
  78.      * @param offest 
  79.      * @return 
  80.      */  
  81.     public String getFrontText(IDocument doc, int offest) {  
  82.         StringBuffer buf = new StringBuffer();  
  83.         while(true) {   //循环添加关键字.  
  84.             try {  
  85.                 char c = doc.getChar(--offest);  
  86.                 startIndex = offest;  
  87.                 if(Character.isWhitespace(c)) {  
  88.                     startIndex++;  
  89.                     break;  
  90.                 }  
  91.                 if(c == ';' || c == '(' || c == ')' || c == '{' || c == '}' || c == ',') {  //结束符号.  
  92.                     startIndex++;  
  93.                     break;  
  94.                 }  
  95.                 buf.append(c);  
  96.             } catch (BadLocationException e) {  
  97.                 break;  
  98.             }  
  99.         }  
  100.         return buf.reverse().toString();  
  101.     }  
  102. }  

KeywordAssistentData:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.test.assistent;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.regex.Matcher;  
  6. import java.util.regex.Pattern;  
  7.   
  8. import org.eclipse.jface.text.BadLocationException;  
  9. import org.eclipse.jface.text.IDocument;  
  10. import org.eclipse.jface.text.contentassist.CompletionProposal;  
  11.   
  12. import com.zdk.platform.studio.dbassistant.codeassistent.Contents;  
  13.   
  14. /** 
  15.  * 类说明: 关键字提示数据类. 
  16.  * @author Administrator 
  17.  * 
  18.  */  
  19. public class KeywordAssistentData implements IAssistentContent {  
  20.       
  21.     private int start = 0;   
  22.       
  23.     @Override  
  24.     public List<CompletionProposal> getAssistentData(IDocument doc, int offset) {  
  25.           
  26.         String str = getFrontText(doc, offset);  
  27.         List<CompletionProposal> list = new ArrayList<CompletionProposal>();  
  28.         //用正则匹配.  
  29.         Pattern p = null;  
  30.         Matcher m = null;  
  31.         for (int i = 0; i < Contents.KEY_WORD_ARRAY.length; i++) {  
  32.             String keyWord = Contents.KEY_WORD_ARRAY[i];  
  33.             p = Pattern.compile("(^" + str + ")", Pattern.CASE_INSENSITIVE);  
  34.             m = p.matcher(keyWord);  
  35.             if(m.find()) {  
  36.                 String insert = Contents.KEY_WORD_ARRAY[i];  
  37.                 //创建替换类容.  insert:替换文本, offset:替换其实位置.  
  38.                 //0:替换结束位置.偏移量, insert.length:替换完成后.光标位置偏移量.  
  39.                 CompletionProposal proposal = new CompletionProposal(insert, start, offset - start, insert.length());  
  40.                 list.add(proposal);  
  41.             }  
  42.         }  
  43.         return list;  
  44.     }  
  45.       
  46.     /** 
  47.      * 方法说明: 获取提示前面的字符串. 
  48.      * @param doc 
  49.      * @param offest 
  50.      * @return 
  51.      */  
  52.     public String getFrontText(IDocument doc, int offest) {  
  53.         StringBuffer buf = new StringBuffer();  
  54.         while(true) {   //循环添加关键字.  
  55.             try {  
  56.                 char c = doc.getChar(--offest);  
  57.                 start = offest;  
  58.                 if(Character.isWhitespace(c)) {  
  59.                     start++;  
  60.                     break;  
  61.                 }  
  62.                 if(c == ';' || c == '(' || c == ')' || c == '{' || c == '}' || c == ',') {  //结束符号.  
  63.                     start++;  
  64.                     break;  
  65.                 }  
  66.                 buf.append(c);  
  67.             } catch (BadLocationException e) {  
  68.                 break;  
  69.             }  
  70.         }  
  71.         return buf.reverse().toString();  
  72.     }  
  73. }  

以上就是大部分代码了.

0 0
原创粉丝点击