Update of /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9420/src/org/hibernate/eclipse/hqleditor Modified Files: HQLWordDetector.java HQLSourceViewerConfiguration.java HQLEditor.java HQLCodeScanner.java HQLCompletionProcessor.java Added Files: MinimalDiffContextInformationValidator.java HQLColors.java DisplayStringProposalComparator.java Removed Files: HQLColorProvider.java Log Message: fix license issues --- NEW FILE: MinimalDiffContextInformationValidator.java --- package org.hibernate.eclipse.hqleditor; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.TextPresentation; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.jface.text.contentassist.IContextInformationPresenter; import org.eclipse.jface.text.contentassist.IContextInformationValidator; /** Will validate context information that were generated within a certain offset. */ class MinimalDiffContextInformationValidator implements IContextInformationValidator, IContextInformationPresenter { protected int basedOffset; private final int minimal; public MinimalDiffContextInformationValidator(int minimal) { this.minimal = minimal; } public boolean isContextInformationValid( int offset ) { return Math.abs( basedOffset - offset ) < minimal; } public boolean updatePresentation( int position, TextPresentation presentation ) { return true; } public void install( IContextInformation info, ITextViewer viewer, int offset ) { basedOffset = offset; } } --- NEW FILE: HQLColors.java --- package org.hibernate.eclipse.hqleditor; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Display; public class HQLColors { public static final RGB HQL_COMMENT_COLOR = new RGB( 0, 0, 128 ); public static final RGB HQL_MULTILINE_COMMENT_COLOR = new RGB( 128, 0, 0 ); public static final RGB HQL_QUOTED_LITERAL_COLOR = new RGB( 0, 0, 255 ); public static final RGB HQL_KEYWORD_COLOR = new RGB( 128, 0, 0 ); public static final RGB HQL_IDENTIFIER_COLOR = new RGB( 0, 0, 128 ); public static final RGB HQL_DEFAULT_COLOR = new RGB( 0, 0, 0 ); protected Map colorTable = new HashMap(10); public void dispose() { Iterator e = colorTable.values().iterator(); while (e.hasNext()) ((Color) e.next()).dispose(); } public Color getColor( RGB rgb ) { Color color = (Color) colorTable.get( rgb ); if (color == null) { color = new Color( Display.getCurrent(), rgb ); colorTable.put( rgb, color ); } return color; } } --- NEW FILE: DisplayStringProposalComparator.java --- package org.hibernate.eclipse.hqleditor; import java.util.Comparator; import org.eclipse.jface.text.contentassist.ICompletionProposal; /** * Sort a comparator according to its display string. * Use the INSTANCE variable to save memory/time. */ public class DisplayStringProposalComparator implements Comparator { static public Comparator INSTANCE = new DisplayStringProposalComparator(); public int compare( Object o1, Object o2 ) { ICompletionProposal c1 = (ICompletionProposal) o1; ICompletionProposal c2 = (ICompletionProposal) o2; return c1.getDisplayString().compareTo( c2.getDisplayString() ); } } Index: HQLWordDetector.java =================================================================== RCS file: /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLWordDetector.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- HQLWordDetector.java 16 Aug 2005 15:14:13 -0000 1.1 +++ HQLWordDetector.java 13 Feb 2006 15:55:24 -0000 1.2 @@ -2,10 +2,6 @@ import org.eclipse.jface.text.rules.IWordDetector; -/** - * This class determines whether a given character is valid as part of an HQL keyword - * in the current context. - */ public class HQLWordDetector implements IWordDetector { public boolean isWordStart( char c ) { Index: HQLSourceViewerConfiguration.java =================================================================== RCS file: /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLSourceViewerConfiguration.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- HQLSourceViewerConfiguration.java 16 Aug 2005 15:14:13 -0000 1.1 +++ HQLSourceViewerConfiguration.java 13 Feb 2006 15:55:24 -0000 1.2 @@ -13,172 +13,86 @@ import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; import org.eclipse.jface.text.rules.DefaultDamagerRepairer; import org.eclipse.jface.text.rules.Token; -import org.eclipse.jface.text.source.IAnnotationHover; import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.SourceViewerConfiguration; -/** - * This class defines the editor add-ons; content assist, content formatter, - * highlighting, auto-indent strategy, double click strategy. - */ public class HQLSourceViewerConfiguration extends SourceViewerConfiguration { - /** The completion processor for completing the user's typing. */ - private HQLCompletionProcessor fCompletionProcessor; + public final static String HQL_PARTITIONING= "__hql_partitioning"; //$NON-NLS-1$ + private HQLCompletionProcessor completionProcessor; private HQLEditor hqlEditor; - /** - * This class implements a single token scanner. - */ static class SingleTokenScanner extends BufferedRuleBasedScanner { public SingleTokenScanner( TextAttribute attribute ) { setDefaultReturnToken( new Token( attribute )); } } - /** - * Constructs an instance of this class with the given HQLEditor to - * configure. - * @param editor - * - * @param editor the HQLEditor to configure - */ public HQLSourceViewerConfiguration(HQLEditor editor) { hqlEditor = editor; - fCompletionProcessor = new HQLCompletionProcessor(editor); + completionProcessor = new HQLCompletionProcessor(editor); } public HQLEditor getHQLEditor() { return hqlEditor; } - - /** - * Returns the annotation hover which will provide the information to be - * shown in a hover popup window when requested for the given - * source viewer. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer) - */ - public IAnnotationHover getAnnotationHover( ISourceViewer sourceViewer ) { - return null; //new HQLAnnotationHover(); - } - - /** - * Returns the auto indentation strategy ready to be used with the given source viewer - * when manipulating text of the given content type. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoIndentStrategy(org.eclipse.jface.text.source.ISourceViewer, java.lang.String) - */ - /* - public IAutoIndentStrategy getAutoIndentStrategy( ISourceViewer sourceViewer, String contentType ) { - return (IDocument.DEFAULT_CONTENT_TYPE.equals( contentType ) ? new HQLAutoIndentStrategy() : new DefaultAutoIndentStrategy()); - }*/ - - public final static String HQL_PARTITIONING= "__hql_partitioning"; //$NON-NLS-1$ - - /** - * Returns the configured partitioning for the given source viewer. The partitioning is - * used when the querying content types from the source viewer's input document. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer) - */ + public String getConfiguredDocumentPartitioning( ISourceViewer sourceViewer ) { return HQL_PARTITIONING; } - /** - * Creates, initializes, and returns the ContentAssistant to use with this editor. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(ISourceViewer) - */ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ContentAssistant assistant = new ContentAssistant(); assistant.setDocumentPartitioning( getConfiguredDocumentPartitioning( sourceViewer )); - // Set content assist processors for various content types. - fCompletionProcessor = new HQLCompletionProcessor(hqlEditor); - assistant.setContentAssistProcessor( fCompletionProcessor, IDocument.DEFAULT_CONTENT_TYPE); + completionProcessor = new HQLCompletionProcessor(hqlEditor); + assistant.setContentAssistProcessor( completionProcessor, IDocument.DEFAULT_CONTENT_TYPE); - // Configure how content assist information will appear. assistant.enableAutoActivation( true ); assistant.setAutoActivationDelay( 500 ); - assistant.setProposalPopupOrientation( IContentAssistant.PROPOSAL_STACKED ); - assistant.setContextInformationPopupOrientation( IContentAssistant.CONTEXT_INFO_ABOVE ); - //assistant.setContextInformationPopupBackground( HQLEditorPlugin.getDefault().getHQLColorProvider().getColor( new RGB( 150, 150, 0 ))); - //Set to Carolina blue -// assistant.setContextInformationPopupBackground( HQLEditorPlugin.getDefault().getHQLColorProvider().getColor( new RGB( 0, 191, 255 ))); + assistant.setProposalPopupOrientation( IContentAssistant.PROPOSAL_STACKED ); return assistant; } - /** - * Creates, configures, and returns the ContentFormatter to use. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentFormatter(ISourceViewer) - */ public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) { ContentFormatter formatter = new ContentFormatter(); formatter.setDocumentPartitioning( HQL_PARTITIONING ); IFormattingStrategy formattingStrategy = new HQLFormattingStrategy(); formatter.setFormattingStrategy( formattingStrategy, IDocument.DEFAULT_CONTENT_TYPE ); - - + return formatter; } - /** - * Creates, configures, and returns a presentation reconciler to help with - * document changes. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(ISourceViewer) - */ public IPresentationReconciler getPresentationReconciler( ISourceViewer sourceViewer ) { - // Get the color provider. - HQLColorProvider colorProvider = new HQLColorProvider(); + HQLColors colorProvider = new HQLColors(); - // Create a presentation reconciler to handle handle document changes. PresentationReconciler reconciler = new PresentationReconciler(); String docPartitioning = getConfiguredDocumentPartitioning( sourceViewer ); reconciler.setDocumentPartitioning( docPartitioning ); - // Add a "damager-repairer" for changes in default text (HQL code). DefaultDamagerRepairer dr = new DefaultDamagerRepairer( new HQLCodeScanner( colorProvider ) ); reconciler.setDamager( dr, IDocument.DEFAULT_CONTENT_TYPE ); reconciler.setRepairer( dr, IDocument.DEFAULT_CONTENT_TYPE ); - // Add a "damager-repairer" for changes witin one-line HQL comments. - dr = new DefaultDamagerRepairer( new SingleTokenScanner( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_COMMENT_COLOR )))); + dr = new DefaultDamagerRepairer( new SingleTokenScanner( new TextAttribute( colorProvider.getColor( HQLColors.HQL_COMMENT_COLOR )))); reconciler.setDamager( dr, HQLPartitionScanner.HQL_COMMENT ); reconciler.setRepairer( dr, HQLPartitionScanner.HQL_COMMENT ); - // Add a "damager-repairer" for changes witin quoted literals. - dr = new DefaultDamagerRepairer( new SingleTokenScanner( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_QUOTED_LITERAL_COLOR )))); + dr = new DefaultDamagerRepairer( new SingleTokenScanner( new TextAttribute( colorProvider.getColor( HQLColors.HQL_QUOTED_LITERAL_COLOR )))); reconciler.setDamager( dr, HQLPartitionScanner.HQL_QUOTED_LITERAL ); reconciler.setRepairer( dr, HQLPartitionScanner.HQL_QUOTED_LITERAL ); - // Add a "damager-repairer" for changes witin delimited identifiers. - dr = new DefaultDamagerRepairer( new SingleTokenScanner( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_DELIMITED_IDENTIFIER_COLOR )))); - reconciler.setDamager( dr, HQLPartitionScanner.HQL_DELIMITED_IDENTIFIER ); - reconciler.setRepairer( dr, HQLPartitionScanner.HQL_DELIMITED_IDENTIFIER ); - return reconciler; } - /** - * Returns the text hover which will provide the information to be shown - * in a text hover popup window when requested for the given source viewer and - * the given content type. - * - * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String) - */ public ITextHover getTextHover( ISourceViewer sourceViewer, String contentType ) { return new HQLTextHover(); } - - -} // end class + +} Index: HQLEditor.java =================================================================== RCS file: /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- HQLEditor.java 7 Feb 2006 16:17:48 -0000 1.6 +++ HQLEditor.java 13 Feb 2006 15:55:24 -0000 1.7 @@ -355,8 +355,8 @@ * * @return the HQL color provider */ - public HQLColorProvider getHQLColorProvider() { - return new HQLColorProvider(); + public HQLColors getHQLColorProvider() { + return new HQLColors(); } /** Index: HQLCodeScanner.java =================================================================== RCS file: /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCodeScanner.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- HQLCodeScanner.java 25 Aug 2005 12:18:57 -0000 1.2 +++ HQLCodeScanner.java 13 Feb 2006 15:55:24 -0000 1.3 @@ -15,15 +15,11 @@ import org.eclipse.jface.text.rules.WhitespaceRule; import org.eclipse.jface.text.rules.WordRule; import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Display; -/** - * This class implements a RuleBaseScanner for HQL source code text. - */ public class HQLCodeScanner extends RuleBasedScanner { - /** Defines the HQL keywords. */ - private static String[] fgKeywords = { + /** Defines the HQL keywords. Based on hql.g antlr grammer in 2005 ;) */ + private static String[] hqlKeywords = { "between", //$NON-NLS-1$ "class", //$NON-NLS-1$ "delete", //$NON-NLS-1$ @@ -87,199 +83,159 @@ }; - /** Defines the SQL built-in function names. */ - private static String[] fgFunctions = { - "log", //$NON-NLS-1$ - "roundmagic", //$NON-NLS-1$ - "rand", //$NON-NLS-1$ - "sign", //$NON-NLS-1$ - "char", //$NON-NLS-1$ - "curdate", //$NON-NLS-1$ - "radians", //$NON-NLS-1$ - "mod", //$NON-NLS-1$ - "pi", //$NON-NLS-1$ - "coalesce", //$NON-NLS-1$ - "ltrim", //$NON-NLS-1$ - "month", //$NON-NLS-1$ - "acos", //$NON-NLS-1$ - "day", //$NON-NLS-1$ - "current_time", //$NON-NLS-1$ - "curtime", //$NON-NLS-1$ - "hextoraw", //$NON-NLS-1$ - "cast", //$NON-NLS-1$ - "space", //$NON-NLS-1$ - "second", //$NON-NLS-1$ - "hour", //$NON-NLS-1$ - "lcase", //$NON-NLS-1$ - "avg", //$NON-NLS-1$ - "monthname", //$NON-NLS-1$ - "soundex", //$NON-NLS-1$ - "extract", //$NON-NLS-1$ - "rawtohex", //$NON-NLS-1$ - "quater", //$NON-NLS-1$ - "current_date", //$NON-NLS-1$ - "abs", //$NON-NLS-1$ - "trim", //$NON-NLS-1$ - "bit_length", //$NON-NLS-1$ - "nullif", //$NON-NLS-1$ - "ucase", //$NON-NLS-1$ - "cot", //$NON-NLS-1$ - "sqrt", //$NON-NLS-1$ - "max", //$NON-NLS-1$ - "week", //$NON-NLS-1$ - "now", //$NON-NLS-1$ - "atan", //$NON-NLS-1$ - "upper", //$NON-NLS-1$ - "lower", //$NON-NLS-1$ - "min", //$NON-NLS-1$ - "concat", //$NON-NLS-1$ - "ceiling", //$NON-NLS-1$ - "asin", //$NON-NLS-1$ - "degrees", //$NON-NLS-1$ - "sum", //$NON-NLS-1$ - "locate", //$NON-NLS-1$ - "user", //$NON-NLS-1$ - "sin", //$NON-NLS-1$ - "tan", //$NON-NLS-1$ - "substring", //$NON-NLS-1$ - "length", //$NON-NLS-1$ - "dayname", //$NON-NLS-1$ - "floor", //$NON-NLS-1$ - "dayofweek", //$NON-NLS-1$ - "dayofyear", //$NON-NLS-1$ - "str", //$NON-NLS-1$ - "dayofmonth", //$NON-NLS-1$ - "ascii", //$NON-NLS-1$ - "reverse", //$NON-NLS-1$ - "exp", //$NON-NLS-1$ - "cos", //$NON-NLS-1$ - "year", //$NON-NLS-1$ - "current_timestamp", //$NON-NLS-1$ - "log10", //$NON-NLS-1$ - "minute", //$NON-NLS-1$ - "rtrim", //$NON-NLS-1$ - "count", //$NON-NLS-1$ - "database", //$NON-NLS-1$ - }; - - /** Defines an array of arrays that combines all the HQL language elements defined previously. */ - private static Object[] fgAllWords = { fgKeywords, fgFunctions }; + /** built-in function names. Various normal builtin functions in SQL/HQL. Maybe sShould try and do this dynamically based on dialect */ + private static String[] builtInFunctions = { + // standard sql92 functions + "substring", + "locate", + "trim", + "length", + "bit_length", + "coalesce", + "nullif", + "abs", + "mod", + "sqrt", + "upper", + "lower", + "cast", + "extract", + + // time functions mapped to ansi extract + "second", + "minute", + "hour", + "day", + "month", + "year", + + "str", + + // misc functions - based on oracle dialect + "sign", + "acos", + "asin", + "atan", + "cos", + "cosh", + "exp", + "ln", + "sin", + "sinh", + "stddev", + "sqrt", + "tan", + "tanh", + "variance", + + "round", + "trunc", + "ceil", + "floor", + + "chr", + "initcap", + "lower", + "ltrim", + "rtrim", + "soundex", + "upper", + "ascii", + "length", + "to_char", + "to_date", + + "current_date", + "current_time", + "current_timestamp", + "lastday", + "sysday", + "systimestamp", + "uid", + "user", + + "rowid", + "rownum", + + "concat", + "instr", + "instrb", + "lpad", + "replace", + "rpad", + "substr", + "substrb", + "translate", + + "substring", + "locate", + "bit_length", + "coalesce", + + "atan2", + "log", + "mod", + "nvl", + "nvl2", + "power", + + "add_months", + "months_between", + "next_day", + + }; static { - Arrays.sort(fgFunctions); - Arrays.sort(fgKeywords); + // for performance in search. + Arrays.sort(builtInFunctions); + Arrays.sort(hqlKeywords); } - /** - * This class determines if a character is a whitespace character. - */ - public class HQLWhiteSpaceDetector implements IWhitespaceDetector { + static public class HQLWhiteSpaceDetector implements IWhitespaceDetector { public boolean isWhitespace( char c ) { return Character.isWhitespace( c ); } - } // end inner class + } - /** - * Constructs an instance of this class using the given color provider. - */ - public HQLCodeScanner( HQLColorProvider colorProvider ) { - // Define tokens for the SQL language elements. - IToken commentToken = null; - IToken stringToken = null; - IToken keywordToken = null; - IToken functionToken = null; -// IToken identifierToken = null; - IToken delimitedIdentifierToken = null; - IToken otherToken = null; + public HQLCodeScanner( HQLColors colorProvider ) { + final IToken commentToken = new Token( new TextAttribute( colorProvider.getColor( HQLColors.HQL_COMMENT_COLOR ))); + final IToken stringToken = new Token( new TextAttribute( colorProvider.getColor( HQLColors.HQL_QUOTED_LITERAL_COLOR ))); + final IToken keywordToken = new Token( new TextAttribute( colorProvider.getColor( HQLColors.HQL_KEYWORD_COLOR ), null, SWT.BOLD)); + final IToken functionToken = new Token( new TextAttribute( colorProvider.getColor( HQLColors.HQL_KEYWORD_COLOR ))); + final IToken otherToken = new Token( new TextAttribute( colorProvider.getColor( HQLColors.HQL_DEFAULT_COLOR ))); - /* On "high contrast" displays the default text color (black) is a problem, - * since the normal high contrast background is black. ("High contrast" is - * a Windows feature that helps vision-impaired people.) When high contrast mode is enabled, - * use different colors that show up better against a black background. - */ - if (Display.getDefault().getHighContrast() == true) { - commentToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_COMMENT_COLOR ))); - stringToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_QUOTED_LITERAL_COLOR ))); - keywordToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_KEYWORD_COLOR ))); - functionToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_KEYWORD_COLOR ))); - delimitedIdentifierToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_DELIMITED_IDENTIFIER_COLOR ))); - otherToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_HC_DEFAULT_COLOR ))); - } - else { - commentToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_COMMENT_COLOR ))); - stringToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_QUOTED_LITERAL_COLOR ))); - keywordToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_KEYWORD_COLOR ), null, SWT.BOLD)); - functionToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_KEYWORD_COLOR ))); - delimitedIdentifierToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_DELIMITED_IDENTIFIER_COLOR ))); - otherToken = new Token( new TextAttribute( colorProvider.getColor( HQLColorProvider.HQL_DEFAULT_COLOR ))); - } - setDefaultReturnToken( otherToken ); List rules = new ArrayList(); - // Add rule for single-line comments. rules.add( new EndOfLineRule( "--", commentToken )); //$NON-NLS-1$ - - // Add rules for delimited identifiers and string literals. rules.add( new SingleLineRule( "'", "'", stringToken, '\\' )); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - rules.add( new SingleLineRule( "\"", "\"", delimitedIdentifierToken, '\\' )); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - - // Add generic whitespace rule. rules.add( new WhitespaceRule( new HQLWhiteSpaceDetector() )); - // Define a word rule and add SQL keywords to it. WordRule wordRule = new WordRule( new HQLWordDetector(), otherToken ); - String[] reservedWords = getHQLKeywords(); - for (int i = 0; i < reservedWords.length; i++) { - wordRule.addWord( reservedWords[i], keywordToken ); - wordRule.addWord( reservedWords[i].toLowerCase(), keywordToken ); - } - - // Add the SQL function names to the word rule. - String[] functions = getHQLFunctionNames(); - for (int i = 0; i< functions.length; i++) { - wordRule.addWord( functions[i], functionToken ); - wordRule.addWord( functions[i].toLowerCase(), functionToken ); - } + addWordRules( keywordToken, wordRule, getHQLKeywords() ); + addWordRules( functionToken, wordRule, getHQLFunctionNames() ); - // Add the word rule to the list of rules. rules.add( wordRule ); - // Convert the list of rules to an array and return it. - IRule[] result = new IRule[ rules.size() ]; - rules.toArray( result ); - setRules( result ); + setRules( (IRule[]) rules.toArray( new IRule[ rules.size() ] ) ); } - /** - * Gets an array of HQL keywords - * - * @return the HQL keywords - */ + private void addWordRules(final IToken token, WordRule wordRule, String[] reservedWords) { + for (int i = 0; i < reservedWords.length; i++) { + wordRule.addWord( reservedWords[i], token ); + wordRule.addWord( reservedWords[i].toLowerCase(), token ); + } + } + public static String[] getHQLKeywords() { - return fgKeywords; + return hqlKeywords; } - /** - * Gets an array of HQL function names - * - * @return the HQL function names - */ public static String[] getHQLFunctionNames() { - return fgFunctions; + return builtInFunctions; } - /** - * Gets an array of arrays containing all HQL words, including keywords - * and function names - * - * @return the array of SQL words - */ - public static Object[] getSQLAllWords() { - return fgAllWords; - } - } // end class Index: HQLCompletionProcessor.java =================================================================== RCS file: /cvsroot/hibernate/HibernateExt/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCompletionProcessor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- HQLCompletionProcessor.java 7 Feb 2006 16:17:48 -0000 1.2 +++ HQLCompletionProcessor.java 13 Feb 2006 15:55:24 -0000 1.3 @@ -10,11 +10,9 @@ import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.ITypedRegion; -import org.eclipse.jface.text.TextPresentation; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; import org.eclipse.jface.text.contentassist.IContextInformation; -import org.eclipse.jface.text.contentassist.IContextInformationPresenter; import org.eclipse.jface.text.contentassist.IContextInformationValidator; import org.hibernate.cfg.Configuration; import org.hibernate.console.ConsoleConfiguration; @@ -24,71 +22,18 @@ */ public class HQLCompletionProcessor implements IContentAssistProcessor { - private char[] fProposalAutoActivationSet; - - /** - * Simple content assist tip closer. The tip is valid in a range of 5 - * characters around its popup location. - */ - protected static class Validator implements IContextInformationValidator, IContextInformationPresenter { - protected int fInstallOffset; - - public boolean isContextInformationValid( int offset ) { - return Math.abs( fInstallOffset - offset ) < 5; - } - - public void install( IContextInformation info, ITextViewer viewer, int offset ) { - fInstallOffset = offset; - } - - public boolean updatePresentation( int position, TextPresentation presentation ) { - return true; - } - }; - - private static class CompletionProposalComparator implements Comparator { - public int compare( Object o1, Object o2 ) { - ICompletionProposal c1 = (ICompletionProposal) o1; - ICompletionProposal c2 = (ICompletionProposal) o2; - return c1.getDisplayString().compareTo( c2.getDisplayString() ); - } - }; - - protected IContextInformationValidator fValidator = new Validator(); - - private Comparator fComparator; - + private char[] autoActivationChars = { '.' }; + protected IContextInformationValidator validator = new MinimalDiffContextInformationValidator(5); + private final Comparator completionComparator; private final HQLEditor editor; - + private String errorMessage; + public HQLCompletionProcessor(HQLEditor editor) { super(); this.editor = editor; - - // activation/trigger to invoke content assist - char[] completionChars = { '.' }; - setCompletionProposalAutoActivationCharacters( completionChars ); - - fComparator = new CompletionProposalComparator(); - } - - /** - * Tells this processor to order the proposals alphabetically. - * - * @param order <code>true</code> if proposals should be ordered. - */ - public void orderProposalsAlphabetically( boolean order ) { - fComparator = order ? new CompletionProposalComparator() : null; + completionComparator = DisplayStringProposalComparator.INSTANCE; } - /** - * Returns a list of proposed content completions based on the specified - * location within the document that corresponds to the current cursor - * position within the text-editor control. - * - * @param viewer the viewer whose document is used to compute the proposals - * @param documentPosition a location within the document - * @return an array of content-assist proposals - */ public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int documentOffset ) { ICompletionProposal[] result = null; @@ -97,62 +42,60 @@ ITypedRegion partition = null; if (documentOffset > 0) { - if (doc.getChar( documentOffset - 1 ) == ';') - partition = viewer.getDocument().getPartition( documentOffset ); - else - // for incomplete statement. - partition = viewer.getDocument().getPartition( documentOffset - 1 ); + partition = viewer.getDocument().getPartition( documentOffset - 1 ); + } + else { + partition = viewer.getDocument().getPartition( documentOffset ); } - else - partition = viewer.getDocument().getPartition( documentOffset ); result = computeProposals( doc, partition, documentOffset ); } catch (BadLocationException x) { - } - ; + } if (result != null) - result = order( result ); + result = sort( result ); return result; } - private ICompletionProposal[] computeProposals(IDocument doc, ITypedRegion partition, int documentOffset) { + private ICompletionProposal[] computeProposals(IDocument doc, ITypedRegion partition, final int currentOffset) { ICompletionProposal[] result = null; - try { - if (doc != null && partition != null && documentOffset >= 0) { - - List proposalList = new ArrayList(); - String replacementStr = null; - String displayStr = null; - String startWord = null; - char docOffsetChar; - - // Get document offset to the start of "word" where content assist - // is requested. - int offset = getPartitionOffset( doc, partition, documentOffset, partition.getOffset() ); - - try { - startWord = doc.get( offset, documentOffset - offset ); - - findMatchingEntities( documentOffset, proposalList, startWord, editor.getConsoleConfiguration() ); - findMatchingWords( documentOffset, proposalList, startWord, HQLCodeScanner.getHQLKeywords(), "keyword" ); - findMatchingWords( documentOffset, proposalList, startWord, HQLCodeScanner.getHQLFunctionNames(), "function"); - - - result = (ICompletionProposal[]) proposalList.toArray(new ICompletionProposal[proposalList.size()]); - - } - catch (BadLocationException exc) { - } - } - } catch (Throwable te) { - te.printStackTrace(); + if (doc != null && partition != null && currentOffset >= 0) { + + List proposalList = new ArrayList(); + String startWord = null; + + int startOffset = findNearestWhiteSpace( doc, currentOffset, partition.getOffset() ); + + int wordLength = currentOffset - startOffset; + startWord = getWord( doc, startOffset, wordLength ); + + if(startWord!=null) { + findMatchingEntities( currentOffset, proposalList, startWord, editor.getConsoleConfiguration() ); + findMatchingWords( currentOffset, proposalList, startWord, HQLCodeScanner.getHQLKeywords(), "keyword" ); + findMatchingWords( currentOffset, proposalList, startWord, HQLCodeScanner.getHQLFunctionNames(), "function"); + + result = (ICompletionProposal[]) proposalList.toArray(new ICompletionProposal[proposalList.size()]); + if(result.length==0) { + errorMessage = "No HQL completions avaialable."; + } + } else { + errorMessage = "No start word found."; + } } - - return result; + + return result; + } + + private String getWord(IDocument doc, int startOffset, int wordLength) { + try { + return doc.get( startOffset, wordLength ); + } + catch (BadLocationException e) { + return null; // no word found. + } } private void findMatchingEntities(int documentOffset, List proposalList, String startWord, ConsoleConfiguration lastUsedConfiguration) { @@ -186,179 +129,42 @@ } } - /** - * Returns a list of content-assist tips based on the specified location - * within the document that corresponds to the current cursor position - * within the text-editor control. - * - * @param viewer the viewer whose document is used to compute the tips - * @param documentPosition a location within the document - * @return an array of content-assist tips - */ - - public IContextInformation[] computeContextInformation( ITextViewer viewer, int documentOffset ) { - IContextInformation[] result = null; - return result; - } - - /** - * Returns a string of characters which when pressed should automatically - * display content-assist proposals. - * - * @see IContentAssistProcessor.getCompletionProposalAutoActivationCharacters() - * - * @return string of characters - */ - public char[] getCompletionProposalAutoActivationCharacters() { - return fProposalAutoActivationSet; - } - - /** - * Returns a string of characters which when pressed should automatically - * display a content-assist tip. - * - * @return string of characters - */ - public char[] getContextInformationAutoActivationCharacters() { - return new char[] { '#' }; - } - - /** - * Returns a delegate used to determine when a displayed tip should be - * dismissed. - * - * @return a tip closer - */ - public IContextInformationValidator getContextInformationValidator() { - return fValidator; - } + public IContextInformation[] computeContextInformation( ITextViewer viewer, int documentOffset ) { return null; } + public char[] getCompletionProposalAutoActivationCharacters() { return autoActivationChars; } - /** - * Returns the reason why the content-assist processor was unable to produce - * any proposals or tips. - * - * @return an error message or null if no error occurred - */ - public String getErrorMessage() { - return null; - } + public char[] getContextInformationAutoActivationCharacters() { return new char[0]; } - /** - * Orders the given proposals. - * - * @params ICompletionProposal[] List of proposals to be ordered - */ - private ICompletionProposal[] order( ICompletionProposal[] proposals ) { - if (fComparator != null) - Arrays.sort( proposals, fComparator ); - return proposals; - } + public IContextInformationValidator getContextInformationValidator() { return validator; } - /** - * Sets this processor's set of characters triggering the activation of the - * completion proposal computation. - * - * @param activationSet the activation set - */ - public void setCompletionProposalAutoActivationCharacters( char[] activationSet ) { - fProposalAutoActivationSet = activationSet; + public String getErrorMessage() { + return errorMessage; } - /** - * Returns the document offset to the start of the "word" where content - * assist is requested. - * - * @param doc the current document - * @param partition document partition region. A region consists of offset, - * length, and type. - * @param documentOffset offset into the document - * @param offset offset in the document to start of the name preceeding the - * activation character - */ - public int getPartitionOffset( IDocument doc, ITypedRegion partition, int documentOffset, int offset ) { - boolean loop = true; - - int offset1 = documentOffset - 1; - try { - while (loop && offset <= offset1) { - switch (doc.getChar( offset1 )) { - case 10: // linefeed - case 32: // space - case 13: // return - case 9: { // tab - loop = false; - } - break; - default: { - offset1--; - } - break; - } - } - } - catch (BadLocationException x) { - } - - offset = offset1 + 1; - - return offset; + private ICompletionProposal[] sort( ICompletionProposal[] proposals ) { + Arrays.sort( proposals, completionComparator ); + return proposals; } - /** - * Returns the document offset to the start of the "word" where content - * assist is requested. - * - * @param doc the current document - * @param partition document partition region. A region consists of offset, - * length, and type. - * @param documentOffset offset into the document - * @param leadingString - * @param position <code>1</code> from current position <code>0</code> - * after leadingString - * @return the partition offset - */ - public int getPartitionOffset( IDocument doc, ITypedRegion partition, int documentOffset, String leadingString, - int position ) { - int offset = partition.getOffset() + leadingString.length(); - - if (documentOffset <= offset) - return offset; - - switch (position) { - case 0: { - } - break; - default: { - boolean loop = true; - int offset1 = documentOffset - 1; - try { - while (loop && offset <= offset1) { - switch (doc.getChar( offset1 )) { - case 10: // linefeed - case 32: // space - case 13: // return - case 9: { // tab - loop = false; - } - break; - default: { - offset1--; - } - break; - } - } - } - catch (BadLocationException x) { - } - ; - - offset = offset1 + 1; - } - break; - } - - return getPartitionOffset( doc, partition, documentOffset, offset ); + public int findNearestWhiteSpace( IDocument doc, int documentOffset, int offset ) { + boolean loop = true; + + int tmpOffset = documentOffset - 1; + try { + while (loop && offset <= tmpOffset) { + if(Character.isWhitespace(doc.getChar(tmpOffset))) { + loop = false; + } else { + tmpOffset--; + } + } + } + catch (BadLocationException x) { + } + + offset = tmpOffset + 1; + + return offset; } -} \ No newline at end of file + } \ No newline at end of file --- HQLColorProvider.java DELETED --- |