From: <dal...@us...> - 2006-10-01 16:48:10
|
Revision: 7151 http://svn.sourceforge.net/jedit/?rev=7151&view=rev Author: daleanson Date: 2006-10-01 09:48:06 -0700 (Sun, 01 Oct 2006) Log Message: ----------- turned on code completion by mode keyword Modified Paths: -------------- plugins/SideKick/trunk/sidekick/SideKickParser.java Modified: plugins/SideKick/trunk/sidekick/SideKickParser.java =================================================================== --- plugins/SideKick/trunk/sidekick/SideKickParser.java 2006-10-01 01:00:43 UTC (rev 7150) +++ plugins/SideKick/trunk/sidekick/SideKickParser.java 2006-10-01 16:48:06 UTC (rev 7151) @@ -1,29 +1,30 @@ /* - * SideKickParser.java - * :tabSize=8:indentSize=8:noTabs=false: - * :folding=explicit:collapseFolds=1: - * - * Copyright 2003 Slava Pestov - * 2005 Robert McKinnon - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ +* SideKickParser.java +* :tabSize=8:indentSize=8:noTabs=false: +* :folding=explicit:collapseFolds=1: +* +* Copyright 2003 Slava Pestov +* 2005 Robert McKinnon +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ package sidekick; //{{{ Imports +import java.util.*; import org.gjt.sp.jedit.*; import errorlist.DefaultErrorSource; import org.gjt.sp.util.Log; @@ -52,8 +53,7 @@ * @version $Id$ * @author Slava Pestov */ -public abstract class SideKickParser -{ +public abstract class SideKickParser { public static final String SERVICE = "sidekick.SideKickParser"; //{{{ SideKickParser constructor @@ -61,8 +61,7 @@ * The parser constructor. * */ - public SideKickParser(String serviceName) - { + public SideKickParser( String serviceName ) { this.name = serviceName; } //}}} @@ -70,8 +69,7 @@ /** * Returns the parser's name. */ - public final String getName() - { + public final String getName() { return name; } //}}} @@ -81,9 +79,7 @@ * parser to implement this. * @since SideKick 0.3 */ - public void stop() - { - } //}}} + public void stop() {} //}}} //{{{ activate() method /** @@ -93,9 +89,7 @@ * @since SideKick 0.2 * @deprecated Use the form taking an <code>EditPane</code> instead. */ - public void activate(View view) - { - } //}}} + public void activate( View view ) {} //}}} //{{{ deactivate() method /** @@ -105,9 +99,7 @@ * @since SideKick 0.2 * @deprecated Use the form taking an <code>EditPane</code> instead. */ - public void deactivate(View view) - { - } //}}} + public void deactivate( View view ) {} //}}} //{{{ activate() method /** @@ -116,10 +108,9 @@ * @param editPane The edit pane * @since SideKick 0.3.1 */ - public void activate(EditPane editPane) - { - activate(editPane.getView()); - Log.log(Log.DEBUG,this,getName() + ": activated for " + editPane.getBuffer()); + public void activate( EditPane editPane ) { + activate( editPane.getView() ); + Log.log( Log.DEBUG, this, getName() + ": activated for " + editPane.getBuffer() ); } //}}} //{{{ deactivate() method @@ -129,10 +120,9 @@ * @param editPane The edit pane * @since SideKick 0.3.1 */ - public void deactivate(EditPane editPane) - { - deactivate(editPane.getView()); - Log.log(Log.DEBUG,this,getName() + ": deactivated"); + public void deactivate( EditPane editPane ) { + deactivate( editPane.getView() ); + Log.log( Log.DEBUG, this, getName() + ": deactivated" ); } //}}} //{{{ parse() method @@ -144,8 +134,8 @@ * * @return A new instance of the <code>SideKickParsedData</code> class. */ - public abstract SideKickParsedData parse(Buffer buffer, - DefaultErrorSource errorSource); + public abstract SideKickParsedData parse( Buffer buffer, + DefaultErrorSource errorSource ); //}}} //{{{ supportsCompletion() method @@ -154,34 +144,32 @@ * * Returns false by default. */ - public boolean supportsCompletion() - { - return false; + public boolean supportsCompletion() { + return true; } //}}} //{{{ canHandleBackspace() method /** - * <p> + * <p> * Returns true if the parser can handle - * the backspace key being typed when - * the completion popup is open, - * else false if not. - * </p><p> - * If false, the completion popup is - * closed when backspace is typed. + * the backspace key being typed when + * the completion popup is open, + * else false if not. + * </p><p> + * If false, the completion popup is + * closed when backspace is typed. * </p><p> - * If true, the - * {@link SideKickCompletion#handleKeystroke(int, char)} - * method must be overidden to handle receiving - * the backspace character, '\b', as a value - * for the keyChar parameter. - * </p><p> + * If true, the + * {@link SideKickCompletion#handleKeystroke(int, char)} + * method must be overidden to handle receiving + * the backspace character, '\b', as a value + * for the keyChar parameter. + * </p><p> * Returns false by default. - * </p> - * @since SideKick 0.3.4 + * </p> + * @since SideKick 0.3.4 */ - public boolean canHandleBackspace() - { + public boolean canHandleBackspace() { return false; } //}}} @@ -193,8 +181,7 @@ * * Returns true by default. */ - public boolean canCompleteAnywhere() - { + public boolean canCompleteAnywhere() { return true; } //}}} @@ -205,8 +192,7 @@ * Returns null by default. * */ - public String getInstantCompletionTriggers() - { + public String getInstantCompletionTriggers() { return null; } //}}} @@ -218,8 +204,7 @@ * @since SideKick 0.3 * */ - public String getParseTriggers() - { + public String getParseTriggers() { return null; } //}}} @@ -232,10 +217,56 @@ * @param editPane The edit pane involved. * @param caret The caret position. */ - public SideKickCompletion complete(EditPane editPane, int caret) - { + public SideKickCompletion complete( EditPane editPane, int caret ) { + try { + String[] keywords = editPane.getBuffer().getMode().getTokenMarker().getMainRuleSet().getKeywords().getKeywords(); + if (keywords.length > 0) { + String word = getWordAtCaret( editPane, caret ); + if (word != null && word.length() > 0) { + List possibles = new ArrayList(); + for (int i = 0; i < keywords.length; i++) { + if (keywords[i].startsWith(word)) { + possibles.add(keywords[i]); + } + } + Collections.sort(possibles); + return new ConcreteSideKickCompletion(editPane.getView(), word, possibles); + } + } + } + catch ( Exception e ) { + } return null; } //}}} - + + private String getWordAtCaret( EditPane editPane, int caret ) { + if ( caret <= 0 ) { + return ""; + } + Buffer buffer = editPane.getBuffer(); + String text = buffer.getText(0, caret); + Mode mode = buffer.getMode(); + String word_break_chars = ( String ) mode.getProperty( "wordBreakChars" ); + if ( word_break_chars == null ) { + word_break_chars = ""; + } + word_break_chars += " \n\r\t"; + int offset = 0; + for (int i = 0; i < word_break_chars.length(); i++) { + int maybe = text.lastIndexOf(word_break_chars.charAt(i)) + 1; + if (maybe > offset) { + offset = maybe; + } + } + return text.substring(offset); + } + + class ConcreteSideKickCompletion extends SideKickCompletion { + public ConcreteSideKickCompletion(View view, String word, List possibles) { + super(view, word, possibles); + } + } + + protected String name; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |