diff options
Diffstat (limited to 'plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations')
6 files changed, 0 insertions, 2016 deletions
diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AbstractAnnotationTagProposal.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AbstractAnnotationTagProposal.java deleted file mode 100644 index 680623279..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AbstractAnnotationTagProposal.java +++ /dev/null @@ -1,925 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2005, 2006 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jst.common.internal.annotations.ui; - -import java.io.IOException; -import java.net.URL; - -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.Platform; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.compiler.CharOperation; -import org.eclipse.jdt.ui.JavaUI; -import org.eclipse.jdt.ui.PreferenceConstants; -import org.eclipse.jface.preference.IPreferenceStore; -import org.eclipse.jface.preference.PreferenceConverter; -import org.eclipse.jface.text.Assert; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.BadPositionCategoryException; -import org.eclipse.jface.text.DefaultPositionUpdater; -import org.eclipse.jface.text.DocumentCommand; -import org.eclipse.jface.text.DocumentEvent; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IInformationControlCreator; -import org.eclipse.jface.text.IPositionUpdater; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.ITextViewer; -import org.eclipse.jface.text.ITextViewerExtension2; -import org.eclipse.jface.text.ITextViewerExtension5; -import org.eclipse.jface.text.Position; -import org.eclipse.jface.text.Region; -import org.eclipse.jface.text.contentassist.IContextInformation; -import org.eclipse.jface.text.link.ILinkedModeListener; -import org.eclipse.jface.text.link.LinkedModeModel; -import org.eclipse.jface.text.link.LinkedModeUI; -import org.eclipse.jface.text.link.LinkedPosition; -import org.eclipse.jface.text.link.LinkedPositionGroup; -import org.eclipse.jface.text.link.LinkedModeUI.ExitFlags; -import org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyleRange; -import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.events.VerifyEvent; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.RGB; -import org.eclipse.ui.texteditor.link.EditorLinkedModeUI; -import org.osgi.framework.Bundle; -import org.eclipse.jst.common.internal.annotations.ui.UiPlugin; - -/** - * - * @since 3.2 - */ -public abstract class AbstractAnnotationTagProposal { - /** - * A class to simplify tracking a reference position in a document. - */ - static final class ReferenceTracker { - - /** The reference position category name. */ - private static final String CATEGORY= "reference_position"; //$NON-NLS-1$ - /** The position updater of the reference position. */ - private final IPositionUpdater fPositionUpdater= new DefaultPositionUpdater(CATEGORY); - /** The reference position. */ - private final Position fPosition= new Position(0); - - /** - * Called before document changes occur. It must be followed by a call to postReplace(). - * - * @param document the document on which to track the reference position. - * - */ - public void preReplace(IDocument document, int offset) throws BadLocationException { - fPosition.setOffset(offset); - try { - document.addPositionCategory(CATEGORY); - document.addPositionUpdater(fPositionUpdater); - document.addPosition(CATEGORY, fPosition); - - } catch (BadPositionCategoryException e) { - // should not happen - UiPlugin.logError(e); - } - } - - /** - * Called after the document changed occurred. It must be preceded by a call to preReplace(). - * - * @param document the document on which to track the reference position. - */ - public int postReplace(IDocument document) { - try { - document.removePosition(CATEGORY, fPosition); - document.removePositionUpdater(fPositionUpdater); - document.removePositionCategory(CATEGORY); - - } catch (BadPositionCategoryException e) { - // should not happen - UiPlugin.logError(e); - } - return fPosition.getOffset(); - } - } - - protected static final class ExitPolicy implements IExitPolicy { - - final char fExitCharacter; - private final IDocument fDocument; - - public ExitPolicy(char exitCharacter, IDocument document) { - fExitCharacter= exitCharacter; - fDocument= document; - } - - /* - * @see org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager, org.eclipse.swt.events.VerifyEvent, int, int) - */ - public ExitFlags doExit(LinkedModeModel environment, VerifyEvent event, int offset, int length) { - - if (event.character == fExitCharacter) { - if (environment.anyPositionContains(offset)) - return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false); - return new ExitFlags(ILinkedModeListener.UPDATE_CARET, true); - } - - switch (event.character) { - case ';': - return new ExitFlags(ILinkedModeListener.NONE, true); - case SWT.CR: - // when entering an anonymous class as a parameter, we don't want - // to jump after the parenthesis when return is pressed - if (offset > 0) { - try { - if (fDocument.getChar(offset - 1) == '{') - return new ExitFlags(ILinkedModeListener.EXIT_ALL, true); - } catch (BadLocationException e) { - } - } - } - return null; - } - - } - - private String fDisplayString; - private String fReplacementString; - private int fReplacementOffset; - private int fReplacementLength; - private int fCursorPosition; - private Image fImage; - private IContextInformation fContextInformation; - //private ProposalInfo fProposalInfo; - private char[] fTriggerCharacters; - private String fSortString; - private int fRelevance; - private boolean fIsInJavadoc; - - private StyleRange fRememberedStyleRange; - private boolean fToggleEating; - private ITextViewer fTextViewer; - - /** - * The URL of the style sheet (css). - * @since 3.2 - */ - private URL fStyleSheetURL; - - protected AbstractAnnotationTagProposal() { - } - - /* - * @see ICompletionProposalExtension#getTriggerCharacters() - */ - public char[] getTriggerCharacters() { - return fTriggerCharacters; - } - - /** - * Sets the trigger characters. - * - * @param triggerCharacters The set of characters which can trigger the application of this - * completion proposal - */ - public void setTriggerCharacters(char[] triggerCharacters) { - fTriggerCharacters= triggerCharacters; - } - - /** - * Sets the proposal info. - * - * @param proposalInfo The additional information associated with this proposal or - * <code>null</code> - */ -// public void setProposalInfo(ProposalInfo proposalInfo) { -// fProposalInfo= proposalInfo; -// } - - /** - * Returns the additional proposal info, or <code>null</code> if none exists. - * - * @return the additional proposal info, or <code>null</code> if none exists - */ -// protected ProposalInfo getProposalInfo() { -// return fProposalInfo; -// } - - /** - * Sets the cursor position relative to the insertion offset. By default this is the length of - * the completion string (Cursor positioned after the completion) - * - * @param cursorPosition The cursorPosition to set - */ - public void setCursorPosition(int cursorPosition) { - Assert.isTrue(cursorPosition >= 0); - fCursorPosition= cursorPosition; - } - - protected int getCursorPosition() { - return fCursorPosition; - } - - /* - * @see ICompletionProposal#apply - */ - public final void apply(IDocument document) { - // not used any longer - apply(document, (char) 0, getReplacementOffset() + getReplacementLength()); - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int) - */ - public void apply(IDocument document, char trigger, int offset) { - try { - // patch replacement length - int delta= offset - (getReplacementOffset() + getReplacementLength()); - if (delta > 0) - setReplacementLength(getReplacementLength() + delta); - - boolean isSmartTrigger= isSmartTrigger(trigger); - - String replacement; - if (isSmartTrigger || trigger == (char) 0) { - replacement= getReplacementString(); - } else { - StringBuffer buffer= new StringBuffer(getReplacementString()); - - // fix for PR #5533. Assumes that no eating takes place. - if ((getCursorPosition() > 0 && getCursorPosition() <= buffer.length() && buffer.charAt(getCursorPosition() - 1) != trigger)) { - buffer.insert(getCursorPosition(), trigger); - setCursorPosition(getCursorPosition() + 1); - } - - replacement= buffer.toString(); - setReplacementString(replacement); - } - - // reference position just at the end of the document change. - int referenceOffset= getReplacementOffset() + getReplacementLength(); - final ReferenceTracker referenceTracker= new ReferenceTracker(); - referenceTracker.preReplace(document, referenceOffset); - - replace(document, getReplacementOffset(), getReplacementLength(), replacement); - - referenceOffset= referenceTracker.postReplace(document); - setReplacementOffset(referenceOffset - (replacement == null ? 0 : replacement.length())); - - // PR 47097 - if (isSmartTrigger) - handleSmartTrigger(document, trigger, referenceOffset); - - } catch (BadLocationException x) { - // ignore - } - } - - private boolean isSmartTrigger(char trigger) { - return trigger == ';' && getBooleanPreference(PreferenceConstants.EDITOR_SMART_SEMICOLON) - || trigger == '{' && getBooleanPreference(PreferenceConstants.EDITOR_SMART_OPENING_BRACE); - } - - private boolean getBooleanPreference(String preferenceKey) { - String value = PreferenceConstants.getPreference(preferenceKey, getProject()); - if (value != null) - return Boolean.valueOf(value).booleanValue(); - return false; - } - - private void handleSmartTrigger(IDocument document, char trigger, int referenceOffset) throws BadLocationException { - DocumentCommand cmd= new DocumentCommand() { - }; - - cmd.offset= referenceOffset; - cmd.length= 0; - cmd.text= Character.toString(trigger); - cmd.doit= true; - cmd.shiftsCaret= true; - cmd.caretOffset= getReplacementOffset() + getCursorPosition(); - -// SmartSemicolonAutoEditStrategy strategy= new SmartSemicolonAutoEditStrategy(IJavaPartitions.JAVA_PARTITIONING); -// strategy.customizeDocumentCommand(document, cmd); - - replace(document, cmd.offset, cmd.length, cmd.text); - setCursorPosition(cmd.caretOffset - getReplacementOffset() + cmd.text.length()); - } - - protected final void replace(IDocument document, int offset, int length, String string) throws BadLocationException { - if (!document.get(offset, length).equals(string)) - document.replace(offset, length, string); - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension1#apply(org.eclipse.jface.text.ITextViewer, char, int, int) - */ - public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { - - IDocument document= viewer.getDocument(); - if (fTextViewer == null) - fTextViewer= viewer; - - // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96059 - // don't apply the proposal if for some reason we're not valid any longer - if (!isInJavadoc() && !validate(document, offset, null)) { - setCursorPosition(offset - getReplacementOffset()); - if (trigger != '\0') { - try { - document.replace(offset, 0, String.valueOf(trigger)); - setCursorPosition(getCursorPosition() + 1); - if (trigger == '(' && autocloseBrackets()) { - document.replace(getReplacementOffset() + getCursorPosition(), 0, ")"); //$NON-NLS-1$ - setUpLinkedMode(document, ')'); - } - } catch (BadLocationException x) { - // ignore - } - } - return; - } - - // don't eat if not in preferences, XOR with modifier key 1 (Ctrl) - // but: if there is a selection, replace it! - Point selection= viewer.getSelectedRange(); - fToggleEating= (stateMask & SWT.MOD1) != 0; - int newLength= selection.x + selection.y - getReplacementOffset(); - if ((insertCompletion() ^ fToggleEating) && newLength >= 0) - setReplacementLength(newLength); - - apply(document, trigger, offset); - fToggleEating= false; - } - - /** - * Returns <code>true</code> if the proposal is within javadoc, <code>false</code> - * otherwise. - * - * @return <code>true</code> if the proposal is within javadoc, <code>false</code> otherwise - */ - protected boolean isInJavadoc(){ - return fIsInJavadoc; - } - - /** - * Sets the javadoc attribute. - * - * @param isInJavadoc <code>true</code> if the proposal is within javadoc - */ - protected void setInJavadoc(boolean isInJavadoc) { - fIsInJavadoc= isInJavadoc; - } - - /* - * @see ICompletionProposal#getSelection - */ - public Point getSelection(IDocument document) { - return new Point(getReplacementOffset() + getCursorPosition(), 0); - } - - /* - * @see ICompletionProposal#getContextInformation() - */ - public IContextInformation getContextInformation() { - return fContextInformation; - } - - /** - * Sets the context information. - * @param contextInformation The context information associated with this proposal - */ - public void setContextInformation(IContextInformation contextInformation) { - fContextInformation= contextInformation; - } - - /* - * @see ICompletionProposal#getDisplayString() - */ - public String getDisplayString() { - return fDisplayString; - } - - /* - * @see ICompletionProposal#getAdditionalProposalInfo() - */ - public String getAdditionalProposalInfo() { -// if (getProposalInfo() != null) { -// String info= getProposalInfo().getInfo(null); -// if (info != null && info.length() > 0) { -// StringBuffer buffer= new StringBuffer(); -// HTMLPrinter.insertPageProlog(buffer, 0, getStyleSheetURL()); -// buffer.append(info); -// HTMLPrinter.addPageEpilog(buffer); -// info= buffer.toString(); -// } -// return info; -// } - return null; - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension5#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor) - */ - public Object getAdditionalProposalInfo(IProgressMonitor monitor) { -// if (getProposalInfo() != null) { -// String info= getProposalInfo().getInfo(monitor); -// if (info != null && info.length() > 0) { -// StringBuffer buffer= new StringBuffer(); -// HTMLPrinter.insertPageProlog(buffer, 0, getStyleSheetURL()); -// buffer.append(info); -// HTMLPrinter.addPageEpilog(buffer); -// info= buffer.toString(); -// } -// return info; -// } - return null; - } - - /** - * Returns the style sheet URL. - * - * @since 3.1 - */ - protected URL getStyleSheetURL() { - if (fStyleSheetURL == null) { - - Bundle bundle= Platform.getBundle(JavaUI.ID_PLUGIN); - fStyleSheetURL= bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$ - if (fStyleSheetURL != null) { - try { - fStyleSheetURL= FileLocator.toFileURL(fStyleSheetURL); - } catch (IOException ex) { - UiPlugin.logError(ex); - } - } - } - return fStyleSheetURL; - } - - /* - * @see ICompletionProposalExtension#getContextInformationPosition() - */ - public int getContextInformationPosition() { - if (getContextInformation() == null) - return getReplacementOffset() - 1; - return getReplacementOffset() + getCursorPosition(); - } - - /** - * Gets the replacement offset. - * @return Returns a int - */ - public int getReplacementOffset() { - return fReplacementOffset; - } - - /** - * Sets the replacement offset. - * @param replacementOffset The replacement offset to set - */ - public void setReplacementOffset(int replacementOffset) { - Assert.isTrue(replacementOffset >= 0); - fReplacementOffset= replacementOffset; - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getCompletionOffset() - */ - public int getPrefixCompletionStart(IDocument document, int completionOffset) { - return getReplacementOffset(); - } - - /** - * Gets the replacement length. - * @return Returns a int - */ - public int getReplacementLength() { - return fReplacementLength; - } - - /** - * Sets the replacement length. - * @param replacementLength The replacementLength to set - */ - public void setReplacementLength(int replacementLength) { - Assert.isTrue(replacementLength >= 0); - fReplacementLength= replacementLength; - } - - /** - * Gets the replacement string. - * @return Returns a String - */ - public String getReplacementString() { - return fReplacementString; - } - - /** - * Sets the replacement string. - * @param replacementString The replacement string to set - */ - public void setReplacementString(String replacementString) { - Assert.isNotNull(replacementString); - fReplacementString= replacementString; - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getReplacementText() - */ - public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { - if (!isCamelCaseMatching()) - return getReplacementString(); - - String prefix= getPrefix(document, completionOffset); - return getCamelCaseCompound(prefix, getReplacementString()); - } - - /* - * @see ICompletionProposal#getImage() - */ - public Image getImage() { - return fImage; - } - - /** - * Sets the image. - * @param image The image to set - */ - public void setImage(Image image) { - fImage= image; - } - - /* - * @see ICompletionProposalExtension#isValidFor(IDocument, int) - */ - public boolean isValidFor(IDocument document, int offset) { - return validate(document, offset, null); - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent) - */ - public boolean validate(IDocument document, int offset, DocumentEvent event) { - - if (offset < getReplacementOffset()) - return false; - - boolean validated= isValidPrefix(getPrefix(document, offset)); - - if (validated && event != null) { - // adapt replacement range to document change - int delta= (event.fText == null ? 0 : event.fText.length()) - event.fLength; - final int newLength= Math.max(getReplacementLength() + delta, 0); - setReplacementLength(newLength); - } - - return validated; - } - - /** - * Checks whether <code>prefix</code> is a valid prefix for this proposal. Usually, while code - * completion is in progress, the user types and edits the prefix in the document in order to - * filter the proposal list. From {@link #validate(IDocument, int, DocumentEvent) }, the - * current prefix in the document is extracted and this method is called to find out whether the - * proposal is still valid. - * <p> - * The default implementation checks if <code>prefix</code> is a prefix of the proposal's - * {@link #getDisplayString() display string} using the {@link #isPrefix(String, String) } - * method. - * </p> - * - * @param prefix the current prefix in the document - * @return <code>true</code> if <code>prefix</code> is a valid prefix of this proposal - */ - protected boolean isValidPrefix(String prefix) { - /* - * See http://dev.eclipse.org/bugs/show_bug.cgi?id=17667 - * why we do not use the replacement string. - * String word= fReplacementString; - */ - return isPrefix(prefix, getDisplayString()); - } - - /** - * Gets the proposal's relevance. - * @return Returns a int - */ - public int getRelevance() { - return fRelevance; - } - - /** - * Sets the proposal's relevance. - * @param relevance The relevance to set - */ - public void setRelevance(int relevance) { - fRelevance= relevance; - } - - /** - * Returns the text in <code>document</code> from {@link #getReplacementOffset()} to - * <code>offset</code>. Returns the empty string if <code>offset</code> is before the - * replacement offset or if an exception occurs when accessing the document. - * - * @since 3.2 - */ - protected String getPrefix(IDocument document, int offset) { - try { - int length= offset - getReplacementOffset(); - if (length > 0) - return document.get(getReplacementOffset(), length); - } catch (BadLocationException x) { - } - return ""; //$NON-NLS-1$ - } - - /** - * Case insensitive comparison of <code>prefix</code> with the start of <code>string</code>. - * Returns <code>false</code> if <code>prefix</code> is longer than <code>string</code> - * - * @since 3.2 - */ - protected boolean isPrefix(String prefix, String string) { - if (prefix == null || string ==null || prefix.length() > string.length()) - return false; - String start= string.substring(0, prefix.length()); - return start.equalsIgnoreCase(prefix) || isCamelCaseMatching() && CharOperation.camelCaseMatch(prefix.toCharArray(), string.toCharArray()); - } - - /** - * Matches <code>prefix</code> against <code>string</code> and replaces the matched region - * by prefix. Case is preserved as much as possible. This method returns <code>string</code> if camel case completion - * is disabled. Examples when camel case completion is enabled: - * <ul> - * <li>getCamelCompound("NuPo", "NullPointerException") -> "NuPointerException"</li> - * <li>getCamelCompound("NuPoE", "NullPointerException") -> "NuPoException"</li> - * <li>getCamelCompound("hasCod", "hashCode") -> "hasCode"</li> - * </ul> - * - * @param prefix the prefix to match against - * @param string the string to match - * @return a compound of prefix and any postfix taken from <code>string</code> - * @since 3.2 - */ - protected final String getCamelCaseCompound(String prefix, String string) { - if (prefix.length() > string.length()) - return string; - - // a normal prefix - no camel case logic at all - String start= string.substring(0, prefix.length()); - if (start.equalsIgnoreCase(prefix)) - return string; - - final char[] patternChars= prefix.toCharArray(); - final char[] stringChars= string.toCharArray(); - - for (int i= 1; i <= stringChars.length; i++) - if (CharOperation.camelCaseMatch(patternChars, 0, patternChars.length, stringChars, 0, i)) - return prefix + string.substring(i); - - // Not a camel case match at all. - // This should not happen -> stay with the default behavior - return string; - } - - /** - * Returns true if camel case matching is enabled. - * - * @since 3.2 - */ - protected boolean isCamelCaseMatching() { - IJavaProject project= getProject(); - String value; - if (project == null) - value= JavaCore.getOption(JavaCore.CODEASSIST_CAMEL_CASE_MATCH); - else - value= project.getOption(JavaCore.CODEASSIST_CAMEL_CASE_MATCH, true); - - return JavaCore.ENABLED.equals(value); - } - - private IJavaProject getProject() { - return null; - } - - - private static boolean insertCompletion() { - IPreferenceStore preference= PreferenceConstants.getPreferenceStore(); - return preference.getBoolean(PreferenceConstants.CODEASSIST_INSERT_COMPLETION); - } - - private static Color getForegroundColor(StyledText text) { - - IPreferenceStore preference= PreferenceConstants.getPreferenceStore(); - RGB rgb= PreferenceConverter.getColor(preference, PreferenceConstants.CODEASSIST_REPLACEMENT_FOREGROUND); - return JavaUI.getColorManager().getColor(rgb); - } - - private static Color getBackgroundColor(StyledText text) { - - IPreferenceStore preference= PreferenceConstants.getPreferenceStore(); - RGB rgb= PreferenceConverter.getColor(preference, PreferenceConstants.CODEASSIST_REPLACEMENT_BACKGROUND); - return JavaUI.getColorManager().getColor(rgb); - } - - private void repairPresentation(ITextViewer viewer) { - if (fRememberedStyleRange != null) { - if (viewer instanceof ITextViewerExtension2) { - // attempts to reduce the redraw area - ITextViewerExtension2 viewer2= (ITextViewerExtension2) viewer; - - if (viewer instanceof ITextViewerExtension5) { - - ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; - IRegion modelRange= extension.widgetRange2ModelRange(new Region(fRememberedStyleRange.start, fRememberedStyleRange.length)); - if (modelRange != null) - viewer2.invalidateTextPresentation(modelRange.getOffset(), modelRange.getLength()); - - } else { - viewer2.invalidateTextPresentation(fRememberedStyleRange.start + viewer.getVisibleRegion().getOffset(), fRememberedStyleRange.length); - } - - } else - viewer.invalidateTextPresentation(); - } - } - - private void updateStyle(ITextViewer viewer) { - - StyledText text= viewer.getTextWidget(); - if (text == null || text.isDisposed()) - return; - - int widgetCaret= text.getCaretOffset(); - - int modelCaret= 0; - if (viewer instanceof ITextViewerExtension5) { - ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; - modelCaret= extension.widgetOffset2ModelOffset(widgetCaret); - } else { - IRegion visibleRegion= viewer.getVisibleRegion(); - modelCaret= widgetCaret + visibleRegion.getOffset(); - } - - if (modelCaret >= getReplacementOffset() + getReplacementLength()) { - repairPresentation(viewer); - return; - } - - int offset= widgetCaret; - int length= getReplacementOffset() + getReplacementLength() - modelCaret; - - Color foreground= getForegroundColor(text); - Color background= getBackgroundColor(text); - - StyleRange range= text.getStyleRangeAtOffset(offset); - int fontStyle= range != null ? range.fontStyle : SWT.NORMAL; - - repairPresentation(viewer); - fRememberedStyleRange= new StyleRange(offset, length, foreground, background, fontStyle); - if (range != null) { - fRememberedStyleRange.strikeout= range.strikeout; - fRememberedStyleRange.underline= range.underline; - } - - // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754 - try { - text.setStyleRange(fRememberedStyleRange); - } catch (IllegalArgumentException x) { - // catching exception as offset + length might be outside of the text widget - fRememberedStyleRange= null; - } - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(ITextViewer, boolean) - */ - public void selected(ITextViewer viewer, boolean smartToggle) { - if (!insertCompletion() ^ smartToggle) - updateStyle(viewer); - else { - repairPresentation(viewer); - fRememberedStyleRange= null; - } - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(ITextViewer) - */ - public void unselected(ITextViewer viewer) { - repairPresentation(viewer); - fRememberedStyleRange= null; - } - - /* - * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getInformationControlCreator() - */ - public IInformationControlCreator getInformationControlCreator() { -// if (!BrowserInformationControl.isAvailable(null)) -// return null; -// -// if (fCreator == null) { -// fCreator= new AbstractReusableInformationControlCreator() { -// -// /* -// * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell) -// */ -// public IInformationControl doCreateInformationControl(Shell parent) { -// return new BrowserInformationControl(parent, SWT.NO_TRIM | SWT.TOOL, SWT.NONE, null); -// } -// }; -// } -// return fCreator; - return null; - } - - public String getSortString() { - return fSortString; - } - - protected void setSortString(String string) { - fSortString= string; - } - - protected ITextViewer getTextViewer() { - return fTextViewer; - } - - protected boolean isToggleEating() { - return fToggleEating; - } - - /** - * Sets up a simple linked mode at {@link #getCursorPosition()} and an exit policy that will - * exit the mode when <code>closingCharacter</code> is typed and an exit position at - * <code>getCursorPosition() + 1</code>. - * - * @param document the document - * @param closingCharacter the exit character - */ - protected void setUpLinkedMode(IDocument document, char closingCharacter) { - if (getTextViewer() != null && autocloseBrackets()) { - int offset= getReplacementOffset() + getCursorPosition(); - int exit= getReplacementOffset() + getReplacementString().length(); - try { - LinkedPositionGroup group= new LinkedPositionGroup(); - group.addPosition(new LinkedPosition(document, offset, 0, LinkedPositionGroup.NO_STOP)); - - LinkedModeModel model= new LinkedModeModel(); - model.addGroup(group); - model.forceInstall(); - - LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer()); - ui.setSimpleMode(true); - ui.setExitPolicy(new ExitPolicy(closingCharacter, document)); - ui.setExitPosition(getTextViewer(), exit, 0, Integer.MAX_VALUE); - ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); - ui.enter(); - } catch (BadLocationException x) { - UiPlugin.logError(x); - } - } - } - - protected boolean autocloseBrackets() { - IPreferenceStore preferenceStore= PreferenceConstants.getPreferenceStore(); - return preferenceStore.getBoolean(PreferenceConstants.EDITOR_CLOSE_BRACKETS); - } - - protected void setDisplayString(String string) { - fDisplayString= string; - } - - /* - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return getDisplayString(); - } - - /** - * Returns the java element proposed by the receiver, possibly <code>null</code>. - * - * @return the java element proposed by the receiver, possibly <code>null</code> - */ - public IJavaElement getJavaElement() { -// if (getProposalInfo() != null) -// try { -// return getProposalInfo().getJavaElement(); -// } catch (JavaModelException x) { -// JavaPlugin.log(x); -// } - return null; - } -} diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagCompletionProc.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagCompletionProc.java deleted file mode 100644 index 5006208e9..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagCompletionProc.java +++ /dev/null @@ -1,724 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2003, 2004 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -/* - * Created on Mar 9, 2004 - * - * To change the template for this generated file go to - * Window - Preferences - Java - Code Generation - Code and Comments - */ -package org.eclipse.jst.common.internal.annotations.ui; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.MissingResourceException; -import java.util.Set; -import java.util.TreeSet; - -import org.eclipse.core.resources.IFile; -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IField; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IMethod; -import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.ui.JavaUI; -import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; -import org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.contentassist.IContextInformation; -import org.eclipse.jst.common.internal.annotations.core.AnnotationTagParser; -import org.eclipse.jst.common.internal.annotations.core.TagParseEventHandler; -import org.eclipse.jst.common.internal.annotations.core.Token; -import org.eclipse.jst.common.internal.annotations.registry.AnnotationTagRegistry; -import org.eclipse.jst.common.internal.annotations.registry.AttributeValueProposalHelper; -import org.eclipse.jst.common.internal.annotations.registry.AttributeValuesHelper; -import org.eclipse.jst.common.internal.annotations.registry.TagAttribSpec; -import org.eclipse.jst.common.internal.annotations.registry.TagSpec; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.part.FileEditorInput; - - -/** - * @author Pat Kelley - * - * To change the template for this generated type comment go to Window - Preferences - Java - Code - * Generation - Code and Comments - */ -public class AnnotationTagCompletionProc implements IJavadocCompletionProcessor, TagParseEventHandler { - private static final String[] BOOLEAN_VALID_VALUES = new String[]{"false", "true"}; //$NON-NLS-1$ //$NON-NLS-2$ - ICompilationUnit m_icu; - - IDocument m_doc; - - List m_tags; - - // Instance variables active when maybeCompleteAttribute is live. - Token m_tagName; - - /** - * Set of all attributes names encountered. Only live when maybeCompleteAttribute is live. - */ - Set m_attSet = new TreeSet(); - - /** - * List of Attribute. Only live when maybeCompleAttribute is live. - */ - List m_attributes = new ArrayList(); - - AnnotationTagParser m_parser = new AnnotationTagParser(this); - - /** - * Scope of the tag. TagSpec.TYPE | TagSpec.METHOD | TagSpec.FIELD. Not valid until - * getAnnotationArea has been called for a completions request, and only then if - * getAnnotationArea() did not return null. - */ - int m_tagScope; - - public AnnotationTagCompletionProc() { - initTagInfo(); - } - - private void initTagInfo() { - if (m_tags == null) - m_tags = AnnotationTagRegistry.getAllTagSpecs(); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor#computeContextInformation(org.eclipse.jdt.core.ICompilationUnit, - * int) - */ - public IContextInformation[] computeContextInformation(ICompilationUnit cu, int offset) { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor#computeCompletionProposals(org.eclipse.jdt.core.ICompilationUnit, - * int, int, int) - */ - public IJavaCompletionProposal[] computeCompletionProposals(ICompilationUnit cu, int offset, int length, int flags) { - if (cu == null) //bug 262362 - return null; - IEditorInput editorInput = new FileEditorInput((IFile) cu.getResource()); - - // Set up completion processor state. - m_doc = JavaUI.getDocumentProvider().getDocument(editorInput); - m_icu = cu; - - try { - AnnotationArea area = getAnnotationArea(offset); - - if (area == null) { - return null; - } - - // Check for tag completion first. ( the easier case ) - String tsf = getTagSoFarIfNotCompleted(area.beginOffset, offset); - - if (tsf != null) { - return getTagCompletionsFor(tsf, area, length); - } - - // Ach, have to try the harder case now, where we parse the - // annotation - return maybeCompleteAttribute(area, offset); - - } catch (JavaModelException e) { - // Silently fail. - return null; - } catch (BadLocationException ex) { - return null; - } - } - - private IJavaCompletionProposal[] maybeCompleteAttribute(AnnotationArea area, int cursorPos) throws BadLocationException { - m_attSet.clear(); - m_attributes.clear(); - - m_parser.setParserInput(m_doc.get(area.beginOffset, area.length())); - m_parser.parse(); - - TagSpec ts = null; - if (m_tagName!=null) - ts = getTagSpecForTagName(m_tagName.getText()); - - // Do we even recognize this tag? - if (ts == null) { - return null; - } - - // Loop through and determine whether the cursor is within a attribute - // assignment, or between assignements. - Attribute target = null; - Attribute last = null; - Attribute before = null; - Attribute a = null; - boolean between = false; - int rCurPos = area.relativeCursorPos(cursorPos); - Iterator i = m_attributes.iterator(); - while (i.hasNext()) { - a = (Attribute) i.next(); - - if (a.contains(rCurPos)) { - target = a; - break; - } else if (last != null) { - // See if the cursor is between, but not directly adjacent to - // the last two attributes. - if (rCurPos > last.maxExtent() + 1 && rCurPos < a.minExtent() - 1) { - between = true; - break; - } else if (a.immediatelyPrecedes(rCurPos)) { - before = a; - break; - } - } - last = a; - } - - if (target == null) { - if (between) { - // If we're between attributes, suggest all possible attributes. - return attributeCompletionsFor(ts, cursorPos, 0, "", true); //$NON-NLS-1$ - } else if (before != null) { - // We're right after the attribute named in 'before', so set the - // target to it, and fall - // through to the target handling code. - target = before; - } else { - // not between and not immediately after an attribute. We are - // past the end of the parsed annotation. - // Only offer suggestions if it looks like the last annotation - // attribute is valid. - if (a == null) { - // No annotations attributes, suggest everything. - return attributeCompletionsFor(ts, cursorPos, 0, "", true); //$NON-NLS-1$ - } else if (rCurPos > a.maxExtent()) { - if (a.hasAssignment() && a.hasValue()) { - // Last annotation was good, and we're past it, so do - // completions for anything - return attributeCompletionsFor(ts, cursorPos, 0, "", true); //$NON-NLS-1$ - } else if (a.hasAssignment()) - return attributeValidValuesFor(ts, a, area, cursorPos); - else - return attributeCompletionsFor(ts, cursorPos - a.name.length(), 0, a.name.getText(), true); - } else { - // Didn't match anything, not past the end - we're probably - // the first attribute - // being added to the tag. - return attributeCompletionsFor(ts, cursorPos, 0, "", true); //$NON-NLS-1$ - } - } - } - - // Completion for a partial attribute name? - if (target.name.immediatelyPrecedes(rCurPos)) { - return attributeCompletionsFor(ts, area.relativeToAbs(target.name.getBeginning()), target.name.length(), target.name.getText(), !target.hasAssignment()); - } - - // Are we in the middle of a name? - if (target.name.contains(rCurPos)) { - // We've opted to replace the entire name for this case, which seems - // to make the most sense. - return attributeCompletionsFor(ts, area.relativeToAbs(target.name.getBeginning()), target.name.length(), target.name.getText().substring(0, rCurPos - target.name.getBeginning()), !target.hasAssignment()); - } - - // If we got this far, we're either in a value, or really confused. - // try and return valid values or bail? - if (a!= null && a.value != null && (a.value.contains(rCurPos) || (target.hasAssignment() && area.relativeCursorPos(cursorPos) > a.name.getBeginning()))) - return attributeValidValuesFor(ts, a, area, cursorPos); - return attributeCompletionsFor(ts, cursorPos, 0, "", true); //$NON-NLS-1$ - } - - /** - * @return valid values for the attribute - */ - private IJavaCompletionProposal[] attributeValidValuesFor(TagSpec ts, Attribute a, AnnotationArea area, int cursorPos) { - TagAttribSpec tas = ts.attributeNamed(a.name.getText()); - if (tas == null) - return null; - String[] validValues = getValidValues(tas, a, area); - String partialValue = calculatePartialValue(a, area, cursorPos); - int valueOffset = calculateValueOffset(a, area, cursorPos); - if (validValues == null || validValues.length == 0) - return createCustomAttributeCompletionProposals(ts, tas, partialValue, valueOffset, a.value.getText(), area.javaElement); - return createAttributeCompletionProposals(partialValue, valueOffset, validValues); - } - - /** - * @param ts - * @param tas - * @param partialValue - * @param valueOffset - * @param value - * @param javaElement - * @return - */ - private IJavaCompletionProposal[] createCustomAttributeCompletionProposals(TagSpec ts, TagAttribSpec tas, String partialValue, int valueOffset, String value, IJavaElement javaElement) { - AttributeValuesHelper helper = ts.getValidValuesHelper(); - if (helper == null) - return null; - AttributeValueProposalHelper[] proposalHelpers = helper.getAttributeValueProposalHelpers(tas, partialValue, valueOffset, javaElement); - if (proposalHelpers == null || proposalHelpers.length == 0) - return null; - IJavaCompletionProposal[] proposals = new IJavaCompletionProposal[proposalHelpers.length]; - AnnotationTagProposal proposal; - for (int i = 0; i < proposalHelpers.length; i++) { - proposal = new AnnotationTagProposal(proposalHelpers[i]); - //proposal.setPartialValueString(partialValue); - proposals[i] = proposal; - } - return proposals; - } - - private IJavaCompletionProposal[] createAttributeCompletionProposals(String partialValue, int valueOffset, String[] validValues) { - List resultingValues = new ArrayList(); - for (int i = 0; i < validValues.length; i++) { - String rplString = validValues[i]; - if (partialValue != null && !rplString.startsWith(partialValue)) - continue; - AnnotationTagProposal prop = new AnnotationTagProposal(rplString, valueOffset, 0, null, rplString, 90); - prop.setEnsureQuoted(true); - //prop.setPartialValueString(partialValue); - resultingValues.add(prop); - } - if (resultingValues.isEmpty()) - return null; - return (IJavaCompletionProposal[]) resultingValues.toArray(new IJavaCompletionProposal[resultingValues.size()]); - } - - private String[] getValidValues(TagAttribSpec tas, Attribute a, AnnotationArea area) { - String[] validValues = tas.getValidValues(); - if (validValues == null || validValues.length == 0) { - AttributeValuesHelper helper = tas.getTagSpec().getValidValuesHelper(); - if (helper == null) - return null; - validValues = helper.getValidValues(tas, area.javaElement); - if ((validValues == null || validValues.length == 0) && tas.valueIsBool()) - validValues = BOOLEAN_VALID_VALUES; - } - return validValues; - } - - /** - * @param a - * @param area - * @param cursorPos - * @return - */ - private int calculateValueOffset(Attribute a, AnnotationArea area, int cursorPos) { - if (a.value == null) - return cursorPos; - int nameEnd = a.name.getEnd(); - int valBeg = a.value.getBeginning(); - if (valBeg > nameEnd + 2) - return area.relativeToAbs(nameEnd + 2); //Value too far away to be correct. - return area.relativeToAbs(valBeg); - } - - /** - * @param a - * @param area - * @param cursorPos - * @return - */ - private String calculatePartialValue(Attribute a, AnnotationArea area, int cursorPos) { - if (a.value == null) - return null; - int nameEnd = a.name.getEnd(); - int valueBeg = a.value.getBeginning(); - if (valueBeg > nameEnd + 2) - return null; //Value is too far away so it must not be part of this attribute. - int relativePos = area.relativeCursorPos(cursorPos); - if (a.value.contains(relativePos)) { - boolean hasBeginQuote = valueBeg - nameEnd == 2; - String value = a.value.getText(); - int end = relativePos - valueBeg; - if (hasBeginQuote) - end--; - if (end > -1) { - int length = value.length(); - if (end < length) - return value.substring(0, end); - else if (end == length) - return value; - } - } - return null; - } - - /** - * @param tagName - * @return - */ - private TagSpec getTagSpecForTagName(String tagName) { - String simpleName = tagName; - if (tagName != null && tagName.length() > 0 && tagName.charAt(0) == '@') - simpleName = tagName.length() == 2 ? "" : tagName.substring(1); //$NON-NLS-1$ - switch (m_tagScope) { - case TagSpec.TYPE : - return AnnotationTagRegistry.getTypeTag(simpleName); - case TagSpec.METHOD : - return AnnotationTagRegistry.getMethodTag(simpleName); - case TagSpec.FIELD : - return AnnotationTagRegistry.getFieldTag(simpleName); - } - return null; - } - - private IJavaCompletionProposal[] attributeCompletionsFor(TagSpec ts, int replaceOffset, int replaceLength, String partialAttributeName, boolean appendEquals) { - Iterator i = ts.getAttributes().iterator(); - List props = new ArrayList(); - while (i.hasNext()) { - TagAttribSpec tas = (TagAttribSpec) i.next(); - String aname = tas.getAttribName(); - - // Don't suggest attributes that have already been specified. - if (!m_attSet.contains(aname)) { - if (aname.startsWith(partialAttributeName)) { - String rtxt = appendEquals ? aname + '=' : aname; - AnnotationTagProposal prop = new AnnotationTagProposal(rtxt, replaceOffset, replaceLength, null, aname, 90); - prop.setHelpText(lookupAttHelp(tas)); - props.add(prop); - } - } - } - if (props.isEmpty()) { - return null; - } - return (IJavaCompletionProposal[]) props.toArray(new IJavaCompletionProposal[props.size()]); - } - - /* - * (non-Javadoc) - * - * @see com.ibm.ws.rd.annotations.TagParseEventHandler#annotationTag(com.ibm.ws.rd.annotations.Token) - */ - public void annotationTag(Token tag) { - m_tagName = tag; - } - - /* - * (non-Javadoc) - * - * @see com.ibm.ws.rd.annotations.TagParseEventHandler#endOfTag(int) - */ - public void endOfTag(int pos) { - // Do nothing - } - - /* - * (non-Javadoc) - * - * @see com.ibm.ws.rd.annotations.TagParseEventHandler#attribute(com.ibm.ws.rd.annotations.Token, - * int, com.ibm.ws.rd.annotations.Token) - */ - public void attribute(Token name, int equalsPosition, Token value) { - m_attributes.add(new Attribute(name, equalsPosition, value)); - m_attSet.add(name.getText()); - } - - private String getReplacementForTag(TagSpec ts, int beginIndex) { - StringBuffer bud = new StringBuffer(32); - - bud.append('@'); - bud.append(ts.getTagName()); - - String prefix = getArrayPrefixForMultipleAttribs(beginIndex); - List attributes = ts.getAttributes(); - - for (int i = 0; i < attributes.size(); i++) { - TagAttribSpec tas = (TagAttribSpec) attributes.get(i); - - if (tas.isRequired()) { - bud.append(prefix); - bud.append(tas.getAttribName()); - bud.append('='); - } - } - return bud.toString(); - } - - private String getArrayPrefixForMultipleAttribs(int beginIndex) { - String result = null; - String source = null; - // Get source from compilation unit - try { - source = m_icu.getSource(); - if (source == null || beginIndex < 0) - return result; - // trim off everything after our begin index - source = source.substring(0, beginIndex + 1); - int newLineIndex = source.lastIndexOf('\n'); - //if we are on first line... - if (newLineIndex == -1) - newLineIndex = 0; - // Get the current line - String currentLine = source.substring(newLineIndex, beginIndex + 1); - // Currently we have to have the '@' sign to show our menu - int annotationIndex = currentLine.lastIndexOf('@'); - result = currentLine.substring(0, annotationIndex); - result = result + " "; //$NON-NLS-1$ - } catch (Exception e) { - // Do nothing - } - - return result; - } - - private IJavaCompletionProposal[] getTagCompletionsFor(String partialTagName, AnnotationArea area, int selectLength) { - List found = new ArrayList(); - - for (int i = 0; i < m_tags.size(); i++) { - TagSpec ts = (TagSpec) m_tags.get(i); - String tname = ts.getTagName(); - - if (ts.getScope() == m_tagScope && tname.startsWith(partialTagName)) { - String rtxt = getReplacementForTag(ts, area.beginOffset); - String labl = '@' + tname; - AnnotationTagProposal prop = new AnnotationTagProposal(rtxt, area.beginOffset, Math.max(selectLength, rtxt.length()), null, labl, 90); - prop.setHelpText(lookupTagHelp(ts)); - found.add(prop); - } - } - - if (!found.isEmpty()) { - return (IJavaCompletionProposal[]) found.toArray(new IJavaCompletionProposal[found.size()]); - } - return null; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor#getErrorMessage() - */ - public String getErrorMessage() { - return null; - } - - private static boolean isWS1(char c) { - return c == ' ' || c == '\t' || c == '*' || c == '\r' || c == '\n'; - } - - private String getTagSoFarIfNotCompleted(int startingAt, int cursorAt) throws BadLocationException { - if (m_doc.getChar(startingAt) != '@') { - return null; - } - - int firstChar = startingAt + 1; - - if (firstChar == cursorAt) { - return ""; //$NON-NLS-1$ - } - - for (int i = firstChar; i < cursorAt; i++) { - char c = m_doc.getChar(i); - - if (isWS1(c)) { - return null; - } - } - - return m_doc.get(firstChar, cursorAt - firstChar); - } - - /** - * Calculates the the area of the annotation we're trying to complete. Also initializes - * m_tagScope. - * - * @param fromOffset - * @return - * @throws JavaModelException - */ - private AnnotationArea getAnnotationArea(int fromOffset) throws JavaModelException { - // First, roughly calculate the end of the comment. - IJavaElement el = m_icu.getElementAt(fromOffset); - int absmax, absmin; - if (el == null) - return null; - int ty = el.getElementType(); - - switch (ty) { - case IJavaElement.FIELD : - IField f = (IField) el; - absmax = f.getNameRange().getOffset(); - absmin = f.getSourceRange().getOffset(); - m_tagScope = TagSpec.FIELD; - break; - - case IJavaElement.TYPE : - IType t = (IType) el; - absmax = t.getNameRange().getOffset(); - absmin = t.getSourceRange().getOffset(); - m_tagScope = TagSpec.TYPE; - break; - - case IJavaElement.METHOD : - IMethod m = (IMethod) el; - absmax = m.getNameRange().getOffset(); - absmin = m.getSourceRange().getOffset(); - m_tagScope = TagSpec.METHOD; - break; - - default : - m_tagScope = -1; - return null; - } - - // Make sure we're not after the name for the member. - if (absmax < fromOffset) { - return null; - } - - int min = 0, max = 0; - try { - // Search backwards for the starting '@'. - boolean found = false; - for (min = fromOffset; min >= absmin; min--) { - if (m_doc.getChar(min) == '@') { - found = true; - break; - } - } - if (!found) { - return null; - } - - // Search forwards for the next '@', or the end of the comment. - for (max = fromOffset + 1; max < absmax; max++) { - if (m_doc.getChar(max) == '@') { - break; - } - } - } catch (BadLocationException e) { - return null; - } - - return new AnnotationArea(el, min, Math.min(absmax, max)); - } - - private String lookupTagHelp(TagSpec ts) { - if (ts != null) - try { - return ts.lookupTagHelp(); - } catch (MissingResourceException e) { - // Do nothing, return null - } - return null; - } - - private String lookupAttHelp(TagAttribSpec tas) { - if (tas != null) - try { - return tas.lookupTagHelp(); - } catch (MissingResourceException e) { - // Do nothing, return null - } - return null; - } - - /** - * A range that goes from the beginning position up to, but not including, the end position. - */ - private static class AnnotationArea { - /** - * Document offset of the beginning of the javadoc annotation. - */ - int beginOffset; - - /** - * Document offset of the end of the area that could contain an annotation. - */ - int endOffset; - /** - * The Java element that this annotation is assigned. - * - * @param beg - * @param end - */ - IJavaElement javaElement; - - public AnnotationArea(IJavaElement javaElement, int beg, int end) { - this.javaElement = javaElement; - beginOffset = beg; - endOffset = end; - } - - public int length() { - return endOffset - beginOffset; - } - - /** - * Returns the cursor position relative to the area. Only valid if - * <code>this.contains( absCursorPos )</code> - * - * @param absCursorPos - * @return - */ - public int relativeCursorPos(int absCursorPos) { - return absCursorPos - beginOffset; - } - - public int relativeToAbs(int relPos) { - return beginOffset + relPos; - } - } - - private static class Attribute { - Token name; - - Token value; - - int equalsPos; - - Attribute(Token n, int ep, Token v) { - name = n; - value = v; - equalsPos = ep; - } - - public boolean hasAssignment() { - return equalsPos != -1; - } - - public boolean hasValue() { - return value.length() != 0; - } - - public boolean contains(int srcPos) { - return srcPos >= minExtent() && srcPos <= maxExtent(); - } - - public int minExtent() { - return name.getBeginning(); - } - - public int maxExtent() { - if (hasAssignment()) { - if (hasValue()) - return value.getEnd(); - return equalsPos; - } - return name.getEnd(); - } - - public boolean immediatelyPrecedes(int pos) { - return maxExtent() + 1 == pos; - } - } - -}
\ No newline at end of file diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagProposal.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagProposal.java deleted file mode 100644 index fb1223dee..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/AnnotationTagProposal.java +++ /dev/null @@ -1,209 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2003, 2004 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -/* - * Created on Sep 4, 2003 - * - * To change the template for this generated file go to - * Window>Preferences>Java>Code Generation>Code and Comments - */ -package org.eclipse.jst.common.internal.annotations.ui; - -import org.eclipse.core.runtime.Assert; -import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.contentassist.ICompletionProposalExtension; -import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; -import org.eclipse.jst.common.internal.annotations.registry.AttributeValueProposalHelper; -import org.eclipse.swt.graphics.Image; - - -/** - * @author kelleyp - * - * A completion proposal especially for Annotation tag completions. This problem this class was - * created to solve was the problem of attaching help text to a proposal. The mechanism inside of - * JavaCompletionProposal was useless to us, since it was tied to the idea that the proposal would - * be for an actual java element, that has javadoc attached to it, etc... So here we subclass - * JavaCompletionProposal and override <code>getAdditionalProposalInfo</code> for a more suitable - * way of associating help text with a proposal. - */ -public class AnnotationTagProposal extends AbstractAnnotationTagProposal implements IJavaCompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 { - private static final char QUOTE = '"'; - private boolean ensureBeginQuote = false; - private boolean ensureEndQuote = false; - private String localString; - //String used to validate the prefix. - private String fValidationString; - - /** - * Localized help text. - */ - private String locText; - - /** - * - * @param replacementString - * @param replacementOffset - * @param replacementLength - * @param image - * @param displayString - * @param relevance - */ - public AnnotationTagProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, int relevance) { - Assert.isNotNull(replacementString); - Assert.isTrue(replacementOffset >= 0); - Assert.isTrue(replacementLength >= 0); - - setReplacementString(replacementString); - setReplacementOffset(replacementOffset); - setReplacementLength(replacementLength); - setImage(image); - setDisplayString(displayString == null ? replacementString : displayString); - setRelevance(relevance); - setCursorPosition(replacementString.length()); - setSortString(displayString == null ? replacementString : displayString); - this.localString = replacementString; - } - - /** - * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#JavaCompletionProposal(java.lang.String, int, int, - * org.eclipse.swt.graphics.Image, java.lang.String, int, - * org.eclipse.jface.text.ITextViewer) - * @param replacementString - * @param replacementOffset - * @param replacementLength - * @param image - * @param displayString - * @param relevance - */ - - public AnnotationTagProposal(AttributeValueProposalHelper proposalHelper) { - this(proposalHelper.getReplacementString(), proposalHelper.getValueOffset(), proposalHelper.getReplacementLength(), null, proposalHelper.getValueDisplayString(), 90); - if (proposalHelper instanceof UIAttributeValueProposalHelper) - setImage(((UIAttributeValueProposalHelper) proposalHelper).getImage()); - setEnsureBeginQuote(proposalHelper.ensureBeginQuote()); - setEnsureEndQuote(proposalHelper.ensureEndQuote()); - } - - public AnnotationTagProposal(UIAttributeValueProposalHelper proposalHelper) { - this(proposalHelper.getReplacementString(), proposalHelper.getValueOffset(), proposalHelper.getReplacementLength(), proposalHelper.getImage(), proposalHelper.getValueDisplayString(), 90); - setEnsureBeginQuote(proposalHelper.ensureBeginQuote()); - setEnsureEndQuote(proposalHelper.ensureEndQuote()); - } - - /** - * Our override that uses <code>textHolder</code> to provide the help text. - */ - @Override - public String getAdditionalProposalInfo() { - return locText; - } - - /** - * Sets the holder of the help text that can be displayed with this proposal. - * - * @param hld - * an LocalizedTextContainer - */ - public void setHelpText(String s) { - locText = s; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#apply(org.eclipse.jface.text.IDocument, - * char, int) - */ - @Override - public void apply(IDocument document, char trigger, int offset) { - ensureQuotedIfNecessary(document, offset); - super.apply(document, trigger, offset); - } - - /** - * @param document - * @param offset - */ - private void ensureQuotedIfNecessary(IDocument document, int offset) { - if (ensureBeginQuote || ensureEndQuote) { - try { - char begin = document.getChar(getReplacementOffset() - 1); - char end = document.getChar(offset); - if (ensureBeginQuote && ensureEndQuote && begin != QUOTE && end != QUOTE) { - StringBuffer b = new StringBuffer(); - b.append(QUOTE).append(localString).append(QUOTE); - localString = b.toString(); - } else if (ensureBeginQuote && begin != QUOTE) - localString = QUOTE + localString; - else if (ensureEndQuote && end != QUOTE) - localString = localString + QUOTE; - setReplacementString(localString); - setCursorPosition(localString.length()); - } catch (BadLocationException e) { - // Do nothing - } - } - } - - public void setEnsureQuoted(boolean ensureQuoted) { - setEnsureBeginQuote(ensureQuoted); - setEnsureEndQuote(ensureQuoted); - } - - //public void setPartialValueString(String partialValueString) { - // this.partialValueString = partialValueString; - //} - public void setEnsureBeginQuote(boolean ensureBeginQuote) { - this.ensureBeginQuote = ensureBeginQuote; - } - - public void setEnsureEndQuote(boolean ensureEndQuote) { - this.ensureEndQuote = ensureEndQuote; - } - - @Override - protected boolean isValidPrefix(String prefix) { - if (getReplacementString().startsWith(getDisplayString())) { - return super.isValidPrefix(prefix); - } - return super.isPrefix(trim(prefix), getValidationString()); - } - - private String getValidationString() { - if (fValidationString == null) { - fValidationString = trim(getReplacementString()); - } - return fValidationString; - } - - - /* - * It is possible that the replacement string is complex and larger than the prefix. - * In this case we want to trim the prefix to the last whitespace character. This - * will provide us with a prefix to use when matching the display string. - */ - private String trim(String aString) { - if (aString == null || aString.length() == 0) - return aString; - StringBuffer buffer = new StringBuffer(); - char[] chars = aString.toCharArray(); - for (int i = chars.length - 1; i > -1; i--) { - if (Character.isWhitespace(chars[i])) { - break; - } - buffer.append(chars[i]); - } - buffer = buffer.reverse(); - return buffer.toString(); - } -}
\ No newline at end of file diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/IWRDResources.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/IWRDResources.java deleted file mode 100644 index 6ef86fa68..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/IWRDResources.java +++ /dev/null @@ -1,29 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jst.common.internal.annotations.ui; - -import org.eclipse.osgi.util.NLS; - -public final class IWRDResources extends NLS { - - private static final String BUNDLE_NAME = "org.eclipse.jst.common.internal.annotations.ui.taghandlerui";//$NON-NLS-1$ - - private IWRDResources() { - // Do not instantiate - } - - public static String J2EEAnnotationsCompletionProcessor_3; - public static String J2EEAnnotationsCompletionProcessor_4; - - static { - NLS.initializeMessages(BUNDLE_NAME, IWRDResources.class); - } -}
\ No newline at end of file diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UIAttributeValueProposalHelper.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UIAttributeValueProposalHelper.java deleted file mode 100644 index d59538458..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UIAttributeValueProposalHelper.java +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2003, 2004 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jst.common.internal.annotations.ui; - -import org.eclipse.jst.common.internal.annotations.registry.AttributeValueProposalHelper; -import org.eclipse.swt.graphics.Image; - - -/** - * @author DABERG - * - */ -public class UIAttributeValueProposalHelper extends AttributeValueProposalHelper { - private Image image; - - /** - * @param replacementString - * @param valueOffset - * @param replacementLength - * @param valueDisplayString - */ - public UIAttributeValueProposalHelper(String replacementString, int valueOffset, int replacementLength, String valueDisplayString) { - super(replacementString, valueOffset, replacementLength, valueDisplayString); - } - - public Image getImage() { - return image; - } - - public void setImage(Image image) { - this.image = image; - } -}
\ No newline at end of file diff --git a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UiPlugin.java b/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UiPlugin.java deleted file mode 100644 index 7587aaf26..000000000 --- a/plugins/org.eclipse.jst.common.annotations.ui/src/org/eclipse/jst/common/internal/annotations/ui/UiPlugin.java +++ /dev/null @@ -1,88 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2003, 2004 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.jst.common.internal.annotations.ui; - -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -import org.eclipse.ui.plugin.AbstractUIPlugin; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import java.lang.Throwable; -import org.eclipse.core.runtime.CoreException; - -/** - * The main plugin class to be used in the desktop. - */ -public class UiPlugin extends AbstractUIPlugin { - //the ID for this plugin (added automatically by logging quickfix) - public static final String PLUGIN_ID = "org.eclipse.jst.common.annotations.ui"; //$NON-NLS-1$ - //The shared instance. - private static UiPlugin plugin; - //Resource bundle. - private ResourceBundle resourceBundle; - - /** - * The constructor. - */ - public UiPlugin() { - super(); - plugin = this; - try { - resourceBundle = ResourceBundle.getBundle("org.eclipse.wst.common.internal.annotations.ui.UiPluginResources"); //$NON-NLS-1$ - } catch (MissingResourceException x) { - resourceBundle = null; - } - } - - /** - * Returns the shared instance. - */ - public static UiPlugin getDefault() { - return plugin; - } - - /** - * Returns the string from the plugin's resource bundle, or 'key' if not found. - */ - public static String getResourceString(String key) { - ResourceBundle bundle = UiPlugin.getDefault().getResourceBundle(); - try { - return (bundle != null) ? bundle.getString(key) : key; - } catch (MissingResourceException e) { - return key; - } - } - - /** - * Returns the plugin's resource bundle, - */ - public ResourceBundle getResourceBundle() { - return resourceBundle; - } - - public static IStatus createStatus(int severity, String message, Throwable exception) { - return new Status(severity, PLUGIN_ID, message, exception); - } - - public static IStatus createStatus(int severity, String message) { - return createStatus(severity, message, null); - } - - public static void logError(Throwable exception) { - Platform.getLog(Platform.getBundle(PLUGIN_ID)).log( createStatus(IStatus.ERROR, exception.getMessage(), exception)); - } - - public static void logError(CoreException exception) { - Platform.getLog(Platform.getBundle(PLUGIN_ID)).log( exception.getStatus() ); - } -}
\ No newline at end of file |